@neo4j-ndl/react 3.0.27 → 3.0.28

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 (76) hide show
  1. package/lib/cjs/index.js +0 -1
  2. package/lib/cjs/index.js.map +1 -1
  3. package/lib/esm/index.js +0 -1
  4. package/lib/esm/index.js.map +1 -1
  5. package/lib/types/index.d.ts +0 -1
  6. package/package.json +1 -5
  7. package/lib/cjs/charts/CartesianGrid.js +0 -107
  8. package/lib/cjs/charts/CartesianGrid.js.map +0 -1
  9. package/lib/cjs/charts/Chart.js +0 -761
  10. package/lib/cjs/charts/Chart.js.map +0 -1
  11. package/lib/cjs/charts/ChartTooltip.js +0 -60
  12. package/lib/cjs/charts/ChartTooltip.js.map +0 -1
  13. package/lib/cjs/charts/Charts.js +0 -124
  14. package/lib/cjs/charts/Charts.js.map +0 -1
  15. package/lib/cjs/charts/Dot.js +0 -31
  16. package/lib/cjs/charts/Dot.js.map +0 -1
  17. package/lib/cjs/charts/HollowDot.js +0 -54
  18. package/lib/cjs/charts/HollowDot.js.map +0 -1
  19. package/lib/cjs/charts/Legend.js +0 -180
  20. package/lib/cjs/charts/Legend.js.map +0 -1
  21. package/lib/cjs/charts/Line.js +0 -132
  22. package/lib/cjs/charts/Line.js.map +0 -1
  23. package/lib/cjs/charts/axis/XAxis.js +0 -205
  24. package/lib/cjs/charts/axis/XAxis.js.map +0 -1
  25. package/lib/cjs/charts/axis/YAxis.js +0 -170
  26. package/lib/cjs/charts/axis/YAxis.js.map +0 -1
  27. package/lib/cjs/charts/axis/chart-axis-utils.js +0 -42
  28. package/lib/cjs/charts/axis/chart-axis-utils.js.map +0 -1
  29. package/lib/cjs/charts/axis/chart-tick-generation.js +0 -245
  30. package/lib/cjs/charts/axis/chart-tick-generation.js.map +0 -1
  31. package/lib/cjs/charts/index.js +0 -38
  32. package/lib/cjs/charts/index.js.map +0 -1
  33. package/lib/cjs/charts/utils.js +0 -40
  34. package/lib/cjs/charts/utils.js.map +0 -1
  35. package/lib/esm/charts/CartesianGrid.js +0 -100
  36. package/lib/esm/charts/CartesianGrid.js.map +0 -1
  37. package/lib/esm/charts/Chart.js +0 -734
  38. package/lib/esm/charts/Chart.js.map +0 -1
  39. package/lib/esm/charts/ChartTooltip.js +0 -54
  40. package/lib/esm/charts/ChartTooltip.js.map +0 -1
  41. package/lib/esm/charts/Charts.js +0 -121
  42. package/lib/esm/charts/Charts.js.map +0 -1
  43. package/lib/esm/charts/Dot.js +0 -28
  44. package/lib/esm/charts/Dot.js.map +0 -1
  45. package/lib/esm/charts/HollowDot.js +0 -28
  46. package/lib/esm/charts/HollowDot.js.map +0 -1
  47. package/lib/esm/charts/Legend.js +0 -174
  48. package/lib/esm/charts/Legend.js.map +0 -1
  49. package/lib/esm/charts/Line.js +0 -106
  50. package/lib/esm/charts/Line.js.map +0 -1
  51. package/lib/esm/charts/axis/XAxis.js +0 -176
  52. package/lib/esm/charts/axis/XAxis.js.map +0 -1
  53. package/lib/esm/charts/axis/YAxis.js +0 -164
  54. package/lib/esm/charts/axis/YAxis.js.map +0 -1
  55. package/lib/esm/charts/axis/chart-axis-utils.js +0 -38
  56. package/lib/esm/charts/axis/chart-axis-utils.js.map +0 -1
  57. package/lib/esm/charts/axis/chart-tick-generation.js +0 -217
  58. package/lib/esm/charts/axis/chart-tick-generation.js.map +0 -1
  59. package/lib/esm/charts/index.js +0 -22
  60. package/lib/esm/charts/index.js.map +0 -1
  61. package/lib/esm/charts/utils.js +0 -37
  62. package/lib/esm/charts/utils.js.map +0 -1
  63. package/lib/types/charts/CartesianGrid.d.ts +0 -32
  64. package/lib/types/charts/Chart.d.ts +0 -109
  65. package/lib/types/charts/ChartTooltip.d.ts +0 -69
  66. package/lib/types/charts/Charts.d.ts +0 -104
  67. package/lib/types/charts/Dot.d.ts +0 -27
  68. package/lib/types/charts/HollowDot.d.ts +0 -28
  69. package/lib/types/charts/Legend.d.ts +0 -25
  70. package/lib/types/charts/Line.d.ts +0 -44
  71. package/lib/types/charts/axis/XAxis.d.ts +0 -28
  72. package/lib/types/charts/axis/YAxis.d.ts +0 -28
  73. package/lib/types/charts/axis/chart-axis-utils.d.ts +0 -33
  74. package/lib/types/charts/axis/chart-tick-generation.d.ts +0 -33
  75. package/lib/types/charts/index.d.ts +0 -21
  76. package/lib/types/charts/utils.d.ts +0 -23
