@keneth80/k-chart 1.6.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +104 -0
  3. package/docs/code-graph-handoff.md +35 -12
  4. package/docs/functional-api.md +97 -1
  5. package/examples/three-constellation-series.ts +375 -0
  6. package/lib/core/contracts.d.ts +452 -0
  7. package/lib/core/contracts.d.ts.map +1 -0
  8. package/lib/core/contracts.js +3 -0
  9. package/lib/core/contracts.js.map +1 -0
  10. package/lib/core/create-kchart.d.ts +4 -0
  11. package/lib/core/create-kchart.d.ts.map +1 -0
  12. package/lib/core/create-kchart.js +1344 -0
  13. package/lib/core/create-kchart.js.map +1 -0
  14. package/lib/core/index.d.ts +3 -0
  15. package/lib/core/index.d.ts.map +1 -0
  16. package/lib/core/index.js +19 -0
  17. package/lib/core/index.js.map +1 -0
  18. package/lib/index.d.ts +5 -1
  19. package/lib/index.d.ts.map +1 -1
  20. package/lib/index.js +5 -1
  21. package/lib/index.js.map +1 -1
  22. package/lib/kchart.d.ts +11 -438
  23. package/lib/kchart.d.ts.map +1 -1
  24. package/lib/kchart.js +24 -3189
  25. package/lib/kchart.js.map +1 -1
  26. package/lib/options/cursor-line.d.ts +4 -0
  27. package/lib/options/cursor-line.d.ts.map +1 -0
  28. package/lib/options/cursor-line.js +36 -0
  29. package/lib/options/cursor-line.js.map +1 -0
  30. package/lib/options/guide-line.d.ts +5 -0
  31. package/lib/options/guide-line.d.ts.map +1 -0
  32. package/lib/options/guide-line.js +129 -0
  33. package/lib/options/guide-line.js.map +1 -0
  34. package/lib/options/index.d.ts +5 -0
  35. package/lib/options/index.d.ts.map +1 -0
  36. package/lib/options/index.js +21 -0
  37. package/lib/options/index.js.map +1 -0
  38. package/lib/options/spec-area.d.ts +5 -0
  39. package/lib/options/spec-area.d.ts.map +1 -0
  40. package/lib/options/spec-area.js +112 -0
  41. package/lib/options/spec-area.js.map +1 -0
  42. package/lib/options/tooltip-note.d.ts +15 -0
  43. package/lib/options/tooltip-note.d.ts.map +1 -0
  44. package/lib/options/tooltip-note.js +265 -0
  45. package/lib/options/tooltip-note.js.map +1 -0
  46. package/lib/series/canvas-candlestick.d.ts +3 -0
  47. package/lib/series/canvas-candlestick.d.ts.map +1 -0
  48. package/lib/series/canvas-candlestick.js +204 -0
  49. package/lib/series/canvas-candlestick.js.map +1 -0
  50. package/lib/series/canvas-line.d.ts +3 -0
  51. package/lib/series/canvas-line.d.ts.map +1 -0
  52. package/lib/series/canvas-line.js +67 -0
  53. package/lib/series/canvas-line.js.map +1 -0
  54. package/lib/series/canvas-point.d.ts +3 -0
  55. package/lib/series/canvas-point.d.ts.map +1 -0
  56. package/lib/series/canvas-point.js +52 -0
  57. package/lib/series/canvas-point.js.map +1 -0
  58. package/lib/series/custom.d.ts +3 -0
  59. package/lib/series/custom.d.ts.map +1 -0
  60. package/lib/series/custom.js +6 -0
  61. package/lib/series/custom.js.map +1 -0
  62. package/lib/series/index.d.ts +9 -0
  63. package/lib/series/index.d.ts.map +1 -0
  64. package/lib/series/index.js +25 -0
  65. package/lib/series/index.js.map +1 -0
  66. package/lib/series/support/canvas.d.ts +19 -0
  67. package/lib/series/support/canvas.d.ts.map +1 -0
  68. package/lib/series/support/canvas.js +130 -0
  69. package/lib/series/support/canvas.js.map +1 -0
  70. package/lib/series/support/scale.d.ts +3 -0
  71. package/lib/series/support/scale.d.ts.map +1 -0
  72. package/lib/series/support/scale.js +14 -0
  73. package/lib/series/support/scale.js.map +1 -0
  74. package/lib/series/support/webgl.d.ts +9 -0
  75. package/lib/series/support/webgl.d.ts.map +1 -0
  76. package/lib/series/support/webgl.js +92 -0
  77. package/lib/series/support/webgl.js.map +1 -0
  78. package/lib/series/svg-globe.d.ts +3 -0
  79. package/lib/series/svg-globe.d.ts.map +1 -0
  80. package/lib/series/svg-globe.js +964 -0
  81. package/lib/series/svg-globe.js.map +1 -0
  82. package/lib/series/svg-line.d.ts +3 -0
  83. package/lib/series/svg-line.d.ts.map +1 -0
  84. package/lib/series/svg-line.js +66 -0
  85. package/lib/series/svg-line.js.map +1 -0
  86. package/lib/series/webgl-line.d.ts +3 -0
  87. package/lib/series/webgl-line.d.ts.map +1 -0
  88. package/lib/series/webgl-line.js +67 -0
  89. package/lib/series/webgl-line.js.map +1 -0
  90. package/lib/series/webgl-point.d.ts +3 -0
  91. package/lib/series/webgl-point.d.ts.map +1 -0
  92. package/lib/series/webgl-point.js +63 -0
  93. package/lib/series/webgl-point.js.map +1 -0
  94. package/lib/utils/downsample-lttb.d.ts +2 -0
  95. package/lib/utils/downsample-lttb.d.ts.map +1 -0
  96. package/lib/utils/downsample-lttb.js +60 -0
  97. package/lib/utils/downsample-lttb.js.map +1 -0
  98. package/lib/utils/index.d.ts +2 -0
  99. package/lib/utils/index.d.ts.map +1 -0
  100. package/lib/utils/index.js +18 -0
  101. package/lib/utils/index.js.map +1 -0
  102. package/lib/worker/index.d.ts +2 -0
  103. package/lib/worker/index.d.ts.map +1 -0
  104. package/lib/worker/index.js +18 -0
  105. package/lib/worker/index.js.map +1 -0
  106. package/lib/worker/render-worker.d.ts +2 -0
  107. package/lib/worker/render-worker.d.ts.map +1 -0
  108. package/lib/worker/render-worker.js +116 -0
  109. package/lib/worker/render-worker.js.map +1 -0
  110. package/package.json +23 -9
