@gravity-ui/chartkit 3.1.3 → 3.2.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/__stories__/LinearCategories.stories.d.ts +4 -0
- package/build/plugins/d3/__stories__/LinearCategories.stories.js +101 -0
- package/build/plugins/d3/__stories__/Timestamp.stories.d.ts +4 -0
- package/build/plugins/d3/__stories__/Timestamp.stories.js +89 -0
- package/build/plugins/d3/__stories__/penguins.json +3098 -0
- package/build/plugins/d3/index.d.ts +7 -0
- package/build/plugins/d3/index.js +10 -0
- package/build/plugins/d3/renderer/D3Widget.d.ts +15 -0
- package/build/plugins/d3/renderer/D3Widget.js +40 -0
- package/build/plugins/d3/renderer/components/AxisX.d.ts +10 -0
- package/build/plugins/d3/renderer/components/AxisX.js +68 -0
- package/build/plugins/d3/renderer/components/AxisY.d.ts +10 -0
- package/build/plugins/d3/renderer/components/AxisY.js +73 -0
- package/build/plugins/d3/renderer/components/Chart.d.ts +10 -0
- package/build/plugins/d3/renderer/components/Chart.js +64 -0
- package/build/plugins/d3/renderer/components/Legend.d.ts +12 -0
- package/build/plugins/d3/renderer/components/Legend.js +66 -0
- package/build/plugins/d3/renderer/components/Title.d.ts +7 -0
- package/build/plugins/d3/renderer/components/Title.js +8 -0
- package/build/plugins/d3/renderer/components/Tooltip/DefaultContent.d.ts +10 -0
- package/build/plugins/d3/renderer/components/Tooltip/DefaultContent.js +21 -0
- package/build/plugins/d3/renderer/components/Tooltip/index.d.ts +12 -0
- package/build/plugins/d3/renderer/components/Tooltip/index.js +53 -0
- package/build/plugins/d3/renderer/components/index.d.ts +1 -0
- package/build/plugins/d3/renderer/components/index.js +1 -0
- package/build/plugins/d3/renderer/components/styles.css +61 -0
- package/build/plugins/d3/renderer/constants.d.ts +1 -0
- package/build/plugins/d3/renderer/constants.js +22 -0
- package/build/plugins/d3/renderer/hooks/index.d.ts +10 -0
- package/build/plugins/d3/renderer/hooks/index.js +10 -0
- package/build/plugins/d3/renderer/hooks/useChartDimensions/index.d.ts +17 -0
- package/build/plugins/d3/renderer/hooks/useChartDimensions/index.js +13 -0
- package/build/plugins/d3/renderer/hooks/useChartEvents/index.d.ts +5 -0
- package/build/plugins/d3/renderer/hooks/useChartEvents/index.js +15 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/chart.d.ts +8 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/chart.js +60 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/constants.d.ts +3 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/constants.js +3 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/index.d.ts +3 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/index.js +32 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/legend.d.ts +6 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/legend.js +5 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/title.d.ts +5 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/title.js +17 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/tooltip.d.ts +5 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/tooltip.js +5 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/types.d.ts +32 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/types.js +1 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/utils.d.ts +5 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/utils.js +18 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/x-axis.d.ts +5 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/x-axis.js +29 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/y-axis.d.ts +5 -0
- package/build/plugins/d3/renderer/hooks/useChartOptions/y-axis.js +34 -0
- package/build/plugins/d3/renderer/hooks/useLegend/index.d.ts +13 -0
- package/build/plugins/d3/renderer/hooks/useLegend/index.js +28 -0
- package/build/plugins/d3/renderer/hooks/useScales/index.d.ts +17 -0
- package/build/plugins/d3/renderer/hooks/useScales/index.js +101 -0
- package/build/plugins/d3/renderer/hooks/useSeries/index.d.ts +14 -0
- package/build/plugins/d3/renderer/hooks/useSeries/index.js +23 -0
- package/build/plugins/d3/renderer/hooks/useShapes/index.d.ts +19 -0
- package/build/plugins/d3/renderer/hooks/useShapes/index.js +73 -0
- package/build/plugins/d3/renderer/hooks/useTooltip/index.d.ts +13 -0
- package/build/plugins/d3/renderer/hooks/useTooltip/index.js +19 -0
- package/build/plugins/d3/renderer/hooks/useTooltip/types.d.ts +7 -0
- package/build/plugins/d3/renderer/hooks/useTooltip/types.js +1 -0
- package/build/plugins/d3/renderer/utils/index.d.ts +18 -0
- package/build/plugins/d3/renderer/utils/index.js +71 -0
- package/build/plugins/d3/types.d.ts +4 -0
- package/build/plugins/d3/types.js +1 -0
- package/build/types/widget-data/axis.d.ts +22 -0
- package/build/types/widget-data/axis.js +1 -0
- package/build/types/widget-data/base.d.ts +15 -0
- package/build/types/widget-data/base.js +1 -0
- package/build/types/widget-data/chart.d.ts +9 -0
- package/build/types/widget-data/chart.js +1 -0
- package/build/types/widget-data/index.d.ts +24 -0
- package/build/types/widget-data/index.js +9 -0
- package/build/types/widget-data/legend.d.ts +3 -0
- package/build/types/widget-data/legend.js +1 -0
- package/build/types/widget-data/pie.d.ts +10 -0
- package/build/types/widget-data/pie.js +1 -0
- package/build/types/widget-data/scatter.d.ts +20 -0
- package/build/types/widget-data/scatter.js +1 -0
- package/build/types/widget-data/series.d.ts +4 -0
- package/build/types/widget-data/series.js +1 -0
- package/build/types/widget-data/title.d.ts +5 -0
- package/build/types/widget-data/title.js +1 -0
- package/build/types/widget-data/tooltip.d.ts +12 -0
- package/build/types/widget-data/tooltip.js +1 -0
- package/build/types/widget.d.ts +5 -0
- package/package.json +4 -2
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const LEGEND_LINE_HEIGHT = 15;
|
|
2
|
+
export const useChartDimensions = (args) => {
|
|
3
|
+
const { margin, legend, title, width, height, xAxis, yAxis } = args;
|
|
4
|
+
const legendHeight = legend.enabled ? LEGEND_LINE_HEIGHT : 0;
|
|
5
|
+
const titleHeight = (title === null || title === void 0 ? void 0 : title.height) || 0;
|
|
6
|
+
const xAxisTitleHeight = (xAxis === null || xAxis === void 0 ? void 0 : xAxis.title.height) || 0;
|
|
7
|
+
const yAxisTitleHeight = (yAxis === null || yAxis === void 0 ? void 0 : yAxis.reduce((acc, axis) => {
|
|
8
|
+
return acc + (axis.title.height || 0);
|
|
9
|
+
}, 0)) || 0;
|
|
10
|
+
const boundsWidth = width - margin.right - margin.left - yAxisTitleHeight;
|
|
11
|
+
const boundsHeight = height - margin.top - margin.bottom - legendHeight - titleHeight - xAxisTitleHeight;
|
|
12
|
+
return { boundsWidth, boundsHeight, legendHeight };
|
|
13
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export const useChartEvents = () => {
|
|
3
|
+
const [chartHovered, setChartHovered] = React.useState(false);
|
|
4
|
+
const handleMouseEnter = React.useCallback(() => {
|
|
5
|
+
setChartHovered(true);
|
|
6
|
+
}, []);
|
|
7
|
+
const handleMouseLeave = React.useCallback(() => {
|
|
8
|
+
setChartHovered(false);
|
|
9
|
+
}, []);
|
|
10
|
+
return {
|
|
11
|
+
chartHovered,
|
|
12
|
+
handleMouseEnter,
|
|
13
|
+
handleMouseLeave,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChartKitWidgetData } from '../../../../../types/widget-data';
|
|
2
|
+
import type { PreparedAxis, PreparedChart } from './types';
|
|
3
|
+
export declare const getPreparedChart: (args: {
|
|
4
|
+
chart: ChartKitWidgetData['chart'];
|
|
5
|
+
series: ChartKitWidgetData['series'];
|
|
6
|
+
preparedXAxis: PreparedAxis;
|
|
7
|
+
preparedY1Axis: PreparedAxis;
|
|
8
|
+
}) => PreparedChart;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { select, max } from 'd3';
|
|
2
|
+
import get from 'lodash/get';
|
|
3
|
+
import { formatAxisTickLabel, getDomainDataYBySeries } from '../../utils';
|
|
4
|
+
import { getHorisontalSvgTextDimensions } from './utils';
|
|
5
|
+
const AXIS_WIDTH = 1;
|
|
6
|
+
const getAxisLabelMaxWidth = (args) => {
|
|
7
|
+
const { axis, series } = args;
|
|
8
|
+
let maxDomainValue;
|
|
9
|
+
let width = 0;
|
|
10
|
+
switch (axis.type) {
|
|
11
|
+
case 'category': {
|
|
12
|
+
const yCatigories = get(axis, 'categories', []);
|
|
13
|
+
maxDomainValue = [...yCatigories].sort((c1, c2) => c2.length - c1.length)[0];
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
case 'datetime': {
|
|
17
|
+
const yTimestamps = get(axis, 'timestamps');
|
|
18
|
+
const domain = yTimestamps || getDomainDataYBySeries(series);
|
|
19
|
+
maxDomainValue = max(domain);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'linear': {
|
|
23
|
+
const domain = getDomainDataYBySeries(series);
|
|
24
|
+
maxDomainValue = max(domain);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const formattedValue = formatAxisTickLabel({
|
|
28
|
+
axisType: axis.type,
|
|
29
|
+
value: maxDomainValue,
|
|
30
|
+
dateFormat: axis.labels.dateFormat,
|
|
31
|
+
numberFormat: axis.labels.numberFormat,
|
|
32
|
+
});
|
|
33
|
+
select(document.body)
|
|
34
|
+
.append('text')
|
|
35
|
+
.style('font-size', axis.labels.style.fontSize)
|
|
36
|
+
.text(formattedValue)
|
|
37
|
+
.each(function () {
|
|
38
|
+
width = this.getBoundingClientRect().width;
|
|
39
|
+
})
|
|
40
|
+
.remove();
|
|
41
|
+
return width;
|
|
42
|
+
};
|
|
43
|
+
export const getPreparedChart = (args) => {
|
|
44
|
+
const { chart, series, preparedXAxis, preparedY1Axis } = args;
|
|
45
|
+
const marginBottom = get(chart, 'margin.bottom', 0) +
|
|
46
|
+
preparedXAxis.labels.padding +
|
|
47
|
+
getHorisontalSvgTextDimensions({ text: 'Tmp', style: preparedXAxis.labels.style });
|
|
48
|
+
const marginLeft = get(chart, 'margin.left', AXIS_WIDTH) +
|
|
49
|
+
preparedY1Axis.labels.padding +
|
|
50
|
+
getAxisLabelMaxWidth({ axis: preparedY1Axis, series }) +
|
|
51
|
+
(preparedY1Axis.title.height || 0);
|
|
52
|
+
return {
|
|
53
|
+
margin: {
|
|
54
|
+
top: get(chart, 'margin.top', 0),
|
|
55
|
+
right: get(chart, 'margin.right', 0),
|
|
56
|
+
bottom: marginBottom,
|
|
57
|
+
left: marginLeft,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getPreparedChart } from './chart';
|
|
3
|
+
import { getPreparedLegend } from './legend';
|
|
4
|
+
import { getPreparedTitle } from './title';
|
|
5
|
+
import { getPreparedTooltip } from './tooltip';
|
|
6
|
+
import { getPreparedXAxis } from './x-axis';
|
|
7
|
+
import { getPreparedYAxis } from './y-axis';
|
|
8
|
+
export const useChartOptions = (args) => {
|
|
9
|
+
const { chart, series, legend, title, tooltip, xAxis, yAxis } = args;
|
|
10
|
+
const options = React.useMemo(() => {
|
|
11
|
+
const preparedTitle = getPreparedTitle({ title });
|
|
12
|
+
const preparedTooltip = getPreparedTooltip({ tooltip });
|
|
13
|
+
const preparedLegend = getPreparedLegend({ legend, series });
|
|
14
|
+
const preparedYAxis = getPreparedYAxis({ yAxis });
|
|
15
|
+
const preparedXAxis = getPreparedXAxis({ xAxis });
|
|
16
|
+
const preparedChart = getPreparedChart({
|
|
17
|
+
chart,
|
|
18
|
+
series,
|
|
19
|
+
preparedXAxis,
|
|
20
|
+
preparedY1Axis: preparedYAxis[0],
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
chart: preparedChart,
|
|
24
|
+
legend: preparedLegend,
|
|
25
|
+
title: preparedTitle,
|
|
26
|
+
tooltip: preparedTooltip,
|
|
27
|
+
xAxis: preparedXAxis,
|
|
28
|
+
yAxis: preparedYAxis,
|
|
29
|
+
};
|
|
30
|
+
}, [chart, legend, title, tooltip, series, xAxis, yAxis]);
|
|
31
|
+
return options;
|
|
32
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ChartKitWidgetData } from '../../../../../types/widget-data';
|
|
2
|
+
import type { PreparedLegend } from './types';
|
|
3
|
+
export declare const getPreparedLegend: (args: {
|
|
4
|
+
legend: ChartKitWidgetData['legend'];
|
|
5
|
+
series: ChartKitWidgetData['series'];
|
|
6
|
+
}) => PreparedLegend;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
import { getHorisontalSvgTextDimensions } from './utils';
|
|
3
|
+
const DEFAULT_TITLE_FONT_SIZE = '15px';
|
|
4
|
+
const TITLE_PADDINGS = 8 * 2;
|
|
5
|
+
export const getPreparedTitle = ({ title, }) => {
|
|
6
|
+
const titleText = get(title, 'text');
|
|
7
|
+
const titleStyle = {
|
|
8
|
+
fontSize: get(title, 'style.fontSize', DEFAULT_TITLE_FONT_SIZE),
|
|
9
|
+
};
|
|
10
|
+
const titleHeight = titleText
|
|
11
|
+
? getHorisontalSvgTextDimensions({ text: titleText, style: titleStyle }) + TITLE_PADDINGS
|
|
12
|
+
: 0;
|
|
13
|
+
const preparedTitle = titleText
|
|
14
|
+
? { text: titleText, style: titleStyle, height: titleHeight }
|
|
15
|
+
: undefined;
|
|
16
|
+
return preparedTitle;
|
|
17
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { BaseTextStyle, ChartKitWidgetData, ChartKitWidgetAxis, ChartKitWidgetAxisType, ChartKitWidgetAxisLabels, ChartKitWidgetLegend, ChartMargin } from '../../../../../types/widget-data';
|
|
2
|
+
type PreparedAxisLabels = Omit<ChartKitWidgetAxisLabels, 'enabled' | 'padding' | 'style'> & Required<Pick<ChartKitWidgetAxisLabels, 'enabled' | 'padding'>> & {
|
|
3
|
+
style: BaseTextStyle;
|
|
4
|
+
};
|
|
5
|
+
export type PreparedChart = {
|
|
6
|
+
margin: ChartMargin;
|
|
7
|
+
};
|
|
8
|
+
export type PreparedLegend = Required<ChartKitWidgetLegend>;
|
|
9
|
+
export type PreparedAxis = Omit<ChartKitWidgetAxis, 'type' | 'labels'> & {
|
|
10
|
+
type: ChartKitWidgetAxisType;
|
|
11
|
+
labels: PreparedAxisLabels;
|
|
12
|
+
title: {
|
|
13
|
+
height: number;
|
|
14
|
+
text: string;
|
|
15
|
+
style: BaseTextStyle;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export type PreparedTitle = ChartKitWidgetData['title'] & {
|
|
19
|
+
height: number;
|
|
20
|
+
};
|
|
21
|
+
export type PreparedTooltip = ChartKitWidgetData['tooltip'] & {
|
|
22
|
+
enabled: boolean;
|
|
23
|
+
};
|
|
24
|
+
export type ChartOptions = {
|
|
25
|
+
chart: PreparedChart;
|
|
26
|
+
legend: PreparedLegend;
|
|
27
|
+
tooltip: PreparedTooltip;
|
|
28
|
+
xAxis: PreparedAxis;
|
|
29
|
+
yAxis: PreparedAxis[];
|
|
30
|
+
title?: PreparedTitle;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
import { DEFAULT_AXIS_LABEL_FONT_SIZE, DEFAULT_AXIS_LABEL_PADDING, DEFAULT_AXIS_TITLE_FONT_SIZE, } from './constants';
|
|
3
|
+
import { getHorisontalSvgTextDimensions } from './utils';
|
|
4
|
+
export const getPreparedXAxis = ({ xAxis }) => {
|
|
5
|
+
const titleText = get(xAxis, 'title.text', '');
|
|
6
|
+
const titleStyle = {
|
|
7
|
+
fontSize: get(xAxis, 'title.style.fontSize', DEFAULT_AXIS_TITLE_FONT_SIZE),
|
|
8
|
+
};
|
|
9
|
+
const preparedXAxis = {
|
|
10
|
+
type: get(xAxis, 'type', 'linear'),
|
|
11
|
+
labels: {
|
|
12
|
+
enabled: get(xAxis, 'labels.enabled', true),
|
|
13
|
+
padding: get(xAxis, 'labels.padding', DEFAULT_AXIS_LABEL_PADDING),
|
|
14
|
+
dateFormat: get(xAxis, 'labels.dateFormat'),
|
|
15
|
+
numberFormat: get(xAxis, 'labels.numberFormat'),
|
|
16
|
+
style: { fontSize: get(xAxis, 'labels.style.fontSize', DEFAULT_AXIS_LABEL_FONT_SIZE) },
|
|
17
|
+
},
|
|
18
|
+
categories: get(xAxis, 'categories'),
|
|
19
|
+
timestamps: get(xAxis, 'timestamps'),
|
|
20
|
+
title: {
|
|
21
|
+
text: titleText,
|
|
22
|
+
style: titleStyle,
|
|
23
|
+
height: titleText
|
|
24
|
+
? getHorisontalSvgTextDimensions({ text: titleText, style: titleStyle })
|
|
25
|
+
: 0,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
return preparedXAxis;
|
|
29
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
import { DEFAULT_AXIS_LABEL_FONT_SIZE, DEFAULT_AXIS_LABEL_PADDING, DEFAULT_AXIS_TITLE_FONT_SIZE, } from './constants';
|
|
3
|
+
import { getHorisontalSvgTextDimensions } from './utils';
|
|
4
|
+
export const getPreparedYAxis = ({ yAxis }) => {
|
|
5
|
+
// FIXME: add support for n axises
|
|
6
|
+
const yAxis1 = yAxis === null || yAxis === void 0 ? void 0 : yAxis[0];
|
|
7
|
+
const y1LabelsStyle = {
|
|
8
|
+
fontSize: get(yAxis1, 'labels.style.fontSize', DEFAULT_AXIS_LABEL_FONT_SIZE),
|
|
9
|
+
};
|
|
10
|
+
const y1TitleText = get(yAxis1, 'title.text', '');
|
|
11
|
+
const y1TitleStyle = {
|
|
12
|
+
fontSize: get(yAxis1, 'title.style.fontSize', DEFAULT_AXIS_TITLE_FONT_SIZE),
|
|
13
|
+
};
|
|
14
|
+
const preparedY1Axis = {
|
|
15
|
+
type: get(yAxis1, 'type', 'linear'),
|
|
16
|
+
labels: {
|
|
17
|
+
enabled: get(yAxis1, 'labels.enabled', true),
|
|
18
|
+
padding: get(yAxis1, 'labels.padding', DEFAULT_AXIS_LABEL_PADDING),
|
|
19
|
+
dateFormat: get(yAxis1, 'labels.dateFormat'),
|
|
20
|
+
numberFormat: get(yAxis1, 'labels.numberFormat'),
|
|
21
|
+
style: y1LabelsStyle,
|
|
22
|
+
},
|
|
23
|
+
categories: get(yAxis1, 'categories'),
|
|
24
|
+
timestamps: get(yAxis1, 'timestamps'),
|
|
25
|
+
title: {
|
|
26
|
+
text: y1TitleText,
|
|
27
|
+
style: y1TitleStyle,
|
|
28
|
+
height: y1TitleText
|
|
29
|
+
? getHorisontalSvgTextDimensions({ text: y1TitleText, style: y1TitleStyle })
|
|
30
|
+
: 0,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
return [preparedY1Axis];
|
|
34
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getVisibleSeriesNames } from '../../utils';
|
|
3
|
+
export const useLegend = (args) => {
|
|
4
|
+
const { series } = args;
|
|
5
|
+
const [activeLegendItems, setActiveLegendItems] = React.useState(getVisibleSeriesNames(series));
|
|
6
|
+
const handleLegendItemClick = React.useCallback(({ name, metaKey }) => {
|
|
7
|
+
const onlyItemSelected = activeLegendItems.length === 1 && activeLegendItems.includes(name);
|
|
8
|
+
let nextActiveLegendItems;
|
|
9
|
+
if (metaKey && activeLegendItems.includes(name)) {
|
|
10
|
+
nextActiveLegendItems = activeLegendItems.filter((item) => item !== name);
|
|
11
|
+
}
|
|
12
|
+
else if (metaKey && !activeLegendItems.includes(name)) {
|
|
13
|
+
nextActiveLegendItems = activeLegendItems.concat(name);
|
|
14
|
+
}
|
|
15
|
+
else if (onlyItemSelected) {
|
|
16
|
+
nextActiveLegendItems = getVisibleSeriesNames(series);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
nextActiveLegendItems = [name];
|
|
20
|
+
}
|
|
21
|
+
setActiveLegendItems(nextActiveLegendItems);
|
|
22
|
+
}, [series, activeLegendItems]);
|
|
23
|
+
// FIXME: remove effect. It initiates extra rerender
|
|
24
|
+
React.useEffect(() => {
|
|
25
|
+
setActiveLegendItems(getVisibleSeriesNames(series));
|
|
26
|
+
}, [series]);
|
|
27
|
+
return { activeLegendItems, handleLegendItemClick };
|
|
28
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ScaleBand, ScaleLinear, ScaleTime } from 'd3';
|
|
2
|
+
import type { ChartKitWidgetSeries } from '../../../../../types/widget-data';
|
|
3
|
+
import type { ChartOptions } from '../useChartOptions/types';
|
|
4
|
+
export type ChartScale = ScaleLinear<number, number> | ScaleBand<string> | ScaleTime<number, number>;
|
|
5
|
+
type Args = {
|
|
6
|
+
boundsWidth: number;
|
|
7
|
+
boundsHeight: number;
|
|
8
|
+
series: ChartKitWidgetSeries[];
|
|
9
|
+
xAxis: ChartOptions['xAxis'];
|
|
10
|
+
yAxis: ChartOptions['yAxis'];
|
|
11
|
+
};
|
|
12
|
+
type ReturnValue = {
|
|
13
|
+
xScale: ChartScale;
|
|
14
|
+
yScale: ChartScale;
|
|
15
|
+
};
|
|
16
|
+
export declare const useScales: (args: Args) => ReturnValue;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { scaleBand, scaleLinear, scaleUtc, extent } from 'd3';
|
|
3
|
+
import get from 'lodash/get';
|
|
4
|
+
import { getOnlyVisibleSeries, getDomainDataXBySeries, getDomainDataYBySeries } from '../../utils';
|
|
5
|
+
const isNumericalArrayData = (data) => {
|
|
6
|
+
return data.every((d) => typeof d === 'number' || d === null);
|
|
7
|
+
};
|
|
8
|
+
const filterCategoriesByVisibleSeries = (categories, series) => {
|
|
9
|
+
return categories.filter((category) => {
|
|
10
|
+
return series.some((s) => {
|
|
11
|
+
return s.data.some((d) => 'category' in d && d.category === category);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
export const useScales = (args) => {
|
|
16
|
+
const { boundsWidth, boundsHeight, series, xAxis, yAxis } = args;
|
|
17
|
+
const scales = React.useMemo(() => {
|
|
18
|
+
const xType = get(xAxis, 'type', 'linear');
|
|
19
|
+
const xCatigories = get(xAxis, 'categories');
|
|
20
|
+
const xTimestamps = get(xAxis, 'timestamps');
|
|
21
|
+
const yType = get(yAxis[0], 'type', 'linear');
|
|
22
|
+
const yCatigories = get(yAxis[0], 'categories');
|
|
23
|
+
const yTimestamps = get(xAxis, 'timestamps');
|
|
24
|
+
let visibleSeries = getOnlyVisibleSeries(series);
|
|
25
|
+
// Reassign to all series in case of all series unselected,
|
|
26
|
+
// otherwise we will get an empty space without grid
|
|
27
|
+
visibleSeries = visibleSeries.length === 0 ? series : visibleSeries;
|
|
28
|
+
let xScale;
|
|
29
|
+
let yScale;
|
|
30
|
+
switch (xType) {
|
|
31
|
+
case 'linear': {
|
|
32
|
+
const domain = getDomainDataXBySeries(visibleSeries);
|
|
33
|
+
if (isNumericalArrayData(domain)) {
|
|
34
|
+
const [xMin, xMax] = extent(domain);
|
|
35
|
+
xScale = scaleLinear().domain([xMin, xMax]).range([0, boundsWidth]).nice();
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case 'category': {
|
|
40
|
+
if (xCatigories) {
|
|
41
|
+
const filteredCategories = filterCategoriesByVisibleSeries(xCatigories, visibleSeries);
|
|
42
|
+
xScale = scaleBand().domain(filteredCategories).range([0, boundsWidth]);
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 'datetime': {
|
|
47
|
+
if (xTimestamps) {
|
|
48
|
+
const [xMin, xMax] = extent(xTimestamps);
|
|
49
|
+
xScale = scaleUtc().domain([xMin, xMax]).range([0, boundsWidth]).nice();
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
const domain = getDomainDataXBySeries(visibleSeries);
|
|
53
|
+
if (isNumericalArrayData(domain)) {
|
|
54
|
+
const [xMin, xMax] = extent(domain);
|
|
55
|
+
xScale = scaleUtc().domain([xMin, xMax]).range([0, boundsWidth]).nice();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!xScale) {
|
|
62
|
+
throw new Error('Failed to create xScale');
|
|
63
|
+
}
|
|
64
|
+
switch (yType) {
|
|
65
|
+
case 'linear': {
|
|
66
|
+
const domain = getDomainDataYBySeries(visibleSeries);
|
|
67
|
+
if (isNumericalArrayData(domain)) {
|
|
68
|
+
const [yMin, yMax] = extent(domain);
|
|
69
|
+
yScale = scaleLinear().domain([yMin, yMax]).range([boundsHeight, 0]).nice();
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case 'category': {
|
|
74
|
+
if (yCatigories) {
|
|
75
|
+
const filteredCategories = filterCategoriesByVisibleSeries(yCatigories, visibleSeries);
|
|
76
|
+
yScale = scaleBand().domain(filteredCategories).range([boundsHeight, 0]);
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case 'datetime': {
|
|
81
|
+
if (yTimestamps) {
|
|
82
|
+
const [yMin, yMax] = extent(yTimestamps);
|
|
83
|
+
yScale = scaleUtc().domain([yMin, yMax]).range([boundsHeight, 0]).nice();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const domain = getDomainDataYBySeries(visibleSeries);
|
|
87
|
+
if (isNumericalArrayData(domain)) {
|
|
88
|
+
const [yMin, yMax] = extent(domain);
|
|
89
|
+
yScale = scaleUtc().domain([yMin, yMax]).range([boundsHeight, 0]).nice();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (!yScale) {
|
|
96
|
+
throw new Error('Failed to create yScale');
|
|
97
|
+
}
|
|
98
|
+
return { xScale, yScale };
|
|
99
|
+
}, [boundsWidth, boundsHeight, series, xAxis, yAxis]);
|
|
100
|
+
return scales;
|
|
101
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ChartKitWidgetSeries } from '../../../../../types/widget-data';
|
|
2
|
+
export type ChartSeries = ChartKitWidgetSeries & {
|
|
3
|
+
color: string;
|
|
4
|
+
name: string;
|
|
5
|
+
visible: boolean;
|
|
6
|
+
};
|
|
7
|
+
type Args = {
|
|
8
|
+
activeLegendItems: string[];
|
|
9
|
+
series: ChartKitWidgetSeries[];
|
|
10
|
+
};
|
|
11
|
+
export declare const useSeries: (args: Args) => {
|
|
12
|
+
chartSeries: ChartSeries[];
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import clone from 'lodash/clone';
|
|
3
|
+
import { scaleOrdinal } from 'd3';
|
|
4
|
+
import { DEFAULT_PALETTE } from '../../constants';
|
|
5
|
+
import { getSeriesNames } from '../../utils';
|
|
6
|
+
export const useSeries = (args) => {
|
|
7
|
+
const { activeLegendItems, series } = args;
|
|
8
|
+
// FIXME: handle case with one pie chart series
|
|
9
|
+
const chartSeries = React.useMemo(() => {
|
|
10
|
+
const seriesNames = getSeriesNames(series);
|
|
11
|
+
const colorScale = scaleOrdinal(seriesNames, DEFAULT_PALETTE);
|
|
12
|
+
return series.map((s) => {
|
|
13
|
+
const preparedSeries = clone(s);
|
|
14
|
+
const name = 'name' in s ? s.name : '';
|
|
15
|
+
const color = 'color' in s && s.color ? s.color : colorScale(name);
|
|
16
|
+
preparedSeries.color = color;
|
|
17
|
+
preparedSeries.name = name;
|
|
18
|
+
preparedSeries.visible = activeLegendItems.includes(name);
|
|
19
|
+
return preparedSeries;
|
|
20
|
+
});
|
|
21
|
+
}, [activeLegendItems, series]);
|
|
22
|
+
return { chartSeries };
|
|
23
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { ChartOptions } from '../useChartOptions/types';
|
|
3
|
+
import type { ChartScale } from '../useScales';
|
|
4
|
+
import type { ChartSeries } from '../useSeries';
|
|
5
|
+
import type { OnSeriesMouseMove, OnSeriesMouseLeave } from '../useTooltip/types';
|
|
6
|
+
type Args = {
|
|
7
|
+
series: ChartSeries[];
|
|
8
|
+
xAxis: ChartOptions['xAxis'];
|
|
9
|
+
xScale: ChartScale;
|
|
10
|
+
yAxis: ChartOptions['yAxis'];
|
|
11
|
+
yScale: ChartScale;
|
|
12
|
+
svgContainer: SVGSVGElement | null;
|
|
13
|
+
onSeriesMouseMove?: OnSeriesMouseMove;
|
|
14
|
+
onSeriesMouseLeave?: OnSeriesMouseLeave;
|
|
15
|
+
};
|
|
16
|
+
export declare const useShapes: (args: Args) => {
|
|
17
|
+
shapes: React.ReactElement<any, string | React.JSXElementConstructor<any>>[];
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import block from 'bem-cn-lite';
|
|
3
|
+
import { pointer } from 'd3';
|
|
4
|
+
import { getOnlyVisibleSeries } from '../../utils';
|
|
5
|
+
const b = block('chartkit-d3-scatter');
|
|
6
|
+
const DEFAULT_SCATTER_POINT_RADIUS = 4;
|
|
7
|
+
const prepareCategoricalScatterData = (data) => {
|
|
8
|
+
return data.filter((d) => typeof d.category === 'string');
|
|
9
|
+
};
|
|
10
|
+
const prepareLinearScatterData = (data) => {
|
|
11
|
+
return data.filter((d) => typeof d.x === 'number' && typeof d.y === 'number');
|
|
12
|
+
};
|
|
13
|
+
const getPointProperties = (args) => {
|
|
14
|
+
const { point, xAxis, xScale, yAxis, yScale } = args;
|
|
15
|
+
const r = point.radius || DEFAULT_SCATTER_POINT_RADIUS;
|
|
16
|
+
let cx;
|
|
17
|
+
let cy;
|
|
18
|
+
if (xAxis.type === 'category') {
|
|
19
|
+
const xBandScale = xScale;
|
|
20
|
+
cx = xBandScale(point.category) + xBandScale.step() / 2;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
const xLinearScale = xScale;
|
|
24
|
+
cx = xLinearScale(point.x);
|
|
25
|
+
}
|
|
26
|
+
if (yAxis[0].type === 'category') {
|
|
27
|
+
const yBandScale = yScale;
|
|
28
|
+
cy = yBandScale(point.category) + yBandScale.step() / 2;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const xLinearScale = yScale;
|
|
32
|
+
cy = xLinearScale(point.y);
|
|
33
|
+
}
|
|
34
|
+
return { r, cx, cy };
|
|
35
|
+
};
|
|
36
|
+
export const useShapes = (args) => {
|
|
37
|
+
const { series, xAxis, xScale, yAxis, yScale, svgContainer, onSeriesMouseMove, onSeriesMouseLeave, } = args;
|
|
38
|
+
const shapes = React.useMemo(() => {
|
|
39
|
+
const visibleSeries = getOnlyVisibleSeries(series);
|
|
40
|
+
return visibleSeries.reduce((acc, s) => {
|
|
41
|
+
var _a;
|
|
42
|
+
const randomKey = Math.random().toString();
|
|
43
|
+
switch (s.type) {
|
|
44
|
+
case 'scatter': {
|
|
45
|
+
const preparedData = xAxis.type === 'category' || ((_a = yAxis[0]) === null || _a === void 0 ? void 0 : _a.type) === 'category'
|
|
46
|
+
? prepareCategoricalScatterData(s.data)
|
|
47
|
+
: prepareLinearScatterData(s.data);
|
|
48
|
+
acc.push(...preparedData.map((point, i) => {
|
|
49
|
+
const pointProps = getPointProperties({
|
|
50
|
+
point,
|
|
51
|
+
xAxis,
|
|
52
|
+
xScale,
|
|
53
|
+
yAxis,
|
|
54
|
+
yScale,
|
|
55
|
+
});
|
|
56
|
+
return (React.createElement("circle", Object.assign({ key: `${i}-${randomKey}`, className: b('point'), fill: s.color }, pointProps, { onMouseMove: function (e) {
|
|
57
|
+
onSeriesMouseMove === null || onSeriesMouseMove === void 0 ? void 0 : onSeriesMouseMove({
|
|
58
|
+
hovered: {
|
|
59
|
+
data: point,
|
|
60
|
+
series: s,
|
|
61
|
+
},
|
|
62
|
+
pointerPosition: pointer(e, svgContainer),
|
|
63
|
+
});
|
|
64
|
+
}, onMouseLeave: onSeriesMouseLeave })));
|
|
65
|
+
}));
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return acc;
|
|
70
|
+
}, []);
|
|
71
|
+
}, [series, xAxis, xScale, yAxis, yScale, svgContainer, onSeriesMouseMove, onSeriesMouseLeave]);
|
|
72
|
+
return { shapes };
|
|
73
|
+
};
|