@gravity-ui/charts 1.8.0 → 1.10.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 (255) hide show
  1. package/dist/cjs/components/Axis/AxisX.d.ts +2 -1
  2. package/dist/cjs/components/Axis/AxisX.js +147 -141
  3. package/dist/cjs/components/Axis/AxisY.d.ts +2 -1
  4. package/dist/cjs/components/Axis/AxisY.js +118 -116
  5. package/dist/cjs/components/ChartInner/index.js +36 -9
  6. package/dist/cjs/components/ChartInner/useChartInnerHandlers.d.ts +1 -1
  7. package/dist/cjs/components/ChartInner/useChartInnerProps.d.ts +8 -5
  8. package/dist/cjs/components/ChartInner/useChartInnerProps.js +59 -10
  9. package/dist/cjs/components/ChartInner/utils.d.ts +3 -0
  10. package/dist/cjs/components/ChartInner/utils.js +28 -0
  11. package/dist/cjs/components/Legend/index.js +202 -191
  12. package/dist/cjs/components/Legend/styles.css +2 -0
  13. package/dist/cjs/components/Tooltip/ChartTooltipContent.d.ts +1 -1
  14. package/dist/cjs/components/Tooltip/DefaultContent.d.ts +1 -1
  15. package/dist/cjs/components/Tooltip/DefaultContent.js +1 -1
  16. package/dist/cjs/components/Tooltip/index.d.ts +1 -1
  17. package/dist/cjs/constants/chart-types.d.ts +12 -0
  18. package/dist/cjs/constants/chart-types.js +12 -0
  19. package/dist/cjs/constants/defaults/axis.d.ts +3 -1
  20. package/dist/cjs/constants/defaults/axis.js +10 -0
  21. package/dist/cjs/constants/index.d.ts +6 -47
  22. package/dist/cjs/constants/index.js +6 -72
  23. package/dist/cjs/constants/layout-algorithms.d.ts +7 -0
  24. package/dist/cjs/constants/layout-algorithms.js +8 -0
  25. package/dist/cjs/constants/line-styles.d.ts +20 -0
  26. package/dist/cjs/constants/line-styles.js +20 -0
  27. package/dist/cjs/constants/palette.d.ts +1 -0
  28. package/dist/cjs/constants/palette.js +22 -0
  29. package/dist/cjs/constants/symbol-types.d.ts +7 -0
  30. package/dist/cjs/constants/symbol-types.js +8 -0
  31. package/dist/cjs/constants/typography.d.ts +1 -0
  32. package/dist/cjs/constants/typography.js +1 -0
  33. package/dist/cjs/hooks/hooks-utils/index.d.ts +1 -0
  34. package/dist/cjs/hooks/hooks-utils/index.js +1 -0
  35. package/dist/cjs/hooks/hooks-utils/zoom.d.ts +8 -0
  36. package/dist/cjs/hooks/hooks-utils/zoom.js +81 -0
  37. package/dist/cjs/hooks/index.d.ts +1 -0
  38. package/dist/cjs/hooks/index.js +1 -0
  39. package/dist/cjs/hooks/useAxisScales/index.d.ts +4 -2
  40. package/dist/cjs/hooks/useAxisScales/index.js +22 -8
  41. package/dist/cjs/hooks/useBrush/index.d.ts +3 -0
  42. package/dist/cjs/hooks/useBrush/index.js +70 -0
  43. package/dist/cjs/hooks/useBrush/styles.css +10 -0
  44. package/dist/cjs/hooks/useBrush/types.d.ts +24 -0
  45. package/dist/cjs/hooks/useBrush/types.js +1 -0
  46. package/dist/cjs/hooks/useChartDimensions/index.d.ts +3 -3
  47. package/dist/cjs/hooks/useChartDimensions/index.js +2 -2
  48. package/dist/cjs/hooks/useChartDimensions/utils.d.ts +2 -2
  49. package/dist/cjs/hooks/useChartOptions/chart.d.ts +2 -1
  50. package/dist/cjs/hooks/useChartOptions/chart.js +80 -1
  51. package/dist/cjs/hooks/useChartOptions/index.js +3 -2
  52. package/dist/cjs/hooks/useChartOptions/types.d.ts +6 -2
  53. package/dist/cjs/hooks/useChartOptions/x-axis.d.ts +3 -3
  54. package/dist/cjs/hooks/useChartOptions/x-axis.js +22 -13
  55. package/dist/cjs/hooks/useChartOptions/y-axis.d.ts +3 -3
  56. package/dist/cjs/hooks/useChartOptions/y-axis.js +36 -21
  57. package/dist/cjs/hooks/useCrosshair/index.d.ts +17 -0
  58. package/dist/cjs/hooks/useCrosshair/index.js +139 -0
  59. package/dist/cjs/hooks/useCrosshair/useCrosshairHover.d.ts +11 -0
  60. package/dist/cjs/hooks/useCrosshair/useCrosshairHover.js +18 -0
  61. package/dist/cjs/hooks/useSeries/index.d.ts +8 -6
  62. package/dist/cjs/hooks/useSeries/index.js +41 -22
  63. package/dist/cjs/hooks/useSeries/prepare-bar-y.d.ts +27 -2
  64. package/dist/cjs/hooks/useSeries/prepare-bar-y.js +5 -5
  65. package/dist/cjs/hooks/useSeries/prepare-legend.d.ts +1 -1
  66. package/dist/cjs/hooks/useSeries/prepare-legend.js +27 -9
  67. package/dist/cjs/hooks/useSeries/prepare-line.d.ts +1 -2
  68. package/dist/cjs/hooks/useSeries/prepare-line.js +3 -3
  69. package/dist/cjs/hooks/useSeries/prepareSeries.d.ts +1 -1
  70. package/dist/cjs/hooks/useSeries/prepareSeries.js +2 -2
  71. package/dist/cjs/hooks/useSeries/types.d.ts +2 -1
  72. package/dist/cjs/hooks/useShapes/area/index.js +1 -1
  73. package/dist/cjs/hooks/useShapes/area/prepare-data.d.ts +1 -1
  74. package/dist/cjs/hooks/useShapes/area/prepare-data.js +32 -16
  75. package/dist/cjs/hooks/useShapes/area/types.d.ts +1 -0
  76. package/dist/cjs/hooks/useShapes/bar-x/prepare-data.d.ts +1 -1
  77. package/dist/cjs/hooks/useShapes/bar-x/prepare-data.js +17 -13
  78. package/dist/cjs/hooks/useShapes/bar-y/prepare-data.d.ts +1 -1
  79. package/dist/cjs/hooks/useShapes/bar-y/prepare-data.js +6 -6
  80. package/dist/cjs/hooks/useShapes/index.d.ts +1 -1
  81. package/dist/cjs/hooks/useShapes/index.js +40 -31
  82. package/dist/cjs/hooks/useShapes/line/prepare-data.d.ts +1 -1
  83. package/dist/cjs/hooks/useShapes/line/prepare-data.js +14 -11
  84. package/dist/cjs/hooks/useShapes/line/types.d.ts +1 -0
  85. package/dist/cjs/hooks/useShapes/marker.js +2 -2
  86. package/dist/cjs/hooks/useShapes/pie/index.js +3 -3
  87. package/dist/cjs/hooks/useShapes/pie/prepare-data.d.ts +1 -1
  88. package/dist/cjs/hooks/useShapes/pie/prepare-data.js +15 -11
  89. package/dist/cjs/hooks/useShapes/radar/prepare-data.d.ts +1 -1
  90. package/dist/cjs/hooks/useShapes/radar/prepare-data.js +6 -7
  91. package/dist/cjs/hooks/useShapes/radar/types.d.ts +1 -0
  92. package/dist/cjs/hooks/useShapes/scatter/index.js +0 -1
  93. package/dist/cjs/hooks/useShapes/scatter/prepare-data.js +2 -0
  94. package/dist/cjs/hooks/useShapes/scatter/types.d.ts +1 -0
  95. package/dist/cjs/hooks/useShapes/treemap/prepare-data.d.ts +1 -1
  96. package/dist/cjs/hooks/useShapes/treemap/prepare-data.js +19 -16
  97. package/dist/cjs/hooks/useShapes/waterfall/index.js +2 -2
  98. package/dist/cjs/hooks/useShapes/waterfall/prepare-data.d.ts +1 -1
  99. package/dist/cjs/hooks/useShapes/waterfall/prepare-data.js +8 -7
  100. package/dist/cjs/hooks/useZoom/index.d.ts +18 -0
  101. package/dist/cjs/hooks/useZoom/index.js +54 -0
  102. package/dist/cjs/hooks/useZoom/types.d.ts +19 -0
  103. package/dist/cjs/hooks/useZoom/types.js +1 -0
  104. package/dist/cjs/hooks/useZoom/utils.d.ts +12 -0
  105. package/dist/cjs/hooks/useZoom/utils.js +128 -0
  106. package/dist/cjs/types/chart/axis.d.ts +26 -21
  107. package/dist/cjs/types/chart/chart.d.ts +5 -0
  108. package/dist/cjs/types/chart/line.d.ts +1 -1
  109. package/dist/cjs/types/chart/pie.d.ts +1 -1
  110. package/dist/cjs/types/chart/series.d.ts +1 -1
  111. package/dist/cjs/types/chart/tooltip.d.ts +1 -1
  112. package/dist/cjs/types/chart/zoom.d.ts +36 -0
  113. package/dist/cjs/types/chart/zoom.js +1 -0
  114. package/dist/cjs/types/index.d.ts +1 -0
  115. package/dist/cjs/types/index.js +1 -0
  116. package/dist/cjs/types/misc.d.ts +7 -0
  117. package/dist/cjs/utils/chart/axis-generators/bottom.d.ts +1 -1
  118. package/dist/cjs/utils/chart/axis-generators/bottom.js +25 -25
  119. package/dist/cjs/utils/chart/axis.d.ts +2 -1
  120. package/dist/cjs/utils/chart/axis.js +10 -2
  121. package/dist/cjs/utils/chart/get-closest-data.js +1 -1
  122. package/dist/cjs/utils/chart/text.d.ts +7 -7
  123. package/dist/cjs/utils/chart/text.js +37 -29
  124. package/dist/cjs/utils/chart-ui/pie-center-text.d.ts +1 -1
  125. package/dist/cjs/utils/chart-ui/pie-center-text.js +2 -2
  126. package/dist/cjs/validation/index.d.ts +1 -1
  127. package/dist/cjs/validation/index.js +16 -16
  128. package/dist/esm/components/Axis/AxisX.d.ts +2 -1
  129. package/dist/esm/components/Axis/AxisX.js +147 -141
  130. package/dist/esm/components/Axis/AxisY.d.ts +2 -1
  131. package/dist/esm/components/Axis/AxisY.js +118 -116
  132. package/dist/esm/components/ChartInner/index.js +36 -9
  133. package/dist/esm/components/ChartInner/useChartInnerHandlers.d.ts +1 -1
  134. package/dist/esm/components/ChartInner/useChartInnerProps.d.ts +8 -5
  135. package/dist/esm/components/ChartInner/useChartInnerProps.js +59 -10
  136. package/dist/esm/components/ChartInner/utils.d.ts +3 -0
  137. package/dist/esm/components/ChartInner/utils.js +28 -0
  138. package/dist/esm/components/Legend/index.js +202 -191
  139. package/dist/esm/components/Legend/styles.css +2 -0
  140. package/dist/esm/components/Tooltip/ChartTooltipContent.d.ts +1 -1
  141. package/dist/esm/components/Tooltip/DefaultContent.d.ts +1 -1
  142. package/dist/esm/components/Tooltip/DefaultContent.js +1 -1
  143. package/dist/esm/components/Tooltip/index.d.ts +1 -1
  144. package/dist/esm/constants/chart-types.d.ts +12 -0
  145. package/dist/esm/constants/chart-types.js +12 -0
  146. package/dist/esm/constants/defaults/axis.d.ts +3 -1
  147. package/dist/esm/constants/defaults/axis.js +10 -0
  148. package/dist/esm/constants/index.d.ts +6 -47
  149. package/dist/esm/constants/index.js +6 -72
  150. package/dist/esm/constants/layout-algorithms.d.ts +7 -0
  151. package/dist/esm/constants/layout-algorithms.js +8 -0
  152. package/dist/esm/constants/line-styles.d.ts +20 -0
  153. package/dist/esm/constants/line-styles.js +20 -0
  154. package/dist/esm/constants/palette.d.ts +1 -0
  155. package/dist/esm/constants/palette.js +22 -0
  156. package/dist/esm/constants/symbol-types.d.ts +7 -0
  157. package/dist/esm/constants/symbol-types.js +8 -0
  158. package/dist/esm/constants/typography.d.ts +1 -0
  159. package/dist/esm/constants/typography.js +1 -0
  160. package/dist/esm/hooks/hooks-utils/index.d.ts +1 -0
  161. package/dist/esm/hooks/hooks-utils/index.js +1 -0
  162. package/dist/esm/hooks/hooks-utils/zoom.d.ts +8 -0
  163. package/dist/esm/hooks/hooks-utils/zoom.js +81 -0
  164. package/dist/esm/hooks/index.d.ts +1 -0
  165. package/dist/esm/hooks/index.js +1 -0
  166. package/dist/esm/hooks/useAxisScales/index.d.ts +4 -2
  167. package/dist/esm/hooks/useAxisScales/index.js +22 -8
  168. package/dist/esm/hooks/useBrush/index.d.ts +3 -0
  169. package/dist/esm/hooks/useBrush/index.js +70 -0
  170. package/dist/esm/hooks/useBrush/styles.css +10 -0
  171. package/dist/esm/hooks/useBrush/types.d.ts +24 -0
  172. package/dist/esm/hooks/useBrush/types.js +1 -0
  173. package/dist/esm/hooks/useChartDimensions/index.d.ts +3 -3
  174. package/dist/esm/hooks/useChartDimensions/index.js +2 -2
  175. package/dist/esm/hooks/useChartDimensions/utils.d.ts +2 -2
  176. package/dist/esm/hooks/useChartOptions/chart.d.ts +2 -1
  177. package/dist/esm/hooks/useChartOptions/chart.js +80 -1
  178. package/dist/esm/hooks/useChartOptions/index.js +3 -2
  179. package/dist/esm/hooks/useChartOptions/types.d.ts +6 -2
  180. package/dist/esm/hooks/useChartOptions/x-axis.d.ts +3 -3
  181. package/dist/esm/hooks/useChartOptions/x-axis.js +22 -13
  182. package/dist/esm/hooks/useChartOptions/y-axis.d.ts +3 -3
  183. package/dist/esm/hooks/useChartOptions/y-axis.js +36 -21
  184. package/dist/esm/hooks/useCrosshair/index.d.ts +17 -0
  185. package/dist/esm/hooks/useCrosshair/index.js +139 -0
  186. package/dist/esm/hooks/useCrosshair/useCrosshairHover.d.ts +11 -0
  187. package/dist/esm/hooks/useCrosshair/useCrosshairHover.js +18 -0
  188. package/dist/esm/hooks/useSeries/index.d.ts +8 -6
  189. package/dist/esm/hooks/useSeries/index.js +41 -22
  190. package/dist/esm/hooks/useSeries/prepare-bar-y.d.ts +27 -2
  191. package/dist/esm/hooks/useSeries/prepare-bar-y.js +5 -5
  192. package/dist/esm/hooks/useSeries/prepare-legend.d.ts +1 -1
  193. package/dist/esm/hooks/useSeries/prepare-legend.js +27 -9
  194. package/dist/esm/hooks/useSeries/prepare-line.d.ts +1 -2
  195. package/dist/esm/hooks/useSeries/prepare-line.js +3 -3
  196. package/dist/esm/hooks/useSeries/prepareSeries.d.ts +1 -1
  197. package/dist/esm/hooks/useSeries/prepareSeries.js +2 -2
  198. package/dist/esm/hooks/useSeries/types.d.ts +2 -1
  199. package/dist/esm/hooks/useShapes/area/index.js +1 -1
  200. package/dist/esm/hooks/useShapes/area/prepare-data.d.ts +1 -1
  201. package/dist/esm/hooks/useShapes/area/prepare-data.js +32 -16
  202. package/dist/esm/hooks/useShapes/area/types.d.ts +1 -0
  203. package/dist/esm/hooks/useShapes/bar-x/prepare-data.d.ts +1 -1
  204. package/dist/esm/hooks/useShapes/bar-x/prepare-data.js +17 -13
  205. package/dist/esm/hooks/useShapes/bar-y/prepare-data.d.ts +1 -1
  206. package/dist/esm/hooks/useShapes/bar-y/prepare-data.js +6 -6
  207. package/dist/esm/hooks/useShapes/index.d.ts +1 -1
  208. package/dist/esm/hooks/useShapes/index.js +40 -31
  209. package/dist/esm/hooks/useShapes/line/prepare-data.d.ts +1 -1
  210. package/dist/esm/hooks/useShapes/line/prepare-data.js +14 -11
  211. package/dist/esm/hooks/useShapes/line/types.d.ts +1 -0
  212. package/dist/esm/hooks/useShapes/marker.js +2 -2
  213. package/dist/esm/hooks/useShapes/pie/index.js +3 -3
  214. package/dist/esm/hooks/useShapes/pie/prepare-data.d.ts +1 -1
  215. package/dist/esm/hooks/useShapes/pie/prepare-data.js +15 -11
  216. package/dist/esm/hooks/useShapes/radar/prepare-data.d.ts +1 -1
  217. package/dist/esm/hooks/useShapes/radar/prepare-data.js +6 -7
  218. package/dist/esm/hooks/useShapes/radar/types.d.ts +1 -0
  219. package/dist/esm/hooks/useShapes/scatter/index.js +0 -1
  220. package/dist/esm/hooks/useShapes/scatter/prepare-data.js +2 -0
  221. package/dist/esm/hooks/useShapes/scatter/types.d.ts +1 -0
  222. package/dist/esm/hooks/useShapes/treemap/prepare-data.d.ts +1 -1
  223. package/dist/esm/hooks/useShapes/treemap/prepare-data.js +19 -16
  224. package/dist/esm/hooks/useShapes/waterfall/index.js +2 -2
  225. package/dist/esm/hooks/useShapes/waterfall/prepare-data.d.ts +1 -1
  226. package/dist/esm/hooks/useShapes/waterfall/prepare-data.js +8 -7
  227. package/dist/esm/hooks/useZoom/index.d.ts +18 -0
  228. package/dist/esm/hooks/useZoom/index.js +54 -0
  229. package/dist/esm/hooks/useZoom/types.d.ts +19 -0
  230. package/dist/esm/hooks/useZoom/types.js +1 -0
  231. package/dist/esm/hooks/useZoom/utils.d.ts +12 -0
  232. package/dist/esm/hooks/useZoom/utils.js +128 -0
  233. package/dist/esm/types/chart/axis.d.ts +26 -21
  234. package/dist/esm/types/chart/chart.d.ts +5 -0
  235. package/dist/esm/types/chart/line.d.ts +1 -1
  236. package/dist/esm/types/chart/pie.d.ts +1 -1
  237. package/dist/esm/types/chart/series.d.ts +1 -1
  238. package/dist/esm/types/chart/tooltip.d.ts +1 -1
  239. package/dist/esm/types/chart/zoom.d.ts +36 -0
  240. package/dist/esm/types/chart/zoom.js +1 -0
  241. package/dist/esm/types/index.d.ts +1 -0
  242. package/dist/esm/types/index.js +1 -0
  243. package/dist/esm/types/misc.d.ts +7 -0
  244. package/dist/esm/utils/chart/axis-generators/bottom.d.ts +1 -1
  245. package/dist/esm/utils/chart/axis-generators/bottom.js +25 -25
  246. package/dist/esm/utils/chart/axis.d.ts +2 -1
  247. package/dist/esm/utils/chart/axis.js +10 -2
  248. package/dist/esm/utils/chart/get-closest-data.js +1 -1
  249. package/dist/esm/utils/chart/text.d.ts +7 -7
  250. package/dist/esm/utils/chart/text.js +37 -29
  251. package/dist/esm/utils/chart-ui/pie-center-text.d.ts +1 -1
  252. package/dist/esm/utils/chart-ui/pie-center-text.js +2 -2
  253. package/dist/esm/validation/index.d.ts +1 -1
  254. package/dist/esm/validation/index.js +16 -16
  255. package/package.json +2 -1
