@gravity-ui/chartkit 4.3.0 → 4.4.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.d.ts +1 -1
- package/build/plugins/d3/renderer/components/Chart.js +1 -0
- package/build/plugins/d3/renderer/hooks/useShapes/bar-x.d.ts +2 -0
- package/build/plugins/d3/renderer/hooks/useShapes/bar-x.js +32 -11
- package/build/plugins/d3/renderer/hooks/useShapes/defaults.d.ts +5 -0
- package/build/plugins/d3/renderer/hooks/useShapes/defaults.js +5 -0
- package/build/plugins/d3/renderer/hooks/useShapes/index.d.ts +2 -0
- package/build/plugins/d3/renderer/hooks/useShapes/index.js +2 -2
- package/build/types/widget-data/series.d.ts +31 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import type { ChartKitWidgetSeriesOptions } from '../../../../../types';
|
|
2
3
|
import type { ChartScale } from '../useAxisScales';
|
|
3
4
|
import type { ChartOptions } from '../useChartOptions/types';
|
|
4
5
|
import type { OnSeriesMouseLeave, OnSeriesMouseMove } from '../useTooltip/types';
|
|
@@ -7,6 +8,7 @@ type Args = {
|
|
|
7
8
|
top: number;
|
|
8
9
|
left: number;
|
|
9
10
|
series: PreparedBarXSeries[];
|
|
11
|
+
seriesOptions?: ChartKitWidgetSeriesOptions;
|
|
10
12
|
xAxis: ChartOptions['xAxis'];
|
|
11
13
|
xScale: ChartScale;
|
|
12
14
|
yAxis: ChartOptions['yAxis'];
|
|
@@ -1,18 +1,35 @@
|
|
|
1
|
-
import { max, pointer, select } from 'd3';
|
|
1
|
+
import { ascending, descending, max, pointer, select, sort } from 'd3';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import get from 'lodash/get';
|
|
4
4
|
import { block } from '../../../../../utils/cn';
|
|
5
5
|
import { getDataCategoryValue } from '../../utils';
|
|
6
|
-
|
|
6
|
+
import { DEFAULT_BAR_X_SERIES_OPTIONS } from './defaults';
|
|
7
7
|
const MIN_RECT_GAP = 1;
|
|
8
|
-
const MAX_RECT_WIDTH = 50;
|
|
9
|
-
const GROUP_PADDING = 0.1;
|
|
10
8
|
const MIN_GROUP_GAP = 1;
|
|
11
9
|
const DEFAULT_LABEL_PADDING = 7;
|
|
12
10
|
const b = block('d3-bar-x');
|
|
13
11
|
function prepareData(args) {
|
|
14
|
-
const { series, xAxis, xScale, yScale } = args;
|
|
12
|
+
const { series, seriesOptions, xAxis, xScale, yScale } = args;
|
|
15
13
|
const categories = get(xAxis, 'categories', []);
|
|
14
|
+
const { barMaxWidth: defaultBarMaxWidth, barPadding: defaultBarPadding, groupPadding: defaultGroupPadding, } = DEFAULT_BAR_X_SERIES_OPTIONS;
|
|
15
|
+
const barMaxWidth = get(seriesOptions, 'bar-x.barMaxWidth', defaultBarMaxWidth);
|
|
16
|
+
const barPadding = get(seriesOptions, 'bar-x.barPadding', defaultBarPadding);
|
|
17
|
+
const groupPadding = get(seriesOptions, 'bar-x.groupPadding', defaultGroupPadding);
|
|
18
|
+
const sortingOptions = get(seriesOptions, 'bar-x.dataSorting');
|
|
19
|
+
const comparator = (sortingOptions === null || sortingOptions === void 0 ? void 0 : sortingOptions.direction) === 'desc' ? descending : ascending;
|
|
20
|
+
const sortKey = (() => {
|
|
21
|
+
switch (sortingOptions === null || sortingOptions === void 0 ? void 0 : sortingOptions.key) {
|
|
22
|
+
case 'y': {
|
|
23
|
+
return 'data.y';
|
|
24
|
+
}
|
|
25
|
+
case 'name': {
|
|
26
|
+
return 'series.name';
|
|
27
|
+
}
|
|
28
|
+
default: {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
16
33
|
const data = {};
|
|
17
34
|
series.forEach((s) => {
|
|
18
35
|
s.data.forEach((d) => {
|
|
@@ -52,17 +69,20 @@ function prepareData(args) {
|
|
|
52
69
|
});
|
|
53
70
|
}
|
|
54
71
|
const maxGroupSize = max(Object.values(data), (d) => Object.values(d).length) || 1;
|
|
55
|
-
const groupGap = Math.max(bandWidth *
|
|
56
|
-
const
|
|
57
|
-
const rectGap = Math.max(
|
|
58
|
-
const rectWidth = Math.min(
|
|
72
|
+
const groupGap = Math.max(bandWidth * groupPadding, MIN_GROUP_GAP);
|
|
73
|
+
const groupWidth = bandWidth - groupGap;
|
|
74
|
+
const rectGap = Math.max(bandWidth * barPadding, MIN_RECT_GAP);
|
|
75
|
+
const rectWidth = Math.min(groupWidth / maxGroupSize - rectGap, barMaxWidth);
|
|
59
76
|
const result = [];
|
|
60
77
|
Object.entries(data).forEach(([xValue, val]) => {
|
|
61
78
|
const stacks = Object.values(val);
|
|
62
79
|
const currentGroupWidth = rectWidth * stacks.length + rectGap * (stacks.length - 1);
|
|
63
80
|
stacks.forEach((yValues, groupItemIndex) => {
|
|
64
81
|
let stackHeight = 0;
|
|
65
|
-
|
|
82
|
+
const sortedData = sortKey
|
|
83
|
+
? sort(yValues, (a, b) => comparator(get(a, sortKey), get(b, sortKey)))
|
|
84
|
+
: yValues;
|
|
85
|
+
sortedData.forEach((yValue) => {
|
|
66
86
|
let xCenter;
|
|
67
87
|
if (xAxis.type === 'category') {
|
|
68
88
|
const xBandScale = xScale;
|
|
@@ -91,7 +111,7 @@ function prepareData(args) {
|
|
|
91
111
|
return result;
|
|
92
112
|
}
|
|
93
113
|
export function BarXSeriesShapes(args) {
|
|
94
|
-
const { top, left, series, xAxis, xScale, yAxis, yScale, onSeriesMouseMove, onSeriesMouseLeave, svgContainer, } = args;
|
|
114
|
+
const { top, left, series, seriesOptions, xAxis, xScale, yAxis, yScale, onSeriesMouseMove, onSeriesMouseLeave, svgContainer, } = args;
|
|
95
115
|
const ref = React.useRef(null);
|
|
96
116
|
React.useEffect(() => {
|
|
97
117
|
if (!ref.current) {
|
|
@@ -101,6 +121,7 @@ export function BarXSeriesShapes(args) {
|
|
|
101
121
|
svgElement.selectAll('*').remove();
|
|
102
122
|
const shapes = prepareData({
|
|
103
123
|
series,
|
|
124
|
+
seriesOptions,
|
|
104
125
|
xAxis,
|
|
105
126
|
xScale,
|
|
106
127
|
yAxis,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import type { ChartKitWidgetSeriesOptions } from '../../../../../types';
|
|
2
3
|
import type { ChartOptions } from '../useChartOptions/types';
|
|
3
4
|
import type { ChartScale } from '../useAxisScales';
|
|
4
5
|
import type { PreparedSeries } from '../';
|
|
@@ -10,6 +11,7 @@ type Args = {
|
|
|
10
11
|
boundsWidth: number;
|
|
11
12
|
boundsHeight: number;
|
|
12
13
|
series: PreparedSeries[];
|
|
14
|
+
seriesOptions?: ChartKitWidgetSeriesOptions;
|
|
13
15
|
xAxis: ChartOptions['xAxis'];
|
|
14
16
|
yAxis: ChartOptions['yAxis'];
|
|
15
17
|
svgContainer: SVGSVGElement | null;
|
|
@@ -7,7 +7,7 @@ import { ScatterSeriesShape } from './scatter';
|
|
|
7
7
|
import { PieSeriesComponent } from './pie';
|
|
8
8
|
import './styles.css';
|
|
9
9
|
export const useShapes = (args) => {
|
|
10
|
-
const { top, left, boundsWidth, boundsHeight, series, xAxis, xScale, yAxis, yScale, svgContainer, onSeriesMouseMove, onSeriesMouseLeave, } = args;
|
|
10
|
+
const { top, left, boundsWidth, boundsHeight, series, seriesOptions, xAxis, xScale, yAxis, yScale, svgContainer, onSeriesMouseMove, onSeriesMouseLeave, } = args;
|
|
11
11
|
const shapes = React.useMemo(() => {
|
|
12
12
|
const visibleSeries = getOnlyVisibleSeries(series);
|
|
13
13
|
const groupedSeries = group(visibleSeries, (item) => item.type);
|
|
@@ -16,7 +16,7 @@ export const useShapes = (args) => {
|
|
|
16
16
|
switch (seriesType) {
|
|
17
17
|
case 'bar-x': {
|
|
18
18
|
if (xScale && yScale) {
|
|
19
|
-
acc.push(React.createElement(BarXSeriesShapes, { key: "bar-x", series: chartSeries, xAxis: xAxis, xScale: xScale, yAxis: yAxis, yScale: yScale, top: top, left: left, svgContainer: svgContainer, onSeriesMouseMove: onSeriesMouseMove, onSeriesMouseLeave: onSeriesMouseLeave }));
|
|
19
|
+
acc.push(React.createElement(BarXSeriesShapes, { key: "bar-x", series: chartSeries, seriesOptions: seriesOptions, xAxis: xAxis, xScale: xScale, yAxis: yAxis, yScale: yScale, top: top, left: left, svgContainer: svgContainer, onSeriesMouseMove: onSeriesMouseMove, onSeriesMouseLeave: onSeriesMouseLeave }));
|
|
20
20
|
}
|
|
21
21
|
break;
|
|
22
22
|
}
|
|
@@ -15,4 +15,35 @@ export type ChartKitWidgetSeriesOptions = {
|
|
|
15
15
|
/** Callback function to render the data label */
|
|
16
16
|
renderer?: (args: DataLabelRendererData) => React.SVGTextElementAttributes<SVGTextElement>;
|
|
17
17
|
};
|
|
18
|
+
'bar-x'?: {
|
|
19
|
+
/** The maximum allowed pixel width for a column.
|
|
20
|
+
* This prevents the columns from becoming too wide when there is a small number of points in the chart.
|
|
21
|
+
*
|
|
22
|
+
* @default 50
|
|
23
|
+
*/
|
|
24
|
+
barMaxWidth?: number;
|
|
25
|
+
/** Padding between each column or bar, in x axis units.
|
|
26
|
+
*
|
|
27
|
+
* @default 0.1
|
|
28
|
+
* */
|
|
29
|
+
barPadding?: number;
|
|
30
|
+
/** Padding between each value groups, in x axis units
|
|
31
|
+
*
|
|
32
|
+
* @default 0.2
|
|
33
|
+
*/
|
|
34
|
+
groupPadding?: number;
|
|
35
|
+
dataSorting?: {
|
|
36
|
+
/** Determines what data value should be used to sort by.
|
|
37
|
+
* Possible values are undefined to disable, "name" to sort by series name or "y"
|
|
38
|
+
*
|
|
39
|
+
* @default undefined
|
|
40
|
+
* */
|
|
41
|
+
key?: 'name' | 'y' | undefined;
|
|
42
|
+
/** Sorting direction.
|
|
43
|
+
*
|
|
44
|
+
* @default 'asc'
|
|
45
|
+
* */
|
|
46
|
+
direction?: 'asc' | 'desc';
|
|
47
|
+
};
|
|
48
|
+
};
|
|
18
49
|
};
|
package/package.json
CHANGED