@gravity-ui/chartkit 3.5.0 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/plugins/d3/renderer/components/Chart.js +7 -11
- package/build/plugins/d3/renderer/components/Legend.d.ts +3 -2
- package/build/plugins/d3/renderer/components/Legend.js +42 -21
- package/build/plugins/d3/renderer/constants.d.ts +3 -0
- package/build/plugins/d3/renderer/constants.js +3 -0
- package/build/plugins/d3/renderer/hooks/index.d.ts +1 -1
- package/build/plugins/d3/renderer/hooks/index.js +1 -1
- package/build/plugins/d3/renderer/hooks/useAxisScales/index.d.ts +2 -2
- package/build/plugins/d3/renderer/hooks/useAxisScales/index.js +2 -2
- package/build/plugins/d3/renderer/hooks/useChartDimensions/index.d.ts +2 -2
- package/build/plugins/d3/renderer/hooks/useChartDimensions/index.js +2 -4
- package/build/plugins/d3/renderer/hooks/useChartOptions/chart.js +5 -6
- package/build/plugins/d3/renderer/hooks/useChartOptions/legend.js +7 -2
- package/build/plugins/d3/renderer/hooks/useChartOptions/title.js +2 -2
- package/build/plugins/d3/renderer/hooks/useChartOptions/types.d.ts +3 -1
- package/build/plugins/d3/renderer/hooks/useChartOptions/x-axis.js +3 -3
- package/build/plugins/d3/renderer/hooks/useChartOptions/y-axis.js +3 -3
- package/build/plugins/d3/renderer/hooks/useSeries/constants.d.ts +1 -0
- package/build/plugins/d3/renderer/hooks/useSeries/constants.js +1 -0
- package/build/plugins/d3/renderer/hooks/useSeries/index.d.ts +10 -8
- package/build/plugins/d3/renderer/hooks/useSeries/index.js +47 -54
- package/build/plugins/d3/renderer/hooks/useSeries/prepareSeries.d.ts +10 -0
- package/build/plugins/d3/renderer/hooks/useSeries/prepareSeries.js +126 -0
- package/build/plugins/d3/renderer/hooks/useSeries/types.d.ts +35 -0
- package/build/plugins/d3/renderer/hooks/useSeries/types.js +1 -0
- package/build/plugins/d3/renderer/hooks/useSeries/utils.d.ts +3 -0
- package/build/plugins/d3/renderer/hooks/useSeries/utils.js +11 -0
- package/build/plugins/d3/renderer/hooks/useShapes/bar-x.d.ts +3 -3
- package/build/plugins/d3/renderer/hooks/useShapes/bar-x.js +93 -21
- package/build/plugins/d3/renderer/hooks/useShapes/index.d.ts +2 -2
- package/build/plugins/d3/renderer/hooks/useShapes/index.js +6 -14
- package/build/plugins/d3/renderer/hooks/useShapes/pie.d.ts +2 -2
- package/build/plugins/d3/renderer/hooks/useShapes/pie.js +87 -12
- package/build/plugins/d3/renderer/hooks/useShapes/scatter.d.ts +0 -1
- package/build/plugins/d3/renderer/hooks/useShapes/scatter.js +4 -2
- package/build/plugins/d3/renderer/hooks/useShapes/styles.css +10 -0
- package/build/plugins/d3/renderer/utils/index.d.ts +42 -6
- package/build/plugins/d3/renderer/utils/index.js +70 -6
- package/build/types/widget-data/bar-x.d.ts +17 -1
- package/build/types/widget-data/base.d.ts +15 -3
- package/build/types/widget-data/legend.d.ts +32 -0
- package/build/types/widget-data/pie.d.ts +23 -4
- package/build/types/widget-data/scatter.d.ts +5 -0
- package/package.json +1 -1
- package/build/plugins/d3/renderer/hooks/useChartOptions/constants.d.ts +0 -3
- package/build/plugins/d3/renderer/hooks/useChartOptions/constants.js +0 -3
- package/build/plugins/d3/renderer/hooks/useChartOptions/utils.d.ts +0 -5
- package/build/plugins/d3/renderer/hooks/useChartOptions/utils.js +0 -18
- package/build/plugins/d3/renderer/hooks/useLegend/index.d.ts +0 -13
- package/build/plugins/d3/renderer/hooks/useLegend/index.js +0 -59
|
@@ -1,17 +1,41 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { ChartKitWidgetSeries, ChartKitWidgetAxisType, ChartKitWidgetAxisLabels } from '../../../../types/widget-data';
|
|
1
|
+
import { AxisDomain } from 'd3';
|
|
2
|
+
import type { BaseTextStyle, ChartKitWidgetSeries, ChartKitWidgetAxisType, ChartKitWidgetAxisLabels } from '../../../../types/widget-data';
|
|
3
3
|
export * from './math';
|
|
4
|
+
type UnknownSeries = {
|
|
5
|
+
type: ChartKitWidgetSeries['type'];
|
|
6
|
+
data: unknown;
|
|
7
|
+
};
|
|
4
8
|
/**
|
|
5
9
|
* Checks whether the series should be drawn with axes.
|
|
6
10
|
*
|
|
7
11
|
* @param series - The series object to check.
|
|
8
12
|
* @returns `true` if the series should be drawn with axes, `false` otherwise.
|
|
9
13
|
*/
|
|
10
|
-
export declare
|
|
11
|
-
export declare
|
|
12
|
-
|
|
14
|
+
export declare function isAxisRelatedSeries(series: UnknownSeries): boolean;
|
|
15
|
+
export declare function isSeriesWithNumericalXValues(series: UnknownSeries): series is {
|
|
16
|
+
type: ChartKitWidgetSeries['type'];
|
|
17
|
+
data: {
|
|
18
|
+
x: number;
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
export declare function isSeriesWithNumericalYValues(series: UnknownSeries): series is {
|
|
22
|
+
type: ChartKitWidgetSeries['type'];
|
|
23
|
+
data: {
|
|
24
|
+
y: number;
|
|
25
|
+
}[];
|
|
26
|
+
};
|
|
27
|
+
export declare function isSeriesWithCategoryValues(series: UnknownSeries): series is {
|
|
28
|
+
type: ChartKitWidgetSeries['type'];
|
|
29
|
+
data: {
|
|
30
|
+
category: string;
|
|
31
|
+
}[];
|
|
32
|
+
};
|
|
33
|
+
export declare const getDomainDataXBySeries: (series: UnknownSeries[]) => number[];
|
|
34
|
+
export declare const getDomainDataYBySeries: (series: UnknownSeries[]) => unknown[];
|
|
13
35
|
export declare const getSeriesNames: (series: ChartKitWidgetSeries[]) => string[];
|
|
14
|
-
export declare const getOnlyVisibleSeries: <T extends
|
|
36
|
+
export declare const getOnlyVisibleSeries: <T extends {
|
|
37
|
+
visible: boolean;
|
|
38
|
+
}>(series: T[]) => T[];
|
|
15
39
|
export declare const parseTransformStyle: (style: string | null) => {
|
|
16
40
|
x?: number | undefined;
|
|
17
41
|
y?: number | undefined;
|
|
@@ -22,3 +46,15 @@ export declare const formatAxisTickLabel: (args: {
|
|
|
22
46
|
dateFormat?: ChartKitWidgetAxisLabels['dateFormat'];
|
|
23
47
|
numberFormat?: ChartKitWidgetAxisLabels['numberFormat'];
|
|
24
48
|
}) => string;
|
|
49
|
+
/**
|
|
50
|
+
* Calculates the height of a text element in a horizontal SVG layout.
|
|
51
|
+
*
|
|
52
|
+
* @param {Object} args - The arguments for the function.
|
|
53
|
+
* @param {string} args.text - The text to be measured.
|
|
54
|
+
* @param {Partial<BaseTextStyle>} args.style - Optional style properties for the text element.
|
|
55
|
+
* @return {number} The height of the text element.
|
|
56
|
+
*/
|
|
57
|
+
export declare const getHorisontalSvgTextHeight: (args: {
|
|
58
|
+
text: string;
|
|
59
|
+
style?: Partial<BaseTextStyle>;
|
|
60
|
+
}) => number;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { group, select } from 'd3';
|
|
2
|
+
import get from 'lodash/get';
|
|
1
3
|
import { dateTime } from '@gravity-ui/date-utils';
|
|
2
4
|
import { formatNumber } from '../../../shared';
|
|
5
|
+
import { DEFAULT_AXIS_LABEL_FONT_SIZE } from '../constants';
|
|
3
6
|
export * from './math';
|
|
4
7
|
const CHARTS_WITHOUT_AXIS = ['pie'];
|
|
5
8
|
/**
|
|
@@ -8,18 +11,56 @@ const CHARTS_WITHOUT_AXIS = ['pie'];
|
|
|
8
11
|
* @param series - The series object to check.
|
|
9
12
|
* @returns `true` if the series should be drawn with axes, `false` otherwise.
|
|
10
13
|
*/
|
|
11
|
-
export
|
|
14
|
+
export function isAxisRelatedSeries(series) {
|
|
12
15
|
return !CHARTS_WITHOUT_AXIS.includes(series.type);
|
|
13
|
-
}
|
|
16
|
+
}
|
|
17
|
+
export function isSeriesWithNumericalXValues(series) {
|
|
18
|
+
return isAxisRelatedSeries(series);
|
|
19
|
+
}
|
|
20
|
+
export function isSeriesWithNumericalYValues(series) {
|
|
21
|
+
return isAxisRelatedSeries(series);
|
|
22
|
+
}
|
|
23
|
+
export function isSeriesWithCategoryValues(series) {
|
|
24
|
+
return isAxisRelatedSeries(series);
|
|
25
|
+
}
|
|
14
26
|
export const getDomainDataXBySeries = (series) => {
|
|
15
|
-
return series.
|
|
16
|
-
|
|
27
|
+
return series.reduce((acc, s) => {
|
|
28
|
+
if (isSeriesWithNumericalXValues(s)) {
|
|
29
|
+
acc.push(...s.data.map((d) => d.x));
|
|
30
|
+
}
|
|
17
31
|
return acc;
|
|
18
32
|
}, []);
|
|
19
33
|
};
|
|
20
34
|
export const getDomainDataYBySeries = (series) => {
|
|
21
|
-
|
|
22
|
-
|
|
35
|
+
const groupedSeries = group(series, (item) => item.type);
|
|
36
|
+
return Array.from(groupedSeries).reduce((acc, [type, seriesList]) => {
|
|
37
|
+
switch (type) {
|
|
38
|
+
case 'bar-x': {
|
|
39
|
+
const barXSeries = seriesList;
|
|
40
|
+
const stackedSeries = group(barXSeries, (item) => item.stackId);
|
|
41
|
+
Array.from(stackedSeries).forEach(([, stack]) => {
|
|
42
|
+
const values = {};
|
|
43
|
+
stack.forEach((singleSeries) => {
|
|
44
|
+
singleSeries.data.forEach((point) => {
|
|
45
|
+
const key = String(point.x || point.category);
|
|
46
|
+
if (typeof values[key] === 'undefined') {
|
|
47
|
+
values[key] = 0;
|
|
48
|
+
}
|
|
49
|
+
if (point.y) {
|
|
50
|
+
values[key] += point.y;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
acc.push(...Object.values(values));
|
|
55
|
+
});
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
default: {
|
|
59
|
+
seriesList.filter(isSeriesWithNumericalYValues).forEach((s) => {
|
|
60
|
+
acc.push(...s.data.map((d) => d.y));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
23
64
|
return acc;
|
|
24
65
|
}, []);
|
|
25
66
|
};
|
|
@@ -64,3 +105,26 @@ export const formatAxisTickLabel = (args) => {
|
|
|
64
105
|
}
|
|
65
106
|
}
|
|
66
107
|
};
|
|
108
|
+
/**
|
|
109
|
+
* Calculates the height of a text element in a horizontal SVG layout.
|
|
110
|
+
*
|
|
111
|
+
* @param {Object} args - The arguments for the function.
|
|
112
|
+
* @param {string} args.text - The text to be measured.
|
|
113
|
+
* @param {Partial<BaseTextStyle>} args.style - Optional style properties for the text element.
|
|
114
|
+
* @return {number} The height of the text element.
|
|
115
|
+
*/
|
|
116
|
+
export const getHorisontalSvgTextHeight = (args) => {
|
|
117
|
+
const { text, style } = args;
|
|
118
|
+
const textSelection = select(document.body).append('text').text(text);
|
|
119
|
+
const fontSize = get(style, 'fontSize', DEFAULT_AXIS_LABEL_FONT_SIZE);
|
|
120
|
+
let height = 0;
|
|
121
|
+
if (fontSize) {
|
|
122
|
+
textSelection.style('font-size', fontSize);
|
|
123
|
+
}
|
|
124
|
+
textSelection
|
|
125
|
+
.each(function () {
|
|
126
|
+
height = this.getBoundingClientRect().height;
|
|
127
|
+
})
|
|
128
|
+
.remove();
|
|
129
|
+
return height;
|
|
130
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BaseSeries, BaseSeriesData } from './base';
|
|
2
2
|
import type { ChartKitWidgetSeriesOptions } from './series';
|
|
3
|
+
import { ChartKitWidgetLegend, RectLegendSymbolOptions } from './legend';
|
|
3
4
|
export type BarXSeriesData<T = any> = BaseSeriesData<T> & {
|
|
4
5
|
/** The x value of the point */
|
|
5
6
|
x?: number;
|
|
@@ -7,6 +8,8 @@ export type BarXSeriesData<T = any> = BaseSeriesData<T> & {
|
|
|
7
8
|
y?: number;
|
|
8
9
|
/** Corresponding value of axis category */
|
|
9
10
|
category?: string;
|
|
11
|
+
/** Data label value of the bar-x column. If not specified, the y value is used. */
|
|
12
|
+
label?: string | number;
|
|
10
13
|
};
|
|
11
14
|
export type BarXSeries<T = any> = BaseSeries & {
|
|
12
15
|
type: 'bar-x';
|
|
@@ -15,6 +18,11 @@ export type BarXSeries<T = any> = BaseSeries & {
|
|
|
15
18
|
name: string;
|
|
16
19
|
/** The main color of the series (hex, rgba) */
|
|
17
20
|
color?: string;
|
|
21
|
+
/** Whether to stack the values of each series on top of each other.
|
|
22
|
+
* Possible values are undefined to disable, "normal" to stack by value or "percent"
|
|
23
|
+
*
|
|
24
|
+
* @default undefined
|
|
25
|
+
* */
|
|
18
26
|
stacking?: 'normal' | 'percent';
|
|
19
27
|
/** This option allows grouping series in a stacked chart */
|
|
20
28
|
stackId?: string;
|
|
@@ -25,7 +33,15 @@ export type BarXSeries<T = any> = BaseSeries & {
|
|
|
25
33
|
* */
|
|
26
34
|
grouping?: boolean;
|
|
27
35
|
dataLabels?: ChartKitWidgetSeriesOptions['dataLabels'] & {
|
|
28
|
-
/**
|
|
36
|
+
/**
|
|
37
|
+
* Whether to align the data label inside or outside the box
|
|
38
|
+
*
|
|
39
|
+
* @default false
|
|
40
|
+
* */
|
|
29
41
|
inside?: boolean;
|
|
30
42
|
};
|
|
43
|
+
/** Individual series legend options. Has higher priority than legend options in widget data */
|
|
44
|
+
legend?: ChartKitWidgetLegend & {
|
|
45
|
+
symbol?: RectLegendSymbolOptions;
|
|
46
|
+
};
|
|
31
47
|
};
|
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import type { ChartKitWidgetLegend } from './legend';
|
|
2
1
|
export type BaseSeries = {
|
|
3
|
-
/** Individual series legend options. Has higher priority than legend options in widget data */
|
|
4
|
-
legend?: ChartKitWidgetLegend;
|
|
5
2
|
/** Initial visibility of the series */
|
|
6
3
|
visible?: boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Options for the series data labels, appearing next to each data point.
|
|
6
|
+
*
|
|
7
|
+
* Note: now this option is supported only for `pie` charts.
|
|
8
|
+
* */
|
|
9
|
+
dataLabels?: {
|
|
10
|
+
/**
|
|
11
|
+
* Enable or disable the data labels
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
style?: Partial<BaseTextStyle>;
|
|
16
|
+
};
|
|
7
17
|
};
|
|
8
18
|
export type BaseSeriesData<T = any> = {
|
|
9
19
|
/**
|
|
@@ -15,4 +25,6 @@ export type BaseSeriesData<T = any> = {
|
|
|
15
25
|
};
|
|
16
26
|
export type BaseTextStyle = {
|
|
17
27
|
fontSize: string;
|
|
28
|
+
fontWeight?: string;
|
|
29
|
+
fontColor?: string;
|
|
18
30
|
};
|
|
@@ -1,3 +1,35 @@
|
|
|
1
1
|
export type ChartKitWidgetLegend = {
|
|
2
2
|
enabled?: boolean;
|
|
3
|
+
/** The horizontal alignment of the legend box within the chart area.
|
|
4
|
+
*
|
|
5
|
+
* @default center
|
|
6
|
+
* */
|
|
7
|
+
align?: 'left' | 'center' | 'right';
|
|
8
|
+
/** Defines the pixel distance between each legend item
|
|
9
|
+
*
|
|
10
|
+
* @default 20
|
|
11
|
+
* */
|
|
12
|
+
itemDistance?: number;
|
|
13
|
+
};
|
|
14
|
+
export type BaseLegendSymbol = {
|
|
15
|
+
/** The pixel padding between the legend item symbol and the legend item text.
|
|
16
|
+
*
|
|
17
|
+
* @default 5
|
|
18
|
+
* */
|
|
19
|
+
padding?: number;
|
|
20
|
+
};
|
|
21
|
+
export type RectLegendSymbolOptions = BaseLegendSymbol & {
|
|
22
|
+
/** The pixel width of the symbol for series types that use a rectangle in the legend
|
|
23
|
+
*
|
|
24
|
+
* @default 10
|
|
25
|
+
* */
|
|
26
|
+
width?: number;
|
|
27
|
+
/** The pixel width of the symbol for series types that use a rectangle in the legend
|
|
28
|
+
*
|
|
29
|
+
* @default 10
|
|
30
|
+
* */
|
|
31
|
+
height?: number;
|
|
32
|
+
/** The border radius of the symbol for series types that use a rectangle in the legend.
|
|
33
|
+
* Defaults to half the symbolHeight, effectively creating a circle. */
|
|
34
|
+
radius?: number;
|
|
3
35
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { BaseSeries, BaseSeriesData } from './base';
|
|
2
|
+
import { ChartKitWidgetLegend, RectLegendSymbolOptions } from './legend';
|
|
2
3
|
export type PieSeriesData<T = any> = BaseSeriesData<T> & {
|
|
3
4
|
/** The value of the pie segment. */
|
|
4
5
|
value: number;
|
|
@@ -8,20 +9,38 @@ export type PieSeriesData<T = any> = BaseSeriesData<T> & {
|
|
|
8
9
|
color?: string;
|
|
9
10
|
/** Initial visibility of the pie segment. */
|
|
10
11
|
visible?: boolean;
|
|
12
|
+
/** Initial data label of the pie segment. If not specified, the value is used. */
|
|
13
|
+
label?: string;
|
|
11
14
|
};
|
|
12
15
|
export type PieSeries<T = any> = BaseSeries & {
|
|
13
16
|
type: 'pie';
|
|
14
17
|
data: PieSeriesData<T>[];
|
|
15
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* The color of the border surrounding each segment.
|
|
20
|
+
* @default `--g-color-base-background` from @gravity-ui/uikit.
|
|
21
|
+
*/
|
|
16
22
|
borderColor?: string;
|
|
17
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* The width of the border surrounding each segment.
|
|
25
|
+
* @default '1px'
|
|
26
|
+
*/
|
|
18
27
|
borderWidth?: number;
|
|
19
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* The corner radius of the border surrounding each segment.
|
|
30
|
+
* @default 0
|
|
31
|
+
*/
|
|
20
32
|
borderRadius?: number;
|
|
21
33
|
/** The center of the pie chart relative to the chart area. */
|
|
22
34
|
center?: [string | number | null, string | number | null];
|
|
23
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* The inner radius of the pie.
|
|
37
|
+
* @default 0
|
|
38
|
+
*/
|
|
24
39
|
innerRadius?: string | number;
|
|
25
40
|
/** The radius of the pie relative to the chart area. The default behaviour is to scale to the chart area. */
|
|
26
41
|
radius?: string | number;
|
|
42
|
+
/** Individual series legend options. Has higher priority than legend options in widget data */
|
|
43
|
+
legend?: ChartKitWidgetLegend & {
|
|
44
|
+
symbol?: RectLegendSymbolOptions;
|
|
45
|
+
};
|
|
27
46
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { BaseSeries, BaseSeriesData } from './base';
|
|
2
|
+
import type { ChartKitWidgetLegend, RectLegendSymbolOptions } from './legend';
|
|
2
3
|
export type ScatterSeriesData<T = any> = BaseSeriesData<T> & {
|
|
3
4
|
/** The x value of the point */
|
|
4
5
|
x?: number;
|
|
@@ -17,4 +18,8 @@ export type ScatterSeries<T = any> = BaseSeries & {
|
|
|
17
18
|
color?: string;
|
|
18
19
|
/** A predefined shape or symbol for the dot */
|
|
19
20
|
symbol?: string;
|
|
21
|
+
/** Individual series legend options. Has higher priority than legend options in widget data */
|
|
22
|
+
legend?: ChartKitWidgetLegend & {
|
|
23
|
+
symbol?: RectLegendSymbolOptions;
|
|
24
|
+
};
|
|
20
25
|
};
|
package/package.json
CHANGED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { select } from 'd3';
|
|
2
|
-
import get from 'lodash/get';
|
|
3
|
-
import { DEFAULT_AXIS_LABEL_FONT_SIZE } from './constants';
|
|
4
|
-
export const getHorisontalSvgTextDimensions = (args) => {
|
|
5
|
-
const { text, style } = args;
|
|
6
|
-
const textSelection = select(document.body).append('text').text(text);
|
|
7
|
-
const fontSize = get(style, 'fontSize', DEFAULT_AXIS_LABEL_FONT_SIZE);
|
|
8
|
-
let height = 0;
|
|
9
|
-
if (fontSize) {
|
|
10
|
-
textSelection.style('font-size', fontSize);
|
|
11
|
-
}
|
|
12
|
-
textSelection
|
|
13
|
-
.each(function () {
|
|
14
|
-
height = this.getBoundingClientRect().height;
|
|
15
|
-
})
|
|
16
|
-
.remove();
|
|
17
|
-
return height;
|
|
18
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { ChartKitWidgetSeries } from '../../../../../types/widget-data';
|
|
2
|
-
export type OnLegendItemClick = (data: {
|
|
3
|
-
name: string;
|
|
4
|
-
metaKey: boolean;
|
|
5
|
-
}) => void;
|
|
6
|
-
type Args = {
|
|
7
|
-
series: ChartKitWidgetSeries[];
|
|
8
|
-
};
|
|
9
|
-
export declare const useLegend: (args: Args) => {
|
|
10
|
-
activeLegendItems: string[];
|
|
11
|
-
handleLegendItemClick: OnLegendItemClick;
|
|
12
|
-
};
|
|
13
|
-
export {};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import get from 'lodash/get';
|
|
3
|
-
import { isAxisRelatedSeries } from '../../utils';
|
|
4
|
-
const getActiveLegendItems = (series) => {
|
|
5
|
-
return series.reduce((acc, s) => {
|
|
6
|
-
const isAxisRelated = isAxisRelatedSeries(s);
|
|
7
|
-
const isLegendEnabled = get(s, 'legend.enabled', true);
|
|
8
|
-
const isSeriesVisible = get(s, 'visible', true);
|
|
9
|
-
if (isLegendEnabled && isAxisRelated && isSeriesVisible && 'name' in s) {
|
|
10
|
-
acc.push(s.name);
|
|
11
|
-
}
|
|
12
|
-
else if (isLegendEnabled && !isAxisRelated) {
|
|
13
|
-
s.data.forEach((d) => {
|
|
14
|
-
const isDataVisible = get(d, 'visible', true);
|
|
15
|
-
if (isDataVisible && 'name' in d) {
|
|
16
|
-
acc.push(d.name);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
return acc;
|
|
21
|
-
}, []);
|
|
22
|
-
};
|
|
23
|
-
const getAllLegendItems = (series) => {
|
|
24
|
-
return series.reduce((acc, s) => {
|
|
25
|
-
if (isAxisRelatedSeries(s) && 'name' in s) {
|
|
26
|
-
acc.push(s.name);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
acc.push(...s.data.map((d) => ('name' in d && d.name) || ''));
|
|
30
|
-
}
|
|
31
|
-
return acc;
|
|
32
|
-
}, []);
|
|
33
|
-
};
|
|
34
|
-
export const useLegend = (args) => {
|
|
35
|
-
const { series } = args;
|
|
36
|
-
const [activeLegendItems, setActiveLegendItems] = React.useState(getActiveLegendItems(series));
|
|
37
|
-
const handleLegendItemClick = React.useCallback(({ name, metaKey }) => {
|
|
38
|
-
const onlyItemSelected = activeLegendItems.length === 1 && activeLegendItems.includes(name);
|
|
39
|
-
let nextActiveLegendItems;
|
|
40
|
-
if (metaKey && activeLegendItems.includes(name)) {
|
|
41
|
-
nextActiveLegendItems = activeLegendItems.filter((item) => item !== name);
|
|
42
|
-
}
|
|
43
|
-
else if (metaKey && !activeLegendItems.includes(name)) {
|
|
44
|
-
nextActiveLegendItems = activeLegendItems.concat(name);
|
|
45
|
-
}
|
|
46
|
-
else if (onlyItemSelected) {
|
|
47
|
-
nextActiveLegendItems = getAllLegendItems(series);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
nextActiveLegendItems = [name];
|
|
51
|
-
}
|
|
52
|
-
setActiveLegendItems(nextActiveLegendItems);
|
|
53
|
-
}, [series, activeLegendItems]);
|
|
54
|
-
// FIXME: remove effect. It initiates extra rerender
|
|
55
|
-
React.useEffect(() => {
|
|
56
|
-
setActiveLegendItems(getActiveLegendItems(series));
|
|
57
|
-
}, [series]);
|
|
58
|
-
return { activeLegendItems, handleLegendItemClick };
|
|
59
|
-
};
|