@gravity-ui/charts 0.5.0 → 0.6.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.
Files changed (91) hide show
  1. package/dist/cjs/components/ChartInner/index.d.ts +2 -8
  2. package/dist/cjs/components/ChartInner/index.js +22 -117
  3. package/dist/cjs/components/ChartInner/types.d.ts +6 -0
  4. package/dist/cjs/components/ChartInner/useChartInnerHandlers.d.ts +26 -0
  5. package/dist/cjs/components/ChartInner/useChartInnerHandlers.js +94 -0
  6. package/dist/cjs/components/ChartInner/useChartInnerProps.d.ts +43 -0
  7. package/dist/cjs/components/ChartInner/useChartInnerProps.js +81 -0
  8. package/dist/cjs/components/ChartInner/useChartInnerState.d.ts +13 -0
  9. package/dist/cjs/components/ChartInner/useChartInnerState.js +34 -0
  10. package/dist/cjs/components/Legend/index.d.ts +1 -0
  11. package/dist/cjs/components/Legend/index.js +3 -2
  12. package/dist/cjs/components/Tooltip/index.d.ts +2 -0
  13. package/dist/cjs/components/Tooltip/index.js +10 -4
  14. package/dist/cjs/components/Tooltip/styles.css +13 -8
  15. package/dist/cjs/hooks/index.d.ts +2 -1
  16. package/dist/cjs/hooks/index.js +2 -1
  17. package/dist/cjs/hooks/usePrevious/index.d.ts +1 -0
  18. package/dist/cjs/hooks/usePrevious/index.js +8 -0
  19. package/dist/cjs/hooks/useShapes/area/index.js +8 -2
  20. package/dist/cjs/hooks/useShapes/area/prepare-data.js +4 -0
  21. package/dist/cjs/hooks/useShapes/bar-x/index.js +8 -2
  22. package/dist/cjs/hooks/useShapes/bar-x/prepare-data.js +1 -0
  23. package/dist/cjs/hooks/useShapes/bar-y/index.js +8 -2
  24. package/dist/cjs/hooks/useShapes/bar-y/prepare-data.js +1 -0
  25. package/dist/cjs/hooks/useShapes/line/index.js +8 -2
  26. package/dist/cjs/hooks/useShapes/line/prepare-data.js +1 -0
  27. package/dist/cjs/hooks/useShapes/pie/index.js +2 -1
  28. package/dist/cjs/hooks/useShapes/pie/prepare-data.js +159 -111
  29. package/dist/cjs/hooks/useShapes/pie/types.d.ts +1 -1
  30. package/dist/cjs/hooks/useShapes/scatter/index.js +8 -2
  31. package/dist/cjs/hooks/useShapes/treemap/index.js +8 -2
  32. package/dist/cjs/hooks/useShapes/treemap/prepare-data.js +1 -0
  33. package/dist/cjs/hooks/useShapes/utils.d.ts +2 -2
  34. package/dist/cjs/hooks/useShapes/utils.js +5 -3
  35. package/dist/cjs/hooks/useShapes/waterfall/index.js +8 -2
  36. package/dist/cjs/hooks/useTooltip/index.d.ts +2 -3
  37. package/dist/cjs/types/chart/chart.d.ts +2 -2
  38. package/dist/cjs/types/chart/series.d.ts +1 -2
  39. package/dist/cjs/types/chart/tooltip.d.ts +6 -6
  40. package/dist/cjs/types/chart-ui.d.ts +4 -0
  41. package/dist/cjs/types/misc.d.ts +1 -0
  42. package/dist/cjs/utils/misc.d.ts +10 -2
  43. package/dist/cjs/utils/misc.js +15 -3
  44. package/dist/esm/components/ChartInner/index.d.ts +2 -8
  45. package/dist/esm/components/ChartInner/index.js +22 -117
  46. package/dist/esm/components/ChartInner/types.d.ts +6 -0
  47. package/dist/esm/components/ChartInner/useChartInnerHandlers.d.ts +26 -0
  48. package/dist/esm/components/ChartInner/useChartInnerHandlers.js +94 -0
  49. package/dist/esm/components/ChartInner/useChartInnerProps.d.ts +43 -0
  50. package/dist/esm/components/ChartInner/useChartInnerProps.js +81 -0
  51. package/dist/esm/components/ChartInner/useChartInnerState.d.ts +13 -0
  52. package/dist/esm/components/ChartInner/useChartInnerState.js +34 -0
  53. package/dist/esm/components/Legend/index.d.ts +1 -0
  54. package/dist/esm/components/Legend/index.js +3 -2
  55. package/dist/esm/components/Tooltip/index.d.ts +2 -0
  56. package/dist/esm/components/Tooltip/index.js +10 -4
  57. package/dist/esm/components/Tooltip/styles.css +13 -8
  58. package/dist/esm/hooks/index.d.ts +2 -1
  59. package/dist/esm/hooks/index.js +2 -1
  60. package/dist/esm/hooks/usePrevious/index.d.ts +1 -0
  61. package/dist/esm/hooks/usePrevious/index.js +8 -0
  62. package/dist/esm/hooks/useShapes/area/index.js +8 -2
  63. package/dist/esm/hooks/useShapes/area/prepare-data.js +4 -0
  64. package/dist/esm/hooks/useShapes/bar-x/index.js +8 -2
  65. package/dist/esm/hooks/useShapes/bar-x/prepare-data.js +1 -0
  66. package/dist/esm/hooks/useShapes/bar-y/index.js +8 -2
  67. package/dist/esm/hooks/useShapes/bar-y/prepare-data.js +1 -0
  68. package/dist/esm/hooks/useShapes/line/index.js +8 -2
  69. package/dist/esm/hooks/useShapes/line/prepare-data.js +1 -0
  70. package/dist/esm/hooks/useShapes/pie/index.js +2 -1
  71. package/dist/esm/hooks/useShapes/pie/prepare-data.js +159 -111
  72. package/dist/esm/hooks/useShapes/pie/types.d.ts +1 -1
  73. package/dist/esm/hooks/useShapes/scatter/index.js +8 -2
  74. package/dist/esm/hooks/useShapes/treemap/index.js +8 -2
  75. package/dist/esm/hooks/useShapes/treemap/prepare-data.js +1 -0
  76. package/dist/esm/hooks/useShapes/utils.d.ts +2 -2
  77. package/dist/esm/hooks/useShapes/utils.js +5 -3
  78. package/dist/esm/hooks/useShapes/waterfall/index.js +8 -2
  79. package/dist/esm/hooks/useTooltip/index.d.ts +2 -3
  80. package/dist/esm/types/chart/chart.d.ts +2 -2
  81. package/dist/esm/types/chart/series.d.ts +1 -2
  82. package/dist/esm/types/chart/tooltip.d.ts +6 -6
  83. package/dist/esm/types/chart-ui.d.ts +4 -0
  84. package/dist/esm/types/misc.d.ts +1 -0
  85. package/dist/esm/utils/misc.d.ts +10 -2
  86. package/dist/esm/utils/misc.js +15 -3
  87. package/package.json +1 -1
  88. package/dist/cjs/hooks/useTooltip/types.d.ts +0 -1
  89. package/dist/esm/hooks/useTooltip/types.d.ts +0 -1
  90. /package/dist/cjs/{hooks/useTooltip → components/ChartInner}/types.js +0 -0
  91. /package/dist/esm/{hooks/useTooltip → components/ChartInner}/types.js +0 -0
