@gravity-ui/chartkit 4.2.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,7 +1,7 @@
1
1
  import React from 'react';
2
- import type { ChartOptions, ChartScale } from '../hooks';
2
+ import type { ChartScale, PreparedAxis } from '../hooks';
3
3
  type Props = {
4
- axis: ChartOptions['xAxis'];
4
+ axis: PreparedAxis;
5
5
  width: number;
6
6
  height: number;
7
7
  scale: ChartScale;
@@ -51,7 +51,10 @@ export const AxisX = ({ axis, width, height, scale }) => {
51
51
  xAxisGenerator = xAxisGenerator.ticks(ticksCount);
52
52
  }
53
53
  svgElement.call(xAxisGenerator).attr('class', b());
54
- svgElement.select('.domain').attr('d', `M0,0V0H${width}`);
54
+ svgElement
55
+ .select('.domain')
56
+ .attr('d', `M0,0V0H${width}`)
57
+ .style('stroke', axis.lineColor || '');
55
58
  if (axis.labels.enabled) {
56
59
  svgElement.selectAll('.tick text').style('font-size', axis.labels.style.fontSize);
57
60
  }
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
- import type { ChartOptions, ChartScale } from '../hooks';
2
+ import type { ChartScale, PreparedAxis } from '../hooks';
3
3
  type Props = {
4
- axises: ChartOptions['yAxis'];
4
+ axises: PreparedAxis[];
5
5
  width: number;
6
6
  height: number;
7
7
  scale: ChartScale;
@@ -52,7 +52,10 @@ export const AxisY = ({ axises, width, height, scale }) => {
52
52
  yAxisGenerator = yAxisGenerator.ticks(ticksCount);
53
53
  }
54
54
  svgElement.call(yAxisGenerator).attr('class', b());
55
- svgElement.select('.domain').attr('d', `M0,${height}H0V0`);
55
+ svgElement
56
+ .select('.domain')
57
+ .attr('d', `M0,${height}H0V0`)
58
+ .style('stroke', axis.lineColor || '');
56
59
  if (axis.labels.enabled) {
57
60
  svgElement
58
61
  .selectAll('.tick text')
@@ -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 isNil from 'lodash/isNil';
2
3
  import { block } from '../../../../../utils/cn';
3
4
  import { DefaultContent } from './DefaultContent';
4
5
  const b = block('d3-tooltip');
@@ -32,13 +33,12 @@ export const Tooltip = (props) => {
32
33
  return undefined;
33
34
  }, [hovered, pointerPosition, size]);
34
35
  const content = React.useMemo(() => {
35
- if (tooltip.renderer && hovered) {
36
- return tooltip.renderer({ hovered });
36
+ var _a;
37
+ if (!hovered) {
38
+ return null;
37
39
  }
38
- else if (hovered) {
39
- return React.createElement(DefaultContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis });
40
- }
41
- return null;
40
+ const customTooltip = (_a = tooltip.renderer) === null || _a === void 0 ? void 0 : _a.call(tooltip, { hovered });
41
+ return isNil(customTooltip) ? (React.createElement(DefaultContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis })) : (customTooltip);
42
42
  }, [hovered, tooltip, xAxis, yAxis]);
43
43
  if (!position || !hovered) {
44
44
  return null;
@@ -15,6 +15,7 @@ export const getPreparedXAxis = ({ xAxis }) => {
15
15
  numberFormat: get(xAxis, 'labels.numberFormat'),
16
16
  style: { fontSize: get(xAxis, 'labels.style.fontSize', DEFAULT_AXIS_LABEL_FONT_SIZE) },
17
17
  },
18
+ lineColor: get(xAxis, 'lineColor'),
18
19
  categories: get(xAxis, 'categories'),
19
20
  timestamps: get(xAxis, 'timestamps'),
20
21
  title: {
@@ -20,6 +20,7 @@ export const getPreparedYAxis = ({ yAxis }) => {
20
20
  numberFormat: get(yAxis1, 'labels.numberFormat'),
21
21
  style: y1LabelsStyle,
22
22
  },
23
+ lineColor: get(yAxis1, 'lineColor'),
23
24
  categories: get(yAxis1, 'categories'),
24
25
  timestamps: get(yAxis1, 'timestamps'),
25
26
  title: {
@@ -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
  }
@@ -2,9 +2,9 @@ import type { FormatNumberOptions } from '../../plugins/shared';
2
2
  import type { BaseTextStyle } from './base';
3
3
  export type ChartKitWidgetAxisType = 'category' | 'datetime' | 'linear';
4
4
  export type ChartKitWidgetAxisLabels = {
5
- /** Enable or disable the axis labels */
5
+ /** Enable or disable the axis labels. */
6
6
  enabled?: boolean;
7
- /** The pixel padding for axis labels */
7
+ /** The pixel padding for axis labels. */
8
8
  padding?: number;
9
9
  dateFormat?: string;
10
10
  numberFormat?: FormatNumberOptions;
@@ -14,13 +14,16 @@ export type ChartKitWidgetAxis = {
14
14
  categories?: string[];
15
15
  timestamps?: number[];
16
16
  type?: ChartKitWidgetAxisType;
17
- /** The axis labels show the number or category for each tick */
17
+ /** The axis labels show the number or category for each tick. */
18
18
  labels?: ChartKitWidgetAxisLabels;
19
+ /** The color of the line marking the axis itself. */
20
+ lineColor?: string;
19
21
  title?: {
20
22
  text?: string;
21
23
  };
22
- /** The minimum value of the axis. If undefined the min value is automatically calculate */
24
+ /** The minimum value of the axis. If undefined the min value is automatically calculate. */
23
25
  min?: number;
26
+ /** The grid lines settings. */
24
27
  grid?: {
25
28
  /** Enable or disable the grid lines.
26
29
  *
@@ -36,7 +39,7 @@ export type ChartKitWidgetAxis = {
36
39
  /** Padding of the max value relative to the length of the axis.
37
40
  * A padding of 0.05 will make a 100px axis 5px longer.
38
41
  *
39
- * Defaults to 0.05 for Y axis and to 0.01 for X axis
42
+ * Defaults to 0.05 for Y axis and to 0.01 for X axis.
40
43
  * */
41
44
  maxPadding?: number;
42
45
  };
@@ -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
  };
@@ -6,6 +6,7 @@ export type TooltipHoveredData<T = any> = {
6
6
  };
7
7
  export type ChartKitWidgetTooltip<T = any> = {
8
8
  enabled?: boolean;
9
+ /** Specifies the renderer for the tooltip. If returned null default tooltip renderer will be used. */
9
10
  renderer?: (data: {
10
11
  hovered: TooltipHoveredData<T>;
11
12
  }) => React.ReactElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "4.2.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",