@gravity-ui/chartkit 4.12.0 → 4.13.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.
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const LineWithMarkers: () => React.JSX.Element;
@@ -0,0 +1,67 @@
1
+ import React from 'react';
2
+ import { ChartKit } from '../../../../components/ChartKit';
3
+ import nintendoGames from '../nintendoGames';
4
+ import { dateTime } from '@gravity-ui/date-utils';
5
+ function prepareData() {
6
+ const dataset = nintendoGames.filter((d) => d.date && d.user_score && new Date(d.date) > new Date(2022, 0, 1));
7
+ const data = dataset.map((d) => ({
8
+ x: d.date || undefined,
9
+ y: d.user_score || undefined,
10
+ custom: d,
11
+ }));
12
+ return {
13
+ series: [
14
+ {
15
+ data,
16
+ name: 'Nintendo games',
17
+ },
18
+ ],
19
+ };
20
+ }
21
+ export const LineWithMarkers = () => {
22
+ const { series } = prepareData();
23
+ const widgetData = {
24
+ series: {
25
+ data: series.map((s) => ({
26
+ type: 'line',
27
+ data: s.data.filter((d) => d.x),
28
+ name: s.name,
29
+ marker: { enabled: true, symbol: 'square' },
30
+ })),
31
+ },
32
+ yAxis: [
33
+ {
34
+ title: {
35
+ text: 'User score',
36
+ },
37
+ },
38
+ ],
39
+ xAxis: {
40
+ type: 'datetime',
41
+ title: {
42
+ text: 'Release dates',
43
+ },
44
+ },
45
+ tooltip: {
46
+ renderer: (d) => {
47
+ var _a;
48
+ const point = (_a = d.hovered[0]) === null || _a === void 0 ? void 0 : _a.data;
49
+ if (!point) {
50
+ return null;
51
+ }
52
+ const title = point.custom.title;
53
+ const score = point.custom.user_score;
54
+ const date = dateTime({ input: point.custom.date }).format('DD MMM YYYY');
55
+ return (React.createElement(React.Fragment, null,
56
+ React.createElement("b", null, title),
57
+ React.createElement("br", null),
58
+ "Release date: ",
59
+ date,
60
+ React.createElement("br", null),
61
+ "User score: ",
62
+ score));
63
+ },
64
+ },
65
+ };
66
+ return React.createElement(ChartKit, { type: "d3", data: widgetData });
67
+ };
@@ -1,13 +1,20 @@
1
1
  import { ScaleOrdinal } from 'd3';
2
2
  import { ChartKitWidgetSeriesOptions, LineSeries } from '../../../../../types';
3
- import { PreparedLegend, PreparedSeries } from './types';
3
+ import { PreparedLineSeries, PreparedLegend } from './types';
4
4
  export declare const DEFAULT_LEGEND_SYMBOL_SIZE = 16;
5
5
  export declare const DEFAULT_LINE_WIDTH = 1;
6
+ export declare const DEFAULT_MARKER: {
7
+ enabled: boolean;
8
+ symbol: string;
9
+ radius: number;
10
+ borderWidth: number;
11
+ borderColor: string;
12
+ };
6
13
  type PrepareLineSeriesArgs = {
7
14
  colorScale: ScaleOrdinal<string, string>;
8
15
  series: LineSeries[];
9
16
  seriesOptions?: ChartKitWidgetSeriesOptions;
10
17
  legend: PreparedLegend;
11
18
  };
12
- export declare function prepareLineSeries(args: PrepareLineSeriesArgs): PreparedSeries[];
19
+ export declare function prepareLineSeries(args: PrepareLineSeriesArgs): PreparedLineSeries[];
13
20
  export {};
@@ -1,8 +1,16 @@
1
1
  import get from 'lodash/get';
2
+ import merge from 'lodash/merge';
2
3
  import { DEFAULT_DATALABELS_PADDING, DEFAULT_DATALABELS_STYLE, DEFAULT_LEGEND_SYMBOL_PADDING, } from './constants';
