@gravity-ui/chartkit 4.6.0 → 4.7.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.
Files changed (99) hide show
  1. package/build/plugins/d3/examples/bar-x/Basic.d.ts +4 -0
  2. package/build/plugins/d3/examples/bar-x/Basic.js +78 -0
  3. package/build/plugins/d3/examples/bar-x/GroupedColumns.d.ts +2 -0
  4. package/build/plugins/d3/examples/bar-x/GroupedColumns.js +44 -0
  5. package/build/plugins/d3/examples/bar-x/StackedColumns.d.ts +2 -0
  6. package/build/plugins/d3/examples/bar-x/StackedColumns.js +45 -0
  7. package/build/plugins/d3/examples/nintendoGames.d.ts +62 -0
  8. package/build/plugins/d3/examples/nintendoGames.js +12037 -0
  9. package/build/plugins/d3/examples/pie/Basic.d.ts +2 -0
  10. package/build/plugins/d3/examples/pie/Basic.js +30 -0
  11. package/build/plugins/d3/examples/pie/Donut.d.ts +2 -0
  12. package/build/plugins/d3/examples/pie/Donut.js +31 -0
  13. package/build/plugins/d3/examples/scatter/Basic.d.ts +2 -0
  14. package/build/plugins/d3/examples/scatter/Basic.js +66 -0
  15. package/build/plugins/d3/renderer/D3Widget.js +11 -1
  16. package/build/plugins/d3/renderer/components/AxisX.d.ts +1 -2
  17. package/build/plugins/d3/renderer/components/AxisX.js +8 -21
  18. package/build/plugins/d3/renderer/components/AxisY.js +50 -18
  19. package/build/plugins/d3/renderer/components/Chart.js +25 -17
  20. package/build/plugins/d3/renderer/components/Legend.js +20 -22
  21. package/build/plugins/d3/renderer/components/Title.js +1 -1
  22. package/build/plugins/d3/renderer/components/Tooltip/DefaultContent.d.ts +2 -2
  23. package/build/plugins/d3/renderer/components/Tooltip/DefaultContent.js +8 -0
  24. package/build/plugins/d3/renderer/components/Tooltip/TooltipTriggerArea.d.ts +14 -0
  25. package/build/plugins/d3/renderer/components/Tooltip/TooltipTriggerArea.js +70 -0
  26. package/build/plugins/d3/renderer/components/Tooltip/index.d.ts +5 -3
  27. package/build/plugins/d3/renderer/components/Tooltip/index.js +4 -2
  28. package/build/plugins/d3/renderer/components/styles.css +3 -0
  29. package/build/plugins/d3/renderer/constants/defaults/axis.d.ts +9 -0
  30. package/build/plugins/d3/renderer/constants/defaults/axis.js +6 -0
  31. package/build/plugins/d3/renderer/constants/defaults/index.d.ts +1 -0
  32. package/build/plugins/d3/renderer/constants/defaults/index.js +1 -0
  33. package/build/plugins/d3/renderer/constants/defaults/series-options.d.ts +11 -0
  34. package/build/plugins/d3/renderer/constants/defaults/series-options.js +41 -0
  35. package/build/plugins/d3/renderer/constants/index.d.ts +0 -1
  36. package/build/plugins/d3/renderer/constants/index.js +0 -1
  37. package/build/plugins/d3/renderer/d3-dispatcher.d.ts +1 -0
  38. package/build/plugins/d3/renderer/d3-dispatcher.js +4 -0
  39. package/build/plugins/d3/renderer/hooks/index.d.ts +0 -1
  40. package/build/plugins/d3/renderer/hooks/index.js +0 -1
  41. package/build/plugins/d3/renderer/hooks/useAxisScales/index.d.ts +6 -5
  42. package/build/plugins/d3/renderer/hooks/useAxisScales/index.js +2 -1
  43. package/build/plugins/d3/renderer/hooks/useChartDimensions/index.js +8 -41
  44. package/build/plugins/d3/renderer/hooks/useChartDimensions/utils.d.ts +3 -0
  45. package/build/plugins/d3/renderer/hooks/useChartDimensions/utils.js +17 -4
  46. package/build/plugins/d3/renderer/hooks/useChartOptions/chart.d.ts +2 -4
  47. package/build/plugins/d3/renderer/hooks/useChartOptions/chart.js +4 -23
  48. package/build/plugins/d3/renderer/hooks/useChartOptions/index.d.ts +1 -1
  49. package/build/plugins/d3/renderer/hooks/useChartOptions/index.js +2 -10
  50. package/build/plugins/d3/renderer/hooks/useChartOptions/title.d.ts +1 -1
  51. package/build/plugins/d3/renderer/hooks/useChartOptions/title.js +1 -0
  52. package/build/plugins/d3/renderer/hooks/useChartOptions/types.d.ts +8 -5
  53. package/build/plugins/d3/renderer/hooks/useChartOptions/x-axis.d.ts +5 -3
  54. package/build/plugins/d3/renderer/hooks/useChartOptions/x-axis.js +61 -6
  55. package/build/plugins/d3/renderer/hooks/useChartOptions/y-axis.d.ts +1 -1
  56. package/build/plugins/d3/renderer/hooks/useChartOptions/y-axis.js +20 -12
  57. package/build/plugins/d3/renderer/hooks/useSeries/index.d.ts +1 -0
  58. package/build/plugins/d3/renderer/hooks/useSeries/index.js +8 -2
  59. package/build/plugins/d3/renderer/hooks/useSeries/prepare-legend.js +8 -3
  60. package/build/plugins/d3/renderer/hooks/useSeries/prepare-options.d.ts +3 -0
  61. package/build/plugins/d3/renderer/hooks/useSeries/prepare-options.js +5 -0
  62. package/build/plugins/d3/renderer/hooks/useSeries/prepareSeries.js +6 -3
  63. package/build/plugins/d3/renderer/hooks/useSeries/types.d.ts +5 -1
  64. package/build/plugins/d3/renderer/hooks/useShapes/bar-x/index.d.ts +12 -0
  65. package/build/plugins/d3/renderer/hooks/useShapes/bar-x/index.js +91 -0
  66. package/build/plugins/d3/renderer/hooks/useShapes/bar-x/prepare-data.d.ts +19 -0
  67. package/build/plugins/d3/renderer/hooks/useShapes/{bar-x.js → bar-x/prepare-data.js} +9 -88
  68. package/build/plugins/d3/renderer/hooks/useShapes/index.d.ts +13 -10
  69. package/build/plugins/d3/renderer/hooks/useShapes/index.js +28 -13
  70. package/build/plugins/d3/renderer/hooks/useShapes/pie.d.ts +6 -4
  71. package/build/plugins/d3/renderer/hooks/useShapes/pie.js +98 -20
  72. package/build/plugins/d3/renderer/hooks/useShapes/scatter/index.d.ts +15 -0
  73. package/build/plugins/d3/renderer/hooks/useShapes/scatter/index.js +89 -0
  74. package/build/plugins/d3/renderer/hooks/useShapes/scatter/prepare-data.d.ts +19 -0
  75. package/build/plugins/d3/renderer/hooks/useShapes/scatter/prepare-data.js +55 -0
  76. package/build/plugins/d3/renderer/hooks/useShapes/styles.css +1 -9
  77. package/build/plugins/d3/renderer/hooks/useTooltip/index.d.ts +6 -6
  78. package/build/plugins/d3/renderer/hooks/useTooltip/index.js +15 -17
  79. package/build/plugins/d3/renderer/hooks/useTooltip/types.d.ts +0 -6
  80. package/build/plugins/d3/renderer/utils/axis-generators/bottom.d.ts +3 -1
  81. package/build/plugins/d3/renderer/utils/axis-generators/bottom.js +77 -38
  82. package/build/plugins/d3/renderer/utils/axis.js +0 -6
  83. package/build/plugins/d3/renderer/utils/index.d.ts +5 -0
  84. package/build/plugins/d3/renderer/utils/index.js +13 -8
  85. package/build/plugins/d3/renderer/utils/math.d.ts +2 -0
  86. package/build/plugins/d3/renderer/utils/math.js +8 -0
  87. package/build/plugins/d3/renderer/utils/text.d.ts +6 -6
  88. package/build/plugins/d3/renderer/utils/text.js +25 -15
  89. package/build/types/widget-data/axis.d.ts +10 -0
  90. package/build/types/widget-data/series.d.ts +51 -0
  91. package/build/types/widget-data/tooltip.d.ts +18 -7
  92. package/package.json +2 -2
  93. package/build/plugins/d3/renderer/hooks/useChartEvents/index.d.ts +0 -5
  94. package/build/plugins/d3/renderer/hooks/useChartEvents/index.js +0 -15
  95. package/build/plugins/d3/renderer/hooks/useShapes/bar-x.d.ts +0 -21
  96. package/build/plugins/d3/renderer/hooks/useShapes/defaults.d.ts +0 -5
  97. package/build/plugins/d3/renderer/hooks/useShapes/defaults.js +0 -5
  98. package/build/plugins/d3/renderer/hooks/useShapes/scatter.d.ts +0 -19
  99. package/build/plugins/d3/renderer/hooks/useShapes/scatter.js +0 -89
