@gravity-ui/chartkit 4.3.0 → 4.4.1

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.
@@ -4,24 +4,6 @@ import { block } from '../../../../utils/cn';
4
4
  import { formatAxisTickLabel, parseTransformStyle } from '../utils';
5
5
  const b = block('d3-axis');
6
6
  const EMPTY_SPACE_BETWEEN_LABELS = 10;
7
- // Note: this method do not prepared for rotated labels
8
- const removeOverlappingXTicks = (axis) => {
9
- var _a;
10
- const a = axis.selectAll('g.tick').nodes();
11
- if (a.length <= 1) {
12
- return;
13
- }
14
- for (let i = 0, x = 0; i < a.length; i++) {
15
- const node = a[i];
16
- const r = node.getBoundingClientRect();
17
- if (r.left < x) {
18
- (_a = node === null || node === void 0 ? void 0 : node.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(node);
19
- }
20
- else {
21
- x = r.right + EMPTY_SPACE_BETWEEN_LABELS;
22
- }
23
- }
24
- };
25
7
  // FIXME: add overflow ellipsis for the labels that out of boundaries
26
8
  export const AxisX = ({ axis, width, height, scale }) => {
27
9
  const ref = React.useRef(null);
@@ -75,7 +57,19 @@ export const AxisX = ({ axis, width, height, scale }) => {
75
57
  .attr('font-size', axis.title.style.fontSize)
76
58
  .text(axis.title.text);
77
59
  }
78
- removeOverlappingXTicks(svgElement);
60
+ let elementX = 0;
61
+ svgElement
62
+ .selectAll('.tick')
63
+ .filter(function () {
64
+ const node = this;
65
+ const r = node.getBoundingClientRect();
66
+ if (r.left < elementX) {
67
+ return true;
68
+ }
69
+ elementX = r.right + EMPTY_SPACE_BETWEEN_LABELS;
70
+ return false;
71
+ })
72
+ .remove();
79
73
  }, [axis, width, height, scale]);
80
74
  return React.createElement("g", { ref: ref });
81
75
  };
@@ -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,
@@ -7,15 +7,15 @@ const isNumericalArrayData = (data) => {
7
7
  };
8
8
  const filterCategoriesByVisibleSeries = (args) => {
9
9
  const { axisDirection, categories, series } = args;
10
- return categories.filter((category) => {
11
- return series.some((s) => {
12
- return (isSeriesWithCategoryValues(s) &&
13
- s.data.some((d) => {
14
- const dataCategory = getDataCategoryValue({ axisDirection, categories, data: d });
15
- return dataCategory === category;
16
- }));
17
- });
10
+ const visibleCategories = new Set();
11
+ series.forEach((s) => {
12
+ if (isSeriesWithCategoryValues(s)) {
13
+ s.data.forEach((d) => {
14
+ visibleCategories.add(getDataCategoryValue({ axisDirection, categories, data: d }));
15
+ });
16
+ }
18
17
  });
18
+ return categories.filter((c) => visibleCategories.has(c));
19
19
  };
20
20
  const createScales = (args) => {
21
21
  const { boundsWidth, boundsHeight, series, xAxis, yAxis } = args;
@@ -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;
@@ -1,13 +1,12 @@
1
1
  import React from 'react';
2
2
  import { group } from 'd3';
3
- import { getRandomCKId } from '../../../../../utils';
4
3
  import { getOnlyVisibleSeries } from '../../utils';
5
4
  import { BarXSeriesShapes } from './bar-x';
6
5
  import { ScatterSeriesShape } from './scatter';
7
6
  import { PieSeriesComponent } from './pie';
8
7
  import './styles.css';
9
8
  export const useShapes = (args) => {
10
- const { top, left, boundsWidth, boundsHeight, series, xAxis, xScale, yAxis, yScale, svgContainer, onSeriesMouseMove, onSeriesMouseLeave, } = args;
9
+ const { top, left, boundsWidth, boundsHeight, series, seriesOptions, xAxis, xScale, yAxis, yScale, svgContainer, onSeriesMouseMove, onSeriesMouseLeave, } = args;
11
10
  const shapes = React.useMemo(() => {
12
11
  const visibleSeries = getOnlyVisibleSeries(series);
13
12
  const groupedSeries = group(visibleSeries, (item) => item.type);
@@ -16,15 +15,14 @@ export const useShapes = (args) => {
16
15
  switch (seriesType) {
17
16
  case 'bar-x': {
18
17
  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 }));
18
+ 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
19
  }
21
20
  break;
22
21
  }
23
22
  case 'scatter': {
24
23
  if (xScale && yScale) {
25
24
  const scatterShapes = chartSeries.map((scatterSeries, i) => {
26
- const id = getRandomCKId();
27
- return (React.createElement(ScatterSeriesShape, { key: `${i}-${id}`, top: top, left: left, series: scatterSeries, xAxis: xAxis, xScale: xScale, yAxis: yAxis, yScale: yScale, onSeriesMouseMove: onSeriesMouseMove, onSeriesMouseLeave: onSeriesMouseLeave, svgContainer: svgContainer }));
25
+ return (React.createElement(ScatterSeriesShape, { key: i, top: top, left: left, series: scatterSeries, xAxis: xAxis, xScale: xScale, yAxis: yAxis, yScale: yScale, onSeriesMouseMove: onSeriesMouseMove, onSeriesMouseLeave: onSeriesMouseLeave, svgContainer: svgContainer }));
28
26
  });
29
27
  acc.push(...scatterShapes);
30
28
  }
@@ -47,16 +47,13 @@ export function ScatterSeriesShape(props) {
47
47
  return;
48
48
  }
49
49
  const svgElement = select(ref.current);
50
- svgElement.selectAll('*').remove();
51
50
  const preparedData = xAxis.type === 'category' || ((_a = yAxis[0]) === null || _a === void 0 ? void 0 : _a.type) === 'category'
52
51
  ? series.data
53
52
  : prepareLinearScatterData(series.data);
54
53
  svgElement
55
- .selectAll('allPoints')
54
+ .selectAll('circle')
56
55
  .data(preparedData)
57
- .enter()
58
- .append('circle')
59
- .attr('class', b('point'))
56
+ .join((enter) => enter.append('circle').attr('class', b('point')), (update) => update, (exit) => exit.remove())
60
57
  .attr('fill', (d) => d.color || series.color || '')
61
58
  .attr('r', (d) => d.radius || DEFAULT_SCATTER_POINT_RADIUS)
62
59
  .attr('cx', (d) => getCxAttr({ point: d, xAxis, xScale }))
@@ -150,8 +150,5 @@ const extractCategoryValue = (args) => {
150
150
  export const getDataCategoryValue = (args) => {
151
151
  const { axisDirection, categories, data } = args;
152
152
  const categoryValue = extractCategoryValue({ axisDirection, categories, data });
153
- if (!categories.includes(categoryValue)) {
154
- throw new Error('It seems you are trying to use category value that is not in categories array');
155
- }
156
153
  return categoryValue;
157
154
  };
@@ -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.1",
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",