@neo4j-ndl/react-charts 1.1.0 → 1.1.2

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 (185) hide show
  1. package/lib/cjs/charts/Chart.js +56 -462
  2. package/lib/cjs/charts/Chart.js.map +1 -1
  3. package/lib/cjs/charts/ChartContainer.js +71 -0
  4. package/lib/cjs/charts/ChartContainer.js.map +1 -0
  5. package/lib/cjs/charts/ChartEmpty.js.map +1 -1
  6. package/lib/cjs/charts/ChartRender.js +97 -0
  7. package/lib/cjs/charts/ChartRender.js.map +1 -0
  8. package/lib/cjs/charts/ChartTooltip.js.map +1 -1
  9. package/lib/cjs/charts/Legend.js +32 -246
  10. package/lib/cjs/charts/Legend.js.map +1 -1
  11. package/lib/cjs/charts/LegendItem.js +68 -0
  12. package/lib/cjs/charts/LegendItem.js.map +1 -0
  13. package/lib/cjs/charts/LegendLayout.js +363 -0
  14. package/lib/cjs/charts/LegendLayout.js.map +1 -0
  15. package/lib/cjs/charts/hooks/use-chart-instance.js +133 -0
  16. package/lib/cjs/charts/hooks/use-chart-instance.js.map +1 -0
  17. package/lib/cjs/charts/hooks/use-chart-option.js +79 -0
  18. package/lib/cjs/charts/hooks/use-chart-option.js.map +1 -0
  19. package/lib/cjs/charts/hooks/use-chart-refs.js +52 -0
  20. package/lib/cjs/charts/hooks/use-chart-refs.js.map +1 -0
  21. package/lib/cjs/charts/hooks/use-chart-zoom.js +117 -0
  22. package/lib/cjs/charts/hooks/use-chart-zoom.js.map +1 -0
  23. package/lib/cjs/charts/hooks/use-legend-interactions.js +149 -0
  24. package/lib/cjs/charts/hooks/use-legend-interactions.js.map +1 -0
  25. package/lib/cjs/charts/hooks/use-legend-series.js +181 -0
  26. package/lib/cjs/charts/hooks/use-legend-series.js.map +1 -0
  27. package/lib/cjs/charts/hooks/use-legend-visibility.js +91 -0
  28. package/lib/cjs/charts/hooks/use-legend-visibility.js.map +1 -0
  29. package/lib/cjs/charts/index.js +1 -1
  30. package/lib/cjs/charts/index.js.map +1 -1
  31. package/lib/cjs/charts/tests/chart-test-utils.js +56 -9
  32. package/lib/cjs/charts/tests/chart-test-utils.js.map +1 -1
  33. package/lib/cjs/charts/themes/ndl-echarts-theme.js.map +1 -1
  34. package/lib/cjs/charts/{aria-description.js → utils/aria-description.js} +4 -45
  35. package/lib/cjs/charts/utils/aria-description.js.map +1 -0
  36. package/lib/cjs/charts/utils/build-chart-option.js +74 -0
  37. package/lib/cjs/charts/utils/build-chart-option.js.map +1 -0
  38. package/lib/cjs/charts/utils/chart-tooltip-formatter.js +86 -0
  39. package/lib/cjs/charts/utils/chart-tooltip-formatter.js.map +1 -0
  40. package/lib/cjs/charts/utils/chart-types.js.map +1 -0
  41. package/lib/cjs/charts/utils/defaults.js.map +1 -0
  42. package/lib/cjs/charts/{utils.js → utils/format-utils.js} +3 -19
  43. package/lib/cjs/charts/utils/format-utils.js.map +1 -0
  44. package/lib/cjs/charts/utils/legend-layout.js +65 -0
  45. package/lib/cjs/charts/utils/legend-layout.js.map +1 -0
  46. package/lib/cjs/charts/{legend-utils.js → utils/legend-utils.js} +1 -78
  47. package/lib/cjs/charts/utils/legend-utils.js.map +1 -0
  48. package/lib/cjs/charts/utils/threshold.js +114 -0
  49. package/lib/cjs/charts/utils/threshold.js.map +1 -0
  50. package/lib/cjs/charts/{user-option-utils.js → utils/user-option-utils.js} +7 -16
  51. package/lib/cjs/charts/utils/user-option-utils.js.map +1 -0
  52. package/lib/esm/charts/Chart.js +50 -457
  53. package/lib/esm/charts/Chart.js.map +1 -1
  54. package/lib/esm/charts/ChartContainer.js +67 -0
  55. package/lib/esm/charts/ChartContainer.js.map +1 -0
  56. package/lib/esm/charts/ChartEmpty.js.map +1 -1
  57. package/lib/esm/charts/ChartRender.js +93 -0
  58. package/lib/esm/charts/ChartRender.js.map +1 -0
  59. package/lib/esm/charts/ChartTooltip.js.map +1 -1
  60. package/lib/esm/charts/Legend.js +32 -243
  61. package/lib/esm/charts/Legend.js.map +1 -1
  62. package/lib/esm/charts/LegendItem.js +61 -0
  63. package/lib/esm/charts/LegendItem.js.map +1 -0
  64. package/lib/esm/charts/LegendLayout.js +323 -0
  65. package/lib/esm/charts/LegendLayout.js.map +1 -0
  66. package/lib/esm/charts/hooks/use-chart-instance.js +128 -0
  67. package/lib/esm/charts/hooks/use-chart-instance.js.map +1 -0
  68. package/lib/esm/charts/hooks/use-chart-option.js +76 -0
  69. package/lib/esm/charts/hooks/use-chart-option.js.map +1 -0
  70. package/lib/esm/charts/hooks/use-chart-refs.js +48 -0
  71. package/lib/esm/charts/hooks/use-chart-refs.js.map +1 -0
  72. package/lib/esm/charts/hooks/use-chart-zoom.js +114 -0
  73. package/lib/esm/charts/hooks/use-chart-zoom.js.map +1 -0
  74. package/lib/esm/charts/hooks/use-legend-interactions.js +145 -0
  75. package/lib/esm/charts/hooks/use-legend-interactions.js.map +1 -0
  76. package/lib/esm/charts/hooks/use-legend-series.js +178 -0
  77. package/lib/esm/charts/hooks/use-legend-series.js.map +1 -0
  78. package/lib/esm/charts/hooks/use-legend-visibility.js +87 -0
  79. package/lib/esm/charts/hooks/use-legend-visibility.js.map +1 -0
  80. package/lib/esm/charts/index.js +1 -1
  81. package/lib/esm/charts/index.js.map +1 -1
  82. package/lib/esm/charts/tests/chart-test-utils.js +53 -8
  83. package/lib/esm/charts/tests/chart-test-utils.js.map +1 -1
  84. package/lib/esm/charts/themes/ndl-echarts-theme.js.map +1 -1
  85. package/lib/esm/charts/{aria-description.js → utils/aria-description.js} +4 -45
  86. package/lib/esm/charts/utils/aria-description.js.map +1 -0
  87. package/lib/esm/charts/utils/build-chart-option.js +69 -0
  88. package/lib/esm/charts/utils/build-chart-option.js.map +1 -0
  89. package/lib/esm/charts/utils/chart-tooltip-formatter.js +82 -0
  90. package/lib/esm/charts/utils/chart-tooltip-formatter.js.map +1 -0
  91. package/lib/esm/charts/utils/chart-types.js.map +1 -0
  92. package/lib/esm/charts/utils/defaults.js.map +1 -0
  93. package/lib/esm/charts/{utils.js → utils/format-utils.js} +2 -17
  94. package/lib/esm/charts/utils/format-utils.js.map +1 -0
  95. package/lib/esm/charts/utils/legend-layout.js +59 -0
  96. package/lib/esm/charts/utils/legend-layout.js.map +1 -0
  97. package/lib/esm/charts/{legend-utils.js → utils/legend-utils.js} +1 -75
  98. package/lib/esm/charts/utils/legend-utils.js.map +1 -0
  99. package/lib/esm/charts/utils/threshold.js +106 -0
  100. package/lib/esm/charts/utils/threshold.js.map +1 -0
  101. package/lib/esm/charts/{user-option-utils.js → utils/user-option-utils.js} +5 -14
  102. package/lib/esm/charts/utils/user-option-utils.js.map +1 -0
  103. package/lib/types/charts/Chart.d.ts +2 -2
  104. package/lib/types/charts/Chart.d.ts.map +1 -1
  105. package/lib/{esm/charts/types.js → types/charts/ChartContainer.d.ts} +14 -1
  106. package/lib/types/charts/ChartContainer.d.ts.map +1 -0
  107. package/lib/types/charts/ChartEmpty.d.ts +1 -1
  108. package/lib/types/charts/ChartEmpty.d.ts.map +1 -1
  109. package/lib/types/charts/ChartRender.d.ts +36 -0
  110. package/lib/types/charts/ChartRender.d.ts.map +1 -0
  111. package/lib/types/charts/ChartTooltip.d.ts +1 -1
  112. package/lib/types/charts/ChartTooltip.d.ts.map +1 -1
  113. package/lib/types/charts/Legend.d.ts +15 -3
  114. package/lib/types/charts/Legend.d.ts.map +1 -1
  115. package/lib/{cjs/charts/types.js → types/charts/LegendItem.d.ts} +10 -3
  116. package/lib/types/charts/LegendItem.d.ts.map +1 -0
  117. package/lib/types/charts/LegendLayout.d.ts +38 -0
  118. package/lib/types/charts/LegendLayout.d.ts.map +1 -0
  119. package/lib/types/charts/hooks/use-chart-instance.d.ts +62 -0
  120. package/lib/types/charts/hooks/use-chart-instance.d.ts.map +1 -0
  121. package/lib/types/charts/hooks/use-chart-option.d.ts +48 -0
  122. package/lib/types/charts/hooks/use-chart-option.d.ts.map +1 -0
  123. package/lib/types/charts/hooks/use-chart-refs.d.ts +38 -0
  124. package/lib/types/charts/hooks/use-chart-refs.d.ts.map +1 -0
  125. package/lib/types/charts/hooks/use-chart-zoom.d.ts +36 -0
  126. package/lib/types/charts/hooks/use-chart-zoom.d.ts.map +1 -0
  127. package/lib/types/charts/hooks/use-legend-interactions.d.ts +56 -0
  128. package/lib/types/charts/hooks/use-legend-interactions.d.ts.map +1 -0
  129. package/lib/types/charts/hooks/use-legend-series.d.ts +42 -0
  130. package/lib/types/charts/hooks/use-legend-series.d.ts.map +1 -0
  131. package/lib/types/charts/hooks/use-legend-visibility.d.ts +24 -0
  132. package/lib/types/charts/hooks/use-legend-visibility.d.ts.map +1 -0
  133. package/lib/types/charts/index.d.ts +2 -2
  134. package/lib/types/charts/index.d.ts.map +1 -1
  135. package/lib/types/charts/tests/chart-test-utils.d.ts +7 -1
  136. package/lib/types/charts/tests/chart-test-utils.d.ts.map +1 -1
  137. package/lib/types/charts/themes/ndl-echarts-theme.d.ts +1 -1
  138. package/lib/types/charts/themes/ndl-echarts-theme.d.ts.map +1 -1
  139. package/lib/types/charts/utils/aria-description.d.ts.map +1 -0
  140. package/lib/types/charts/utils/build-chart-option.d.ts +52 -0
  141. package/lib/types/charts/utils/build-chart-option.d.ts.map +1 -0
  142. package/lib/types/charts/utils/chart-tooltip-formatter.d.ts +37 -0
  143. package/lib/types/charts/utils/chart-tooltip-formatter.d.ts.map +1 -0
  144. package/lib/types/charts/{chart-types.d.ts → utils/chart-types.d.ts} +23 -22
  145. package/lib/types/charts/utils/chart-types.d.ts.map +1 -0
  146. package/lib/types/charts/utils/defaults.d.ts.map +1 -0
  147. package/lib/types/charts/{utils.d.ts → utils/format-utils.d.ts} +2 -4
  148. package/lib/types/charts/utils/format-utils.d.ts.map +1 -0
  149. package/lib/types/charts/utils/legend-layout.d.ts +37 -0
  150. package/lib/types/charts/utils/legend-layout.d.ts.map +1 -0
  151. package/lib/types/charts/{legend-utils.d.ts → utils/legend-utils.d.ts} +1 -11
  152. package/lib/types/charts/utils/legend-utils.d.ts.map +1 -0
  153. package/lib/types/charts/utils/threshold.d.ts +45 -0
  154. package/lib/types/charts/utils/threshold.d.ts.map +1 -0
  155. package/lib/types/charts/utils/user-option-utils.d.ts.map +1 -0
  156. package/package.json +3 -3
  157. package/lib/cjs/charts/aria-description.js.map +0 -1
  158. package/lib/cjs/charts/chart-types.js.map +0 -1
  159. package/lib/cjs/charts/defaults.js.map +0 -1
  160. package/lib/cjs/charts/legend-utils.js.map +0 -1
  161. package/lib/cjs/charts/types.js.map +0 -1
  162. package/lib/cjs/charts/user-option-utils.js.map +0 -1
  163. package/lib/cjs/charts/utils.js.map +0 -1
  164. package/lib/esm/charts/aria-description.js.map +0 -1
  165. package/lib/esm/charts/chart-types.js.map +0 -1
  166. package/lib/esm/charts/defaults.js.map +0 -1
  167. package/lib/esm/charts/legend-utils.js.map +0 -1
  168. package/lib/esm/charts/types.js.map +0 -1
  169. package/lib/esm/charts/user-option-utils.js.map +0 -1
  170. package/lib/esm/charts/utils.js.map +0 -1
  171. package/lib/types/charts/aria-description.d.ts.map +0 -1
  172. package/lib/types/charts/chart-types.d.ts.map +0 -1
  173. package/lib/types/charts/defaults.d.ts.map +0 -1
  174. package/lib/types/charts/legend-utils.d.ts.map +0 -1
  175. package/lib/types/charts/types.d.ts +0 -44
  176. package/lib/types/charts/types.d.ts.map +0 -1
  177. package/lib/types/charts/user-option-utils.d.ts.map +0 -1
  178. package/lib/types/charts/utils.d.ts.map +0 -1
  179. /package/lib/cjs/charts/{chart-types.js → utils/chart-types.js} +0 -0
  180. /package/lib/cjs/charts/{defaults.js → utils/defaults.js} +0 -0
  181. /package/lib/esm/charts/{chart-types.js → utils/chart-types.js} +0 -0
  182. /package/lib/esm/charts/{defaults.js → utils/defaults.js} +0 -0
  183. /package/lib/types/charts/{aria-description.d.ts → utils/aria-description.d.ts} +0 -0
  184. /package/lib/types/charts/{defaults.d.ts → utils/defaults.d.ts} +0 -0
  185. /package/lib/types/charts/{user-option-utils.d.ts → utils/user-option-utils.d.ts} +0 -0
