@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.
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import type { ChartKitWidgetData } from '../../../../types/widget-data';
2
+ import type { ChartKitWidgetData } from '../../../../types';
3
3
  import './styles.css';
4
4
  type Props = {
5
5
  top: number;
@@ -37,6 +37,7 @@ export const Chart = (props) => {
37
37
  boundsWidth,
38
38
  boundsHeight,
39
39
  series: preparedSeries,
40
+ seriesOptions: data.series.options,
40
41
  xAxis,
41
42
  xScale,
42
43
  yAxis,
@@ -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
- const RECT_PADDING = 0.1;
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 * GROUP_PADDING, MIN_GROUP_GAP);
56
- const maxGroupWidth = bandWidth - groupGap;
57
- const rectGap = Math.max((maxGroupWidth / maxGroupSize) * RECT_PADDING, MIN_RECT_GAP);
58
- const rectWidth = Math.min(maxGroupWidth / maxGroupSize - rectGap, MAX_RECT_WIDTH);
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
- yValues.forEach((yValue) => {
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,
@@ -0,0 +1,5 @@
1
+ export declare const DEFAULT_BAR_X_SERIES_OPTIONS: {
2
+ barMaxWidth: number;
3
+ barPadding: number;
4
+ groupPadding: number;
5
+ };
@@ -0,0 +1,5 @@
1
+ export const DEFAULT_BAR_X_SERIES_OPTIONS = {
2
+ barMaxWidth: 50,
3
+ barPadding: 0.1,
4
+ groupPadding: 0.2,
5
+ };
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "description": "React component used to render charts based on any sources you need",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:gravity-ui/ChartKit.git",