Skip to content

Commit

Permalink
feat(D3 plugin): add opacity and marker setting to shape (#432)
Browse files Browse the repository at this point in the history
feat(D3 plugin): add opacity and marker setting to shape (#425)

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

* add jsdoc

Co-authored-by: Irina Kuzmina <[email protected]>
  • Loading branch information
korvin89 and kuzmadom authored Feb 20, 2024
1 parent 5376d96 commit 36fc2be
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 7 deletions.
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 @@ -120,6 +120,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 @@ -61,6 +61,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 @@ -44,7 +44,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 @@ -38,7 +38,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 @@ -57,7 +57,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 @@ -82,6 +82,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 @@ -47,7 +47,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 @@ -79,6 +79,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 {ConnectorCurve} from '../../../../../../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 @@ -46,7 +46,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';
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 @@ -20,6 +20,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 @@ -26,6 +26,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 @@ -20,6 +20,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 @@ -20,6 +20,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 @@ -44,4 +51,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 @@ -12,6 +12,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 @@ -23,7 +23,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

0 comments on commit 36fc2be

Please sign in to comment.