@@ -0,0 +1,1344 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
25
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.createKChart = void 0;
29
+ var d3_array_1 = require("d3-array");
30
+ var d3_axis_1 = require("d3-axis");
31
+ var d3_scale_1 = require("d3-scale");
32
+ var d3_scale_chromatic_1 = require("d3-scale-chromatic");
33
+ var d3_selection_1 = require("d3-selection");
34
+ var d3_zoom_1 = require("d3-zoom");
35
+ var cursor_line_1 = require("../options/cursor-line");
36
+ var guide_line_1 = require("../options/guide-line");
37
+ var spec_area_1 = require("../options/spec-area");
38
+ var tooltip_note_1 = require("../options/tooltip-note");
39
+ var canvas_1 = require("../series/support/canvas");
40
+ var scale_1 = require("../series/support/scale");
41
+ var downsample_lttb_1 = require("../utils/downsample-lttb");
42
+ __exportStar(require("./contracts"), exports);
43
+ var defaultMargin = {
44
+ top: 24,
45
+ right: 24,
46
+ bottom: 36,
47
+ left: 44
48
+ };
49
+ var cloneAxisDomain = function (domain) { return domain
50
+ ? domain.map(function (value) { return value instanceof Date ? new Date(value) : value; })
51
+ : undefined; };
52
+ var cloneAxes = function (axes) { return axes.map(function (axis) { return (__assign(__assign({}, axis), { min: axis.min instanceof Date ? new Date(axis.min) : axis.min, max: axis.max instanceof Date ? new Date(axis.max) : axis.max, domain: cloneAxisDomain(axis.domain) })); }); };
53
+ var isZoomScaleSupported = function (axis) { return axis.type === 'number' || axis.type === 'time'; };
54
+ var isHorizontalAxis = function (axis) { return axis.placement === 'bottom' || axis.placement === 'top'; };
55
+ var isZoomEnabled = function (state) {
56
+ var _a;
57
+ return ((_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.enabled) === true
58
+ && state.axes.some(function (axis) { return isZoomScaleSupported(axis); });
59
+ };
60
+ var isTouchLikeZoomEvent = function (event) {
61
+ var _a, _b, _c;
62
+ return ((_b = (_a = event === null || event === void 0 ? void 0 : event.type) === null || _a === void 0 ? void 0 : _a.startsWith) === null || _b === void 0 ? void 0 : _b.call(_a, 'touch')) === true
63
+ || (event === null || event === void 0 ? void 0 : event.pointerType) === 'touch'
64
+ || typeof ((_c = event === null || event === void 0 ? void 0 : event.touches) === null || _c === void 0 ? void 0 : _c.length) === 'number';
65
+ };
66
+ var isMobileLikeInput = function () { return typeof window !== 'undefined'
67
+ && typeof window.matchMedia === 'function'
68
+ && (window.matchMedia('(pointer: coarse)').matches || window.matchMedia('(hover: none)').matches); };
69
+ var matchesZoomInputDevice = function (devices, event) {
70
+ if (devices === 'all') {
71
+ return true;
72
+ }
73
+ var touchLikeEvent = isTouchLikeZoomEvent(event);
74
+ var mobileLikeInput = isMobileLikeInput();
75
+ return devices === 'mobile'
76
+ ? touchLikeEvent && mobileLikeInput
77
+ : !touchLikeEvent && !mobileLikeInput;
78
+ };
79
+ var isZoomOptionEnabled = function (option, fallback) {
80
+ if (typeof option === 'boolean') {
81
+ return option;
82
+ }
83
+ if ((option === null || option === void 0 ? void 0 : option.enabled) !== undefined) {
84
+ return option.enabled;
85
+ }
86
+ return fallback;
87
+ };
88
+ var getZoomWheelSensitivity = function (config) {
89
+ var option = config === null || config === void 0 ? void 0 : config.wheelZoom;
90
+ return typeof option === 'object' && typeof option.sensitivity === 'number' && option.sensitivity > 0
91
+ ? option.sensitivity
92
+ : 1;
93
+ };
94
+ var getZoomWheelDevice = function (config) {
95
+ var option = config === null || config === void 0 ? void 0 : config.wheelZoom;
96
+ return typeof option === 'object' && option.devices ? option.devices : 'pc';
97
+ };
98
+ var getZoomGestureDevice = function (config) {
99
+ var option = config === null || config === void 0 ? void 0 : config.gestureZoom;
100
+ return typeof option === 'object' && option.devices ? option.devices : 'mobile';
101
+ };
102
+ var getZoomGestureMinTouches = function (config) {
103
+ var option = config === null || config === void 0 ? void 0 : config.gestureZoom;
104
+ return typeof option === 'object' && typeof option.minTouches === 'number'
105
+ ? Math.max(1, option.minTouches)
106
+ : 1;
107
+ };
108
+ var hasZoomInputOptions = function (config) { return (config === null || config === void 0 ? void 0 : config.wheelZoom) !== undefined
109
+ || (config === null || config === void 0 ? void 0 : config.gestureZoom) !== undefined; };
110
+ var getEventTouchCount = function (event) {
111
+ var _a, _b, _c;
112
+ if (typeof ((_a = event === null || event === void 0 ? void 0 : event.touches) === null || _a === void 0 ? void 0 : _a.length) === 'number') {
113
+ return event.touches.length;
114
+ }
115
+ if (typeof ((_c = (_b = event === null || event === void 0 ? void 0 : event.sourceEvent) === null || _b === void 0 ? void 0 : _b.touches) === null || _c === void 0 ? void 0 : _c.length) === 'number') {
116
+ return event.sourceEvent.touches.length;
117
+ }
118
+ return 0;
119
+ };
120
+ var hasTitleText = function (config) { var _a, _b; return Boolean((_b = (_a = config.title) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.trim()); };
121
+ var hasTopOptionLabels = function (config) {
122
+ var _a, _b, _c, _d, _e;
123
+ var hasSpecAreaLabels = Boolean((_a = config.specAreas) === null || _a === void 0 ? void 0 : _a.some(function (area) { return area.visible !== false && area.label; }))
124
+ || Boolean((_b = config.options) === null || _b === void 0 ? void 0 : _b.some(function (option) { return option.type === 'spec-area'
125
+ && option.visible !== false
126
+ && option.areas.some(function (area) { return area.visible !== false && area.label; }); }));
127
+ var hasGuideLineLabels = Boolean((_d = (_c = config.guideLines) === null || _c === void 0 ? void 0 : _c.x) === null || _d === void 0 ? void 0 : _d.some(function (line) { return line.visible !== false && line.label; }))
128
+ || Boolean((_e = config.options) === null || _e === void 0 ? void 0 : _e.some(function (option) {
129
+ var _a;
130
+ return option.type === 'guide-line'
131
+ && option.visible !== false
132
+ && ((_a = option.x) === null || _a === void 0 ? void 0 : _a.some(function (line) { return line.visible !== false && line.label; }));
133
+ }));
134
+ return hasSpecAreaLabels || hasGuideLineLabels;
135
+ };
136
+ var resolveEffectiveMargin = function (config, size, baseMargin) {
137
+ var legend = config.legend;
138
+ var hasTopLegend = (legend === null || legend === void 0 ? void 0 : legend.visible) !== false && (legend === null || legend === void 0 ? void 0 : legend.placement) !== 'right' && (legend === null || legend === void 0 ? void 0 : legend.placement) !== 'bottom' && config.series.length > 0;
139
+ var titleVisible = hasTitleText(config);
140
+ var topOptionLabelsVisible = hasTopOptionLabels(config);
141
+ if (!hasTopLegend && titleVisible) {
142
+ return baseMargin;
143
+ }
144
+ var itemWidth = 148;
145
+ var itemHeight = 22;
146
+ var availableLegendWidth = Math.max(itemWidth, size.width - baseMargin.left - baseMargin.right);
147
+ var legendColumns = Math.max(1, Math.floor(availableLegendWidth / itemWidth));
148
+ var legendRows = hasTopLegend ? Math.ceil(config.series.length / legendColumns) : 0;
149
+ var legendY = titleVisible
150
+ ? topOptionLabelsVisible ? 58 : 54
151
+ : 16;
152
+ var legendHeight = legendRows > 0 ? legendRows * itemHeight : 0;
153
+ var optionLabelLift = topOptionLabelsVisible ? 18 : 0;
154
+ var gap = topOptionLabelsVisible ? 12 : 18;
155
+ var requiredTop = Math.ceil(legendY + legendHeight + optionLabelLift + gap);
156
+ if (!titleVisible || topOptionLabelsVisible) {
157
+ return __assign(__assign({}, baseMargin), { top: Math.max(defaultMargin.top, requiredTop) });
158
+ }
159
+ return baseMargin;
160
+ };
161
+ var resolveContainer = function (selector) {
162
+ var container = typeof selector === 'string'
163
+ ? (0, d3_selection_1.select)(selector)
164
+ : (0, d3_selection_1.select)(selector);
165
+ if (!container.node()) {
166
+ throw new Error('KChart container not found.');
167
+ }
168
+ return container;
169
+ };
170
+ var resolveSize = function (container, config, margin) {
171
+ var _a, _b;
172
+ var rect = container.node().getBoundingClientRect();
173
+ var size = {
174
+ width: (_a = config.width) !== null && _a !== void 0 ? _a : Math.max(rect.width, 320),
175
+ height: (_b = config.height) !== null && _b !== void 0 ? _b : Math.max(rect.height, 240)
176
+ };
177
+ var effectiveMargin = resolveEffectiveMargin(config, size, margin);
178
+ return {
179
+ size: size,
180
+ plotSize: {
181
+ width: Math.max(size.width - effectiveMargin.left - effectiveMargin.right, 0),
182
+ height: Math.max(size.height - effectiveMargin.top - effectiveMargin.bottom, 0)
183
+ },
184
+ margin: effectiveMargin
185
+ };
186
+ };
187
+ var createLayers = function (container, config, getSize, getMargin) {
188
+ container
189
+ .classed('kchart', true)
190
+ .style('position', 'relative');
191
+ if (config.className) {
192
+ container.classed(config.className, true);
193
+ }
194
+ var svg = container.selectAll('svg.kchart-svg')
195
+ .data([undefined])
196
+ .join('svg')
197
+ .attr('class', 'kchart-svg')
198
+ .style('position', 'relative')
199
+ .style('z-index', 3)
200
+ .style('pointer-events', 'all');
201
+ var rootGroup = svg.selectAll('g.kchart-root')
202
+ .data([undefined])
203
+ .join('g')
204
+ .attr('class', 'kchart-root');
205
+ var plotGroup = rootGroup.selectAll('g.kchart-plot')
206
+ .data([undefined])
207
+ .join('g')
208
+ .attr('class', 'kchart-plot');
209
+ var seriesGroup = plotGroup.selectAll('g.kchart-series')
210
+ .data([undefined])
211
+ .join('g')
212
+ .attr('class', 'kchart-series');
213
+ var overlayGroup = plotGroup.selectAll('g.kchart-overlay')
214
+ .data([undefined])
215
+ .join('g')
216
+ .attr('class', 'kchart-overlay');
217
+ var getCanvas = function (name) {
218
+ if (name === void 0) { name = 'default'; }
219
+ return ensureCanvas(container, name, getSize(), getMargin(), '2d');
220
+ };
221
+ var getWebglCanvas = function (name) {
222
+ if (name === void 0) { name = 'webgl'; }
223
+ return ensureCanvas(container, name, getSize(), getMargin(), 'webgl');
224
+ };
225
+ return {
226
+ svg: svg,
227
+ rootGroup: rootGroup,
228
+ plotGroup: plotGroup,
229
+ seriesGroup: seriesGroup,
230
+ overlayGroup: overlayGroup,
231
+ getCanvas: getCanvas,
232
+ getWebglCanvas: getWebglCanvas
233
+ };
234
+ };
235
+ var ensureCanvas = function (container, name, size, margin, renderer) {
236
+ var className = "kchart-".concat(renderer, "-canvas-").concat(name);
237
+ var width = Math.max(size.width - margin.left - margin.right, 0);
238
+ var height = Math.max(size.height - margin.top - margin.bottom, 0);
239
+ var canvas = container.selectAll("canvas.".concat(className))
240
+ .data([undefined])
241
+ .join('canvas')
242
+ .attr('class', className)
243
+ .style('position', 'absolute')
244
+ .style('z-index', 1)
245
+ .style('pointer-events', 'none')
246
+ .style('left', "".concat(margin.left, "px"))
247
+ .style('top', "".concat(margin.top, "px"))
248
+ .style('width', "".concat(width, "px"))
249
+ .style('height', "".concat(height, "px"))
250
+ .attr('data-kchart-width', String(width))
251
+ .attr('data-kchart-height', String(height))
252
+ .node();
253
+ if (!(0, canvas_1.isCanvasTransferred)(canvas)) {
254
+ canvas.width = width;
255
+ canvas.height = height;
256
+ }
257
+ return canvas;
258
+ };
259
+ var resolveAxisDomain = function (axis, data) {
260
+ var _a, _b, _c, _d, _e, _f, _g;
261
+ if (axis.domain) {
262
+ return axis.domain;
263
+ }
264
+ if (axis.type === 'string' || axis.type === 'point') {
265
+ return data.map(function (item) { return item[axis.field]; });
266
+ }
267
+ var domainFields = ((_a = axis.domainFields) === null || _a === void 0 ? void 0 : _a.length) ? axis.domainFields : [axis.field];
268
+ var values = data.flatMap(function (item) { return domainFields.map(function (field) {
269
+ var value = item[field];
270
+ return axis.type === 'time'
271
+ ? new Date(value)
272
+ : Number(value);
273
+ }); }).filter(function (value) { return axis.type === 'time'
274
+ ? value instanceof Date && Number.isFinite(value.getTime())
275
+ : Number.isFinite(value); });
276
+ var _h = (0, d3_array_1.extent)(values), minValue = _h[0], maxValue = _h[1];
277
+ var resolvedMin = (_c = (_b = axis.min) !== null && _b !== void 0 ? _b : minValue) !== null && _c !== void 0 ? _c : 0;
278
+ var resolvedMax = (_e = (_d = axis.max) !== null && _d !== void 0 ? _d : maxValue) !== null && _e !== void 0 ? _e : 1;
279
+ var canApplyPadding = axis.padding !== undefined && axis.min === undefined && axis.max === undefined;
280
+ if (canApplyPadding && axis.type === 'time') {
281
+ var minTime = resolvedMin instanceof Date ? resolvedMin.getTime() : new Date(resolvedMin).getTime();
282
+ var maxTime = resolvedMax instanceof Date ? resolvedMax.getTime() : new Date(resolvedMax).getTime();
283
+ var span = Math.max(maxTime - minTime, 24 * 60 * 60 * 1000);
284
+ var padding = span * Math.max((_f = axis.padding) !== null && _f !== void 0 ? _f : 0, 0);
285
+ return [
286
+ new Date(minTime - padding),
287
+ new Date(maxTime + padding)
288
+ ];
289
+ }
290
+ if (canApplyPadding && axis.type === 'number') {
291
+ var minNumber = Number(resolvedMin);
292
+ var maxNumber = Number(resolvedMax);
293
+ var span = Math.max(maxNumber - minNumber, 1);
294
+ var padding = span * Math.max((_g = axis.padding) !== null && _g !== void 0 ? _g : 0, 0);
295
+ return [
296
+ minNumber - padding,
297
+ maxNumber + padding
298
+ ];
299
+ }
300
+ return [
301
+ resolvedMin,
302
+ resolvedMax
303
+ ];
304
+ };
305
+ var resolveScales = function (data, axes, plotSize) { return axes.map(function (axis) {
306
+ var _a, _b;
307
+ var isHorizontal = axis.placement === 'bottom' || axis.placement === 'top';
308
+ var range = isHorizontal ? [0, plotSize.width] : [plotSize.height, 0];
309
+ var domain = resolveAxisDomain(axis, data);
310
+ var scale;
311
+ if (axis.type === 'string') {
312
+ scale = (0, d3_scale_1.scaleBand)()
313
+ .domain(domain.map(String))
314
+ .range(range)
315
+ .padding((_a = axis.padding) !== null && _a !== void 0 ? _a : 0.1);
316
+ }
317
+ else if (axis.type === 'point') {
318
+ scale = (0, d3_scale_1.scalePoint)()
319
+ .domain(domain.map(String))
320
+ .range(range)
321
+ .padding((_b = axis.padding) !== null && _b !== void 0 ? _b : 0.1);
322
+ }
323
+ else if (axis.type === 'time') {
324
+ scale = (0, d3_scale_1.scaleTime)()
325
+ .domain(domain.map(function (value) { return value instanceof Date ? value : new Date(value); }))
326
+ .range(range);
327
+ }
328
+ else {
329
+ scale = (0, d3_scale_1.scaleLinear)()
330
+ .domain(domain.map(Number))
331
+ .range(range)
332
+ .nice();
333
+ }
334
+ return __assign(__assign({}, axis), { scale: scale });
335
+ }); };
336
+ var renderAxes = function (state) {
337
+ var axisLayer = state.layers.rootGroup.selectAll('g.kchart-axes')
338
+ .data([undefined])
339
+ .join('g')
340
+ .attr('class', 'kchart-axes')
341
+ .attr('transform', "translate(".concat(state.margin.left, ", ").concat(state.margin.top, ")"));
342
+ state.scales.forEach(function (scale) {
343
+ var axisGroup = axisLayer.selectAll("g.kchart-axis-".concat(scale.placement))
344
+ .data(scale.visible === false ? [] : [scale])
345
+ .join('g')
346
+ .attr('class', "kchart-axis kchart-axis-".concat(scale.placement));
347
+ var axisFactory = {
348
+ top: d3_axis_1.axisTop,
349
+ right: d3_axis_1.axisRight,
350
+ bottom: d3_axis_1.axisBottom,
351
+ left: d3_axis_1.axisLeft
352
+ }[scale.placement](scale.scale);
353
+ if (scale.tickCount && typeof axisFactory.ticks === 'function') {
354
+ axisFactory.ticks(scale.tickCount);
355
+ }
356
+ if (scale.tickFormat) {
357
+ axisFactory.tickFormat(scale.tickFormat);
358
+ }
359
+ axisGroup
360
+ .attr('transform', resolveAxisTransform(scale.placement, state.plotSize))
361
+ .call(axisFactory);
362
+ renderAxisTitle(axisGroup, scale, state.plotSize);
363
+ });
364
+ };
365
+ var renderAxisTitle = function (axisGroup, scale, plotSize) {
366
+ var _a;
367
+ var title = axisGroup.selectAll('text.kchart-axis-title')
368
+ .data(scale.title ? [scale] : [])
369
+ .join('text')
370
+ .attr('class', 'kchart-axis-title')
371
+ .style('fill', '#9fb1bf')
372
+ .style('font-size', '12px')
373
+ .style('font-weight', 600)
374
+ .style('text-anchor', 'middle')
375
+ .text((_a = scale.title) !== null && _a !== void 0 ? _a : '');
376
+ if (scale.placement === 'bottom') {
377
+ title.attr('x', plotSize.width / 2).attr('y', 34).attr('transform', null);
378
+ }
379
+ else if (scale.placement === 'top') {
380
+ title.attr('x', plotSize.width / 2).attr('y', -24).attr('transform', null);
381
+ }
382
+ else if (scale.placement === 'left') {
383
+ title.attr('x', -plotSize.height / 2).attr('y', -36).attr('transform', 'rotate(-90)');
384
+ }
385
+ else {
386
+ title.attr('x', plotSize.height / 2).attr('y', -36).attr('transform', 'rotate(90)');
387
+ }
388
+ };
389
+ var resolveAxisTransform = function (placement, plotSize) {
390
+ if (placement === 'bottom') {
391
+ return "translate(0, ".concat(plotSize.height, ")");
392
+ }
393
+ if (placement === 'right') {
394
+ return "translate(".concat(plotSize.width, ", 0)");
395
+ }
396
+ return 'translate(0, 0)';
397
+ };
398
+ var resolveDownsampleValue = function (value, scale) {
399
+ if (value instanceof Date) {
400
+ return value.getTime();
401
+ }
402
+ if ((scale === null || scale === void 0 ? void 0 : scale.type) === 'time' && typeof value === 'string') {
403
+ return new Date(value).getTime();
404
+ }
405
+ return Number(value);
406
+ };
407
+ var resolveDownsampleAccessor = function (field, scale) {
408
+ if (!field) {
409
+ return undefined;
410
+ }
411
+ return function (point) { return resolveDownsampleValue(point[field], scale); };
412
+ };
413
+ var resolveDownsampleThreshold = function (downsample, context) {
414
+ var _a;
415
+ if (typeof downsample === 'boolean') {
416
+ return Math.max(3, Math.floor(context.plotSize.width));
417
+ }
418
+ if (typeof downsample.threshold === 'function') {
419
+ return downsample.threshold(context);
420
+ }
421
+ return (_a = downsample.threshold) !== null && _a !== void 0 ? _a : Math.max(3, Math.floor(context.plotSize.width));
422
+ };
423
+ var resolveSeriesRenderData = function (state, series, xScale, yScale) {
424
+ var _a, _b;
425
+ var downsample = series.downsample;
426
+ if (!downsample || (typeof downsample !== 'boolean' && downsample.enabled === false)) {
427
+ return state.data;
428
+ }
429
+ var xAccessor = typeof downsample === 'boolean'
430
+ ? resolveDownsampleAccessor(series.xField, xScale)
431
+ : (_a = downsample.xAccessor) !== null && _a !== void 0 ? _a : resolveDownsampleAccessor(series.xField, xScale);
432
+ var yAccessor = typeof downsample === 'boolean'
433
+ ? resolveDownsampleAccessor(series.yField, yScale)
434
+ : (_b = downsample.yAccessor) !== null && _b !== void 0 ? _b : resolveDownsampleAccessor(series.yField, yScale);
435
+ if (!xAccessor || !yAccessor) {
436
+ return state.data;
437
+ }
438
+ var context = {
439
+ data: state.data,
440
+ plotSize: state.plotSize,
441
+ series: series,
442
+ xField: series.xField,
443
+ yField: series.yField
444
+ };
445
+ var threshold = resolveDownsampleThreshold(downsample, context);
446
+ return (0, downsample_lttb_1.downsampleLTTB)(state.data, threshold, xAccessor, yAccessor);
447
+ };
448
+ var renderTitle = function (state) {
449
+ var _a, _b, _c, _d, _e;
450
+ var title = state.config.title;
451
+ var hasTitle = Boolean((_a = title === null || title === void 0 ? void 0 : title.text) === null || _a === void 0 ? void 0 : _a.trim());
452
+ var titleY = Math.max(Math.min(state.margin.top - 42, 28), 20);
453
+ var titleGroup = state.layers.rootGroup.selectAll('text.kchart-title')
454
+ .data(hasTitle ? [undefined] : [])
455
+ .join('text')
456
+ .attr('class', 'kchart-title')
457
+ .attr('y', titleY)
458
+ .style('fill', (_b = title === null || title === void 0 ? void 0 : title.color) !== null && _b !== void 0 ? _b : '#edf3f8')
459
+ .style('font-size', "".concat((_c = title === null || title === void 0 ? void 0 : title.fontSize) !== null && _c !== void 0 ? _c : 14, "px"))
460
+ .style('font-weight', (_d = title === null || title === void 0 ? void 0 : title.fontWeight) !== null && _d !== void 0 ? _d : 700)
461
+ .text((_e = title === null || title === void 0 ? void 0 : title.text) !== null && _e !== void 0 ? _e : '');
462
+ if (!title || !hasTitle) {
463
+ return;
464
+ }
465
+ if (title.align === 'left') {
466
+ titleGroup.attr('x', state.margin.left).style('text-anchor', 'start');
467
+ }
468
+ else if (title.align === 'right') {
469
+ titleGroup.attr('x', state.size.width - state.margin.right).style('text-anchor', 'end');
470
+ }
471
+ else {
472
+ titleGroup.attr('x', state.size.width / 2).style('text-anchor', 'middle');
473
+ }
474
+ };
475
+ var renderGrid = function (state) {
476
+ var _a;
477
+ var grid = state.config.grid;
478
+ var isVisible = (grid === null || grid === void 0 ? void 0 : grid.visible) !== false;
479
+ var gridGroup = state.layers.plotGroup.selectAll('g.kchart-grid')
480
+ .data(isVisible ? [undefined] : [])
481
+ .join('g')
482
+ .attr('class', 'kchart-grid');
483
+ if (!isVisible) {
484
+ return;
485
+ }
486
+ var xScale = state.scales.find(function (scale) { return scale.placement === 'bottom' || scale.placement === 'top'; });
487
+ var yScale = state.scales.find(function (scale) { return scale.placement === 'left' || scale.placement === 'right'; });
488
+ var color = (_a = grid === null || grid === void 0 ? void 0 : grid.color) !== null && _a !== void 0 ? _a : 'rgba(188, 206, 218, 0.18)';
489
+ var styleLine = function (selection) {
490
+ var _a;
491
+ return selection
492
+ .style('stroke', color)
493
+ .style('stroke-width', 1)
494
+ .style('stroke-dasharray', (_a = grid === null || grid === void 0 ? void 0 : grid.dasharray) !== null && _a !== void 0 ? _a : '2 6')
495
+ .style('shape-rendering', 'crispEdges');
496
+ };
497
+ if ((grid === null || grid === void 0 ? void 0 : grid.x) !== false && xScale && typeof xScale.scale.ticks === 'function') {
498
+ gridGroup.selectAll('line.kchart-grid-x')
499
+ .data(xScale.scale.ticks())
500
+ .join('line')
501
+ .attr('class', 'kchart-grid-x')
502
+ .attr('x1', function (value) { return (0, scale_1.resolveScalePosition)(xScale, value); })
503
+ .attr('x2', function (value) { return (0, scale_1.resolveScalePosition)(xScale, value); })
504
+ .attr('y1', 0)
505
+ .attr('y2', state.plotSize.height)
506
+ .call(styleLine);
507
+ }
508
+ else {
509
+ gridGroup.selectAll('line.kchart-grid-x').remove();
510
+ }
511
+ if ((grid === null || grid === void 0 ? void 0 : grid.y) !== false && yScale && typeof yScale.scale.ticks === 'function') {
512
+ gridGroup.selectAll('line.kchart-grid-y')
513
+ .data(yScale.scale.ticks())
514
+ .join('line')
515
+ .attr('class', 'kchart-grid-y')
516
+ .attr('x1', 0)
517
+ .attr('x2', state.plotSize.width)
518
+ .attr('y1', function (value) { return (0, scale_1.resolveScalePosition)(yScale, value); })
519
+ .attr('y2', function (value) { return (0, scale_1.resolveScalePosition)(yScale, value); })
520
+ .call(styleLine);
521
+ }
522
+ else {
523
+ gridGroup.selectAll('line.kchart-grid-y').remove();
524
+ }
525
+ };
526
+ var renderLegend = function (state) {
527
+ var _a, _b, _c;
528
+ var legend = state.config.legend;
529
+ var isVisible = (legend === null || legend === void 0 ? void 0 : legend.visible) !== false && state.series.length > 0;
530
+ var legendGroup = state.layers.rootGroup.selectAll('g.kchart-legend')
531
+ .data(isVisible ? [undefined] : [])
532
+ .join('g')
533
+ .attr('class', 'kchart-legend');
534
+ if (!isVisible) {
535
+ return;
536
+ }
537
+ var itemGap = (_a = legend === null || legend === void 0 ? void 0 : legend.itemGap) !== null && _a !== void 0 ? _a : 14;
538
+ var itemWidth = 148;
539
+ var itemHeight = 22;
540
+ var availableLegendWidth = Math.max(itemWidth, state.size.width - state.margin.left - state.margin.right);
541
+ var legendColumns = Math.max(1, Math.floor(availableLegendWidth / itemWidth));
542
+ var items = state.series.map(function (series, index) {
543
+ var _a, _b;
544
+ return ({
545
+ selector: series.selector,
546
+ name: (_a = series.displayName) !== null && _a !== void 0 ? _a : series.selector,
547
+ color: (_b = series.color) !== null && _b !== void 0 ? _b : state.colors[index % state.colors.length],
548
+ hidden: state.hiddenSeries.has(series.selector)
549
+ });
550
+ });
551
+ var selectable = (legend === null || legend === void 0 ? void 0 : legend.selectable) !== false;
552
+ var item = legendGroup.selectAll('g.kchart-legend-item')
553
+ .data(items)
554
+ .join('g')
555
+ .attr('class', 'kchart-legend-item')
556
+ .style('cursor', selectable ? 'pointer' : 'default')
557
+ .style('opacity', function (itemData) { return itemData.hidden ? 0.48 : 1; })
558
+ .attr('transform', function (_item, index) {
559
+ if ((legend === null || legend === void 0 ? void 0 : legend.placement) === 'right') {
560
+ return "translate(0, ".concat(index * 20, ")");
561
+ }
562
+ return "translate(".concat((index % legendColumns) * itemWidth, ", ").concat(Math.floor(index / legendColumns) * itemHeight, ")");
563
+ })
564
+ .on('click', function (_event, itemData) {
565
+ if (!selectable) {
566
+ return;
567
+ }
568
+ if (state.hiddenSeries.has(itemData.selector)) {
569
+ state.hiddenSeries.delete(itemData.selector);
570
+ }
571
+ else {
572
+ state.hiddenSeries.add(itemData.selector);
573
+ }
574
+ render(state);
575
+ });
576
+ item.selectAll('rect.kchart-legend-check')
577
+ .data(function (itemData) { return [itemData]; })
578
+ .join('rect')
579
+ .attr('class', 'kchart-legend-check')
580
+ .attr('x', -6)
581
+ .attr('y', -8)
582
+ .attr('width', 14)
583
+ .attr('height', 14)
584
+ .attr('rx', 3)
585
+ .style('fill', function (itemData) { return itemData.hidden ? 'transparent' : itemData.color; })
586
+ .style('stroke', '#f8fbff')
587
+ .style('stroke-width', 1.4);
588
+ item.selectAll('path.kchart-legend-checkmark')
589
+ .data(function (itemData) { return [itemData]; })
590
+ .join('path')
591
+ .attr('class', 'kchart-legend-checkmark')
592
+ .attr('d', 'M -2 -1 L 0 3 L 5 -5')
593
+ .style('fill', 'none')
594
+ .style('stroke', '#ffffff')
595
+ .style('stroke-width', 1.6)
596
+ .style('stroke-linecap', 'round')
597
+ .style('stroke-linejoin', 'round')
598
+ .style('opacity', function (itemData) { return itemData.hidden ? 0 : 1; });
599
+ item.selectAll('text')
600
+ .data(function (itemData) { return [itemData]; })
601
+ .join('text')
602
+ .attr('x', 16)
603
+ .attr('y', 4)
604
+ .style('fill', '#b6c4cf')
605
+ .style('font-size', '12px')
606
+ .style('text-decoration', function (itemData) { return itemData.hidden ? 'line-through' : 'none'; })
607
+ .text(function (itemData) { return itemData.name; });
608
+ if ((legend === null || legend === void 0 ? void 0 : legend.placement) === 'right') {
609
+ legendGroup.attr('transform', "translate(".concat(state.size.width - state.margin.right + itemGap, ", ").concat(state.margin.top, ")"));
610
+ }
611
+ else if ((legend === null || legend === void 0 ? void 0 : legend.placement) === 'bottom') {
612
+ legendGroup.attr('transform', "translate(".concat(state.margin.left, ", ").concat(state.size.height - 10, ")"));
613
+ }
614
+ else {
615
+ var hasSpecAreas = (0, spec_area_1.resolveSpecAreas)(state).length > 0;
616
+ var hasTitle = Boolean((_c = (_b = state.config.title) === null || _b === void 0 ? void 0 : _b.text) === null || _c === void 0 ? void 0 : _c.trim());
617
+ var legendY = hasSpecAreas
618
+ ? (hasTitle ? Math.max(Math.min(state.margin.top - 38, 58), 44) : 20)
619
+ : hasTitle
620
+ ? Math.max(Math.min(state.margin.top - 30, 64), 54)
621
+ : 16;
622
+ legendGroup.attr('transform', "translate(".concat(state.margin.left, ", ").concat(legendY, ")"));
623
+ }
624
+ };
625
+ var getTooltipElement = function (state) { return state.container
626
+ .selectAll('div.kchart-tooltip')
627
+ .data([undefined])
628
+ .join('div')
629
+ .attr('class', 'kchart-tooltip')
630
+ .style('position', 'absolute')
631
+ .style('z-index', 6)
632
+ .style('pointer-events', 'none')
633
+ .style('opacity', 0)
634
+ .style('padding', '8px 10px')
635
+ .style('border', '1px solid rgba(233, 242, 247, 0.18)')
636
+ .style('border-radius', '8px')
637
+ .style('background', 'rgba(10, 14, 20, 0.95)')
638
+ .style('box-shadow', '0 14px 30px rgba(0, 0, 0, 0.32)')
639
+ .style('color', '#edf3f8')
640
+ .style('font-size', '12px')
641
+ .style('line-height', '1.45'); };
642
+ var formatTooltip = function (state, data, series, color) {
643
+ var _a, _b;
644
+ if ((_a = state.config.tooltip) === null || _a === void 0 ? void 0 : _a.formatter) {
645
+ return state.config.tooltip.formatter({
646
+ data: data,
647
+ series: series,
648
+ x: series.xField ? data[series.xField] : undefined,
649
+ y: series.yField ? data[series.yField] : undefined,
650
+ color: color
651
+ });
652
+ }
653
+ var label = (_b = series.displayName) !== null && _b !== void 0 ? _b : series.selector;
654
+ var xValue = series.xField ? data[series.xField] : '';
655
+ var yValue = series.yField ? data[series.yField] : '';
656
+ return "<strong style=\"color:".concat(color, "\">").concat(label, "</strong><br/>x: ").concat(xValue, "<br/>y: ").concat(yValue);
657
+ };
658
+ var resolveZoomedAxes = function (state, transform) {
659
+ var _a, _b;
660
+ var direction = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.direction) !== null && _b !== void 0 ? _b : 'x';
661
+ var baseScales = resolveScales(state.data, state.baseAxes, state.plotSize);
662
+ var xDomain;
663
+ var yDomain;
664
+ var axes = state.baseAxes.map(function (axis) {
665
+ var horizontal = isHorizontalAxis(axis);
666
+ var shouldZoom = isZoomScaleSupported(axis)
667
+ && ((horizontal && (direction === 'x' || direction === 'xy'))
668
+ || (!horizontal && (direction === 'y' || direction === 'xy')));
669
+ if (!shouldZoom) {
670
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
671
+ }
672
+ var scale = baseScales.find(function (item) { return item.field === axis.field && item.placement === axis.placement; });
673
+ if (!scale) {
674
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
675
+ }
676
+ var zoomedScale = horizontal
677
+ ? transform.rescaleX(scale.scale)
678
+ : transform.rescaleY(scale.scale);
679
+ var domain = zoomedScale.domain().map(function (value) { return axis.type === 'time'
680
+ ? value instanceof Date ? value : new Date(value)
681
+ : Number(value); });
682
+ if (horizontal) {
683
+ xDomain = domain;
684
+ }
685
+ else {
686
+ yDomain = domain;
687
+ }
688
+ return __assign(__assign({}, axis), { domain: domain });
689
+ });
690
+ return {
691
+ axes: axes,
692
+ xDomain: xDomain,
693
+ yDomain: yDomain,
694
+ transform: transform
695
+ };
696
+ };
697
+ var resetZoom = function (state) {
698
+ var _a, _b;
699
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
700
+ state.axes = cloneAxes(state.initialAxes);
701
+ state.baseAxes = cloneAxes(state.initialAxes);
702
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
703
+ (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.onZoom) === null || _b === void 0 ? void 0 : _b.call(_a, {
704
+ axes: state.axes,
705
+ transform: state.zoomTransform
706
+ });
707
+ render(state);
708
+ };
709
+ var drawZoomSelection = function (state) {
710
+ var selection = state.zoomSelection;
711
+ var rect = state.layers.overlayGroup.selectAll('rect.kchart-zoom-selection')
712
+ .data((selection === null || selection === void 0 ? void 0 : selection.active) ? [undefined] : [])
713
+ .join('rect')
714
+ .attr('class', 'kchart-zoom-selection')
715
+ .style('fill', 'rgba(93, 184, 255, 0.16)')
716
+ .style('stroke', 'rgba(148, 214, 255, 0.9)')
717
+ .style('stroke-width', 1)
718
+ .style('stroke-dasharray', '4 4')
719
+ .style('pointer-events', 'none');
720
+ if (!(selection === null || selection === void 0 ? void 0 : selection.active)) {
721
+ return;
722
+ }
723
+ rect
724
+ .attr('x', Math.min(selection.startX, selection.currentX))
725
+ .attr('y', Math.min(selection.startY, selection.currentY))
726
+ .attr('width', Math.abs(selection.currentX - selection.startX))
727
+ .attr('height', Math.abs(selection.currentY - selection.startY));
728
+ };
729
+ var resolveSelectionZoomAxes = function (state, selection) {
730
+ var _a, _b;
731
+ var direction = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.direction) !== null && _b !== void 0 ? _b : 'x';
732
+ var minX = Math.max(0, Math.min(selection.startX, selection.currentX));
733
+ var maxX = Math.min(state.plotSize.width, Math.max(selection.startX, selection.currentX));
734
+ var minY = Math.max(0, Math.min(selection.startY, selection.currentY));
735
+ var maxY = Math.min(state.plotSize.height, Math.max(selection.startY, selection.currentY));
736
+ var width = maxX - minX;
737
+ var height = maxY - minY;
738
+ var shouldZoomX = direction === 'x' || direction === 'xy';
739
+ var shouldZoomY = direction === 'y' || direction === 'xy';
740
+ if ((shouldZoomX && width < 8) || (shouldZoomY && height < 8)) {
741
+ return null;
742
+ }
743
+ var xDomain;
744
+ var yDomain;
745
+ var axes = state.axes.map(function (axis) {
746
+ var horizontal = isHorizontalAxis(axis);
747
+ var shouldZoom = isZoomScaleSupported(axis)
748
+ && ((horizontal && shouldZoomX) || (!horizontal && shouldZoomY));
749
+ if (!shouldZoom) {
750
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
751
+ }
752
+ var scale = state.scales.find(function (item) { return item.field === axis.field && item.placement === axis.placement; });
753
+ if (!scale || typeof scale.scale.invert !== 'function') {
754
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
755
+ }
756
+ var rawDomain = horizontal
757
+ ? [scale.scale.invert(minX), scale.scale.invert(maxX)]
758
+ : [scale.scale.invert(maxY), scale.scale.invert(minY)];
759
+ var domain = rawDomain.map(function (value) { return axis.type === 'time'
760
+ ? value instanceof Date ? value : new Date(value)
761
+ : Number(value); });
762
+ if (horizontal) {
763
+ xDomain = domain;
764
+ }
765
+ else {
766
+ yDomain = domain;
767
+ }
768
+ return __assign(__assign({}, axis), { domain: domain });
769
+ });
770
+ return {
771
+ axes: axes,
772
+ xDomain: xDomain,
773
+ yDomain: yDomain,
774
+ transform: d3_zoom_1.zoomIdentity
775
+ };
776
+ };
777
+ var applySelectionZoom = function (state) {
778
+ var _a, _b;
779
+ var selection = state.zoomSelection;
780
+ if (!(selection === null || selection === void 0 ? void 0 : selection.active)) {
781
+ return;
782
+ }
783
+ var nextZoom = resolveSelectionZoomAxes(state, selection);
784
+ state.zoomSelection = undefined;
785
+ drawZoomSelection(state);
786
+ if (!nextZoom) {
787
+ return;
788
+ }
789
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
790
+ state.axes = nextZoom.axes;
791
+ state.baseAxes = cloneAxes(nextZoom.axes);
792
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
793
+ (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.onZoom) === null || _b === void 0 ? void 0 : _b.call(_a, nextZoom);
794
+ render(state);
795
+ };
796
+ var renderZoom = function (state) {
797
+ var _a, _b, _c;
798
+ var overlay = state.layers.overlayGroup.select('rect.kchart-tooltip-overlay');
799
+ var overlayNode = overlay.node();
800
+ if (!overlayNode) {
801
+ return;
802
+ }
803
+ var mode = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.mode) !== null && _b !== void 0 ? _b : 'wheel';
804
+ var zoomConfig = state.config.zoom;
805
+ var inputOptionsConfigured = hasZoomInputOptions(zoomConfig);
806
+ var selectionEnabled = isZoomEnabled(state) && (mode === 'select' || mode === 'both');
807
+ var wheelEnabled = isZoomEnabled(state)
808
+ && (mode === 'wheel' || mode === 'both')
809
+ && isZoomOptionEnabled(zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.wheelZoom, true);
810
+ var panEnabled = isZoomEnabled(state) && mode === 'wheel';
811
+ var desktopPanEnabled = panEnabled && isZoomOptionEnabled(zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.wheelZoom, true);
812
+ var gestureEnabled = isZoomEnabled(state)
813
+ && isZoomOptionEnabled(zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.gestureZoom, !inputOptionsConfigured && panEnabled);
814
+ overlay
815
+ .style('cursor', selectionEnabled ? 'crosshair' : isZoomEnabled(state) ? 'grab' : null)
816
+ .on('dblclick.kchart-zoom-reset', null)
817
+ .on('mousedown.kchart-select-zoom', null)
818
+ .on('mousemove.kchart-select-zoom', null)
819
+ .on('mouseup.kchart-select-zoom', null)
820
+ .on('mouseleave.kchart-select-zoom', null);
821
+ if (!isZoomEnabled(state)) {
822
+ overlay.on('.zoom', null);
823
+ state.layers.overlayGroup.selectAll('rect.kchart-zoom-selection').remove();
824
+ return;
825
+ }
826
+ if (wheelEnabled || desktopPanEnabled || gestureEnabled) {
827
+ var wheelSensitivity_1 = getZoomWheelSensitivity(zoomConfig);
828
+ var zoomBehavior = (0, d3_zoom_1.zoom)()
829
+ .scaleExtent((_c = zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.scaleExtent) !== null && _c !== void 0 ? _c : [1, 40])
830
+ .extent([[0, 0], [state.plotSize.width, state.plotSize.height]])
831
+ .translateExtent([[0, 0], [state.plotSize.width, state.plotSize.height]])
832
+ .touchable(function () { return gestureEnabled; })
833
+ .wheelDelta(function (event) { return -event.deltaY
834
+ * (event.deltaMode === 1 ? 0.05 : event.deltaMode ? 1 : 0.002)
835
+ * (event.ctrlKey ? 10 : 1)
836
+ * wheelSensitivity_1; })
837
+ .filter(function (event) {
838
+ if (event.type === 'wheel') {
839
+ return wheelEnabled && (!inputOptionsConfigured
840
+ || matchesZoomInputDevice(getZoomWheelDevice(zoomConfig), event));
841
+ }
842
+ if (event.type === 'mousedown') {
843
+ return desktopPanEnabled && (!inputOptionsConfigured
844
+ || matchesZoomInputDevice(getZoomWheelDevice(zoomConfig), event));
845
+ }
846
+ if (event.type === 'touchstart' || event.type === 'touchmove') {
847
+ var touchCount = getEventTouchCount(event);
848
+ return gestureEnabled
849
+ && touchCount >= getZoomGestureMinTouches(zoomConfig)
850
+ && (!inputOptionsConfigured
851
+ || matchesZoomInputDevice(getZoomGestureDevice(zoomConfig), event));
852
+ }
853
+ return false;
854
+ })
855
+ .on('zoom', function (event) {
856
+ var _a;
857
+ state.zoomTransform = event.transform;
858
+ var nextZoom = resolveZoomedAxes(state, event.transform);
859
+ state.axes = nextZoom.axes;
860
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
861
+ (_a = zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.onZoom) === null || _a === void 0 ? void 0 : _a.call(zoomConfig, nextZoom);
862
+ render(state);
863
+ });
864
+ overlay.call(zoomBehavior);
865
+ overlayNode.__zoom = state.zoomTransform;
866
+ }
867
+ else {
868
+ overlay.on('.zoom', null);
869
+ }
870
+ if (selectionEnabled) {
871
+ overlay
872
+ .on('mousedown.kchart-select-zoom', function (event) {
873
+ if (event.button !== 0) {
874
+ return;
875
+ }
876
+ event.preventDefault();
877
+ event.stopPropagation();
878
+ var _a = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _a[0], mouseY = _a[1];
879
+ state.zoomSelection = {
880
+ active: true,
881
+ startX: mouseX,
882
+ startY: mouseY,
883
+ currentX: mouseX,
884
+ currentY: mouseY
885
+ };
886
+ drawZoomSelection(state);
887
+ })
888
+ .on('mousemove.kchart-select-zoom', function (event) {
889
+ var _a;
890
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
891
+ return;
892
+ }
893
+ var _b = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _b[0], mouseY = _b[1];
894
+ state.zoomSelection.currentX = mouseX;
895
+ state.zoomSelection.currentY = mouseY;
896
+ drawZoomSelection(state);
897
+ })
898
+ .on('mouseup.kchart-select-zoom', function (event) {
899
+ var _a;
900
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
901
+ return;
902
+ }
903
+ var _b = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _b[0], mouseY = _b[1];
904
+ state.zoomSelection.currentX = mouseX;
905
+ state.zoomSelection.currentY = mouseY;
906
+ applySelectionZoom(state);
907
+ })
908
+ .on('mouseleave.kchart-select-zoom', function () {
909
+ var _a;
910
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
911
+ return;
912
+ }
913
+ state.zoomSelection = undefined;
914
+ drawZoomSelection(state);
915
+ });
916
+ }
917
+ if ((zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.resetOnDoubleClick) !== false) {
918
+ overlay.on('dblclick.kchart-zoom-reset', function () { return resetZoom(state); });
919
+ }
920
+ };
921
+ var renderTooltip = function (state) {
922
+ var _a, _b;
923
+ var tooltipEnabled = ((_a = state.config.tooltip) === null || _a === void 0 ? void 0 : _a.visible) === true;
924
+ var tooltipNoteConfiguration = (0, tooltip_note_1.resolveTooltipNoteConfiguration)(state);
925
+ var cursorGuide = (0, cursor_line_1.resolveCursorGuide)(state);
926
+ var guideEnabled = (cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.visible) === true;
927
+ var enabled = tooltipEnabled || guideEnabled || isZoomEnabled(state);
928
+ var overlay = state.layers.overlayGroup.selectAll('rect.kchart-tooltip-overlay')
929
+ .data(enabled ? [undefined] : [])
930
+ .join('rect')
931
+ .attr('class', 'kchart-tooltip-overlay')
932
+ .attr('width', state.plotSize.width)
933
+ .attr('height', state.plotSize.height)
934
+ .style('fill', 'transparent')
935
+ .style('pointer-events', 'all');
936
+ var guideLine = state.layers.overlayGroup.selectAll('line.kchart-guide-line')
937
+ .data(guideEnabled ? [undefined] : [])
938
+ .join('line')
939
+ .attr('class', 'kchart-guide-line')
940
+ .attr('y1', 0)
941
+ .attr('y2', state.plotSize.height)
942
+ .style('stroke', (_b = cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.color) !== null && _b !== void 0 ? _b : 'rgba(248, 251, 255, 0.72)')
943
+ .style('stroke-width', 1)
944
+ .style('stroke-dasharray', '3 5')
945
+ .style('opacity', 0)
946
+ .style('pointer-events', 'none');
947
+ var guideMarkerGroup = state.layers.overlayGroup.selectAll('g.kchart-guide-markers')
948
+ .data(guideEnabled ? [undefined] : [])
949
+ .join('g')
950
+ .attr('class', 'kchart-guide-markers')
951
+ .style('opacity', 0)
952
+ .style('pointer-events', 'none');
953
+ var guideAxisLabel = state.layers.overlayGroup.selectAll('g.kchart-guide-axis-label')
954
+ .data(guideEnabled ? [undefined] : [])
955
+ .join('g')
956
+ .attr('class', 'kchart-guide-axis-label')
957
+ .style('opacity', 0)
958
+ .style('pointer-events', 'none');
959
+ var tooltip = getTooltipElement(state);
960
+ (0, tooltip_note_1.renderTooltipNotes)(state);
961
+ tooltip
962
+ .style('pointer-events', (tooltipNoteConfiguration === null || tooltipNoteConfiguration === void 0 ? void 0 : tooltipNoteConfiguration.enabled) ? 'auto' : 'none');
963
+ if (!enabled) {
964
+ tooltip.style('opacity', 0);
965
+ return;
966
+ }
967
+ var activeTooltipCandidate;
968
+ var hideHoverTooltip = function () {
969
+ activeTooltipCandidate = undefined;
970
+ tooltip.style('opacity', 0);
971
+ };
972
+ overlay
973
+ .on('mousemove', function (event) {
974
+ var _a, _b, _c;
975
+ var target = overlay.node();
976
+ if (!target) {
977
+ return;
978
+ }
979
+ var _d = (0, d3_selection_1.pointer)(event, target), mouseX = _d[0], mouseY = _d[1];
980
+ var nearest = null;
981
+ var guideMarkers = [];
982
+ state.series.forEach(function (series, index) {
983
+ var _a, _b, _c, _d, _e;
984
+ if (state.hiddenSeries.has(series.selector)) {
985
+ return;
986
+ }
987
+ var color = (_a = series.color) !== null && _a !== void 0 ? _a : state.colors[index % state.colors.length];
988
+ var customTooltip = (_b = series.tooltip) === null || _b === void 0 ? void 0 : _b.call(series, __assign(__assign({}, state.layers), { data: state.data, scales: state.scales, size: state.size, plotSize: state.plotSize, margin: state.margin, color: color, seriesIndex: index, mouseX: mouseX, mouseY: mouseY }));
989
+ if (customTooltip) {
990
+ var distance = (_c = customTooltip.distance) !== null && _c !== void 0 ? _c : Math.hypot(mouseX - customTooltip.x, mouseY - customTooltip.y);
991
+ if (!nearest || distance < nearest.distance) {
992
+ nearest = {
993
+ data: customTooltip.data,
994
+ series: series,
995
+ color: (_d = customTooltip.color) !== null && _d !== void 0 ? _d : color,
996
+ distance: distance,
997
+ x: customTooltip.x,
998
+ y: customTooltip.y,
999
+ html: customTooltip.html
1000
+ };
1001
+ }
1002
+ }
1003
+ if (series.tooltip) {
1004
+ return;
1005
+ }
1006
+ if (!series.xField || !series.yField) {
1007
+ return;
1008
+ }
1009
+ var xScale = state.scales.find(function (scale) { return scale.field === series.xField; });
1010
+ var yScale = (_e = state.scales.find(function (scale) { return scale.field === series.yField; })) !== null && _e !== void 0 ? _e : state.scales.find(function (scale) { return scale.placement === 'left' || scale.placement === 'right'; });
1011
+ if (!xScale || !yScale) {
1012
+ return;
1013
+ }
1014
+ state.data.forEach(function (datum) {
1015
+ var _a;
1016
+ var x = (0, scale_1.resolveScalePosition)(xScale, datum[series.xField]);
1017
+ var y = (0, scale_1.resolveScalePosition)(yScale, datum[series.yField]);
1018
+ var distance = Math.hypot(mouseX - x, mouseY - y);
1019
+ var guideDistance = Math.abs(mouseX - x);
1020
+ if (!nearest || distance < nearest.distance) {
1021
+ nearest = {
1022
+ data: datum,
1023
+ series: series,
1024
+ color: color,
1025
+ distance: distance,
1026
+ x: x,
1027
+ y: y
1028
+ };
1029
+ }
1030
+ if (guideEnabled) {
1031
+ var label_1 = (_a = series.displayName) !== null && _a !== void 0 ? _a : series.selector;
1032
+ var currentMarker = guideMarkers.find(function (marker) { return marker.label === label_1; });
1033
+ if (!currentMarker || guideDistance < Math.abs(mouseX - currentMarker.x)) {
1034
+ if (currentMarker) {
1035
+ currentMarker.x = x;
1036
+ currentMarker.y = y;
1037
+ currentMarker.value = datum[series.yField];
1038
+ currentMarker.xValue = datum[series.xField];
1039
+ }
1040
+ else {
1041
+ guideMarkers.push({
1042
+ x: x,
1043
+ y: y,
1044
+ color: color,
1045
+ label: label_1,
1046
+ value: datum[series.yField],
1047
+ xValue: datum[series.xField]
1048
+ });
1049
+ }
1050
+ }
1051
+ }
1052
+ });
1053
+ });
1054
+ if (guideEnabled && guideMarkers.length) {
1055
+ var guideX = guideMarkers[0].x;
1056
+ var arrangedGuideMarkers = guideMarkers
1057
+ .slice()
1058
+ .sort(function (a, b) { return a.y - b.y; })
1059
+ .map(function (item, index, sorted) {
1060
+ var previous = index > 0 ? sorted[index - 1] : undefined;
1061
+ var labelY = Math.max(item.y, (previous === null || previous === void 0 ? void 0 : previous.labelY) !== undefined ? previous.labelY + 20 : item.y);
1062
+ return __assign(__assign({}, item), { labelY: Math.min(labelY, state.plotSize.height - 12) });
1063
+ });
1064
+ guideLine
1065
+ .attr('x1', guideX)
1066
+ .attr('x2', guideX)
1067
+ .style('opacity', 1);
1068
+ var xFormat = cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.xFormat;
1069
+ var xLabel = xFormat
1070
+ ? xFormat(guideMarkers[0].xValue)
1071
+ : String(guideMarkers[0].xValue);
1072
+ var xLabelWidth = Math.max(44, xLabel.length * 7 + 16);
1073
+ guideAxisLabel
1074
+ .attr('transform', "translate(".concat(guideX, ", ").concat(state.plotSize.height + 18, ")"))
1075
+ .style('opacity', 1);
1076
+ guideAxisLabel.selectAll('rect')
1077
+ .data([xLabel])
1078
+ .join('rect')
1079
+ .attr('x', -xLabelWidth / 2)
1080
+ .attr('y', -12)
1081
+ .attr('width', xLabelWidth)
1082
+ .attr('height', 18)
1083
+ .attr('rx', 5)
1084
+ .style('fill', 'rgba(10, 14, 20, 0.9)')
1085
+ .style('stroke', 'rgba(248, 251, 255, 0.24)')
1086
+ .style('stroke-width', 1);
1087
+ guideAxisLabel.selectAll('text')
1088
+ .data([xLabel])
1089
+ .join('text')
1090
+ .attr('x', 0)
1091
+ .attr('y', 1)
1092
+ .style('fill', '#edf3f8')
1093
+ .style('font-size', '11px')
1094
+ .style('font-weight', 700)
1095
+ .style('text-anchor', 'middle')
1096
+ .text(function (item) { return item; });
1097
+ var marker = guideMarkerGroup.selectAll('g.kchart-guide-marker')
1098
+ .data(arrangedGuideMarkers)
1099
+ .join('g')
1100
+ .attr('class', 'kchart-guide-marker')
1101
+ .attr('transform', function (item) { return "translate(".concat(item.x, ", ").concat(item.y, ")"); });
1102
+ marker.selectAll('circle')
1103
+ .data(function (item) { return [item]; })
1104
+ .join('circle')
1105
+ .attr('r', (_a = cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.markerRadius) !== null && _a !== void 0 ? _a : 4)
1106
+ .style('fill', function (item) { return item.color; })
1107
+ .style('stroke', '#ffffff')
1108
+ .style('stroke-width', 1.5);
1109
+ marker.selectAll('rect')
1110
+ .data(function (item) { return [item]; })
1111
+ .join('rect')
1112
+ .attr('x', 8)
1113
+ .attr('y', function (item) { return item.labelY - item.y - 10; })
1114
+ .attr('width', function (item) {
1115
+ var valueFormat = cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.valueFormat;
1116
+ var value = valueFormat ? valueFormat(item.value) : Number(item.value).toFixed(1);
1117
+ return Math.max(54, "".concat(item.label, ": ").concat(value).length * 6.3 + 12);
1118
+ })
1119
+ .attr('height', 18)
1120
+ .attr('rx', 5)
1121
+ .style('fill', 'rgba(10, 14, 20, 0.86)')
1122
+ .style('stroke', function (item) { return item.color; })
1123
+ .style('stroke-width', 1);
1124
+ marker.selectAll('text')
1125
+ .data(function (item) { return [item]; })
1126
+ .join('text')
1127
+ .attr('x', 14)
1128
+ .attr('y', function (item) { return item.labelY - item.y + 4; })
1129
+ .style('fill', '#edf3f8')
1130
+ .style('font-size', '11px')
1131
+ .style('font-weight', 700)
1132
+ .text(function (item) {
1133
+ var valueFormat = cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.valueFormat;
1134
+ var value = valueFormat ? valueFormat(item.value) : Number(item.value).toFixed(1);
1135
+ return "".concat(item.label, ": ").concat(value);
1136
+ });
1137
+ guideMarkerGroup.style('opacity', 1);
1138
+ }
1139
+ else {
1140
+ guideLine.style('opacity', 0);
1141
+ guideMarkerGroup.style('opacity', 0);
1142
+ guideAxisLabel.style('opacity', 0);
1143
+ }
1144
+ if (!tooltipEnabled) {
1145
+ hideHoverTooltip();
1146
+ return;
1147
+ }
1148
+ if (!nearest || nearest.distance > 36) {
1149
+ hideHoverTooltip();
1150
+ return;
1151
+ }
1152
+ var tooltipHtml = (_b = nearest.html) !== null && _b !== void 0 ? _b : formatTooltip(state, nearest.data, nearest.series, nearest.color);
1153
+ activeTooltipCandidate = {
1154
+ data: nearest.data,
1155
+ series: nearest.series,
1156
+ color: nearest.color,
1157
+ x: nearest.x,
1158
+ y: nearest.y,
1159
+ html: tooltipHtml
1160
+ };
1161
+ var tooltipContent = tooltip
1162
+ .selectAll('div.kchart-tooltip-content')
1163
+ .data([tooltipHtml])
1164
+ .join('div')
1165
+ .attr('class', 'kchart-tooltip-content')
1166
+ .html(function (html) { return html; });
1167
+ tooltipContent.style('padding-right', (tooltipNoteConfiguration === null || tooltipNoteConfiguration === void 0 ? void 0 : tooltipNoteConfiguration.enabled) ? '36px' : null);
1168
+ var pinButton = tooltip
1169
+ .selectAll('button.kchart-tooltip-pin')
1170
+ .data((tooltipNoteConfiguration === null || tooltipNoteConfiguration === void 0 ? void 0 : tooltipNoteConfiguration.enabled) ? [undefined] : [])
1171
+ .join('button')
1172
+ .attr('class', 'kchart-tooltip-pin')
1173
+ .attr('type', 'button')
1174
+ .attr('aria-label', 'Pin tooltip as note')
1175
+ .attr('title', 'Pin tooltip as note')
1176
+ .text((_c = tooltipNoteConfiguration === null || tooltipNoteConfiguration === void 0 ? void 0 : tooltipNoteConfiguration.pinButtonLabel) !== null && _c !== void 0 ? _c : 'Pin')
1177
+ .style('position', 'absolute')
1178
+ .style('top', '6px')
1179
+ .style('right', '6px')
1180
+ .style('height', '24px')
1181
+ .style('padding', '0 7px')
1182
+ .style('border', '1px solid rgba(248, 251, 255, 0.28)')
1183
+ .style('border-radius', '5px')
1184
+ .style('background', 'rgba(255, 255, 255, 0.08)')
1185
+ .style('color', '#f8fbff')
1186
+ .style('font-size', '10px')
1187
+ .style('font-weight', 700)
1188
+ .style('cursor', 'pointer');
1189
+ pinButton.on('click', function (event) {
1190
+ event.preventDefault();
1191
+ event.stopPropagation();
1192
+ if (activeTooltipCandidate) {
1193
+ (0, tooltip_note_1.pinTooltipNote)(state, activeTooltipCandidate);
1194
+ hideHoverTooltip();
1195
+ }
1196
+ });
1197
+ tooltip
1198
+ .style('left', "".concat(state.margin.left + nearest.x + 12, "px"))
1199
+ .style('top', "".concat(state.margin.top + nearest.y + 12, "px"))
1200
+ .style('opacity', 1);
1201
+ })
1202
+ .on('mouseleave', function (event) {
1203
+ var tooltipNode = tooltip.node();
1204
+ if (event.relatedTarget instanceof Node &&
1205
+ (tooltipNode === null || tooltipNode === void 0 ? void 0 : tooltipNode.contains(event.relatedTarget))) {
1206
+ return;
1207
+ }
1208
+ state.series.forEach(function (series) {
1209
+ var _a;
1210
+ if (!state.hiddenSeries.has(series.selector)) {
1211
+ (_a = series.clearTooltip) === null || _a === void 0 ? void 0 : _a.call(series, state.layers);
1212
+ }
1213
+ });
1214
+ hideHoverTooltip();
1215
+ guideLine.style('opacity', 0);
1216
+ guideMarkerGroup.style('opacity', 0);
1217
+ guideAxisLabel.style('opacity', 0);
1218
+ });
1219
+ tooltip.on('mouseleave.kchart-tooltip-note', function (event) {
1220
+ var overlayNode = overlay.node();
1221
+ if (event.relatedTarget instanceof Node &&
1222
+ (overlayNode === null || overlayNode === void 0 ? void 0 : overlayNode.contains(event.relatedTarget))) {
1223
+ return;
1224
+ }
1225
+ hideHoverTooltip();
1226
+ });
1227
+ };
1228
+ var renderSeries = function (state) {
1229
+ state.layers.svg
1230
+ .attr('width', state.size.width)
1231
+ .attr('height', state.size.height);
1232
+ state.layers.plotGroup.attr('transform', "translate(".concat(state.margin.left, ", ").concat(state.margin.top, ")"));
1233
+ state.layers.seriesGroup.selectAll('*').remove();
1234
+ state.series.forEach(function (series, index) {
1235
+ var _a, _b, _c;
1236
+ if (state.hiddenSeries.has(series.selector)) {
1237
+ (_a = series.destroy) === null || _a === void 0 ? void 0 : _a.call(series, state.layers);
1238
+ return;
1239
+ }
1240
+ var xScale = series.xField
1241
+ ? state.scales.find(function (scale) { return scale.field === series.xField; })
1242
+ : state.scales.find(function (scale) { return scale.placement === 'bottom' || scale.placement === 'top'; });
1243
+ var yScale = series.yField
1244
+ ? (_b = state.scales.find(function (scale) { return scale.field === series.yField; })) !== null && _b !== void 0 ? _b : state.scales.find(function (scale) { return scale.placement === 'left' || scale.placement === 'right'; })
1245
+ : state.scales.find(function (scale) { return scale.placement === 'left' || scale.placement === 'right'; });
1246
+ var seriesGroup = state.layers.seriesGroup.selectAll("g.".concat(series.selector, "-group"))
1247
+ .data([series])
1248
+ .join('g')
1249
+ .attr('class', "".concat(series.selector, "-group"));
1250
+ var renderData = resolveSeriesRenderData(state, series, xScale, yScale);
1251
+ series.render(__assign(__assign({}, state.layers), { group: seriesGroup, seriesGroup: seriesGroup, data: renderData, scales: state.scales, xScale: xScale, yScale: yScale, size: state.size, plotSize: state.plotSize, margin: state.margin, color: (_c = series.color) !== null && _c !== void 0 ? _c : state.colors[index % state.colors.length], seriesIndex: index }));
1252
+ });
1253
+ };
1254
+ var render = function (state) {
1255
+ var baseMargin = __assign(__assign({}, defaultMargin), state.config.margin);
1256
+ var sizes = resolveSize(state.container, state.config, baseMargin);
1257
+ state.size = sizes.size;
1258
+ state.margin = sizes.margin;
1259
+ state.plotSize = sizes.plotSize;
1260
+ state.scales = resolveScales(state.data, state.axes, state.plotSize);
1261
+ renderTitle(state);
1262
+ renderAxes(state);
1263
+ renderGrid(state);
1264
+ (0, spec_area_1.renderSpecAreas)(state);
1265
+ (0, guide_line_1.renderGuideLines)(state);
1266
+ renderSeries(state);
1267
+ renderLegend(state);
1268
+ renderTooltip(state);
1269
+ renderZoom(state);
1270
+ };
1271
+ var createKChart = function (config) {
1272
+ var _a;
1273
+ var container = resolveContainer(config.selector);
1274
+ var baseMargin = __assign(__assign({}, defaultMargin), config.margin);
1275
+ var sizes = resolveSize(container, config, baseMargin);
1276
+ var state = {
1277
+ config: config,
1278
+ container: container,
1279
+ data: config.data,
1280
+ axes: cloneAxes(config.axes),
1281
+ initialAxes: cloneAxes(config.axes),
1282
+ baseAxes: cloneAxes(config.axes),
1283
+ series: config.series,
1284
+ size: sizes.size,
1285
+ plotSize: sizes.plotSize,
1286
+ margin: sizes.margin,
1287
+ colors: (_a = config.colors) !== null && _a !== void 0 ? _a : d3_scale_chromatic_1.schemeCategory10.slice(),
1288
+ scales: [],
1289
+ layers: undefined,
1290
+ hiddenSeries: new Set(),
1291
+ zoomTransform: d3_zoom_1.zoomIdentity
1292
+ };
1293
+ state.layers = createLayers(container, config, function () { return state.size; }, function () { return state.margin; });
1294
+ var controller = {
1295
+ render: function () {
1296
+ render(state);
1297
+ return controller;
1298
+ },
1299
+ updateData: function (data) {
1300
+ state.data = data;
1301
+ state.config = __assign(__assign({}, state.config), { data: data });
1302
+ return controller.render();
1303
+ },
1304
+ updateAxes: function (axes) {
1305
+ state.axes = cloneAxes(axes);
1306
+ state.initialAxes = cloneAxes(axes);
1307
+ state.baseAxes = cloneAxes(axes);
1308
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
1309
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1310
+ return controller.render();
1311
+ },
1312
+ updateSeries: function (series) {
1313
+ state.series = series;
1314
+ return controller.render();
1315
+ },
1316
+ resize: function (size) {
1317
+ if (size === void 0) { size = {}; }
1318
+ var nextConfig = __assign(__assign({}, state.config), size);
1319
+ var nextBaseMargin = __assign(__assign({}, defaultMargin), nextConfig.margin);
1320
+ var nextSizes = resolveSize(state.container, nextConfig, nextBaseMargin);
1321
+ state.config = nextConfig;
1322
+ state.margin = nextSizes.margin;
1323
+ state.size = nextSizes.size;
1324
+ state.plotSize = nextSizes.plotSize;
1325
+ return controller.render();
1326
+ },
1327
+ destroy: function () {
1328
+ state.series.forEach(function (series) {
1329
+ if (series.destroy) {
1330
+ series.destroy(state.layers);
1331
+ }
1332
+ });
1333
+ state.container.selectAll('.kchart-svg').remove();
1334
+ state.container.selectAll('canvas[class^="kchart-"]').remove();
1335
+ (0, tooltip_note_1.destroyTooltipNotes)(state);
1336
+ },
1337
+ getState: function () {
1338
+ return state;
1339
+ }
1340
+ };
1341
+ return controller;
1342
+ };
1343
+ exports.createKChart = createKChart;
1344
+ //# sourceMappingURL=create-kchart.js.map