@@ -6,6 +6,9 @@ export * from './text';
6
6
  export * from './time';
7
7
  export * from './axis';
8
8
  export type AxisDirection = 'x' | 'y';
9
+ export type NodeWithD3Data<T = unknown> = Element & {
10
+ __data__: T;
11
+ };
9
12
  type UnknownSeries = {
10
13
  type: ChartKitWidgetSeries['type'];
11
14
  data: unknown;
@@ -68,3 +71,5 @@ export declare const getDataCategoryValue: (args: {
68
71
  data: ChartKitWidgetSeriesData;
69
72
  }) => string;
70
73
  export declare function getClosestPointsRange(axis: PreparedAxis, points: AxisDomain[]): number | undefined;
74
+ export declare const isNodeContainsD3Data: (node?: Element | null) => node is NodeWithD3Data<unknown>;
75
+ export declare const extractD3DataFromNode: <T extends unknown>(node: NodeWithD3Data<T>) => T;
@@ -120,18 +120,16 @@ export const formatAxisTickLabel = (args) => {
120
120
  * @return {number} The height of the text element.
121
121
  */
122
122
  export const getHorisontalSvgTextHeight = (args) => {
123
+ var _a;
123
124
  const { text, style } = args;
124
- const textSelection = select(document.body).append('text').text(text);
125
+ const container = select(document.body).append('svg');
126
+ const textSelection = container.append('text').text(text);
125
127
  const fontSize = get(style, 'fontSize', DEFAULT_AXIS_LABEL_FONT_SIZE);
126
- let height = 0;
127
128
  if (fontSize) {
128
- textSelection.style('font-size', fontSize);
129
+ textSelection.style('font-size', fontSize).style('alignment-baseline', 'after-edge');
129
130
  }
130
- textSelection
131
- .each(function () {
132
- height = this.getBoundingClientRect().height;
133
- })
134
- .remove();
131
+ const height = ((_a = textSelection.node()) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().height) || 0;
132
+ container.remove();
135
133
  return height;
136
134
  };
137
135
  const extractCategoryValue = (args) => {
@@ -163,3 +161,10 @@ export function getClosestPointsRange(axis, points) {
163
161
  }
164
162
  return points[1] - points[0];
165
163
  }
164
+ // https://d3js.org/d3-selection/joining#selection_data
165
+ export const isNodeContainsD3Data = (node) => {
166
+ return Boolean(node && '__data__' in node);
167
+ };
168
+ export const extractD3DataFromNode = (node) => {
169
+ return node.__data__;
170
+ };
@@ -21,3 +21,5 @@ export declare const calculateNumericProperty: (args: {
21
21
  value?: string | number | null;
22
22
  base?: number;
23
23
  }) => number | undefined;
24
+ export declare function calculateCos(deg: number, precision?: number): number;
25
+ export declare function calculateSin(deg: number, precision?: number): number;
@@ -41,3 +41,11 @@ export const calculateNumericProperty = (args) => {
41
41
  }
42
42
  return value;
43
43
  };
44
+ export function calculateCos(deg, precision = 2) {
45
+ const factor = Math.pow(10, precision);
46
+ return Math.floor(Math.cos((Math.PI / 180) * deg) * factor) / factor;
47
+ }
48
+ export function calculateSin(deg, precision = 2) {
49
+ const factor = Math.pow(10, precision);
50
+ return Math.floor(Math.sin((Math.PI / 180) * deg) * factor) / factor;
51
+ }
@@ -8,13 +8,13 @@ export declare function hasOverlappingLabels({ width, labels, padding, style, }:
8
8
  style?: BaseTextStyle;
9
9
  padding?: number;
10
10
  }): boolean;