@@ -1,761 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.Chart = exports.ChartsChartContext = exports.canvasRightPadding = exports.canvasLeftPadding = void 0;
27
- exports.getPointPixels = getPointPixels;
28
- const jsx_runtime_1 = require("react/jsx-runtime");
29
- /**
30
- *
31
- * Copyright (c) "Neo4j"
32
- * Neo4j Sweden AB [http://neo4j.com]
33
- *
34
- * This file is part of Neo4j.
35
- *
36
- * Neo4j is free software: you can redistribute it and/or modify
37
- * it under the terms of the GNU General Public License as published by
38
- * the Free Software Foundation, either version 3 of the License, or
39
- * (at your option) any later version.
40
- *
41
- * This program is distributed in the hope that it will be useful,
42
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
43
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44
- * GNU General Public License for more details.
45
- *
46
- * You should have received a copy of the GNU General Public License
47
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
48
- */
49
- const react_1 = __importStar(require("react"));
50
- const d3 = __importStar(require("d3"));
51
- const Charts_1 = require("./Charts");
52
- const defaultImports_1 = require("../_common/defaultImports");
53
- const Line_1 = require("./Line");
54
- const Dot_1 = require("./Dot");
55
- const d3_shape_1 = require("d3-shape");
56
- const ChartTooltip_1 = require("./ChartTooltip");
57
- const usehooks_ts_1 = require("usehooks-ts");
58
- const utils_1 = require("./utils");
59
- // Used to visualize the voronoi polygons.
60
- const SHOULD_SHOW_VORONOI_DEBUG = false;
61
- // Used to throttle calls to on mouse move over the polygons.
62
- const THROTTLE_MOUSE_MOVE_MILLISECONDS = 100;
63
- const defaultChartProps = {
64
- scales: {},
65
- pointSelectionType: 'single',
66
- };
67
- exports.canvasLeftPadding = 0;
68
- exports.canvasRightPadding = 0;
69
- // eslint-disable-next-line no-redeclare
70
- exports.ChartsChartContext = (0, react_1.createContext)({
71
- chartRef: null,
72
- contentRef: null,
73
- contentSize: undefined,
74
- scales: {},
75
- linePointArray: [],
76
- mousePositionRef: undefined,
77
- });
78
- function getPointPixels(linePoint, scales) {
79
- const { accessorXAxis, scaleXAxisId } = linePoint;
80
- const { accessorYAxis, scaleYAxisId } = linePoint;
81
- const valueX = linePoint[accessorXAxis];
82
- const valueY = linePoint[accessorYAxis];
83
- const scaleXAxisObject = scales[scaleXAxisId];
84
- const scaleYAxisObject = scales[scaleYAxisId];
85
- const scaleXAxis = scaleXAxisObject.functionWithContentWidthAsRange;
86
- const scaleYAxis = scaleYAxisObject.functionWithContentHeightAsRange;
87
- const pointPixelX = scaleXAxis(valueX);
88
- const pointPixelY = scaleYAxis(valueY);
89
- return { pointPixelX, pointPixelY };
90
- }
91
- function dotsPropsAreEqual(prevProps, nextProps) {
92
- const isEqual = JSON.stringify(prevProps.selectedPoints) ==
93
- JSON.stringify(nextProps.selectedPoints) &&
94
- JSON.stringify(prevProps.metadata) == JSON.stringify(nextProps.metadata);
95
- return isEqual;
96
- }
97
- const DotsMemoized = react_1.default.memo(function Dots({ selectedPoints, metadata, scales, }) {
98
- console.info('DotsMemoized >> render');
99
- return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: selectedPoints.map((linePoint) => {
100
- const { key, dataId } = linePoint;
101
- const { pointPixelX, pointPixelY } = getPointPixels(linePoint, scales);
102
- const { isVisible, color } = metadata[dataId];
103
- if (!isVisible)
104
- return null;
105
- return ((0, jsx_runtime_1.jsx)(Dot_1.Dot, { x: pointPixelX, y: pointPixelY, color: color }, `chart-dot-${key}`));
106
- }) }));
107
- }, dotsPropsAreEqual);
108
- function tooltipPropsAreEqual(prevProps, nextProps) {
109
- var _a, _b, _c, _d;
110
- console.info(prevProps, nextProps);
111
- if (prevProps.isOpen !== nextProps.isOpen)
112
- return false;
113
- if (((_a = prevProps.anchorPosition) === null || _a === void 0 ? void 0 : _a.x) !== ((_b = nextProps.anchorPosition) === null || _b === void 0 ? void 0 : _b.x))
114
- return false;
115
- if (((_c = prevProps.anchorPosition) === null || _c === void 0 ? void 0 : _c.y) !== ((_d = nextProps.anchorPosition) === null || _d === void 0 ? void 0 : _d.y))
116
- return false;
117
- const isEqual = JSON.stringify(prevProps.selectedPoints) ==
118
- JSON.stringify(nextProps.selectedPoints) &&
119
- JSON.stringify(prevProps.metadata) == JSON.stringify(nextProps.metadata);
120
- return isEqual;
121
- }
122
- const TooltipMemoized = react_1.default.memo(function Tooltip({ selectedPoints, metadata, anchorPosition, isOpen, }) {
123
- var _a, _b, _c;
124
- return ((0, jsx_runtime_1.jsxs)(ChartTooltip_1.ChartTooltip, { anchorPosition: anchorPosition, isOpen: isOpen, children: [(0, jsx_runtime_1.jsx)(ChartTooltip_1.ChartTooltip.Title, { children: String((_c = (_a = selectedPoints === null || selectedPoints === void 0 ? void 0 : selectedPoints[0]) === null || _a === void 0 ? void 0 : _a[(_b = selectedPoints === null || selectedPoints === void 0 ? void 0 : selectedPoints[0]) === null || _b === void 0 ? void 0 : _b.accessorXAxis]) !== null && _c !== void 0 ? _c : '') }), selectedPoints.map((linePoint) => {
125
- var _a;
126
- if (linePoint === undefined)
127
- return null;
128
- if (metadata[linePoint.dataId].isVisible === false)
129
- return null;
130
- const { key, dataId, accessorYAxis } = linePoint;
131
- const contentKey = `chart-tooltip-content-${key}`;
132
- console.info(contentKey);
133
- return ((0, jsx_runtime_1.jsx)(ChartTooltip_1.ChartTooltip.Content, { leftElement: metadata[dataId].label, rightElement: ((_a = linePoint[accessorYAxis]) === null || _a === void 0 ? void 0 : _a.toString()) || '', indentSquareColor: metadata[dataId].color }, contentKey));
134
- })] }));
135
- }, tooltipPropsAreEqual);
136
- const ChartComponent = (0, react_1.forwardRef)(function ChartComponent({ children, className, scales: scalesProp = defaultChartProps.scales, pointSelectionType = defaultChartProps.pointSelectionType, }, ref) {
137
- ////////////////////////////////////////////////////////////////////////////////////////////////////
138
- ////////////////////////////////////////////////////////////////////////////////////////////////////
139
- ////////////////////////////////////////////////////////////////////////////////////////////////////
140
- console.info('ChartComponent');
141
- const chartsContextValue = (0, react_1.useContext)(Charts_1.ChartsContext);
142
- const { data, metadata } = chartsContextValue;
143
- const chartRef = (0, react_1.useRef)(null);
144
- const contentRef = (0, react_1.useRef)(null);
145
- const horizontalIntersectionLineRef = (0, react_1.useRef)(null);
146
- const mousePositionRef = (0, react_1.useRef)({ x: 0, y: 0 });
147
- const [draggingPosition, setDraggingPosition] = (0, react_1.useState)(undefined);
148
- const [isMouseDown, setIsMouseDown] = (0, react_1.useState)(false);
149
- const [hasLines, setHasLines] = (0, react_1.useState)(false);
150
- const [contentSize, setContentSize] = (0, react_1.useState)(undefined);
151
- // The svg paths calculated for the voronoi polygons, includes
152
- // the assigned line point for the section for the onHover event.
153
- const [voronoiPolygons, setVoronoiPolygons] = (0, react_1.useState)();
154
- const [tooltipAnchorPosition, setTooltipAnchorPosition] = (0, react_1.useState)({
155
- x: 0,
156
- y: 0,
157
- });
158
- // The points that will display a thumb and be included in the tooltip.
159
- const [selectedPoints, setSelectedPoints] = (0, react_1.useState)(null);
160
- ////////////////////////////////////////////////////////////////////////////////////////////////////
161
- ////////////////////////////////////////////////////////////////////////////////////////////////////
162
- ////////////////////////////////////////////////////////////////////////////////////////////////////
163
- const initialScales = (0, react_1.useMemo)(() => {
164
- // Map the string scales to actual d3 function scales.
165
- // Will only re-run if the scalesProp changes.
166
- const initialScales = {};
167
- Object.entries(scalesProp).forEach(([key, value]) => {
168
- switch (value.type) {
169
- case 'linear':
170
- initialScales[key] = {
171
- originalDomain: null,
172
- functionWithContentWidthAsRange: d3.scaleLinear(),
173
- functionWithContentHeightAsRange: d3.scaleLinear(),
174
- function: d3.scaleLinear(),
175
- };
176
- break;
177
- case 'log':
178
- initialScales[key] = {
179
- originalDomain: null,
180
- functionWithContentWidthAsRange: d3.scaleLog(),
181
- functionWithContentHeightAsRange: d3.scaleLog(),
182
- function: d3.scaleLog(),
183
- };
184
- break;
185
- case 'time':
186
- initialScales[key] = {
187
- originalDomain: null,
188
- functionWithContentWidthAsRange: d3.scaleTime(),
189
- functionWithContentHeightAsRange: d3.scaleTime(),
190
- function: d3.scaleTime(),
191
- };
192
- break;
193
- case 'utc':
194
- initialScales[key] = {
195
- originalDomain: null,
196
- functionWithContentWidthAsRange: d3.scaleUtc(),
197
- functionWithContentHeightAsRange: d3.scaleUtc(),
198
- function: d3.scaleUtc(),
199
- };
200
- break;
201
- default:
202
- console.error('Invalid scale type');
203
- break;
204
- }
205
- });
206
- return initialScales;
207
- }, [scalesProp]);
208
- const [scales, setScales] = (0, react_1.useState)(initialScales);
209
- const voronoiScale = (0, react_1.useMemo)(() => {
210
- const initialVoronoiScale = {
211
- originalDomain: null,
212
- functionWithContentWidthAsRange: d3.scaleLinear(),
213
- functionWithContentHeightAsRange: d3.scaleLinear(),
214
- function: d3.scaleLinear(),
215
- };
216
- return initialVoronoiScale;
217
- }, []);
218
- (0, react_1.useEffect)(() => {
219
- // Needs to run after first render to determine size of content container.
220
- if (!contentRef.current)
221
- return;
222
- const contentElement = contentRef.current;
223
- requestAnimationFrame(() => {
224
- if (contentElement && contentElement.getBoundingClientRect) {
225
- const newContentSize = {
226
- width: contentElement.getBoundingClientRect().width,
227
- height: contentElement.getBoundingClientRect().height,
228
- };
229
- // Will force a re-render, calculations require
230
- // width and height of the content container.
231
- setContentSize(newContentSize);
232
- }
233
- });
234
- }, [contentRef]);
235
- const onResize = (0, usehooks_ts_1.useDebounceCallback)(() => {
236
- requestAnimationFrame(() => {
237
- const contentElement = contentRef.current;
238
- if (!contentElement)
239
- return;
240
- const rect = contentElement.getBoundingClientRect();
241
- if (!rect)
242
- return;
243
- const newContentSize = {
244
- width: rect.width - exports.canvasLeftPadding - exports.canvasRightPadding,
245
- height: rect.height,
246
- };
247
- setContentSize(newContentSize);
248
- });
249
- }, 100);
250
- (0, usehooks_ts_1.useResizeObserver)({
251
- ref: chartRef,
252
- onResize: onResize,
253
- });
254
- (0, react_1.useImperativeHandle)(ref, () => {
255
- const svgChart = chartRef.current;
256
- if (!svgChart) {
257
- return null;
258
- }
259
- return svgChart;
260
- });
261
- // The initial line point array is the representation of the full data
262
- // that came into the component via the prop.
263
- // useMemo:initialLinePointArray
264
- const { initialLinePointArray, lineMap } = (0, react_1.useMemo)(() => {
265
- if (!data)
266
- return { initialLinePointArray: [], lineMap: {} };
267
- // This use effect is specific for Line components.
268
- // If there are no line components then we don't need to do anything.
269
- const arrayChildren = react_1.Children.toArray(children);
270
- const hasLines = arrayChildren.some((child) => (0, react_1.isValidElement)(child) && child.type === Line_1.Line);
271
- if (!hasLines)
272
- return;
273
- setHasLines(true);
274
- const lineMap = {};
275
- // Used by react for the key properties.
276
- let key = 0;
277
- // Here we are figuring out the min and max values for all the scales.
278
- // To do this we need to iterate through all of the lines and assign
279
- // each lines designated scale to a min max object specific for that scale.
280
- const newInitialLinePointArray = [];
281
- arrayChildren.forEach((child) => {
282
- // Per line.
283
- if (!(0, react_1.isValidElement)(child) || child.type !== Line_1.Line)
284
- return;
285
- const { scaleXAxis: scaleXAxisId, scaleYAxis: scaleYAxisId, accessorXAxis: initialAccessorXAxis, accessorYAxis: initialAccessorYAxis, accessorHollowDot, dataId, seriesInterval, seriesIntervalStartValue, } = child.props;
286
- // Need to set these values for series type data.
287
- const accessorXAxis = seriesInterval
288
- ? 'xAxisValue'
289
- : initialAccessorXAxis;
290
- const accessorYAxis = seriesInterval
291
- ? 'yAxisValue'
292
- : initialAccessorYAxis;
293
- const lineDataIndex = data.findIndex((data) => data.id === dataId);
294
- const lineData = data[lineDataIndex];
295
- if (!lineData)
296
- return; // Id given does not exist in the data array.
297
- // Convert line data to d3 line data.
298
- let singleLinePointArray = [];
299
- if (seriesInterval && seriesIntervalStartValue) {
300
- // Series interval.
301
- const seriesLineData = lineData;
302
- singleLinePointArray = seriesLineData.data.map((dataPoint, index) => {
303
- const newLinePoint = {
304
- key,
305
- dataId,
306
- pointIndex: index,
307
- dataIndex: lineDataIndex,
308
- accessorXAxis,
309
- accessorYAxis,
310
- accessorHollowDot,
311
- xAxisValue: dataPoint !== undefined && dataPoint !== null
312
- ? seriesIntervalStartValue + seriesInterval * index
313
- : null,
314
- yAxisValue: dataPoint,
315
- scaleXAxisId,
316
- scaleYAxisId,
317
- selectionGroup: null,
318
- };
319
- return newLinePoint;
320
- });
321
- }
322
- else {
323
- // At least two dimensions are required for the line data.
324
- const nonSeriesData = lineData;
325
- singleLinePointArray = nonSeriesData.data.map((dataPointArray, index) => {
326
- const newLinePoint = {
327
- key,
328
- dataId,
329
- pointIndex: index,
330
- dataIndex: lineDataIndex,
331
- accessorXAxis,
332
- accessorYAxis,
333
- accessorHollowDot,
334
- scaleXAxisId,
335
- scaleYAxisId,
336
- selectionGroup: null,
337
- };
338
- dataPointArray.forEach((dataPoint, index) => {
339
- newLinePoint[nonSeriesData.metadata[index]] = dataPoint;
340
- });
341
- return newLinePoint;
342
- });
343
- }
344
- key += 1;
345
- // Add this single line to the array that holds all of the lines.
346
- newInitialLinePointArray.push(...singleLinePointArray);
347
- // Line map for easier access to points from the same line. No need
348
- // to iterate through the line point array.
349
- // console.info('setting lineMap', lineMap);
350
- lineMap[dataId] = singleLinePointArray;
351
- });
352
- const initialLinePointArray = newInitialLinePointArray;
353
- return { initialLinePointArray, lineMap };
354
- }, [data, children]) || {};
355
- (0, react_1.useEffect)(() => {
356
- var _a, _b;
357
- if (!contentRef.current)
358
- return;
359
- // Round the width and height to avoid floating point precision issues.
360
- const width = Math.round((_a = contentSize === null || contentSize === void 0 ? void 0 : contentSize.width) !== null && _a !== void 0 ? _a : 0);
361
- const height = Math.round((_b = contentSize === null || contentSize === void 0 ? void 0 : contentSize.height) !== null && _b !== void 0 ? _b : 0);
362
- if (width === 0 || height === 0)
363
- return;
364
- if (initialLinePointArray === undefined)
365
- return;
366
- // Need to reset the originalDomain for each scale.
367
- let newScales = {};
368
- Object.entries(scales).forEach(([key, scale]) => {
369
- newScales[key] = Object.assign(Object.assign({}, scale), { originalDomain: null });
370
- });
371
- initialLinePointArray.forEach((linePoint) => {
372
- var _a, _b;
373
- const { accessorXAxis, accessorYAxis, scaleXAxisId, scaleYAxisId } = linePoint;
374
- // Get min and max values for both x and y axis from the data point array.
375
- const minValueXAxis = d3.min(initialLinePointArray,
376
- // @ts-expect-error d3 types are not correct
377
- (linePoint) => linePoint[accessorXAxis]);
378
- const maxValueXAxis = d3.max(initialLinePointArray,
379
- // @ts-expect-error d3 types are not correct
380
- (linePoint) => linePoint[accessorXAxis]);
381
- const minValueYAxis = d3.min(initialLinePointArray,
382
- // @ts-expect-error d3 types are not correct
383
- (linePoint) => linePoint[accessorYAxis]);
384
- const maxValueYAxis = d3.max(initialLinePointArray,
385
- // @ts-expect-error d3 types are not correct
386
- (linePoint) => linePoint[accessorYAxis]);
387
- const scaleXAxis = newScales[scaleXAxisId];
388
- const scaleYAxis = newScales[scaleYAxisId];
389
- // Here we figure out the min and max values for the specific scale,
390
- // this compares previous lines min max values to the current lines
391
- // min max values on the same scale.
392
- let scaleXAxisMin = undefined;
393
- let scaleXAxisMax = undefined;
394
- let scaleYAxisMin = undefined;
395
- let scaleYAxisMax = undefined;
396
- if (scalesProp[scaleXAxisId].domain !== undefined) {
397
- // The domain value is set manually.
398
- scaleXAxisMin = scalesProp[scaleXAxisId].domain[0];
399
- scaleXAxisMax = scalesProp[scaleXAxisId].domain[1];
400
- }
401
- else if (scaleXAxis !== undefined &&
402
- (scaleXAxis === null || scaleXAxis === void 0 ? void 0 : scaleXAxis.originalDomain) !== null) {
403
- //@ts-expect-error d3 types are not correct - it can handle undefined
404
- scaleXAxisMin = d3.min([scaleXAxis.originalDomain[0], minValueXAxis]);
405
- //@ts-expect-error d3 types are not correct - it can handle undefined
406
- scaleXAxisMax = d3.max([scaleXAxis.originalDomain[1], maxValueXAxis]);
407
- }
408
- else {
409
- scaleXAxisMin = minValueXAxis;
410
- scaleXAxisMax = maxValueXAxis;
411
- }
412
- if (scalesProp[scaleYAxisId].domain !== undefined) {
413
- // The domain value is set manually.
414
- scaleYAxisMin = scalesProp[scaleYAxisId].domain[0];
415
- scaleYAxisMax = scalesProp[scaleYAxisId].domain[1];
416
- }
417
- else if (scaleYAxis !== undefined &&
418
- scaleXAxis.originalDomain !== null) {
419
- //@ts-expect-error d3 types are not correct - it can handle undefined
420
- scaleYAxisMin = d3.min([(_a = scaleYAxis.originalDomain) === null || _a === void 0 ? void 0 : _a[0], minValueYAxis]);
421
- //@ts-expect-error d3 types are not correct - it can handle undefined
422
- scaleYAxisMax = d3.max([(_b = scaleYAxis.originalDomain) === null || _b === void 0 ? void 0 : _b[1], maxValueYAxis]);
423
- }
424
- else {
425
- scaleYAxisMin = minValueYAxis;
426
- scaleYAxisMax = maxValueYAxis;
427
- }
428
- // D3 types are not correct it will be the same type as the original domain.
429
- // casting it to [number, number] to avoid type errors.
430
- const xValuesDomain = [scaleXAxisMin, scaleXAxisMax];
431
- // D3 types are not correct it will be the same type as the original domain.
432
- // casting it to [number, number] to avoid type errors.
433
- const yValuesDomain = [scaleYAxisMin, scaleYAxisMax];
434
- scaleXAxis.function.domain(xValuesDomain);
435
- scaleXAxis.originalDomain = xValuesDomain;
436
- scaleXAxis.functionWithContentWidthAsRange.domain(xValuesDomain);
437
- scaleXAxis.functionWithContentWidthAsRange.range([0, width !== null && width !== void 0 ? width : 0]);
438
- scaleXAxis.functionWithContentHeightAsRange.domain(xValuesDomain);
439
- scaleXAxis.functionWithContentHeightAsRange.range([height !== null && height !== void 0 ? height : 0, 0]);
440
- scaleYAxis.function.domain(yValuesDomain);
441
- scaleYAxis.originalDomain = yValuesDomain;
442
- scaleYAxis.functionWithContentWidthAsRange.domain(yValuesDomain);
443
- scaleYAxis.functionWithContentWidthAsRange.range([0, width !== null && width !== void 0 ? width : 0]);
444
- scaleYAxis.functionWithContentHeightAsRange.domain(yValuesDomain);
445
- scaleYAxis.functionWithContentHeightAsRange.range([height !== null && height !== void 0 ? height : 0, 0]); // inverting vertical scale since SVG has 0,0 at top left.
446
- // Update the specific scale.
447
- newScales = Object.assign(Object.assign({}, newScales), { [scaleXAxisId]: scaleXAxis, [scaleYAxisId]: scaleYAxis });
448
- });
449
- voronoiScale.function.domain([0, width !== null && width !== void 0 ? width : 0]);
450
- voronoiScale.originalDomain = [0, width !== null && width !== void 0 ? width : 0];
451
- voronoiScale.functionWithContentWidthAsRange.domain([0, width !== null && width !== void 0 ? width : 0]);
452
- voronoiScale.functionWithContentWidthAsRange.range([0, width !== null && width !== void 0 ? width : 0]);
453
- setScales(newScales);
454
- }, [contentSize, scalesProp, children, initialLinePointArray]);
455
- const { linePointArray, pointSelectionGroups } = (0, react_1.useMemo)(() => {
456
- if (initialLinePointArray === undefined)
457
- return {};
458
- // The following is only used for pointSelectionType x-axis and y-axis
459
- const pointSelectionGroups = {};
460
- const linePointArray = initialLinePointArray.map((linePoint) => {
461
- const { pointPixelX, pointPixelY } = getPointPixels(linePoint, scales);
462
- const pointPixelAxis = pointSelectionType === 'x-axis' || pointSelectionType === 'single'
463
- ? pointPixelX
464
- : pointPixelY;
465
- if (pointPixelAxis === undefined)
466
- return linePoint;
467
- linePoint.selectionGroup = pointPixelAxis;
468
- if (!pointSelectionGroups[pointPixelAxis]) {
469
- pointSelectionGroups[pointPixelAxis] = [];
470
- pointSelectionGroups[pointPixelAxis].push(linePoint);
471
- }
472
- else {
473
- // Pixel point already exists.
474
- pointSelectionGroups[pointPixelAxis].push(linePoint);
475
- }
476
- return linePoint;
477
- });
478
- return { linePointArray, pointSelectionGroups };
479
- }, [pointSelectionType, initialLinePointArray, scales]);
480
- const voronoiPolygonPointsArray = (0, react_1.useMemo)(() => {
481
- const width = contentSize === null || contentSize === void 0 ? void 0 : contentSize.width;
482
- const height = contentSize === null || contentSize === void 0 ? void 0 : contentSize.height;
483
- if (!width || !height)
484
- return;
485
- if (lineMap === undefined)
486
- return;
487
- if (linePointArray === undefined)
488
- return;
489
- const visibleLinePointArray = linePointArray.filter((linePoint) => {
490
- return metadata[linePoint.dataId].isVisible;
491
- });
492
- const delaunay = d3.Delaunay.from(visibleLinePointArray,
493
- // @ts-expect-error - d3 types are not correct, it can handle null being returned.
494
- (linePoint) => {
495
- const { accessorXAxis, scaleXAxisId } = linePoint;
496
- const valueX = linePoint[accessorXAxis];
497
- const scaleXAxisObject = scales[scaleXAxisId];
498
- const scaleXAxis = scaleXAxisObject.functionWithContentWidthAsRange;
499
- const scaleXAxisCopy = scaleXAxis.copy();
500
- scaleXAxisCopy.domain(scaleXAxisObject.originalDomain);
501
- const pointPixelX = scaleXAxisCopy(valueX);
502
- // We must return null otherwise some points won't work with voronoi/tooltips.
503
- return pointPixelX === undefined ? null : pointPixelX;
504
- }, (linePoint) => {
505
- const { accessorYAxis, scaleYAxisId } = linePoint;
506
- const valueY = linePoint[accessorYAxis];
507
- const scaleYAxisObject = scales[scaleYAxisId];
508
- const scaleYAxis = scaleYAxisObject.functionWithContentHeightAsRange;
509
- const scaleYAxisCopy = scaleYAxis.copy();
510
- scaleYAxisCopy.domain(scaleYAxisObject.originalDomain);
511
- const pointPixelY = scaleYAxis(valueY);
512
- // We must return null otherwise some points won't work with voronoi/tooltips.
513
- return pointPixelY === undefined ? null : pointPixelY;
514
- });
515
- const voronoi = delaunay.voronoi([0, 0, width, height]);
516
- const voronoiPolygons = voronoi.cellPolygons();
517
- const voronoiPolygonPointsArray = Array.from(voronoiPolygons);
518
- return voronoiPolygonPointsArray;
519
- }, [metadata, linePointArray]);
520
- (0, react_1.useEffect)(() => {
521
- if (isMouseDown)
522
- return;
523
- if (voronoiPolygonPointsArray === undefined)
524
- return;
525
- if (linePointArray === undefined)
526
- return;
527
- const visibleLinePointArray = linePointArray.filter((linePoint) => {
528
- return metadata[linePoint.dataId].isVisible;
529
- });
530
- const delaunayLineFn = (0, d3_shape_1.line)();
531
- const newVoronoiPolygons = voronoiPolygonPointsArray.map((voronoiPolygonPoints) => {
532
- const newVoronoiPolygonPoints = voronoiPolygonPoints.map((voronoiPolygonPoint) => {
533
- const [x, y] = voronoiPolygonPoint;
534
- const scale = voronoiScale.functionWithContentWidthAsRange;
535
- const newX = scale(x);
536
- const newVoronoiPolygonPoint = [newX, y];
537
- return newVoronoiPolygonPoint;
538
- });
539
- newVoronoiPolygonPoints.index = voronoiPolygonPoints.index;
540
- const { index } = newVoronoiPolygonPoints;
541
- const path = delaunayLineFn(newVoronoiPolygonPoints);
542
- const linePoint = visibleLinePointArray[index];
543
- const voronoiIndex = index;
544
- return {
545
- path,
546
- linePoint,
547
- voronoiIndex,
548
- };
549
- });
550
- setVoronoiPolygons(newVoronoiPolygons);
551
- }, [voronoiPolygonPointsArray, linePointArray, scales, isMouseDown]);
552
- const handleMouseMove = (event, voronoiPolygon) => {
553
- var _a;
554
- const { linePoint } = voronoiPolygon;
555
- const [xm, ym] = d3.pointer(event, contentRef.current);
556
- const mousePixelX = xm;
557
- const mousePixelY = ym;
558
- mousePositionRef.current.x = mousePixelX;
559
- mousePositionRef.current.y = mousePixelY;
560
- // Zooming
561
- if (isMouseDown) {
562
- // We care about zooming when dragging because we want to fill space
563
- // showing what is being selected.
564
- const newDraggingPosition = Object.assign(Object.assign({}, draggingPosition), { endX: mousePixelX });
565
- setDraggingPosition(newDraggingPosition);
566
- return;
567
- }
568
- const { pointPixelX, pointPixelY } = getPointPixels(linePoint, scales);
569
- if (pointPixelX === undefined || pointPixelY === undefined)
570
- return;
571
- const distance = Math.hypot(pointPixelX - mousePixelX, pointPixelY - mousePixelY);
572
- const radius = 32;
573
- const shouldTrigger = distance < radius && pointSelectionGroups !== undefined;
574
- if (shouldTrigger) {
575
- const selectionGroup = linePoint.selectionGroup;
576
- const hasSelectionGroup = selectionGroup !== null;
577
- if (!hasSelectionGroup) {
578
- console.warn('No selection group for point');
579
- return;
580
- }
581
- const newSelectedPoints = pointSelectionGroups[selectionGroup];
582
- const isNewSelectedPoints = newSelectedPoints !== selectedPoints;
583
- if (isNewSelectedPoints) {
584
- //selectedPointRef.current = { dataId, pointIndex };
585
- const svgRect = (_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
586
- if (svgRect) {
587
- setTooltipAnchorPosition({
588
- x: svgRect.left + pointPixelX,
589
- y: svgRect.top + pointPixelY,
590
- });
591
- }
592
- setSelectedPoints(newSelectedPoints);
593
- }
594
- }
595
- else if (selectedPoints !== null && selectedPoints.length > 0) {
596
- setSelectedPoints(null);
597
- }
598
- };
599
- const throttledHandleMouseMove = (0, utils_1.useThrottle)(handleMouseMove, THROTTLE_MOUSE_MOVE_MILLISECONDS);
600
- const handleMouseLeave = () => {
601
- if (isMouseDown) {
602
- return;
603
- }
604
- setIsMouseDown(false);
605
- setDraggingPosition(undefined);
606
- // Mouse moves outside of the content area.
607
- if (selectedPoints !== null && selectedPoints.length > 0)
608
- setSelectedPoints(null);
609
- };
610
- const handleMouseDown = (event) => {
611
- const amountOfMouseClicks = event.detail;
612
- const isDoubleClick = amountOfMouseClicks === 2;
613
- if (isDoubleClick &&
614
- contentSize !== undefined &&
615
- contentSize.width !== undefined) {
616
- // If zoomed in, return to the original view.
617
- Object.entries(scales).forEach(([scaleId, scale]) => {
618
- if (scale.originalDomain === null)
619
- return;
620
- const newScale = Object.assign({}, scale);
621
- newScale.function.domain(scale.originalDomain);
622
- newScale.functionWithContentWidthAsRange.domain(scale.originalDomain);
623
- scales[scaleId] = newScale;
624
- });
625
- voronoiScale.function.domain([0, contentSize.width]);
626
- voronoiScale.functionWithContentWidthAsRange.domain([
627
- 0,
628
- contentSize.width,
629
- ]);
630
- }
631
- // Set start position for dragging.
632
- const [xm, ym] = d3.pointer(event, contentRef.current);
633
- const mousePixelX = xm;
634
- const mousePixelY = ym;
635
- mousePositionRef.current.x = mousePixelX;
636
- mousePositionRef.current.y = mousePixelY;
637
- // Initial dragging position.
638
- const newDraggingPosition = {
639
- startX: mousePixelX,
640
- endX: undefined,
641
- };
642
- // Needed for the dragging motion.
643
- setIsMouseDown(true);
644
- setDraggingPosition(newDraggingPosition);
645
- };
646
- // must attach mouse listener to window
647
- const handleMouseUp = () => {
648
- const [xm] = d3.pointer(event, contentRef.current);
649
- const mousePixelX = xm;
650
- if ((draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX) === mousePixelX) {
651
- // The user clicked on the same place, no zooming.
652
- setIsMouseDown(false);
653
- setDraggingPosition(undefined);
654
- return;
655
- }
656
- const leftX = (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX) > (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX)
657
- ? draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX
658
- : draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX;
659
- const rightX = (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX) > (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX)
660
- ? draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX
661
- : draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX;
662
- Object.entries(scales).forEach(([scaleId, scale]) => {
663
- const newScale = Object.assign({}, scale);
664
- newScale.function.domain([
665
- newScale.function.invert(leftX),
666
- newScale.function.invert(rightX),
667
- ]);
668
- newScale.functionWithContentWidthAsRange.domain([
669
- newScale.functionWithContentWidthAsRange.invert(leftX),
670
- newScale.functionWithContentWidthAsRange.invert(rightX),
671
- ]);
672
- scales[scaleId] = newScale;
673
- });
674
- // update voronoi scale
675
- voronoiScale.function.domain([
676
- voronoiScale.function.invert(leftX),
677
- voronoiScale.function.invert(rightX),
678
- ]);
679
- voronoiScale.functionWithContentWidthAsRange.domain([
680
- voronoiScale.functionWithContentWidthAsRange.invert(leftX),
681
- voronoiScale.functionWithContentWidthAsRange.invert(rightX),
682
- ]);
683
- setIsMouseDown(false);
684
- setDraggingPosition(undefined);
685
- };
686
- const isCanvasComponent = (child) => {
687
- return (0, react_1.isValidElement)(child) && child.type === Line_1.Line;
688
- };
689
- const width = (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX) && (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX)
690
- ? draggingPosition.endX > draggingPosition.startX
691
- ? draggingPosition.endX - draggingPosition.startX
692
- : draggingPosition.startX - draggingPosition.endX
693
- : 1;
694
- const leftX = (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX) && (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX)
695
- ? (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX) > (draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX)
696
- ? draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.endX
697
- : draggingPosition === null || draggingPosition === void 0 ? void 0 : draggingPosition.startX
698
- : undefined;
699
- const shouldRenderDots = hasLines &&
700
- contentSize !== undefined &&
701
- selectedPoints !== null &&
702
- selectedPoints.length > 0 &&
703
- metadata !== undefined;
704
- const shouldRenderTooltip = hasLines &&
705
- contentSize !== undefined &&
706
- selectedPoints !== null &&
707
- selectedPoints.length > 0 &&
708
- metadata !== undefined;
709
- if (linePointArray === undefined)
710
- return null;
711
- const chartContextValue = {
712
- chartRef,
713
- contentRef,
714
- contentSize,
715
- scales,
716
- linePointArray,
717
- mousePositionRef,
718
- };
719
- const chartClasses = (0, defaultImports_1.classNames)(`ndl-charts-chart`, className);
720
- const contentClasses = (0, defaultImports_1.classNames)(`ndl-charts-chart-content`);
721
- const zoomingClasses = (0, defaultImports_1.classNames)(`ndl-charts-chart-zoom`);
722
- return ((0, jsx_runtime_1.jsx)(exports.ChartsChartContext.Provider, { value: chartContextValue, children: (0, jsx_runtime_1.jsxs)("div", { ref: chartRef, className: chartClasses, onMouseLeave: handleMouseLeave, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, children: [(0, jsx_runtime_1.jsxs)("svg", { ref: contentRef, className: contentClasses, children: [(0, jsx_runtime_1.jsxs)("g", { children: [draggingPosition && contentSize && (
723
- // The zooming rectangle visualizing the selected area.
724
- (0, jsx_runtime_1.jsx)("g", { children: (0, jsx_runtime_1.jsx)("rect", { className: zoomingClasses, x: leftX, width: width, height: contentSize.height }) })), react_1.Children.toArray(children).map((child) => {
725
- return isCanvasComponent(child) ? child : null;
726
- }), hasLines &&
727
- contentSize &&
728
- selectedPoints &&
729
- selectedPoints.length > 0 && (
730
- // Dotted line for x-axis selection.
731
- (0, jsx_runtime_1.jsx)("line", { ref: horizontalIntersectionLineRef, x1: getPointPixels(selectedPoints[0], scales).pointPixelX, x2: getPointPixels(selectedPoints[0], scales).pointPixelX, y1: "0", y2: contentSize.height, style: {
732
- strokeDasharray: '5,7',
733
- stroke: 'rgb(111, 117, 126)',
734
- } })), shouldRenderDots && ((0, jsx_runtime_1.jsx)(DotsMemoized
735
- // Dots together with tooltip to be rendered
736
- , {
737
- // Dots together with tooltip to be rendered
738
- selectedPoints: selectedPoints, metadata: metadata, scales: scales }))] }), (0, jsx_runtime_1.jsx)("g", { children: hasLines &&
739
- voronoiPolygons &&
740
- voronoiPolygons.map((voronoiPolygon, index) => {
741
- // Hover area for each line point.
742
- const { path } = voronoiPolygon;
743
- return ((0, jsx_runtime_1.jsx)("path", { d: path, onMouseMove: (event) => {
744
- throttledHandleMouseMove(event, voronoiPolygon);
745
- }, style: {
746
- strokeWidth: 1,
747
- stroke: 'black',
748
- strokeOpacity: 0.5,
749
- fill: '#fff',
750
- opacity: SHOULD_SHOW_VORONOI_DEBUG ? 0.5 : 0,
751
- } }, index));
752
- }) })] }), (0, jsx_runtime_1.jsx)(TooltipMemoized, { isOpen: shouldRenderTooltip, selectedPoints: selectedPoints !== null && selectedPoints !== void 0 ? selectedPoints : [], metadata: metadata, anchorPosition: tooltipAnchorPosition }), react_1.Children.toArray(children).map((child) => {
753
- return !isCanvasComponent(child) ? child : null;
754
- })] }) }));
755
- });
756
- // Issue with TypeScript forwardRef and subcomponents: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34757#issuecomment-894053907
757
- const Chart = Object.assign(ChartComponent, {
758
- Line: Line_1.Line,
759
- });
760
- exports.Chart = Chart;
761
- //# sourceMappingURL=Chart.js.map