@@ -9,6 +9,7 @@ export { prepareScatterData } from './prepare-data';
9
9
  const b = block('d3-scatter');
10
10
  export function ScatterSeriesShape(props) {
11
11
  const { dispatcher, preparedData, seriesOptions, htmlLayout } = props;
12
+ const hoveredDataRef = React.useRef(null);
12
13
  const ref = React.useRef(null);
13
14
  React.useEffect(() => {
14
15
  if (!ref.current) {
@@ -28,8 +29,9 @@ export function ScatterSeriesShape(props) {
28
29
  .attr('cursor', (d) => d.point.series.cursor);
29
30
  const hoverEnabled = hoverOptions === null || hoverOptions === void 0 ? void 0 : hoverOptions.enabled;
30
31
  const inactiveEnabled = inactiveOptions === null || inactiveOptions === void 0 ? void 0 : inactiveOptions.enabled;
31
- dispatcher.on('hover-shape.scatter', (data) => {
32
+ function handleShapeHover(data) {
32
33
  var _a;
34
+ hoveredDataRef.current = data;
33
35
  const selected = data === null || data === void 0 ? void 0 : data.find((d) => d.series.type === 'scatter');
34
36
  const selectedDataItem = selected === null || selected === void 0 ? void 0 : selected.data;
35
37
  const selectedSeriesId = (_a = selected === null || selected === void 0 ? void 0 : selected.series) === null || _a === void 0 ? void 0 : _a.id;
@@ -58,7 +60,11 @@ export function ScatterSeriesShape(props) {
58
60
  }
59
61
  return d;
60
62
  });
61
- });
63
+ }
64
+ if (hoveredDataRef.current !== null) {
65
+ handleShapeHover(hoveredDataRef.current);
66
+ }
67
+ dispatcher.on('hover-shape.scatter', handleShapeHover);
62
68
  return () => {
63
69
  dispatcher.on('hover-shape.scatter', null);
64
70
  };
@@ -6,6 +6,7 @@ import { HtmlLayer } from '../HtmlLayer';
6
6
  const b = block('d3-treemap');
7
7
  export const TreemapSeriesShape = (props) => {
8
8
  const { dispatcher, preparedData, seriesOptions, htmlLayout } = props;
9
+ const hoveredDataRef = React.useRef(null);
9
10
  const ref = React.useRef(null);
10
11
  React.useEffect(() => {
11
12
  if (!ref.current) {
@@ -48,8 +49,9 @@ export const TreemapSeriesShape = (props) => {
48
49
  const eventName = `hover-shape.treemap`;
49
50
  const hoverOptions = get(seriesOptions, 'treemap.states.hover');
50
51
  const inactiveOptions = get(seriesOptions, 'treemap.states.inactive');
51
- dispatcher.on(eventName, (data) => {
52
+ function handleShapeHover(data) {
52
53
  var _a;
54
+ hoveredDataRef.current = data;
53
55
  const hoverEnabled = hoverOptions === null || hoverOptions === void 0 ? void 0 : hoverOptions.enabled;
54
56
  const inactiveEnabled = inactiveOptions === null || inactiveOptions === void 0 ? void 0 : inactiveOptions.enabled;
55
57
  const hoveredData = (_a = data === null || data === void 0 ? void 0 : data[0]) === null || _a === void 0 ? void 0 : _a.data;
@@ -87,7 +89,11 @@ export const TreemapSeriesShape = (props) => {
87
89
  });
88
90
  return d;
89
91
  });
90
- });
92
+ }
93
+ if (hoveredDataRef.current !== null) {
94
+ handleShapeHover(hoveredDataRef.current);
95
+ }
96
+ dispatcher.on(eventName, handleShapeHover);
91
97
  return () => {
92
98
  dispatcher.on(eventName, null);
93
99
  };
@@ -33,6 +33,7 @@ function getLabels(args) {
33
33
  content: text,
34
34
  x,
35
35
  y,
36
+ size: { width, height: lineHeight },
36
37
  }
37
38
  : {
38
39
  text,
@@ -17,7 +17,7 @@ export declare function getYValue(args: {
17
17
  yAxis: PreparedAxis;
18
18
  yScale: ChartScale;
19
19
  }): number;
20
- export declare const shapeKey: (d: unknown) => string | -1;
20
+ export declare function shapeKey(d: unknown): string | -1;
21
21
  export declare function setActiveState<T extends {
22
22
  active?: boolean;
23
23
  }>(args: {
@@ -26,4 +26,4 @@ export declare function setActiveState<T extends {
26
26
  state: BasicInactiveState | undefined;
27
27
  active: boolean;
28
28
  }): T;
29
- export declare const getLineDashArray: (dashStyle: DashStyle, strokeWidth?: number) => string;
29
+ export declare function getLineDashArray(dashStyle: DashStyle, strokeWidth?: number): string;
@@ -23,7 +23,9 @@ export function getYValue(args) {
23
23
  const yLinearScale = yScale;
24
24
  return yLinearScale(point.y);
25
25
  }
26
- export const shapeKey = (d) => d.id || -1;
26
+ export function shapeKey(d) {
27
+ return d.id || -1;
28
+ }
27
29
  export function setActiveState(args) {
28
30
  const { element, datum, state, active } = args;
29
31
  const elementSelection = select(element);
@@ -34,7 +36,7 @@ export function setActiveState(args) {
34
36
  }
35
37
  return datum;
36
38
  }
37
- export const getLineDashArray = (dashStyle, strokeWidth = 2) => {
39
+ export function getLineDashArray(dashStyle, strokeWidth = 2) {
38
40
  const value = dashStyle.toLowerCase();
39
41
  const arrayValue = value
40
42
  .replace('shortdashdotdot', '3,1,1,1,1,1,')
@@ -50,4 +52,4 @@ export const getLineDashArray = (dashStyle, strokeWidth = 2) => {
50
52
  return `${parseInt(part, 10) * strokeWidth}`;
51
53
  });
52
54
  return arrayValue.join(',').replace(/NaN/g, 'none');
53
- };
55
+ }
@@ -10,6 +10,7 @@ export * from './types';
10
10
  const b = block('d3-waterfall');
11
11
  export const WaterfallSeriesShapes = (args) => {
12
12
  const { dispatcher, preparedData, seriesOptions, htmlLayout } = args;
13
+ const hoveredDataRef = React.useRef(null);
13
14
  const ref = React.useRef(null);
14
15
  const connectorSelector = `.${b('connector')}`;
15
16
  React.useEffect(() => {
@@ -78,7 +79,8 @@ export const WaterfallSeriesShapes = (args) => {
78
79
  })
79
80
  .attr('stroke-width', 1)
80
81
  .attr('stroke-dasharray', () => getLineDashArray(DashStyle.Dash, 1));
81
- dispatcher.on('hover-shape.waterfall', (data) => {
82
+ function handleShapeHover(data) {
83
+ hoveredDataRef.current = data;
82
84
  const hoverEnabled = hoverOptions === null || hoverOptions === void 0 ? void 0 : hoverOptions.enabled;
83
85
  const inactiveEnabled = inactiveOptions === null || inactiveOptions === void 0 ? void 0 : inactiveOptions.enabled;
84
86
  if (!data) {
@@ -116,7 +118,11 @@ export const WaterfallSeriesShapes = (args) => {
116
118
  : (inactiveOptions === null || inactiveOptions === void 0 ? void 0 : inactiveOptions.opacity) || null;
117
119
  });
118
120
  }
119
- });
121
+ }
122
+ if (hoveredDataRef.current !== null) {
123
+ handleShapeHover(hoveredDataRef.current);
124
+ }
125
+ dispatcher.on('hover-shape.waterfall', handleShapeHover);
120
126
  return () => {
121
127
  dispatcher.on('hover-shape.waterfall', null);
122
128
  };
@@ -1,13 +1,12 @@
1
1
  import type { Dispatch } from 'd3';
2
- import type { TooltipDataChunk } from '../../types';
2
+ import type { PointPosition, TooltipDataChunk } from '../../types';
3
3
  import type { PreparedTooltip } from '../useChartOptions/types';
4
- import type { PointerPosition } from './types';
5
4
  type Args = {
6
5
  dispatcher: Dispatch<object>;
7
6
  tooltip: PreparedTooltip;
8
7
  };
9
8
  export declare const useTooltip: ({ dispatcher, tooltip }: Args) => {
10
9
  hovered: TooltipDataChunk[] | undefined;
11
- pointerPosition: PointerPosition | undefined;
10
+ pointerPosition: PointPosition | undefined;
12
11
  };
13
12
  export {};
@@ -1,5 +1,5 @@
1
1
  import type { MeaningfulAny } from '../misc';
2
- import type { ChartTooltipRendererData } from './tooltip';
2
+ import type { ChartTooltipRendererArgs } from './tooltip';
3
3
  export type ChartMargin = {
4
4
  top: number;
5
5
  right: number;
@@ -13,6 +13,6 @@ export type ChartOptions = {
13
13
  point: MeaningfulAny;
14
14
  series: MeaningfulAny;
15
15
  }, event: PointerEvent) => void;
16
- pointermove?: (data: ChartTooltipRendererData | undefined, event: PointerEvent) => void;
16
+ pointermove?: (data: ChartTooltipRendererArgs | undefined, event: PointerEvent) => void;
17
17
  };
18
18
  };
@@ -16,7 +16,7 @@ export type ChartSeriesData<T = MeaningfulAny> = ScatterSeriesData<T> | PieSerie
16
16
  export type DataLabelRendererData<T = MeaningfulAny> = {
17
17
  data: ChartSeriesData<T>;
18
18
  };
19
- type BasicHoverState = {
19
+ export type BasicHoverState = {
20
20
  /**
21
21
  * Enable separate styles for the hovered series.
22
22
  *
@@ -223,4 +223,3 @@ export type ChartSeriesOptions = {
223
223
  };
224
224
  };
225
225
  };
226
- export {};
@@ -59,7 +59,7 @@ export type TooltipDataChunkWaterfall<T = MeaningfulAny> = {
59
59
  export type TooltipDataChunk<T = MeaningfulAny> = (TooltipDataChunkBarX<T> | TooltipDataChunkBarY<T> | TooltipDataChunkPie<T> | TooltipDataChunkScatter<T> | TooltipDataChunkLine<T> | TooltipDataChunkArea<T> | TooltipDataChunkTreemap<T> | TooltipDataChunkWaterfall<T>) & {
60
60
  closest?: boolean;
61
61
  };
62
- export type ChartTooltipRendererData<T = MeaningfulAny> = {
62
+ export type ChartTooltipRendererArgs<T = MeaningfulAny> = {
63
63
  hovered: TooltipDataChunk<T>[];
64
64
  xAxis?: ChartXAxis;
65
65
  yAxis?: ChartYAxis;
@@ -67,9 +67,9 @@ export type ChartTooltipRendererData<T = MeaningfulAny> = {
67
67
  export type ChartTooltip<T = MeaningfulAny> = {
68
68
  enabled?: boolean;
69
69
  /** Specifies the renderer for the tooltip. If returned null default tooltip renderer will be used. */
70
- renderer?: (args: {
71
- hovered: TooltipDataChunk<T>[];
72
- xAxis?: ChartXAxis;
73
- yAxis?: ChartYAxis;
74
- }) => React.ReactElement | null;
70
+ renderer?: (args: ChartTooltipRendererArgs<T>) => React.ReactElement | null;
71
+ pin?: {
72
+ enabled?: boolean;
73
+ modifierKey?: 'altKey' | 'metaKey';
74
+ };
75
75
  };
@@ -18,6 +18,10 @@ export type HtmlItem = {
18
18
  x: number;
19
19
  y: number;
20
20
  content: string;
21
+ size: {
22
+ width: number;
23
+ height: number;
24
+ };
21
25
  };
22
26
  export type ShapeDataWithHtmlItems = {
23
27
  htmlElements: HtmlItem[];
@@ -2,3 +2,4 @@
2
2
  * A utilitarian type to describe `any` when it does not contradict the typescript usage paradigm.
3
3
  */
4
4
  export type MeaningfulAny = any;
5
+ export type PointPosition = [number, number];
@@ -1,2 +1,10 @@
1
- export declare const randomString: (length: number, chars: string) => string;
2
- export declare const getUniqId: () => string;
1
+ export declare function randomString(length: number, chars: string): string;
2
+ export declare function getUniqId(): string;
3
+ /**
4
+ * Checks Macintosh hardware is used.
5
+ *
6
+ * Note: there is no better way to get this information as using depricated property `navigator.platform`.
7
+ *
8
+ * More details [here](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#examples).
9
+ */
10
+ export declare function isMacintosh(): boolean;
@@ -1,8 +1,20 @@
1
- export const randomString = (length, chars) => {
1
+ export function randomString(length, chars) {
2
2
  let result = '';
3
3
  for (let i = length; i > 0; --i) {
4
4
  result += chars[Math.floor(Math.random() * chars.length)];
5
5
  }
6
6
  return result;
7
- };
8
- export const getUniqId = () => `gravity-chart.${randomString(5, '0123456789abcdefghijklmnopqrstuvwxyz')}`;
7
+ }
8
+ export function getUniqId() {
9
+ return `gravity-chart.${randomString(5, '0123456789abcdefghijklmnopqrstuvwxyz')}`;
10
+ }
11
+ /**
12
+ * Checks Macintosh hardware is used.
13
+ *
14
+ * Note: there is no better way to get this information as using depricated property `navigator.platform`.
15
+ *
16
+ * More details [here](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#examples).
17
+ */
18
+ export function isMacintosh() {
19
+ return typeof navigator === 'undefined' ? false : /Mac|iP(hone|[oa]d)/.test(navigator.platform);
20
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/charts",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "React component used to render charts",
5
5
  "license": "MIT",
6
6
  "main": "dist/cjs/index.js",
@@ -1 +0,0 @@
1
- export type PointerPosition = [number, number];
@@ -1 +0,0 @@
1
- export type PointerPosition = [number, number];