3
4
  import { getRandomCKId } from '../../../../../utils';
4
5
  export const DEFAULT_LEGEND_SYMBOL_SIZE = 16;
5
6
  export const DEFAULT_LINE_WIDTH = 1;
7
+ export const DEFAULT_MARKER = {
8
+ enabled: false,
9
+ symbol: 'circle',
10
+ radius: 4,
11
+ borderWidth: 0,
12
+ borderColor: '',
13
+ };
6
14
  function prepareLineLegendSymbol(series, seriesOptions) {
7
15
  var _a;
8
16
  const symbolOptions = ((_a = series.legend) === null || _a === void 0 ? void 0 : _a.symbol) || {};
@@ -14,6 +22,28 @@ function prepareLineLegendSymbol(series, seriesOptions) {
14
22
  strokeWidth: get(series, 'lineWidth', defaultLineWidth),
15
23
  };
16
24
  }
25
+ function prepareMarker(series, seriesOptions) {
26
+ var _a;
27
+ const seriesHoverState = get(seriesOptions, 'line.states.hover');
28
+ const markerNormalState = Object.assign({}, DEFAULT_MARKER, (_a = seriesOptions === null || seriesOptions === void 0 ? void 0 : seriesOptions.line) === null || _a === void 0 ? void 0 : _a.marker, series.marker);
29
+ const hoveredMarkerDefaultOptions = {
30
+ enabled: true,
31
+ radius: markerNormalState.radius,
32
+ borderWidth: 1,
33
+ borderColor: '#ffffff',
34
+ halo: {
35
+ enabled: true,
36
+ opacity: 0.25,
37
+ radius: 10,
38
+ },
39
+ };
40
+ return {
41
+ states: {
42
+ normal: markerNormalState,
43
+ hover: merge(hoveredMarkerDefaultOptions, seriesHoverState === null || seriesHoverState === void 0 ? void 0 : seriesHoverState.marker),
44
+ },
45
+ };
46
+ }
17
47
  export function prepareLineSeries(args) {
18
48
  const { colorScale, series: seriesList, seriesOptions, legend } = args;
19
49
  const defaultLineWidth = get(seriesOptions, 'line.lineWidth', DEFAULT_LINE_WIDTH);
@@ -22,7 +52,7 @@ export function prepareLineSeries(args) {
22
52
  const id = getRandomCKId();
23
53
  const name = series.name || '';
24
54
  const color = series.color || colorScale(name);
25
- return {
55
+ const prepared = {
26
56
  type: series.type,
27
57
  color,
28
58
  lineWidth: get(series, 'lineWidth', defaultLineWidth),
@@ -40,6 +70,8 @@ export function prepareLineSeries(args) {
40
70
  padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING),
41
71
  allowOverlap: get(series, 'dataLabels.allowOverlap', false),
42
72
  },
73
+ marker: prepareMarker(series, seriesOptions),
43
74
  };
75
+ return prepared;
44
76
  }, []);
45
77
  }
@@ -104,6 +104,28 @@ export type PreparedLineSeries = {
104
104
  padding: number;
105
105
  allowOverlap: boolean;
106
106
  };
107
+ marker: {
108
+ states: {
109
+ normal: {
110
+ symbol: string;
111
+ enabled: boolean;
112
+ radius: number;
113
+ borderWidth: number;
114
+ borderColor: string;
115
+ };
116
+ hover: {
117
+ enabled: boolean;
118
+ radius: number;
119
+ borderWidth: number;
120
+ borderColor: string;
121
+ halo: {
122
+ enabled: boolean;
123
+ opacity: number;
124
+ radius: number;
125
+ };
126
+ };
127
+ };
128
+ };
107
129
  } & BasePreparedSeries;
108
130
  export type PreparedSeries = PreparedScatterSeries | PreparedBarXSeries | PreparedBarYSeries | PreparedPieSeries | PreparedLineSeries;
109
131
  export type PreparedSeriesOptions = SeriesOptionsDefaults;