11
- export declare function getLabelsMaxWidth({ labels, style, transform, }: {
11
+ export declare function getLabelsMaxWidth({ labels, style, rotation, }: {
12
12
  labels: string[];
13
- style?: BaseTextStyle;
14
- transform?: string;
13
+ style?: Record<string, string>;
14
+ rotation?: number;
15
15
  }): number;
16
- export declare function getLabelsMaxHeight({ labels, style, transform, }: {
16
+ export declare function getLabelsMaxHeight({ labels, style, rotation, }: {
17
17
  labels: string[];
18
- style?: BaseTextStyle;
19
- transform?: string;
18
+ style?: Record<string, string>;
19
+ rotation?: number;
20
20
  }): number;
@@ -1,14 +1,14 @@
1
1
  import { select } from 'd3';
2
2
  export function setEllipsisForOverflowText(selection, maxWidth) {
3
- var _a, _b;
3
+ var _a, _b, _c, _d;
4
4
  let text = selection.text();
5
- selection.text(null).attr('text-anchor', 'left').append('title').text(text);
6
- const tSpan = selection.append('tspan').text(text);
7
- let textLength = ((_a = tSpan.node()) === null || _a === void 0 ? void 0 : _a.getComputedTextLength()) || 0;
5
+ selection.text(null).append('title').text(text);
6
+ const tSpan = selection.append('tspan').text(text).style('alignment-baseline', 'inherit');
7
+ let textLength = ((_b = (_a = tSpan.node()) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect()) === null || _b === void 0 ? void 0 : _b.width) || 0;
8
8
  while (textLength > maxWidth && text.length > 1) {
9
9
  text = text.slice(0, -1);
10
10
  tSpan.text(text + '…');
11
- textLength = ((_b = tSpan.node()) === null || _b === void 0 ? void 0 : _b.getComputedTextLength()) || 0;
11
+ textLength = ((_d = (_c = tSpan.node()) === null || _c === void 0 ? void 0 : _c.getBoundingClientRect()) === null || _d === void 0 ? void 0 : _d.width) || 0;
12
12
  }
13
13
  }
14
14
  export function setEllipsisForOverflowTexts(selection, maxWidth) {
@@ -29,12 +29,14 @@ export function hasOverlappingLabels({ width, labels, padding = 0, style, }) {
29
29
  textElement.remove();
30
30
  return result;
31
31
  }
32
- function renderLabels(selection, { labels, style, transform, }) {
33
- const text = selection
34
- .append('g')
35
- .append('text')
36
- .attr('transform', transform || '')
37
- .style('font-size', (style === null || style === void 0 ? void 0 : style.fontSize) || '');
32
+ function renderLabels(selection, { labels, style = {}, attrs = {}, }) {
33
+ const text = selection.append('g').append('text');
34
+ Object.entries(style).forEach(([name, value]) => {
35
+ text.style(name, value);
36
+ });
37
+ Object.entries(attrs).forEach(([name, value]) => {
38
+ text.attr(name, value);
39
+ });
38
40
  text.selectAll('tspan')
39
41
  .data(labels)
40
42
  .enter()
@@ -44,18 +46,26 @@ function renderLabels(selection, { labels, style, transform, }) {
44
46
  .text((d) => d);
45
47
  return text;
46
48
  }
47
- export function getLabelsMaxWidth({ labels, style, transform, }) {
49
+ export function getLabelsMaxWidth({ labels, style, rotation, }) {
48
50
  var _a, _b;
49
51
  const svg = select(document.body).append('svg');
50
- svg.call(renderLabels, { labels, style, transform });
52
+ const textSelection = renderLabels(svg, { labels, style });
53
+ if (rotation) {
54
+ textSelection.attr('text-anchor', 'end').style('transform', `rotate(${rotation}deg)`);
55
+ }
51
56
  const maxWidth = ((_b = (_a = svg.select('g').node()) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect()) === null || _b === void 0 ? void 0 : _b.width) || 0;
52
57
  svg.remove();
53
58
  return maxWidth;
54
59
  }
55
- export function getLabelsMaxHeight({ labels, style, transform, }) {
60
+ export function getLabelsMaxHeight({ labels, style, rotation, }) {
56
61
  var _a, _b;
57
62
  const svg = select(document.body).append('svg');
58
- svg.call(renderLabels, { labels, style, transform });
63
+ const textSelection = renderLabels(svg, { labels, style });
64
+ if (rotation) {
65
+ textSelection
66
+ .attr('text-anchor', rotation > 0 ? 'start' : 'end')
67
+ .style('transform', `rotate(${rotation}deg)`);
68
+ }
59
69
  const maxHeight = ((_b = (_a = svg.select('g').node()) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect()) === null || _b === void 0 ? void 0 : _b.height) || 0;
60
70
  svg.remove();
61
71
  return maxHeight;
@@ -21,6 +21,11 @@ export type ChartKitWidgetAxisLabels = {
21
21
  * If there is enough space, labels are not rotated.
22
22
  * As the chart gets narrower, it will start rotating the labels -45 degrees. */
23
23
  autoRotation?: boolean;
24
+ /** Rotation of the labels in degrees.
25
+ *
26
+ * @default: 0
27
+ */
28
+ rotation?: number;
24
29
  };
25
30
  export type ChartKitWidgetAxis = {
26
31
  categories?: string[];
@@ -32,6 +37,11 @@ export type ChartKitWidgetAxis = {
32
37
  lineColor?: string;
33
38
  title?: {
34
39
  text?: string;
40
+ /** The pixel distance between the axis labels or line and the title.
41
+ *
42
+ * Defaults to 4 for horizontal axes, 8 for vertical.
43
+ * */
44
+ margin?: number;
35
45
  };
36
46
  /** The minimum value of the axis. If undefined the min value is automatically calculate. */
37
47
  min?: number;
@@ -7,6 +7,37 @@ export type ChartKitWidgetSeriesData<T = any> = ScatterSeriesData<T> | PieSeries
7
7
  export type DataLabelRendererData<T = any> = {
8
8
  data: ChartKitWidgetSeriesData<T>;
9
9
  };
10
+ type BasicHoverState = {
11
+ /**
12
+ * Enable separate styles for the hovered series.
13
+ *
14
+ * @default true
15
+ * */
16
+ enabled?: boolean;
17
+ /**
18
+ * How much to brighten/darken the point on hover. Use positive to brighten, negative to darken.
19
+ * The behavior of this property is dependent on the implementing color space ([more details](https://d3js.org/d3-color#color_brighter)).
20
+ * For example in case of using rgb color you can use floating point number from `-5.0` to `5.0`.
21
+ * Rgb color space is used by default.
22
+ *
23
+ * @default 0.3
24
+ */
25
+ brightness?: number;
26
+ };
27
+ type BasicInactiveState = {
28
+ /**
29
+ * Enable separate styles for the inactive series.
30
+ *
31
+ * @default true
32
+ * */
33
+ enabled?: boolean;
34
+ /**
35
+ * Opacity of series elements (bars, data labels)
36
+ *
37
+ * @default 0.5
38
+ * */
39
+ opacity?: number;
40
+ };
10
41
  export type ChartKitWidgetSeriesOptions = {
11
42
  /** Individual data label for each point. */
12
43
  dataLabels?: {
@@ -45,5 +76,25 @@ export type ChartKitWidgetSeriesOptions = {
45
76
  * */
46
77
  direction?: 'asc' | 'desc';
47
78
  };
79
+ /** Options for the series states that provide additional styling information to the series. */
80
+ states?: {
81
+ hover?: BasicHoverState;
82
+ inactive?: BasicInactiveState;
83
+ };
84
+ };
85
+ pie?: {
86
+ /** Options for the series states that provide additional styling information to the series. */
87
+ states?: {
88
+ hover?: BasicHoverState;
89
+ inactive?: BasicInactiveState;
90
+ };
91
+ };
92
+ scatter?: {
93
+ /** Options for the series states that provide additional styling information to the series. */
94
+ states?: {
95
+ hover?: BasicHoverState;
96
+ inactive?: BasicInactiveState;
97
+ };
48
98
  };
49
99
  };
100
+ export {};
@@ -1,13 +1,24 @@
1
1
  /// <reference types="react" />
2
- import type { ChartKitWidgetSeries, ChartKitWidgetSeriesData } from './series';
3
- export type TooltipHoveredData<T = any> = {
4
- data: ChartKitWidgetSeriesData<T>;
5
- series: ChartKitWidgetSeries;
2
+ import type { BarXSeries, BarXSeriesData } from './bar-x';
3
+ import type { PieSeries, PieSeriesData } from './pie';
4
+ import type { ScatterSeries, ScatterSeriesData } from './scatter';
5
+ export type TooltipDataChunkBarX<T = any> = {
6
+ data: BarXSeriesData<T>;
7
+ series: BarXSeries<T>;
6
8
  };
9
+ export type TooltipDataChunkPie<T = any> = {
10
+ data: PieSeriesData<T>;
11
+ series: Omit<PieSeries<T>, 'data'>;
12
+ };
13
+ export type TooltipDataChunkScatter<T = any> = {
14
+ data: ScatterSeriesData<T>;
15
+ series: ScatterSeries<T>;
16
+ };
17
+ export type TooltipDataChunk<T = any> = TooltipDataChunkBarX<T> | TooltipDataChunkPie<T> | TooltipDataChunkScatter<T>;
7
18
  export type ChartKitWidgetTooltip<T = any> = {
8
19
  enabled?: boolean;
9
20
  /** Specifies the renderer for the tooltip. If returned null default tooltip renderer will be used. */
10
- renderer?: (data: {
11
- hovered: TooltipHoveredData<T>;
12
- }) => React.ReactElement;
21
+ renderer?: (args: {
22
+ hovered: TooltipDataChunk<T>[];
23
+ }) => React.ReactElement | null;
13
24
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "4.6.0",
3
+ "version": "4.7.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": "^3.8.0",
51
+ "@gravity-ui/yagr": "^3.10.0",
52
52
  "d3": "^7.8.5",
53
53
  "lodash": "^4.17.21",
54
54
  "react-split-pane": "^0.1.92"
@@ -1,5 +0,0 @@
1
- export declare const useChartEvents: () => {
2
- chartHovered: boolean;
3
- handleMouseEnter: () => void;
4
- handleMouseLeave: () => void;
5
- };
@@ -1,15 +0,0 @@
1
- import React from 'react';
2
- export const useChartEvents = () => {
3
- const [chartHovered, setChartHovered] = React.useState(false);
4
- const handleMouseEnter = React.useCallback(() => {
5
- setChartHovered(true);
6
- }, []);
7
- const handleMouseLeave = React.useCallback(() => {
8
- setChartHovered(false);
9
- }, []);
10
- return {
11
- chartHovered,
12
- handleMouseEnter,
13
- handleMouseLeave,
14
- };
15
- };
@@ -1,21 +0,0 @@
1
- import React from 'react';
2
- import type { ChartKitWidgetSeriesOptions } from '../../../../../types';
3
- import type { ChartScale } from '../useAxisScales';
4
- import type { ChartOptions } from '../useChartOptions/types';
5
- import type { OnSeriesMouseLeave, OnSeriesMouseMove } from '../useTooltip/types';
6
- import type { PreparedBarXSeries } from '../useSeries/types';
7
- type Args = {
8
- top: number;
9
- left: number;
10
- series: PreparedBarXSeries[];
11
- seriesOptions?: ChartKitWidgetSeriesOptions;
12
- xAxis: ChartOptions['xAxis'];
13
- xScale: ChartScale;
14
- yAxis: ChartOptions['yAxis'];
15
- yScale: ChartScale;
16
- onSeriesMouseMove?: OnSeriesMouseMove;
17
- onSeriesMouseLeave?: OnSeriesMouseLeave;
18
- svgContainer: SVGSVGElement | null;
19
- };
20
- export declare function BarXSeriesShapes(args: Args): React.JSX.Element;
21
- export {};
@@ -1,5 +0,0 @@
1
- export declare const DEFAULT_BAR_X_SERIES_OPTIONS: {
2
- barMaxWidth: number;
3
- barPadding: number;
4
- groupPadding: number;
5
- };
@@ -1,5 +0,0 @@
1
- export const DEFAULT_BAR_X_SERIES_OPTIONS = {
2
- barMaxWidth: 50,
3
- barPadding: 0.1,
4
- groupPadding: 0.2,
5
- };
@@ -1,19 +0,0 @@
1
- import React from 'react';
2
- import type { ScatterSeries } from '../../../../../types/widget-data';
3
- import type { ChartScale } from '../useAxisScales';
4
- import type { PreparedAxis } from '../useChartOptions/types';
5
- import type { OnSeriesMouseLeave, OnSeriesMouseMove } from '../useTooltip/types';
6
- type ScatterSeriesShapeProps = {
7
- top: number;
8
- left: number;
9
- series: ScatterSeries;
10
- xAxis: PreparedAxis;
11
- xScale: ChartScale;
12
- yAxis: PreparedAxis[];
13
- yScale: ChartScale;
14
- svgContainer: SVGSVGElement | null;
15
- onSeriesMouseMove?: OnSeriesMouseMove;
16
- onSeriesMouseLeave?: OnSeriesMouseLeave;
17
- };
18
- export declare function ScatterSeriesShape(props: ScatterSeriesShapeProps): React.JSX.Element;
19
- export {};
@@ -1,89 +0,0 @@
1
- import React from 'react';
2
- import { pointer, select } from 'd3';
3
- import get from 'lodash/get';
4
- import { block } from '../../../../../utils/cn';
5
- import { getDataCategoryValue } from '../../utils';
6
- const b = block('d3-scatter');
7
- const DEFAULT_SCATTER_POINT_RADIUS = 4;
8
- const prepareLinearScatterData = (data) => {
9
- return data.filter((d) => typeof d.x === 'number' && typeof d.y === 'number');
10
- };
11
- const getCxAttr = (args) => {
12
- const { point, xAxis, xScale } = args;
13
- let cx;
14
- if (xAxis.type === 'category') {
15
- const xBandScale = xScale;
16
- const categories = get(xAxis, 'categories', []);
17
- const dataCategory = getDataCategoryValue({ axisDirection: 'x', categories, data: point });
18
- cx = (xBandScale(dataCategory) || 0) + xBandScale.step() / 2;
19
- }
20
- else {
21
- const xLinearScale = xScale;
22
- cx = xLinearScale(point.x);
23
- }
24
- return cx;
25
- };
26
- const getCyAttr = (args) => {
27
- const { point, yAxis, yScale } = args;
28
- let cy;
29
- if (yAxis.type === 'category') {
30
- const yBandScale = yScale;
31
- const categories = get(yAxis, 'categories', []);
32
- const dataCategory = getDataCategoryValue({ axisDirection: 'y', categories, data: point });
33
- cy = (yBandScale(dataCategory) || 0) + yBandScale.step() / 2;
34
- }
35
- else {
36
- const yLinearScale = yScale;
37
- cy = yLinearScale(point.y);
38
- }
39
- return cy;
40
- };
41
- export function ScatterSeriesShape(props) {
42
- const { series, xAxis, xScale, yAxis, yScale, svgContainer, left, top, onSeriesMouseMove, onSeriesMouseLeave, } = props;
43
- const ref = React.useRef(null);
44
- React.useEffect(() => {
45
- var _a;
46
- if (!ref.current) {
47
- return;
48
- }
49
- const svgElement = select(ref.current);
50
- const preparedData = xAxis.type === 'category' || ((_a = yAxis[0]) === null || _a === void 0 ? void 0 : _a.type) === 'category'
51
- ? series.data
52
- : prepareLinearScatterData(series.data);
53
- svgElement
54
- .selectAll('circle')
55
- .data(preparedData)
56
- .join((enter) => enter.append('circle').attr('class', b('point')), (update) => update, (exit) => exit.remove())
57
- .attr('fill', (d) => d.color || series.color || '')
58
- .attr('r', (d) => d.radius || DEFAULT_SCATTER_POINT_RADIUS)
59
- .attr('cx', (d) => getCxAttr({ point: d, xAxis, xScale }))
60
- .attr('cy', (d) => getCyAttr({ point: d, yAxis: yAxis[0], yScale }))
61
- .on('mousemove', (e, d) => {
62
- const [x, y] = pointer(e, svgContainer);
63
- onSeriesMouseMove === null || onSeriesMouseMove === void 0 ? void 0 : onSeriesMouseMove({
64
- hovered: {
65
- data: d,
66
- series,
67
- },
68
- pointerPosition: [x - left, y - top],
69
- });
70
- })
71
- .on('mouseleave', () => {
72
- if (onSeriesMouseLeave) {
73
- onSeriesMouseLeave();
74
- }
75
- });
76
- }, [
77
- series,
78
- xAxis,
79
- xScale,
80
- yAxis,
81
- yScale,
82
- svgContainer,
83
- left,
84
- top,
85
- onSeriesMouseMove,
86
- onSeriesMouseLeave,
87
- ]);
88
- return React.createElement("g", { ref: ref, className: b() });
89
- }