@@ -1,7 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Chart = Chart;
4
- const jsx_runtime_1 = require("react/jsx-runtime");
5
1
  /**
6
2
  *
7
3
  * Copyright (c) "Neo4j"
@@ -22,74 +18,46 @@ const jsx_runtime_1 = require("react/jsx-runtime");
22
18
  * You should have received a copy of the GNU General Public License
23
19
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
20
  */
25
- const base_1 = require("@neo4j-ndl/base");
26
- const react_1 = require("@neo4j-ndl/react");
27
- const echarts_1 = require("echarts");
28
- const react_2 = require("react");
29
- const server_1 = require("react-dom/server");
30
- const aria_description_1 = require("./aria-description");
31
- const ChartTooltip_1 = require("./ChartTooltip");
32
- const defaults_1 = require("./defaults");
21
+
22
+ "use strict";
23
+ var __rest = (this && this.__rest) || function (s, e) {
24
+ var t = {};
25
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
26
+ t[p] = s[p];
27
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
28
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
29
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
30
+ t[p[i]] = s[p[i]];
31
+ }
32
+ return t;
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.Chart = void 0;
36
+ const jsx_runtime_1 = require("react/jsx-runtime");
37
+ const react_1 = require("react");
38
+ const ChartContainer_1 = require("./ChartContainer");
39
+ const ChartRender_1 = require("./ChartRender");
40
+ const use_chart_refs_1 = require("./hooks/use-chart-refs");
41
+ const use_legend_series_1 = require("./hooks/use-legend-series");
33
42
  const Legend_1 = require("./Legend");