@@ -1,10 +1,43 @@
1
1
  import React from 'react';
2
- import { select, line as lineGenerator, color } from 'd3';
2
+ import { color, line as lineGenerator, select, symbol, symbolCircle, symbolSquare } from 'd3';
3
3
  import get from 'lodash/get';
4
4
  import { block } from '../../../../../../utils/cn';
5
5
  import { filterOverlappingLabels } from '../../../utils';
6
6
  import { setActiveState } from '../utils';
7
7
  const b = block('d3-line');
8
+ function setMarker(selection, state) {
9
+ selection
10
+ .attr('d', (d) => {
11
+ const radius = d.point.series.marker.states[state].radius +
12
+ d.point.series.marker.states[state].borderWidth;
13
+ return getMarkerSymbol(d.point.series.marker.states.normal.symbol, radius);
14
+ })
15
+ .attr('stroke-width', (d) => d.point.series.marker.states[state].borderWidth)
16
+ .attr('stroke', (d) => d.point.series.marker.states[state].borderColor);
17
+ }
18
+ function getMarkerSymbol(type, radius) {
19
+ switch (type) {
20
+ case 'square': {
21
+ const size = Math.pow(radius, 2) * Math.PI;
22
+ return symbol(symbolSquare, size)();
23
+ }
24
+ case 'circle':
25
+ default: {
26
+ const size = Math.pow(radius, 2) * Math.PI;
27
+ return symbol(symbolCircle, size)();
28
+ }
29
+ }
30
+ }
31
+ const getMarkerVisibility = (d) => {
32
+ const markerStates = d.point.series.marker.states;
33
+ const enabled = (markerStates.hover.enabled && d.hovered) || markerStates.normal.enabled;
34
+ return enabled ? '' : 'hidden';
35
+ };
36
+ const getMarkerHaloVisibility = (d) => {
37
+ const markerStates = d.point.series.marker.states;
38
+ const enabled = markerStates.hover.halo.enabled && d.hovered;
39
+ return enabled ? '' : 'hidden';
40
+ };
8
41
  export const LineSeriesShapes = (args) => {
9
42
  const { dispatcher, preparedData, seriesOptions } = args;
10
43
  const ref = React.useRef(null);
@@ -48,11 +81,40 @@ export const LineSeriesShapes = (args) => {
48
81
  .style('font-size', (d) => d.style.fontSize)
49
82
  .style('font-weight', (d) => d.style.fontWeight || null)
50
83
  .style('fill', (d) => d.style.fontColor || null);
84
+ const markers = preparedData.reduce((acc, d) => acc.concat(d.markers), []);
85
+ const markerSelection = svgElement
86
+ .selectAll('marker')
87
+ .data(markers)
88
+ .join('g')
89
+ .attr('class', b('marker'))
90
+ .attr('visibility', getMarkerVisibility)
91
+ .attr('transform', (d) => {
92
+ return `translate(${d.point.x},${d.point.y})`;
93
+ });
94
+ markerSelection
95
+ .append('path')
96
+ .attr('class', b('marker-halo'))
97
+ .attr('d', (d) => {
98
+ const type = d.point.series.marker.states.normal.symbol;
99
+ const radius = d.point.series.marker.states.hover.halo.radius;
100
+ return getMarkerSymbol(type, radius);
101
+ })
102
+ .attr('fill', (d) => d.point.series.color)
103
+ .attr('opacity', (d) => d.point.series.marker.states.hover.halo.opacity)
104
+ .attr('z-index', -1)
105
+ .attr('visibility', getMarkerHaloVisibility);
106
+ markerSelection
107
+ .append('path')
108
+ .attr('class', b('marker-symbol'))
109
+ .call(setMarker, 'normal')
110
+ .attr('fill', (d) => d.point.series.color);
51
111
  const hoverEnabled = hoverOptions === null || hoverOptions === void 0 ? void 0 : hoverOptions.enabled;
52
112
  const inactiveEnabled = inactiveOptions === null || inactiveOptions === void 0 ? void 0 : inactiveOptions.enabled;
53
113
  dispatcher.on('hover-shape.line', (data) => {
54
- var _a, _b;
55
- const selectedSeriesId = (_b = (_a = data === null || data === void 0 ? void 0 : data.find((d) => d.series.type === 'line')) === null || _a === void 0 ? void 0 : _a.series) === null || _b === void 0 ? void 0 : _b.id;
114
+ var _a;
115
+ const selected = data === null || data === void 0 ? void 0 : data.find((d) => d.series.type === 'line');
116
+ const selectedDataItem = selected === null || selected === void 0 ? void 0 : selected.data;
117
+ const selectedSeriesId = (_a = selected === null || selected === void 0 ? void 0 : selected.series) === null || _a === void 0 ? void 0 : _a.id;
56
118
  lineSelection.datum((d, index, list) => {
57
119
  const elementSelection = select(list[index]);
58
120
  const hovered = Boolean(hoverEnabled && d.id === selectedSeriesId);
@@ -82,6 +144,32 @@ export const LineSeriesShapes = (args) => {
82
144
  datum: d,
83
145
  });
84
146
  });
147
+ markerSelection.datum((d, index, list) => {
148
+ const elementSelection = select(list[index]);
149
+ const hovered = Boolean(hoverEnabled && d.point.data === selectedDataItem);
150
+ if (d.hovered !== hovered) {
151
+ d.hovered = hovered;
152
+ elementSelection.attr('visibility', getMarkerVisibility(d));
153
+ elementSelection
154
+ .select(`.${b('marker-halo')}`)
155
+ .attr('visibility', getMarkerHaloVisibility);
156
+ elementSelection
157
+ .select(`.${b('marker-symbol')}`)
158
+ .call(setMarker, hovered ? 'hover' : 'normal');
159
+ }
160
+ if (d.point.series.marker.states.normal.enabled) {
161
+ const isActive = Boolean(!inactiveEnabled ||
162
+ !selectedSeriesId ||
163
+ selectedSeriesId === d.point.series.id);
164
+ setActiveState({
165
+ element: list[index],
166
+ state: inactiveOptions,
167
+ active: isActive,
168
+ datum: d,
169
+ });
170
+ }
171
+ return d;
172
+ });
85
173
  });
86
174
  return () => {
87
175
  dispatcher.on('hover-shape.line', null);
@@ -35,14 +35,25 @@ export const prepareLineData = (args) => {
35
35
  const points = s.data.map((d) => ({
36
36
  x: getXValue({ point: d, xAxis, xScale }),
37
37
  y: getYValue({ point: d, yAxis, yScale }),
38
+ active: true,
38
39
  data: d,
40
+ series: s,
39
41
  }));
40
42
  let labels = [];
41
43
  if (s.dataLabels.enabled) {
42
44
  labels = points.map((p) => getLabelData(p, s, xMax));
43
45
  }
46
+ let markers = [];
47
+ if (s.marker.states.normal.enabled || s.marker.states.hover.enabled) {
48
+ markers = points.map((p) => ({
49
+ point: p,
50
+ active: true,
51
+ hovered: false,
52
+ }));
53
+ }
44
54
  acc.push({
45
55
  points,
56
+ markers,
46
57
  labels,
47
58
  color: s.color,
48
59
  width: s.lineWidth,
@@ -5,10 +5,17 @@ export type PointData = {
5
5
  x: number;
6
6
  y: number;
7
7
  data: LineSeriesData;
8
+ series: PreparedLineSeries;
9
+ };
10
+ export type MarkerData = {
11
+ point: PointData;
12
+ active: boolean;
13
+ hovered: boolean;
8
14
  };
9
15
  export type PreparedLineData = {
10
16
  id: string;
11
17
  points: PointData[];
18
+ markers: MarkerData[];
12
19
  color: string;
13
20
  width: number;
14
21
  series: PreparedLineSeries;
@@ -1,5 +1,6 @@
1
1
  import { BaseSeries, BaseSeriesData } from './base';
2
2
  import { ChartKitWidgetLegend, RectLegendSymbolOptions } from './legend';
3
+ import { PointMarkerOptions } from './marker';
3
4
  export type LineSeriesData<T = any> = BaseSeriesData<T> & {
4
5
  /**
5
6
  * The `x` value of the point. Depending on the context , it may represents:
@@ -18,6 +19,10 @@ export type LineSeriesData<T = any> = BaseSeriesData<T> & {
18
19
  /** Data label value of the point. If not specified, the y value is used. */
19
20
  label?: string | number;
20
21
  };
22
+ export type LineMarkerSymbol = 'circle' | 'square';
23
+ export type LineMarkerOptions = PointMarkerOptions & {
24
+ symbol?: LineMarkerSymbol;
25
+ };
21
26
  export type LineSeries<T = any> = BaseSeries & {
22
27
  type: 'line';
23
28
  data: LineSeriesData<T>[];
@@ -34,4 +39,6 @@ export type LineSeries<T = any> = BaseSeries & {
34
39
  legend?: ChartKitWidgetLegend & {
35
40
  symbol?: RectLegendSymbolOptions;
36
41
  };
42
+ /** Options for the point markers of line series */
43
+ marker?: LineMarkerOptions;
37
44
  };
@@ -0,0 +1,10 @@
1
+ export type PointMarkerOptions = {
2
+ /** Enable or disable the point marker */
3
+ enabled?: boolean;
4
+ /** The radius of the point marker */
5
+ radius?: number;
6
+ /** The color of the point marker's border */
7
+ borderColor?: string;
8
+ /** The width of the point marker's border */
9
+ borderWidth?: number;
10
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -2,8 +2,9 @@ import React from 'react';
2
2
  import type { PieSeries, PieSeriesData } from './pie';
3
3
  import type { ScatterSeries, ScatterSeriesData } from './scatter';
4
4
  import type { BarXSeries, BarXSeriesData } from './bar-x';
5
- import type { LineSeries, LineSeriesData } from './line';
5
+ import type { LineSeries, LineSeriesData, LineMarkerOptions } from './line';
6
6
  import type { BarYSeries, BarYSeriesData } from './bar-y';
7
+ import { PointMarkerOptions } from './marker';
7
8
  export type ChartKitWidgetSeries<T = any> = ScatterSeries<T> | PieSeries<T> | BarXSeries<T> | BarYSeries<T> | LineSeries<T>;
8
9
  export type ChartKitWidgetSeriesData<T = any> = ScatterSeriesData<T> | PieSeriesData<T> | BarXSeriesData<T> | BarYSeriesData<T> | LineSeriesData<T>;
9
10
  export type DataLabelRendererData<T = any> = {
@@ -142,9 +143,20 @@ export type ChartKitWidgetSeriesOptions = {
142
143
  lineWidth?: number;
143
144
  /** Options for the series states that provide additional styling information to the series. */
144
145
  states?: {
145
- hover?: BasicHoverState;
146
+ hover?: BasicHoverState & {
147
+ marker?: PointMarkerOptions & {
148
+ /** Options for the halo appearing around the hovered point */
149
+ halo?: {
150
+ enabled?: boolean;
151
+ opacity?: number;
152
+ radius?: number;
153
+ };
154
+ };
155
+ };
146
156
  inactive?: BasicInactiveState;
147
157
  };
158
+ /** Options for the point markers of line series */
159
+ marker?: LineMarkerOptions;
148
160
  };
149
161
  };
150
162
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "4.12.0",
3
+ "version": "4.13.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",
@@ -48,7 +48,7 @@
48
48
  "dependencies": {
49
49
  "@bem-react/classname": "^1.6.0",
50
50
  "@gravity-ui/date-utils": "^1.4.1",
51
- "@gravity-ui/yagr": "^4.0.1",
51
+ "@gravity-ui/yagr": "^4.0.3",
52
52
  "afterframe": "^1.0.2",
53
53
  "d3": "^7.8.5",
54
54
  "lodash": "^4.17.21",