Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(D3 plugin): add opacity and marker setting to shape #425

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useSeries/prepare-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function prepareLineSeries(args: PrepareLineSeriesArgs) {
marker: prepareMarker(series, seriesOptions),
dashStyle: dashStyle as DashStyle,
linecap: prepareLinecap(dashStyle as DashStyle, series, seriesOptions) as LineCap,
opacity: get(series, 'opacity', null),
};

return prepared;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useSeries/prepare-pie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function preparePieSeries(args: PreparePieSeriesArgs) {
},
},
renderCustomShape: series.renderCustomShape,
opacity: get(dataItem, 'opacity', null),
};

return result;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/d3/renderer/hooks/useSeries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export type PreparedPieSeries = {
};
};
renderCustomShape?: PieSeries['renderCustomShape'];
opacity: number | null;
} & BasePreparedSeries;

export type PreparedLineSeries = {
Expand Down Expand Up @@ -195,6 +196,7 @@ export type PreparedLineSeries = {
};
dashStyle: DashStyle;
linecap: LineCap;
opacity: number | null;
} & BasePreparedSeries;

export type PreparedAreaSeries = {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/hooks/useShapes/bar-x/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export const BarXSeriesShapes = (args: Args) => {
.attr('y', (d) => d.y)
.attr('height', (d) => d.height)
.attr('width', (d) => d.width)
.attr('fill', (d) => d.data.color || d.series.color);
.attr('fill', (d) => d.data.color || d.series.color)
.attr('opacity', (d) => d.opacity);

let dataLabels = preparedData.map((d) => d.label).filter(Boolean) as LabelData[];
if (!preparedData[0]?.series.dataLabels.allowOverlap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export const prepareBarXData = (args: {
y: y - stackHeight,
width: rectWidth,
height,
opacity: get(yValue.data, 'opacity', null),
data: yValue.data,
series: yValue.series,
};
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useShapes/bar-x/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type PreparedBarXData = Omit<TooltipDataChunkBarX, 'series'> & {
y: number;
width: number;
height: number;
opacity: number | null;
series: PreparedBarXSeries;
label?: LabelData;
};
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/hooks/useShapes/bar-y/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export const BarYSeriesShapes = (args: Args) => {
.attr('y', (d) => d.y)
.attr('height', (d) => d.height)
.attr('width', (d) => d.width)
.attr('fill', (d) => d.color);
.attr('fill', (d) => d.color)
.attr('opacity', (d) => d.data.opacity || null);

const dataLabels = preparedData.filter((d) => d.series.dataLabels.enabled);
const labelSelection = svgElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export const prepareBarYData = (args: {
width,
height: barHeight,
color: data.color || s.color,
opacity: get(data, 'opacity', null),
data,
series: s,
});
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useShapes/bar-y/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export type PreparedBarYData = Omit<TooltipDataChunkBarX, 'series'> & {
width: number;
height: number;
color: string;
opacity: number | null;
series: PreparedBarYSeries;
};
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/hooks/useShapes/line/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export const LineSeriesShapes = (args: Args) => {
.attr('stroke-width', (d) => d.width)
.attr('stroke-linejoin', (d) => d.linecap)
.attr('stroke-linecap', (d) => d.linecap)
.attr('stroke-dasharray', (d) => getLineDashArray(d.dashStyle, d.width));
.attr('stroke-dasharray', (d) => getLineDashArray(d.dashStyle, d.width))
.attr('opacity', (d) => d.opacity);

let dataLabels = preparedData.reduce((acc, d) => {
return acc.concat(d.labels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const prepareLineData = (args: {
id: s.id,
dashStyle: s.dashStyle,
linecap: s.linecap,
opacity: s.opacity,
};

acc.push(result);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useShapes/line/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export type PreparedLineData = {
labels: LabelData[];
dashStyle: DashStyle;
linecap: LineCap;
opacity: number | null;
};
10 changes: 9 additions & 1 deletion src/plugins/d3/renderer/hooks/useShapes/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ export function renderMarker<T extends MarkerData>(

export function getMarkerVisibility(d: MarkerData) {
const markerStates = d.point.series.marker.states;
const enabled = (markerStates.hover.enabled && d.hovered) || markerStates.normal.enabled;
let enabled: Boolean;

if (d.hovered) {
enabled = markerStates.hover.enabled && d.hovered;
} else {
enabled =
markerStates.normal.enabled || get(d.point.data, 'marker.states.normal.enabled', false);
}

return enabled ? '' : 'hidden';
}

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/d3/renderer/hooks/useShapes/pie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export function PieSeriesShapes(args: PreparePieSeriesArgs) {
return arcGenerator(d);
})
.attr('class', b('segment'))
.attr('fill', (d) => d.data.color);
.attr('fill', (d) => d.data.color)
.attr('opacity', (d) => d.data.opacity);

shapesSelection
.selectAll<SVGTextElement, PieLabelData>('text')
Expand Down Expand Up @@ -167,7 +168,7 @@ export function PieSeriesShapes(args: PreparePieSeriesArgs) {
type: 'pie',
name: currentSegment.series.name,
},
data: currentSegment.series,
data: currentSegment.series.data,
};

dispatcher.call('hover-shape', {}, [data], pointer(e, svgContainer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function preparePieData(args: Args): PreparedPieData[] {
return {
value: item.value,
color: item.color,
opacity: item.opacity,
series: item,
hovered: false,
active: true,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useShapes/pie/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {PreparedPieSeries} from '../../useSeries/types';
export type SegmentData = {
value: number;
color: string;
opacity: number | null;
series: PreparedPieSeries;
hovered: boolean;
active: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/hooks/useShapes/scatter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export function ScatterSeriesShape(props: ScatterSeriesShapeProps) {
.data(preparedData, shapeKey)
.join('g')
.call(renderMarker)
.attr('fill', (d) => d.point.data.color || d.point.series.color || '');
.attr('fill', (d) => d.point.data.color || d.point.series.color || '')
.attr('opacity', (d) => d.point.opacity);

const getSelectedPoint = (element: Element) => {
return select<BaseType, PreparedScatterData>(element).datum();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import get from 'lodash/get';

import type {ScatterSeriesData} from '../../../../../../types';
import type {ChartScale} from '../../useAxisScales';
import type {PreparedAxis} from '../../useChartOptions/types';
Expand Down Expand Up @@ -32,6 +34,7 @@ export const prepareScatterData = (args: {
series: s,
x: getXValue({point: d, xAxis, xScale}),
y: getYValue({point: d, yAxis, yScale}),
opacity: get(d, 'opacity', null),
},
hovered: false,
active: true,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useShapes/scatter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {PreparedScatterSeries} from '../../useSeries/types';
type PointData = {
x: number;
y: number;
opacity: number | null;
data: ScatterSeriesData;
series: PreparedScatterSeries;
};
Expand Down
15 changes: 15 additions & 0 deletions src/types/widget-data/area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ export type AreaSeriesData<T = any> = BaseSeriesData<T> & {
y?: string | number;
/** Data label value of the point. If not specified, the y value is used. */
label?: string | number;
/** Individual marker options for the point. */
marker?: {
/** States for a single point marker. */
states?: {
/** The normal state of a single point marker. */
normal?: {
/**
* Enable or disable the point marker.
*
* @default false
* */
enabled: boolean;
};
};
};
};

export type AreaMarkerSymbol = 'circle' | 'square';
Expand Down
2 changes: 2 additions & 0 deletions src/types/widget-data/bar-x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type BarXSeriesData<T = any> = BaseSeriesData<T> & {
category?: string;
/** Data label value of the bar-x column. If not specified, the y value is used. */
label?: string | number;
/** Individual opacity for the bar-x column. */
opacity?: number;
};

export type BarXSeries<T = any> = BaseSeries & {
Expand Down
2 changes: 2 additions & 0 deletions src/types/widget-data/bar-y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type BarYSeriesData<T = any> = BaseSeriesData<T> & {
y?: string | number;
/** Data label value of the bar. If not specified, the x value is used. */
label?: string | number;
/** Individual opacity for the bar. */
opacity?: number;
};

export type BarYSeries<T = any> = BaseSeries & {
Expand Down
9 changes: 9 additions & 0 deletions src/types/widget-data/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export type LineSeriesData<T = any> = BaseSeriesData<T> & {
y?: string | number;
/** Data label value of the point. If not specified, the y value is used. */
label?: string | number;
marker?: {
states?: {
normal?: {
enabled: boolean;
};
};
};
};

export type LineSeries<T = any> = BaseSeries & {
Expand All @@ -45,4 +52,6 @@ export type LineSeries<T = any> = BaseSeries & {
dashStyle?: `${DashStyle}`;
/** Option for line cap style */
linecap?: `${LineCap}`;
/** Individual opacity for the line. */
opacity?: number;
};
2 changes: 2 additions & 0 deletions src/types/widget-data/pie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type PieSeriesData<T = any> = BaseSeriesData<T> & {
visible?: boolean;
/** Initial data label of the pie segment. If not specified, the value is used. */
label?: string;
/** Individual opacity for the pie segment. */
opacity?: number;
};

export type ConnectorShape = 'straight-line' | 'polyline';
Expand Down
3 changes: 3 additions & 0 deletions src/types/widget-data/scatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export type ScatterSeriesData<T = any> = BaseSeriesData<T> & {
* @deprecated use `x` or `y` instead
*/
category?: string;
/** Individual radius for the point. */
radius?: number;
/** Individual opacity for the point. */
opacity?: number;
};

export type ScatterSeries<T = any> = BaseSeries & {
Expand Down
Loading