34
- const ndl_echarts_theme_1 = require("./themes/ndl-echarts-theme");
35
- const user_option_utils_1 = require("./user-option-utils");
36
- const utils_1 = require("./utils");
37
- // This returns a boolean if the condition is met
38
- // and also what to display in the tooltip.
39
- function checkCondition(value, condition, threshold) {
40
- switch (condition) {
41
- case 'greater':
42
- return {
43
- conditionText: 'Above',
44
- isConditionMet: value > threshold,
45
- };
46
- case 'greaterOrEqual':
47
- return {
48
- conditionText: value > threshold
49
- ? 'Above'
50
- : value === threshold
51
- ? 'Equal'
52
- : undefined,
53
- isConditionMet: value >= threshold,
54
- };
55
- case 'less':
56
- return {
57
- conditionText: 'Below',
58
- isConditionMet: value < threshold,
59
- };
60
- case 'lessOrEqual':
61
- return {
62
- conditionText: value < threshold
63
- ? 'Below'
64
- : value === threshold
65
- ? 'Equal'
66
- : undefined,
67
- isConditionMet: value <= threshold,
68
- };
69
- case 'equal':
70
- return {
71
- conditionText: 'Equal',
72
- isConditionMet: value === threshold,
73
- };
74
- case 'notEqual':
75
- return {
76
- conditionText: 'Not equal',
77
- isConditionMet: value !== threshold,
78
- };
79
- default:
80
- return { conditionText: undefined, isConditionMet: false };
43
+ /**
44
+ * Bridges the rendered ECharts option into the React legend.
45
+ *
46
+ * `useLegendSeries` reads the current chart option and ECharts color state to
47
+ * derive the legend rows. Rendering is deferred until the chart has completed
48
+ * its first resize so the legend does not measure against an uninitialized
49
+ * chart instance.
50
+ */
51
+ const ChartLegend = ({ chartOption, isLayoutReady, isWaitingForFirstResize, onLayoutReady, }) => {
52
+ const legendSeries = (0, use_legend_series_1.useLegendSeries)({
53
+ chartOption,
54
+ isWaitingForFirstResize,
55
+ });
56
+ if (isWaitingForFirstResize) {
57
+ return ((0, jsx_runtime_1.jsx)("div", { "aria-hidden": "true", className: "ndl-chart-legend-container ndl-chart-legend-container-placeholder" }));
81
58
  }
82
- }
83
- /*
84
- Keep this, if we want more than one type of the same threshold line
85
- this will be useful to use.
86
-
87
- export interface NewRegisteredSeriesOption extends RegisteredSeriesOption {
88
- thresholdLine: ThresholdLineSeriesOption;
89
- }
90
- export type SeriesOption = Values<NewRegisteredSeriesOption>;
91
- export type Series = SeriesOption | SeriesOption[];
92
- */
59
+ return ((0, jsx_runtime_1.jsx)(Legend_1.Legend, { series: legendSeries !== null && legendSeries !== void 0 ? legendSeries : [], onLayoutReady: onLayoutReady, isLayoutReady: isLayoutReady }));
60
+ };
93
61
  /**
94
62
  * A chart component powered by Apache ECharts with Needle's customization and theming.
95
63
  *
@@ -104,397 +72,23 @@ export type Series = SeriesOption | SeriesOption[];
104
72
  * </div>
105
73
  * ```
106
74
  */
107
- function Chart({ dataset, option: userOption, xAxis: propXAxis, yAxis: propYAxis, series: propsSeries, style, settings = {
108
- lazyUpdate: false,
109
- notMerge: true,
110
- silent: false,
111
- }, isLoading, isChartZoomDisabled = false, legend, callbacks, palette, }) {
112
- const chartRef = (0, react_2.useRef)(null);
113
- const chartEchartRef = (0, react_2.useRef)(null);
114
- const chartLegendRef = (0, react_2.useRef)(null);
115
- const legendSelectedRef = (0, react_2.useRef)({});
116
- const legendColorCacheRef = (0, react_2.useRef)(new Map());
117
- const [isWaitingForFirstResize, setIsWaitingForFirstResize] = (0, react_2.useState)(true);
118
- const dataZoomOptions = userOption === null || userOption === void 0 ? void 0 : userOption.dataZoom;
119
- const toolboxOptions = userOption === null || userOption === void 0 ? void 0 : userOption.toolbox;
120
- const hasSliderZoom = Array.isArray(dataZoomOptions)
121
- ? dataZoomOptions.some((dataZoomOption) => {
122
- return (dataZoomOption === null || dataZoomOption === void 0 ? void 0 : dataZoomOption.type) === 'slider';
123
- })
124
- : (dataZoomOptions === null || dataZoomOptions === void 0 ? void 0 : dataZoomOptions.type) === 'slider';
125
- const { theme } = (0, react_1.useNeedleTheme)();
126
- const thresholdLines = (0, react_2.useMemo)(() => {
127
- const seriesArray = Array.isArray(propsSeries)
128
- ? propsSeries
129
- : [propsSeries];
130
- const thresholdLineSeries = seriesArray.filter((currentSeries) => {
131
- return currentSeries.type === 'thresholdLine';
132
- });
133
- return thresholdLineSeries.map((currentThresholdLineSeriesOption) => {
134
- var _a;
135
- return Object.assign(Object.assign({}, currentThresholdLineSeriesOption), { condition: (_a = currentThresholdLineSeriesOption.condition) !== null && _a !== void 0 ? _a : defaults_1.defaultThresholdLineSeriesOption.condition, value: currentThresholdLineSeriesOption.yAxis });
136
- });
137
- }, [propsSeries]);
138
- const dataZoom = (0, react_2.useMemo)(() => {
139
- return (0, user_option_utils_1.mergeDataZoom)(dataZoomOptions);
140
- }, [dataZoomOptions]);
141
- const series = (0, user_option_utils_1.mergeSeries)(propsSeries, theme, userOption, palette);
142
- const xAxis = (0, user_option_utils_1.mergeXAxis)(propXAxis, theme);
143
- const yAxis = (0, user_option_utils_1.mergeYAxis)(propYAxis, theme);
144
- const hasCategoryXAxis = Array.isArray(xAxis)
145
- ? xAxis.some((x) => x.type === 'category')
146
- : (xAxis === null || xAxis === void 0 ? void 0 : xAxis.type) === 'category';
147
- // The initial option used, the charts option is not necessarily
148
- // the same as this due to mutation via dispatch and setOption.
149
- // use getOption to get the current option of the chart.
150
- const initialOption = (0, react_2.useMemo)(() => {
151
- if (chartEchartRef.current === null) {
152
- return;
75
+ const Chart = (_a) => {
76
+ var { style, legend } = _a, chartProps = __rest(_a, ["style", "legend"]);
77
+ const [isWaitingForFirstResize, setIsWaitingForFirstResize] = (0, react_1.useState)(true);
78
+ const [chartOption, setChartOption] = (0, react_1.useState)();
79
+ const [isLegendLayoutReady, setIsLegendLayoutReady] = (0, react_1.useState)(!(legend === null || legend === void 0 ? void 0 : legend.show));
80
+ const handleLegendLayoutReady = (0, react_1.useCallback)(() => {
81
+ setIsLegendLayoutReady(true);
82
+ }, []);
83
+ (0, react_1.useEffect)(() => {
84
+ if (!(legend === null || legend === void 0 ? void 0 : legend.show)) {
85
+ setIsLegendLayoutReady(true);
153
86
  }
154
- const chart = (0, echarts_1.getInstanceByDom)(chartEchartRef.current);
155
- const customAriaDescription = (0, aria_description_1.generateThresholdAriaDescription)(propsSeries, dataset);
156
- const option = Object.assign(Object.assign({ dataset,
157
- xAxis,
158
- yAxis }, userOption), { aria: Object.assign(Object.assign({ decal: {
159
- show: false,
160
- }, enabled: true }, (customAriaDescription !== undefined && {
161
- label: { description: customAriaDescription },
162
- })), userOption === null || userOption === void 0 ? void 0 : userOption.aria), grid: Object.assign({ left: hasCategoryXAxis ? '15px' : '10px', right: hasCategoryXAxis ? '15px' : '10px', top: '10px', bottom: hasSliderZoom ? '60px' : '10px', type: 'solid', containLabel: true }, userOption === null || userOption === void 0 ? void 0 : userOption.grid), legend: {
163
- // Removes in-built echarts legend
164
- show: false,
165
- // Preserve legend filter state across re-renders. legendSelectedRef
166
- // is the single source of truth, written by the Legend component.
167
- // ECharts mutates the options object, so pass a shallow copy here
168
- // to avoid it mutating the React ref object.
169
- selected: Object.assign({}, (legendSelectedRef.current || {})),
170
- }, series, tooltip: {
171
- trigger: 'axis',
172
- confine: true,
173
- // Reset the default tooltip css styling
174
- padding: 0,
175
- borderRadius: 0,
176
- borderWidth: 0,
177
- extraCssText: 'box-shadow: none; background-color: transparent;',
178
- formatter: function (params) {
179
- var _a;
180
- const paramsArray = Array.isArray(params) ? params : [params];
181
- const firstParam = paramsArray[0];
182
- let valueFormatter = utils_1.formatNumberEnUS;
183
- if (typeof (userOption === null || userOption === void 0 ? void 0 : userOption.tooltip) === 'object' &&
184
- userOption.tooltip !== null &&
185
- 'valueFormatter' in userOption.tooltip) {
186
- valueFormatter = userOption.tooltip.valueFormatter;
187
- }
188
- return `${(0, server_1.renderToString)((0, jsx_runtime_1.jsxs)("span", { className: "ndl-charts-chart-tooltip", children: [(0, jsx_runtime_1.jsx)(ChartTooltip_1.ChartTooltip.Title, { children: (_a = firstParam === null || firstParam === void 0 ? void 0 : firstParam.axisValueLabel) !== null && _a !== void 0 ? _a : '' }), paramsArray.map((series) => {
189
- const value = (0, utils_1.extractValueFromTooltipSeries)(series.value, series.encode, series.axisDim);
190
- const seriesValue = typeof value === 'number' ? value : Number(value);
191
- const isThresholdLine = series.seriesName.startsWith('thresholdLine');
192
- const notifications = thresholdLines
193
- .filter((threshold) => {
194
- const { value: thresholdValue, condition, customCondition, } = threshold;
195
- if (Number.isNaN(seriesValue)) {
196
- return false;
197
- }
198
- const isConditionMet = customCondition
199
- ? customCondition(seriesValue, thresholdValue)
200
- : checkCondition(seriesValue, condition, thresholdValue)
201
- .isConditionMet;
202
- return !isThresholdLine && isConditionMet;
203
- })
204
- .map((threshold) => {
205
- const { notificationType, value: thresholdValue, condition, customConditionText, customCondition, } = threshold;
206
- return {
207
- id: `threshold-${notificationType}`,
208
- leadingElement: customCondition
209
- ? customConditionText
210
- : `${checkCondition(seriesValue, condition, thresholdValue).conditionText} threshold`,
211
- notificationType,
212
- trailingElement: valueFormatter(thresholdValue),
213
- };
214
- });
215
- return ((0, jsx_runtime_1.jsx)(ChartTooltip_1.ChartTooltip.Content, { leadingElement: isThresholdLine
216
- ? `${(0, utils_1.capitalizeFirstLetter)(series.seriesName.replace('thresholdLine-', ''))} threshold`
217
- : series.seriesName, trailingElement: valueFormatter(value), indentSquareColor: series.color, notifications: notifications }, series.seriesName));
218
- })] }))}`;
219
- },
220
- }, dataZoom, toolbox: (0, user_option_utils_1.mergeToolbox)(toolboxOptions) });
221
- // Update chart with initial option
222
- chart === null || chart === void 0 ? void 0 : chart.setOption(option, settings);
223
- // Get option returns the current option of the chart.
224
- // This is slightly different than the option we gave as an argument.
225
- // Because we use useMemo in other areas we want to get this set first,
226
- // this is why we are setting then getting then returning. If we did this in
227
- // a useEffect it would run after the other useMemos which is not what we want.
228
- // This is purely for order of operations.
229
- const chartOption = chart === null || chart === void 0 ? void 0 : chart.getOption();
230
- return chartOption;
231
- }, [
232
- thresholdLines,
233
- propsSeries,
234
- dataset,
235
- xAxis,
236
- yAxis,
237
- userOption,
238
- hasCategoryXAxis,
239
- hasSliderZoom,
240
- series,
241
- dataZoom,
242
- toolboxOptions,
243
- settings,
244
- ]);
245
- (0, react_2.useEffect)(() => {
246
- var _a;
247
- // Initialize chart
248
- let chart;
249
- if (chartEchartRef.current !== null) {
250
- (0, echarts_1.registerTheme)('ndl-light', (0, ndl_echarts_theme_1.ndlEchartsTheme)('light', palette));
251
- (0, echarts_1.registerTheme)('ndl-dark', (0, ndl_echarts_theme_1.ndlEchartsTheme)('dark', palette));
252
- const currentChart = (0, echarts_1.getInstanceByDom)(chartEchartRef.current);
253
- if (currentChart) {
254
- chart = currentChart;
255
- }
256
- else {
257
- const echartsTheme = theme === 'light' ? 'ndl-light' : 'ndl-dark';
258
- chart = (0, echarts_1.init)(chartEchartRef.current, echartsTheme, {
259
- renderer: 'svg',
260
- });
261
- }
262
- }
263
- if (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onZoom) {
264
- chart === null || chart === void 0 ? void 0 : chart.on('datazoom', () => {
265
- var _a;
266
- if (chartEchartRef.current === null) {
267
- return;
268
- }
269
- const currentChart = (0, echarts_1.getInstanceByDom)(chartEchartRef.current);
270
- if (!currentChart) {
271
- return;
272
- }
273
- const option = currentChart.getOption();
274
- if (!option || !option.dataZoom) {
275
- return;
276
- }
277
- const dataZoom = Array.isArray(option.dataZoom)
278
- ? option.dataZoom[0]
279
- : option.dataZoom;
280
- const { startValue, endValue } = dataZoom;
281
- (_a = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onZoom) === null || _a === void 0 ? void 0 : _a.call(callbacks, { endValue, startValue });
282
- });
283
- }
284
- // Add chart resize listener
285
- // ResizeObserver is leading to a bit janky UX
286
- function resizeChart() {
287
- // TODO: We need to revisit this. Right now using grid containLabel seems to work.
288
- // We still need to visit this for overflowing of the x-axis labels.
289
- // const calculateGridLeft = () => {
290
- // const textElements = chartEchartRef.current?.querySelectorAll('text');
291
- // const filteredTextElements = Array.from(textElements || []).filter(
292
- // (element) => element.getAttribute('text-anchor') === 'end',
293
- // );
294
- // let maxWidth = 0;
295
- // filteredTextElements.forEach((element) => {
296
- // const bbox = element.getBBox();
297
- // maxWidth = Math.max(maxWidth, bbox.width);
298
- // });
299
- // const tickLength = 5;
300
- // const tickPadding = 3;
301
- // return maxWidth + tickLength + tickPadding;
302
- // };
303
- //
304
- // // Example of dynamically setting the grid's left
305
- // const gridLeft = calculateGridLeft();
306
- // chart?.setOption({
307
- // grid: {
308
- // left: gridLeft,
309
- // },
310
- // });
311
- var _a, _b, _c;
312
- // We want to fit the chart to the charts container.
313
- const chartContainerHeight = (_a = chartRef.current) === null || _a === void 0 ? void 0 : _a.clientHeight;
314
- const chartContainerWidth = (_b = chartRef.current) === null || _b === void 0 ? void 0 : _b.clientWidth;
315
- // Need to take legends height into consideration, otherwise it will overflow the parent.
316
- const chartLegendHeight = ((_c = chartLegendRef === null || chartLegendRef === void 0 ? void 0 : chartLegendRef.current) === null || _c === void 0 ? void 0 : _c.clientHeight) || 0;
317
- const height = chartContainerHeight
318
- ? chartContainerHeight - chartLegendHeight
319
- : undefined;
320
- chart === null || chart === void 0 ? void 0 : chart.resize({
321
- height,
322
- width: chartContainerWidth,
323
- });
324
- }
325
- window.addEventListener('resize', resizeChart);
326
- requestAnimationFrame(() => {
327
- resizeChart();
328
- setIsWaitingForFirstResize(false);
329
- });
330
- // Add chart zoom listeners
331
- const handleMouseMove = () => {
332
- // I do not like this at all: https://github.com/apache/echarts/issues/19819
333
- // echarts updates the svgs on every mouse movement so we need to do this.
334
- chart === null || chart === void 0 ? void 0 : chart.getZr().setCursorStyle('default');
335
- };
336
- // We cannot use chart.getZr().on('mousemove', handleMouseMove)
337
- // This is because it doesn't respect our callbacks. It will
338
- // always run echarts code last instead of our callback.
339
- const chartChild = (_a = chartEchartRef.current) === null || _a === void 0 ? void 0 : _a.children[0];
340
- if (!isChartZoomDisabled) {
341
- chartChild.addEventListener('mousemove', handleMouseMove);
342
- }
343
- const handleMouseDown = (params) => {
344
- var _a;
345
- const event = params.event;
346
- const amountOfMouseClicks = event.detail;
347
- const isDoubleClick = amountOfMouseClicks === 2;
348
- if (isDoubleClick) {
349
- // Reset zooming.
350
- if (chart === undefined) {
351
- return;
352
- }
353
- chart === null || chart === void 0 ? void 0 : chart.dispatchAction({
354
- end: 100,
355
- start: 0,
356
- type: 'dataZoom',
357
- });
358
- if (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onZoom) {
359
- (_a = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onZoom) === null || _a === void 0 ? void 0 : _a.call(callbacks, { endValue: 100, startValue: 0 });
360
- }
361
- }
362
- };
363
- if (!isChartZoomDisabled) {
364
- chart === null || chart === void 0 ? void 0 : chart.getZr().on('mousedown', handleMouseDown);
365
- }
366
- // Return cleanup function
367
- const chartRefCurrentElement = chartEchartRef.current;
368
- const element = chartRefCurrentElement === null || chartRefCurrentElement === void 0 ? void 0 : chartRefCurrentElement.children[0];
369
- return () => {
370
- window.removeEventListener('resize', resizeChart);
371
- element === null || element === void 0 ? void 0 : element.removeEventListener('mousemove', handleMouseMove);
372
- // Remove chart zoom handlers
373
- if (chartRefCurrentElement) {
374
- const chart = (0, echarts_1.getInstanceByDom)(chartRefCurrentElement);
375
- chart === null || chart === void 0 ? void 0 : chart.getZr().off('mousedown', handleMouseDown);
376
- }
377
- };
378
- });
379
- (0, react_2.useEffect)(() => {
380
- if (chartEchartRef.current === null) {
381
- return;
382
- }
383
- const chart = (0, echarts_1.getInstanceByDom)(chartEchartRef === null || chartEchartRef === void 0 ? void 0 : chartEchartRef.current);
384
- if (isLoading === true || isWaitingForFirstResize) {
385
- chart === null || chart === void 0 ? void 0 : chart.showLoading({
386
- color: base_1.tokens.theme[theme].color.primary.bg.status,
387
- fontFamily: base_1.tokens.typography['label'].fontFamily,
388
- fontSize: base_1.tokens.typography['label'].fontSize,
389
- fontWeight: base_1.tokens.typography['label'].fontWeight,
390
- maskColor: `rgb( from ${base_1.tokens.theme[theme].color.neutral.text.inverse} r g b / 0.8)`,
391
- text: 'Loading',
392
- textColor: base_1.tokens.theme[theme].color.neutral.text.default,
393
- });
394
- }
395
- else {
396
- chart === null || chart === void 0 ? void 0 : chart.hideLoading();
397
- }
398
- }, [isLoading, isWaitingForFirstResize, theme]);
399
- const legendSeries = (0, react_2.useMemo)(() => {
400
- var _a;
401
- if (chartEchartRef.current === null || isWaitingForFirstResize) {
402
- return;
403
- }
404
- const chart = (0, echarts_1.getInstanceByDom)(chartEchartRef === null || chartEchartRef === void 0 ? void 0 : chartEchartRef.current);
405
- const optionSeries = (_a = initialOption === null || initialOption === void 0 ? void 0 : initialOption.series) !== null && _a !== void 0 ? _a : [];
406
- const optionDataset = initialOption === null || initialOption === void 0 ? void 0 : initialOption.dataset;
407
- const legendSeries = [];
408
- if (Array.isArray(optionSeries)) {
409
- optionSeries.forEach((currentSeries, index) => {
410
- var _a, _b, _c, _d;
411
- if (currentSeries === null) {
412
- return;
413
- }
414
- else if (currentSeries.type === 'line' &&
415
- typeof currentSeries.name === 'string' &&
416
- ((_a = currentSeries.name) === null || _a === void 0 ? void 0 : _a.includes('thresholdLine'))) {
417
- return;
418
- }
419
- else if (currentSeries.type === 'pie') {
420
- const encodedItemName = (_b = currentSeries.encode) === null || _b === void 0 ? void 0 : _b.itemName;
421
- let currentDataset = Array.isArray(optionDataset)
422
- ? optionDataset[0]
423
- : optionDataset;
424
- if (currentSeries.datasetId && Array.isArray(optionDataset)) {
425
- currentDataset = optionDataset.find((ds) => ds.id === currentSeries.datasetId);
426
- }
427
- else if (currentSeries.datasetIndex !== undefined) {
428
- currentDataset = Array.isArray(optionDataset)
429
- ? optionDataset[currentSeries.datasetIndex]
430
- : optionDataset;
431
- }
432
- const firstDatasetRow = Array.isArray(currentDataset === null || currentDataset === void 0 ? void 0 : currentDataset.source)
433
- ? currentDataset === null || currentDataset === void 0 ? void 0 : currentDataset.source[0]
434
- : undefined;
435
- if (!firstDatasetRow) {
436
- return;
437
- }
438
- if (Array.isArray(firstDatasetRow)) {
439
- const firstDatasetRowIndex = firstDatasetRow.findIndex((item) => item === encodedItemName);
440
- if (firstDatasetRowIndex === -1) {
441
- return;
442
- }
443
- const source = currentDataset === null || currentDataset === void 0 ? void 0 : currentDataset.source;
444
- if (!source) {
445
- return;
446
- }
447
- const sourceLength = Array.isArray(source) ? source.length : 0;
448
- const selected = legendSelectedRef.current;
449
- const isAllVisible = Object.values(selected).every((v) => v);
450
- for (let rowIndex = 1; rowIndex < sourceLength; rowIndex++) {
451
- const row = source[rowIndex];
452
- const name = String(row[firstDatasetRowIndex]);
453
- const freshColor = chart === null || chart === void 0 ? void 0 : chart.getVisual({ dataIndexInside: rowIndex - 1, seriesIndex: index }, 'color');
454
- // Only trust getVisual colors when all items are visible.
455
- // When items are deselected, getVisual returns grayed-out
456
- // colors, so we use the cached color from when it was visible.
457
- if (isAllVisible && typeof freshColor === 'string') {
458
- legendColorCacheRef.current.set(name, freshColor);
459
- }
460
- const color = (_c = legendColorCacheRef.current.get(name)) !== null && _c !== void 0 ? _c : (typeof freshColor === 'string' ? freshColor : '#000000');
461
- legendSeries.push({ color, name });
462
- }
463
- }
464
- else {
465
- // TODO: handle dictionary
466
- }
467
- return;
468
- }
469
- else {
470
- const name = Array.isArray(optionSeries)
471
- ? (_d = optionSeries[index]) === null || _d === void 0 ? void 0 : _d.name
472
- : undefined;
473
- if (name === undefined) {
474
- return null;
475
- }
476
- const color = chart === null || chart === void 0 ? void 0 : chart.getVisual({ seriesIndex: index }, 'color');
477
- legendSeries.push({
478
- color: typeof color === 'string' ? color : '#000000',
479
- name: String(name),
480
- });
481
- }
482
- });
483
- }
484
- return legendSeries.filter((currentSeries) => currentSeries !== null);
485
- }, [isWaitingForFirstResize, initialOption]);
486
- if (chartEchartRef.current !== null && !isChartZoomDisabled) {
487
- const chart = (0, echarts_1.getInstanceByDom)(chartEchartRef.current);
488
- if (chart) {
489
- // Needs to be re-set on every re-render.
490
- // Sets the selectable zoom area over the chart (not the slider).
491
- chart.dispatchAction({
492
- dataZoomSelectActive: true,
493
- key: 'dataZoomSelect',
494
- type: 'takeGlobalCursor',
495
- });
496
- }
497
- }
498
- return ((0, jsx_runtime_1.jsxs)("div", { ref: chartRef, className: "ndl-chart", style: style, children: [(0, jsx_runtime_1.jsx)("div", { ref: chartEchartRef }), (legend === null || legend === void 0 ? void 0 : legend.show) && !isWaitingForFirstResize && ((0, jsx_runtime_1.jsx)(Legend_1.Legend, { ref: chartLegendRef, wrappingType: legend.wrappingType, series: legendSeries !== null && legendSeries !== void 0 ? legendSeries : [], chartRef: chartEchartRef, selectedRef: legendSelectedRef }))] }));
499
- }
87
+ }, [legend === null || legend === void 0 ? void 0 : legend.show]);
88
+ const chartLayoutClassName = (legend === null || legend === void 0 ? void 0 : legend.show) && !isLegendLayoutReady
89
+ ? 'ndl-chart-layout ndl-chart-layout-hidden'
90
+ : 'ndl-chart-layout';
91
+ return ((0, jsx_runtime_1.jsx)(use_chart_refs_1.ChartRefsProvider, { children: (0, jsx_runtime_1.jsxs)(ChartContainer_1.ChartContainer, { style: style, children: [(0, jsx_runtime_1.jsx)("div", { className: chartLayoutClassName, children: (0, jsx_runtime_1.jsx)(ChartRender_1.ChartRender, Object.assign({}, chartProps, { onChartOptionChange: setChartOption, onFirstResizeChange: setIsWaitingForFirstResize })) }), (legend === null || legend === void 0 ? void 0 : legend.show) && ((0, jsx_runtime_1.jsx)(ChartLegend, { chartOption: chartOption, isLayoutReady: isLegendLayoutReady, isWaitingForFirstResize: isWaitingForFirstResize, onLayoutReady: handleLegendLayoutReady }))] }) }));
92
+ };
93
+ exports.Chart = Chart;
500
94
  //# sourceMappingURL=Chart.js.map