@@ -1,4 +1,7 @@
1
1
  import React from 'react';
2
+ import { ArrowRotateLeft } from '@gravity-ui/icons';
3
+ import { Button, ButtonIcon } from '@gravity-ui/uikit';
4
+ import { useCrosshair } from '../../hooks';
2
5
  import { EventType, block, getDispatcher } from '../../utils';
3
6
  import { AxisX, AxisY } from '../Axis';
4
7
  import { Legend } from '../Legend';
@@ -14,10 +17,13 @@ export const ChartInner = (props) => {
14
17
  var _a, _b, _c, _d;
15
18
  const { width, height, data } = props;
16
19
  const svgRef = React.useRef(null);
17
- const htmlLayerRef = React.useRef(null);
20
+ const [htmlLayout, setHtmlLayout] = React.useState(null);
18
21
  const plotRef = React.useRef(null);
22
+ const plotBeforeRef = React.useRef(null);
23
+ const plotAfterRef = React.useRef(null);
19
24
  const dispatcher = React.useMemo(() => getDispatcher(), []);
20
- const { boundsHeight, boundsOffsetLeft, boundsOffsetTop, boundsWidth, handleLegendItemClick, legendConfig, legendItems, preparedSeries, preparedSplit, preparedLegend, prevHeight, prevWidth, shapes, shapesData, title, tooltip, xAxis, xScale, yAxis, yScale, svgXPos, svgBottomPos, } = useChartInnerProps(Object.assign(Object.assign({}, props), { dispatcher, htmlLayout: htmlLayerRef.current, svgContainer: svgRef.current }));
25
+ const { boundsHeight, boundsOffsetLeft, boundsOffsetTop, boundsWidth, handleLegendItemClick, handleZoomReset, legendConfig, legendItems, preparedSeries, preparedSplit, preparedLegend, prevHeight, prevWidth, shapes, shapesData, title, tooltip, xAxis, xScale, yAxis, yScale, svgXPos, svgBottomPos, } = useChartInnerProps(Object.assign(Object.assign({}, props), { dispatcher,
26
+ htmlLayout, svgContainer: svgRef.current, plotNode: plotRef.current }));
21
27
  const { tooltipPinned, togglePinTooltip, unpinTooltip } = useChartInnerState({
22
28
  dispatcher,
23
29
  tooltip,
@@ -39,6 +45,19 @@ export const ChartInner = (props) => {
39
45
  });
40
46
  const clickHandler = (_b = (_a = data.chart) === null || _a === void 0 ? void 0 : _a.events) === null || _b === void 0 ? void 0 : _b.click;
41
47
  const pointerMoveHandler = (_d = (_c = data.chart) === null || _c === void 0 ? void 0 : _c.events) === null || _d === void 0 ? void 0 : _d.pointermove;
48
+ useCrosshair({
49
+ split: preparedSplit,
50
+ plotElement: plotAfterRef.current,
51
+ boundsOffsetLeft,
52
+ boundsOffsetTop,
53
+ width: boundsWidth,
54
+ height: boundsHeight,
55
+ xAxis,
56
+ yAxes: yAxis,
57
+ yScale,
58
+ xScale,
59
+ dispatcher,
60
+ });
42
61
  React.useEffect(() => {
43
62
  if (clickHandler) {
44
63
  dispatcher.on(EventType.CLICK_CHART, clickHandler);
@@ -60,20 +79,28 @@ export const ChartInner = (props) => {
60
79
  }
61
80
  }, [prevWidth, width, prevHeight, height, tooltipPinned, unpinTooltip]);
62
81
  return (React.createElement("div", { className: b() },
63
- React.createElement("svg", { ref: svgRef, width: width, height: height, onMouseMove: throttledHandleMouseMove, onMouseLeave: handleMouseLeave, onTouchStart: throttledHandleTouchMove, onTouchMove: throttledHandleTouchMove, onClick: handleChartClick },
82
+ React.createElement("svg", { ref: svgRef, width: width, height: height,
83
+ // We use onPointerMove here because onMouseMove works incorrectly when the zoom setting is enabled:
84
+ // when starting to select an area, the tooltip remains in the position where the selection began
85
+ onPointerMove: throttledHandleMouseMove, onMouseLeave: handleMouseLeave, onTouchStart: throttledHandleTouchMove, onTouchMove: throttledHandleTouchMove, onClick: handleChartClick },
64
86
  title && React.createElement(Title, Object.assign({}, title, { chartWidth: width })),
65
87
  React.createElement("g", { transform: `translate(0, ${boundsOffsetTop})` }, preparedSplit.plots.map((plot, index) => {
66
88
  return React.createElement(PlotTitle, { key: `plot-${index}`, title: plot.title });
67
89
  })),
68
90
  React.createElement("g", { width: boundsWidth, height: boundsHeight, transform: `translate(${[boundsOffsetLeft, boundsOffsetTop].join(',')})`, ref: plotRef },
69
91
  xScale && (yScale === null || yScale === void 0 ? void 0 : yScale.length) && (React.createElement(React.Fragment, null,
70
- React.createElement(AxisY, { bottomLimit: svgBottomPos, axes: yAxis, width: boundsWidth, height: boundsHeight, scale: yScale, split: preparedSplit, plotRef: plotRef }),
71
- React.createElement("g", { transform: `translate(0, ${boundsHeight})` },
72
- React.createElement(AxisX, { leftmostLimit: svgXPos, axis: xAxis, width: boundsWidth, height: boundsHeight, scale: xScale, split: preparedSplit, plotRef: plotRef })))),
73
- shapes),
74
- preparedLegend.enabled && (React.createElement(Legend, { chartSeries: preparedSeries, boundsWidth: boundsWidth, legend: preparedLegend, items: legendItems, config: legendConfig, onItemClick: handleLegendItemClick, onUpdate: unpinTooltip, htmlLayout: htmlLayerRef.current }))),
75
- React.createElement("div", { className: b('html-layer'), ref: htmlLayerRef, style: {
92
+ React.createElement(AxisY, { bottomLimit: svgBottomPos, axes: yAxis, width: boundsWidth, height: boundsHeight, scale: yScale, split: preparedSplit, plotBeforeRef: plotBeforeRef, plotAfterRef: plotAfterRef }),
93
+ xAxis && (React.createElement("g", { transform: `translate(0, ${boundsHeight})` },
94
+ React.createElement(AxisX, { leftmostLimit: svgXPos, axis: xAxis, width: boundsWidth, height: boundsHeight, scale: xScale, split: preparedSplit, plotBeforeRef: plotBeforeRef, plotAfterRef: plotAfterRef }))))),
95
+ React.createElement("g", { ref: plotBeforeRef }),
96
+ shapes,
97
+ React.createElement("g", { ref: plotAfterRef })),
98
+ (preparedLegend === null || preparedLegend === void 0 ? void 0 : preparedLegend.enabled) && legendConfig && (React.createElement(Legend, { chartSeries: preparedSeries, boundsWidth: boundsWidth, legend: preparedLegend, items: legendItems, config: legendConfig, onItemClick: handleLegendItemClick, onUpdate: unpinTooltip, htmlLayout: htmlLayout }))),
99
+ React.createElement("div", { className: b('html-layer'), ref: setHtmlLayout, style: {
76
100
  '--g-html-layout-transform': `translate(${boundsOffsetLeft}px, ${boundsOffsetTop}px)`,
77
101
  } }),
102
+ handleZoomReset && (React.createElement(Button, { style: { position: 'absolute', top: 0, right: 0 }, onClick: handleZoomReset },
103
+ React.createElement(ButtonIcon, null,
104
+ React.createElement(ArrowRotateLeft, null)))),
78
105
  React.createElement(Tooltip, { dispatcher: dispatcher, tooltip: tooltip, svgContainer: svgRef.current, xAxis: xAxis, yAxis: yAxis[0], onOutsideClick: unpinTooltip, tooltipPinned: tooltipPinned })));
79
106
  };
@@ -14,7 +14,7 @@ type Props = {
14
14
  togglePinTooltip: ChartInnerState['togglePinTooltip'];
15
15
  tooltipPinned: boolean;
16
16
  unpinTooltip: ChartInnerState['unpinTooltip'];
17
- xAxis: PreparedAxis;
17
+ xAxis: PreparedAxis | null;
18
18
  yAxis: PreparedAxis[];
19
19
  tooltipThrottle: number;
20
20
  };
@@ -1,10 +1,12 @@
1
1
  import React from 'react';
2
2
  import type { Dispatch } from 'd3';
3
+ import type { PreparedAxis } from '../../hooks';
3
4
  import type { ChartInnerProps } from './types';
4
5
  type Props = ChartInnerProps & {
5
6
  dispatcher: Dispatch<object>;
6
7
  htmlLayout: HTMLElement | null;
7
8
  svgContainer: SVGGElement | null;
9
+ plotNode: SVGGElement | null;
8
10
  };
9
11
  export declare function useChartInnerProps(props: Props): {
10
12
  svgBottomPos: number | undefined;
@@ -14,6 +16,7 @@ export declare function useChartInnerProps(props: Props): {
14
16
  boundsOffsetTop: number;
15
17
  boundsWidth: number;
16
18
  handleLegendItemClick: import("../../hooks").OnLegendItemClick;
19
+ handleZoomReset: (() => void) | undefined;
17
20
  legendConfig: {
18
21
  offset: {
19
22
  left: number;
@@ -25,9 +28,9 @@ export declare function useChartInnerProps(props: Props): {
25
28
  end: number;
26
29
  }[];
27
30
  } | undefined;
28
- };
29
- legendItems: import("../../hooks").LegendItem[][];
30
- preparedLegend: import("../../hooks").PreparedLegend;
31
+ } | undefined;
32
+ legendItems: never[] | import("../../hooks").LegendItem[][];
33
+ preparedLegend: import("../../hooks").PreparedLegend | null;
31
34
  preparedSeries: import("../../hooks").PreparedSeries[];
32
35
  preparedSplit: import("../../hooks").PreparedSplit;
33
36
  prevHeight: number | undefined;
@@ -41,9 +44,9 @@ export declare function useChartInnerProps(props: Props): {
41
44
  enabled: boolean;
42
45
  throttle: number;
43
46
  };
44
- xAxis: import("../../hooks").PreparedAxis;
47
+ xAxis: PreparedAxis | null;
45
48
  xScale: import("../../hooks").ChartScale | undefined;
46
- yAxis: import("../../hooks").PreparedAxis[];
49
+ yAxis: PreparedAxis[];
47
50
  yScale: import("../../hooks").ChartScale[] | undefined;
48
51
  };
49
52
  export {};
@@ -1,28 +1,44 @@
1
1
  import React from 'react';
2
2
  import { useAxisScales, useChartDimensions, useChartOptions, usePrevious, useSeries, useShapes, useSplit, } from '../../hooks';
3
+ import { getZoomedSeriesData } from '../../hooks/hooks-utils';
3
4
  import { getYAxisWidth } from '../../hooks/useChartDimensions/utils';
4
5
  import { getPreparedXAxis } from '../../hooks/useChartOptions/x-axis';
5
6
  import { getPreparedYAxis } from '../../hooks/useChartOptions/y-axis';
7
+ import { useZoom } from '../../hooks/useZoom';
8
+ import { hasAtLeastOneSeriesDataPerPlot } from './utils';
6
9
  export function useChartInnerProps(props) {
7
10
  var _a;
8
- const { width, height, data, dispatcher, htmlLayout, svgContainer } = props;
11
+ const { width, height, data, dispatcher, htmlLayout, svgContainer, plotNode } = props;
9
12
  const prevWidth = usePrevious(width);
10
13
  const prevHeight = usePrevious(height);
14
+ const [zoomState, setZoomState] = React.useState({});
11
15
  const { chart, title, tooltip, colors } = useChartOptions({ data });
12
- const xAxis = React.useMemo(() => getPreparedXAxis({ xAxis: data.xAxis, width, series: data.series.data }), [data, width]);
13
- const yAxis = React.useMemo(() => getPreparedYAxis({
14
- series: data.series.data,
15
- yAxis: data.yAxis,
16
- height,
17
- }), [data, height]);
16
+ const zoomedSeriesData = React.useMemo(() => {
17
+ return getZoomedSeriesData({
18
+ seriesData: data.series.data,
19
+ xAxis: data.xAxis,
20
+ yAxises: data.yAxis,
21
+ zoomState,
22
+ });
23
+ }, [data.series.data, data.xAxis, data.yAxis, zoomState]);
24
+ const [xAxis, setXAxis] = React.useState(null);
25
+ React.useEffect(() => {
26
+ getPreparedXAxis({ xAxis: data.xAxis, width, seriesData: zoomedSeriesData }).then((val) => setXAxis(val));
27
+ }, [data.xAxis, width, zoomedSeriesData]);
28
+ const [yAxis, setYAxis] = React.useState([]);
29
+ React.useEffect(() => {
30
+ getPreparedYAxis({ yAxis: data.yAxis, height, seriesData: zoomedSeriesData }).then((val) => setYAxis(val));
31
+ }, [data.yAxis, height, zoomedSeriesData]);
18
32
  const { legendItems, legendConfig, preparedSeries, preparedSeriesOptions, preparedLegend, handleLegendItemClick, } = useSeries({
19
33
  chartWidth: width,
20
34
  chartHeight: height,
21
35
  chartMargin: chart.margin,
22
- series: data.series,
36
+ colors,
23
37
  legend: data.legend,
38
+ originalSeriesData: data.series.data,
39
+ seriesData: zoomedSeriesData,
40
+ seriesOptions: data.series.options,
24
41
  preparedYAxis: yAxis,
25
- colors,
26
42
  });
27
43
  const { boundsWidth, boundsHeight } = useChartDimensions({
28
44
  width,
@@ -37,6 +53,8 @@ export function useChartInnerProps(props) {
37
53
  const { xScale, yScale } = useAxisScales({
38
54
  boundsWidth,
39
55
  boundsHeight,
56
+ hasZoomX: Boolean(zoomState.x),
57
+ hasZoomY: Boolean(zoomState.y),
40
58
  series: preparedSeries,
41
59
  xAxis,
42
60
  yAxis,
@@ -55,10 +73,37 @@ export function useChartInnerProps(props) {
55
73
  split: preparedSplit,
56
74
  htmlLayout,
57
75
  });
76
+ const handleAttemptToSetZoomState = React.useCallback((nextZoomState) => {
77
+ const nextZoomedSeriesData = getZoomedSeriesData({
78
+ seriesData: zoomedSeriesData,
79
+ xAxis: data.xAxis,
80
+ yAxises: data.yAxis,
81
+ zoomState: nextZoomState,
82
+ });
83
+ const hasData = hasAtLeastOneSeriesDataPerPlot(nextZoomedSeriesData, yAxis);
84
+ if (hasData) {
85
+ setZoomState(nextZoomState);
86
+ }
87
+ }, [data.xAxis, data.yAxis, yAxis, zoomedSeriesData]);
88
+ useZoom({
89
+ node: plotNode,
90
+ onUpdate: handleAttemptToSetZoomState,
91
+ plotContainerHeight: boundsHeight,
92
+ plotContainerWidth: boundsWidth,
93
+ preparedSplit,
94
+ preparedZoom: chart.zoom,
95
+ xAxis,
96
+ xScale,
97
+ yAxis,
98
+ yScale,
99
+ });
58
100
  const boundsOffsetTop = chart.margin.top;
59
- // We need to calculate the width of each axis because the first axis can be hidden
101
+ // We need to calculate the width of each left axis because the first axis can be hidden
60
102
  const boundsOffsetLeft = chart.margin.left +
61
103
  yAxis.reduce((acc, axis) => {
104
+ if (axis.position !== 'left') {
105
+ return acc;
106
+ }
62
107
  const axisWidth = getYAxisWidth(axis);
63
108
  if (acc < axisWidth) {
64
109
  acc = axisWidth;
@@ -66,6 +111,9 @@ export function useChartInnerProps(props) {
66
111
  return acc;
67
112
  }, 0);
68
113
  const { x, bottom } = (_a = svgContainer === null || svgContainer === void 0 ? void 0 : svgContainer.getBoundingClientRect()) !== null && _a !== void 0 ? _a : {};
114
+ const handleZoomReset = React.useCallback(() => {
115
+ setZoomState({});
116
+ }, []);
69
117
  return {
70
118
  svgBottomPos: bottom,
71
119
  svgXPos: x,
@@ -74,6 +122,7 @@ export function useChartInnerProps(props) {
74
122
  boundsOffsetTop,
75
123
  boundsWidth,
76
124
  handleLegendItemClick,
125
+ handleZoomReset: Object.keys(zoomState).length > 0 ? handleZoomReset : undefined,
77
126
  legendConfig,
78
127
  legendItems,
79
128
  preparedLegend,
@@ -0,0 +1,3 @@
1
+ import type { PreparedAxis } from '../../hooks/useChartOptions/types';
2
+ import type { ChartSeries } from '../../types';
3
+ export declare function hasAtLeastOneSeriesDataPerPlot(seriesData: ChartSeries[], yAxises?: PreparedAxis[]): boolean;
@@ -0,0 +1,28 @@
1
+ export function hasAtLeastOneSeriesDataPerPlot(seriesData, yAxises = []) {
2
+ const hasDataMap = new Map();
3
+ yAxises.forEach((yAxis) => {
4
+ var _a;
5
+ const plotIndex = (_a = yAxis.plotIndex) !== null && _a !== void 0 ? _a : 0;
6
+ if (!hasDataMap.has(plotIndex)) {
7
+ hasDataMap.set(plotIndex, false);
8
+ }
9
+ });
10
+ if (hasDataMap.size === 0) {
11
+ return false;
12
+ }
13
+ seriesData.forEach((seriesDataChunk) => {
14
+ var _a;
15
+ let yAxisIndex = 0;
16
+ if ('yAxis' in seriesDataChunk && typeof seriesDataChunk.yAxis === 'number') {
17
+ yAxisIndex = seriesDataChunk.yAxis;
18
+ }
19
+ const yAxis = yAxises[yAxisIndex];
20
+ const plotIndex = (_a = yAxis === null || yAxis === void 0 ? void 0 : yAxis.plotIndex) !== null && _a !== void 0 ? _a : 0;
21
+ if (!hasDataMap.get(plotIndex)) {
22
+ if (seriesDataChunk.data.length > 0) {
23
+ hasDataMap.set(plotIndex, true);
24
+ }
25
+ }
26
+ });
27
+ return [...hasDataMap.values()].every((hasData) => hasData);
28
+ }