@keneth80/k-chart 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/kchart.js CHANGED
@@ -20,13 +20,14 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
20
20
  return to.concat(ar || Array.prototype.slice.call(from));
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.createKChart = exports.createWebglLineSeries = exports.createWebglPointSeries = exports.createCanvasPointSeries = exports.createCanvasLineSeries = exports.createLineSeries = exports.createCursorLineOption = exports.createGuideLineOption = exports.createSpecAreaOption = exports.createCustomSeries = exports.startKChartRenderWorker = exports.downsampleLTTB = void 0;
23
+ exports.createKChart = exports.createWebglLineSeries = exports.createWebglPointSeries = exports.createCanvasCandlestickSeries = exports.createCanvasPointSeries = exports.createCanvasLineSeries = exports.createLineSeries = exports.createCursorLineOption = exports.createGuideLineOption = exports.createSpecAreaOption = exports.createCustomSeries = exports.startKChartRenderWorker = exports.downsampleLTTB = void 0;
24
24
  var d3_array_1 = require("d3-array");
25
25
  var d3_axis_1 = require("d3-axis");
26
26
  var d3_scale_1 = require("d3-scale");
27
27
  var d3_scale_chromatic_1 = require("d3-scale-chromatic");
28
28
  var d3_selection_1 = require("d3-selection");
29
29
  var d3_shape_1 = require("d3-shape");
30
+ var d3_zoom_1 = require("d3-zoom");
30
31
  var defaultMargin = {
31
32
  top: 24,
32
33
  right: 24,
@@ -36,6 +37,77 @@ var defaultMargin = {
36
37
  var transferredCanvases = new WeakSet();
37
38
  var asyncCanvasEntries = new WeakMap();
38
39
  var asyncCanvasId = 0;
40
+ var cloneAxisDomain = function (domain) { return domain
41
+ ? domain.map(function (value) { return value instanceof Date ? new Date(value) : value; })
42
+ : undefined; };
43
+ 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) })); }); };
44
+ var isZoomScaleSupported = function (axis) { return axis.type === 'number' || axis.type === 'time'; };
45
+ var isHorizontalAxis = function (axis) { return axis.placement === 'bottom' || axis.placement === 'top'; };
46
+ var isZoomEnabled = function (state) {
47
+ var _a;
48
+ return ((_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.enabled) === true
49
+ && state.axes.some(function (axis) { return isZoomScaleSupported(axis); });
50
+ };
51
+ var isTouchLikeZoomEvent = function (event) {
52
+ var _a, _b, _c;
53
+ 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
54
+ || (event === null || event === void 0 ? void 0 : event.pointerType) === 'touch'
55
+ || typeof ((_c = event === null || event === void 0 ? void 0 : event.touches) === null || _c === void 0 ? void 0 : _c.length) === 'number';
56
+ };
57
+ var isMobileLikeInput = function () { return typeof window !== 'undefined'
58
+ && typeof window.matchMedia === 'function'
59
+ && (window.matchMedia('(pointer: coarse)').matches || window.matchMedia('(hover: none)').matches); };
60
+ var matchesZoomInputDevice = function (devices, event) {
61
+ if (devices === 'all') {
62
+ return true;
63
+ }
64
+ var touchLikeEvent = isTouchLikeZoomEvent(event);
65
+ var mobileLikeInput = isMobileLikeInput();
66
+ return devices === 'mobile'
67
+ ? touchLikeEvent && mobileLikeInput
68
+ : !touchLikeEvent && !mobileLikeInput;
69
+ };
70
+ var isZoomOptionEnabled = function (option, fallback) {
71
+ if (typeof option === 'boolean') {
72
+ return option;
73
+ }
74
+ if ((option === null || option === void 0 ? void 0 : option.enabled) !== undefined) {
75
+ return option.enabled;
76
+ }
77
+ return fallback;
78
+ };
79
+ var getZoomWheelSensitivity = function (config) {
80
+ var option = config === null || config === void 0 ? void 0 : config.wheelZoom;
81
+ return typeof option === 'object' && typeof option.sensitivity === 'number' && option.sensitivity > 0
82
+ ? option.sensitivity
83
+ : 1;
84
+ };
85
+ var getZoomWheelDevice = function (config) {
86
+ var option = config === null || config === void 0 ? void 0 : config.wheelZoom;
87
+ return typeof option === 'object' && option.devices ? option.devices : 'pc';
88
+ };
89
+ var getZoomGestureDevice = function (config) {
90
+ var option = config === null || config === void 0 ? void 0 : config.gestureZoom;
91
+ return typeof option === 'object' && option.devices ? option.devices : 'mobile';
92
+ };
93
+ var getZoomGestureMinTouches = function (config) {
94
+ var option = config === null || config === void 0 ? void 0 : config.gestureZoom;
95
+ return typeof option === 'object' && typeof option.minTouches === 'number'
96
+ ? Math.max(1, option.minTouches)
97
+ : 1;
98
+ };
99
+ var hasZoomInputOptions = function (config) { return (config === null || config === void 0 ? void 0 : config.wheelZoom) !== undefined
100
+ || (config === null || config === void 0 ? void 0 : config.gestureZoom) !== undefined; };
101
+ var getEventTouchCount = function (event) {
102
+ var _a, _b, _c;
103
+ if (typeof ((_a = event === null || event === void 0 ? void 0 : event.touches) === null || _a === void 0 ? void 0 : _a.length) === 'number') {
104
+ return event.touches.length;
105
+ }
106
+ 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') {
107
+ return event.sourceEvent.touches.length;
108
+ }
109
+ return 0;
110
+ };
39
111
  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()); };
40
112
  var hasTopOptionLabels = function (config) {
41
113
  var _a, _b, _c, _d, _e;
@@ -176,23 +248,49 @@ var ensureCanvas = function (container, name, size, margin, renderer) {
176
248
  return canvas;
177
249
  };
178
250
  var resolveAxisDomain = function (axis, data) {
179
- var _a, _b, _c, _d;
251
+ var _a, _b, _c, _d, _e, _f, _g;
180
252
  if (axis.domain) {
181
253
  return axis.domain;
182
254
  }
183
255
  if (axis.type === 'string' || axis.type === 'point') {
184
256
  return data.map(function (item) { return item[axis.field]; });
185
257
  }
186
- var values = data.map(function (item) {
187
- var value = item[axis.field];
258
+ var domainFields = ((_a = axis.domainFields) === null || _a === void 0 ? void 0 : _a.length) ? axis.domainFields : [axis.field];
259
+ var values = data.flatMap(function (item) { return domainFields.map(function (field) {
260
+ var value = item[field];
188
261
  return axis.type === 'time'
189
262
  ? new Date(value)
190
263
  : Number(value);
191
- });
192
- var _e = (0, d3_array_1.extent)(values), minValue = _e[0], maxValue = _e[1];
264
+ }); }).filter(function (value) { return axis.type === 'time'
265
+ ? value instanceof Date && Number.isFinite(value.getTime())
266
+ : Number.isFinite(value); });
267
+ var _h = (0, d3_array_1.extent)(values), minValue = _h[0], maxValue = _h[1];
268
+ var resolvedMin = (_c = (_b = axis.min) !== null && _b !== void 0 ? _b : minValue) !== null && _c !== void 0 ? _c : 0;
269
+ var resolvedMax = (_e = (_d = axis.max) !== null && _d !== void 0 ? _d : maxValue) !== null && _e !== void 0 ? _e : 1;
270
+ var canApplyPadding = axis.padding !== undefined && axis.min === undefined && axis.max === undefined;
271
+ if (canApplyPadding && axis.type === 'time') {
272
+ var minTime = resolvedMin instanceof Date ? resolvedMin.getTime() : new Date(resolvedMin).getTime();
273
+ var maxTime = resolvedMax instanceof Date ? resolvedMax.getTime() : new Date(resolvedMax).getTime();
274
+ var span = Math.max(maxTime - minTime, 24 * 60 * 60 * 1000);
275
+ var padding = span * Math.max((_f = axis.padding) !== null && _f !== void 0 ? _f : 0, 0);
276
+ return [
277
+ new Date(minTime - padding),
278
+ new Date(maxTime + padding)
279
+ ];
280
+ }
281
+ if (canApplyPadding && axis.type === 'number') {
282
+ var minNumber = Number(resolvedMin);
283
+ var maxNumber = Number(resolvedMax);
284
+ var span = Math.max(maxNumber - minNumber, 1);
285
+ var padding = span * Math.max((_g = axis.padding) !== null && _g !== void 0 ? _g : 0, 0);
286
+ return [
287
+ minNumber - padding,
288
+ maxNumber + padding
289
+ ];
290
+ }
193
291
  return [
194
- (_b = (_a = axis.min) !== null && _a !== void 0 ? _a : minValue) !== null && _b !== void 0 ? _b : 0,
195
- (_d = (_c = axis.max) !== null && _c !== void 0 ? _c : maxValue) !== null && _d !== void 0 ? _d : 1
292
+ resolvedMin,
293
+ resolvedMax
196
294
  ];
197
295
  };
198
296
  var resolveScales = function (data, axes, plotSize) { return axes.map(function (axis) {
@@ -289,11 +387,71 @@ var resolveAxisTransform = function (placement, plotSize) {
289
387
  return 'translate(0, 0)';
290
388
  };
291
389
  var resolveScalePosition = function (scale, value) {
292
- var position = scale.scale(value);
390
+ var scaleValue = scale.type === 'time' && !(value instanceof Date)
391
+ ? new Date(value)
392
+ : value;
393
+ var position = scale.scale(scaleValue);
293
394
  return typeof scale.scale.bandwidth === 'function'
294
395
  ? position + scale.scale.bandwidth() / 2
295
396
  : position;
296
397
  };
398
+ var clampNumber = function (value, min, max) { return Math.max(min, Math.min(max, value)); };
399
+ var resolveCanvasCandlestickWidth = function (data, xScale, xField, plotSize, configuration) {
400
+ var _a, _b;
401
+ if (typeof configuration.candleWidth === 'number') {
402
+ return configuration.candleWidth;
403
+ }
404
+ if (typeof configuration.candleWidth === 'function') {
405
+ return configuration.candleWidth({ data: data, xScale: xScale, plotSize: plotSize });
406
+ }
407
+ var minWidth = (_a = configuration.minCandleWidth) !== null && _a !== void 0 ? _a : 3;
408
+ var maxWidth = (_b = configuration.maxCandleWidth) !== null && _b !== void 0 ? _b : 18;
409
+ if (typeof xScale.scale.bandwidth === 'function') {
410
+ return clampNumber(xScale.scale.bandwidth() * 0.72, minWidth, maxWidth);
411
+ }
412
+ var positions = data
413
+ .map(function (point) { return resolveScalePosition(xScale, point[xField]); })
414
+ .filter(Number.isFinite)
415
+ .sort(function (a, b) { return a - b; });
416
+ var minDistance = Number.POSITIVE_INFINITY;
417
+ for (var index = 1; index < positions.length; index += 1) {
418
+ var distance = positions[index] - positions[index - 1];
419
+ if (distance > 0 && distance < minDistance) {
420
+ minDistance = distance;
421
+ }
422
+ }
423
+ if (!Number.isFinite(minDistance)) {
424
+ return clampNumber(plotSize.width / Math.max(data.length, 1) * 0.72, minWidth, maxWidth);
425
+ }
426
+ return clampNumber(minDistance * 0.72, minWidth, maxWidth);
427
+ };
428
+ var resolveCandlestickColor = function (point, previousPoint, configuration, fallbackColor) {
429
+ var _a, _b, _c, _d;
430
+ var previousCloseFromField = configuration.previousCloseField
431
+ ? Number(point[configuration.previousCloseField])
432
+ : undefined;
433
+ var previousCloseFromPoint = previousPoint
434
+ ? Number(previousPoint[configuration.closeField])
435
+ : undefined;
436
+ var open = configuration.colorMode === 'previous-close'
437
+ ? (Number.isFinite(previousCloseFromField) ? previousCloseFromField : previousCloseFromPoint)
438
+ : Number(point[configuration.openField]);
439
+ var close = Number(point[configuration.closeField]);
440
+ if (!Number.isFinite(open) || !Number.isFinite(close)) {
441
+ return (_a = configuration.neutralColor) !== null && _a !== void 0 ? _a : fallbackColor;
442
+ }
443
+ if (close > open) {
444
+ return (_b = configuration.upColor) !== null && _b !== void 0 ? _b : '#22c55e';
445
+ }
446
+ if (close < open) {
447
+ return (_c = configuration.downColor) !== null && _c !== void 0 ? _c : '#ef4444';
448
+ }
449
+ return (_d = configuration.neutralColor) !== null && _d !== void 0 ? _d : fallbackColor;
450
+ };
451
+ var formatCandlestickTooltipValue = function (value) {
452
+ var numberValue = Number(value);
453
+ return Number.isFinite(numberValue) ? numberValue.toLocaleString(undefined, { maximumFractionDigits: 4 }) : String(value);
454
+ };
297
455
  var downsampleLTTB = function (data, threshold, xAccessor, yAccessor) {
298
456
  var dataLength = data.length;
299
457
  var targetLength = Math.floor(threshold);
@@ -1060,12 +1218,275 @@ var getCursorGuide = function (state) {
1060
1218
  }
1061
1219
  return (_c = state.config.cursorGuide) !== null && _c !== void 0 ? _c : state.config.guideLine;
1062
1220
  };
1221
+ var resolveZoomedAxes = function (state, transform) {
1222
+ var _a, _b;
1223
+ var direction = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.direction) !== null && _b !== void 0 ? _b : 'x';
1224
+ var baseScales = resolveScales(state.data, state.baseAxes, state.plotSize);
1225
+ var xDomain;
1226
+ var yDomain;
1227
+ var axes = state.baseAxes.map(function (axis) {
1228
+ var horizontal = isHorizontalAxis(axis);
1229
+ var shouldZoom = isZoomScaleSupported(axis)
1230
+ && ((horizontal && (direction === 'x' || direction === 'xy'))
1231
+ || (!horizontal && (direction === 'y' || direction === 'xy')));
1232
+ if (!shouldZoom) {
1233
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1234
+ }
1235
+ var scale = baseScales.find(function (item) { return item.field === axis.field && item.placement === axis.placement; });
1236
+ if (!scale) {
1237
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1238
+ }
1239
+ var zoomedScale = horizontal
1240
+ ? transform.rescaleX(scale.scale)
1241
+ : transform.rescaleY(scale.scale);
1242
+ var domain = zoomedScale.domain().map(function (value) { return axis.type === 'time'
1243
+ ? value instanceof Date ? value : new Date(value)
1244
+ : Number(value); });
1245
+ if (horizontal) {
1246
+ xDomain = domain;
1247
+ }
1248
+ else {
1249
+ yDomain = domain;
1250
+ }
1251
+ return __assign(__assign({}, axis), { domain: domain });
1252
+ });
1253
+ return {
1254
+ axes: axes,
1255
+ xDomain: xDomain,
1256
+ yDomain: yDomain,
1257
+ transform: transform
1258
+ };
1259
+ };
1260
+ var resetZoom = function (state) {
1261
+ var _a, _b;
1262
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
1263
+ state.axes = cloneAxes(state.initialAxes);
1264
+ state.baseAxes = cloneAxes(state.initialAxes);
1265
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1266
+ (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.onZoom) === null || _b === void 0 ? void 0 : _b.call(_a, {
1267
+ axes: state.axes,
1268
+ transform: state.zoomTransform
1269
+ });
1270
+ render(state);
1271
+ };
1272
+ var drawZoomSelection = function (state) {
1273
+ var selection = state.zoomSelection;
1274
+ var rect = state.layers.overlayGroup.selectAll('rect.kchart-zoom-selection')
1275
+ .data((selection === null || selection === void 0 ? void 0 : selection.active) ? [undefined] : [])
1276
+ .join('rect')
1277
+ .attr('class', 'kchart-zoom-selection')
1278
+ .style('fill', 'rgba(93, 184, 255, 0.16)')
1279
+ .style('stroke', 'rgba(148, 214, 255, 0.9)')
1280
+ .style('stroke-width', 1)
1281
+ .style('stroke-dasharray', '4 4')
1282
+ .style('pointer-events', 'none');
1283
+ if (!(selection === null || selection === void 0 ? void 0 : selection.active)) {
1284
+ return;
1285
+ }
1286
+ rect
1287
+ .attr('x', Math.min(selection.startX, selection.currentX))
1288
+ .attr('y', Math.min(selection.startY, selection.currentY))
1289
+ .attr('width', Math.abs(selection.currentX - selection.startX))
1290
+ .attr('height', Math.abs(selection.currentY - selection.startY));
1291
+ };
1292
+ var resolveSelectionZoomAxes = function (state, selection) {
1293
+ var _a, _b;
1294
+ var direction = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.direction) !== null && _b !== void 0 ? _b : 'x';
1295
+ var minX = Math.max(0, Math.min(selection.startX, selection.currentX));
1296
+ var maxX = Math.min(state.plotSize.width, Math.max(selection.startX, selection.currentX));
1297
+ var minY = Math.max(0, Math.min(selection.startY, selection.currentY));
1298
+ var maxY = Math.min(state.plotSize.height, Math.max(selection.startY, selection.currentY));
1299
+ var width = maxX - minX;
1300
+ var height = maxY - minY;
1301
+ var shouldZoomX = direction === 'x' || direction === 'xy';
1302
+ var shouldZoomY = direction === 'y' || direction === 'xy';
1303
+ if ((shouldZoomX && width < 8) || (shouldZoomY && height < 8)) {
1304
+ return null;
1305
+ }
1306
+ var xDomain;
1307
+ var yDomain;
1308
+ var axes = state.axes.map(function (axis) {
1309
+ var horizontal = isHorizontalAxis(axis);
1310
+ var shouldZoom = isZoomScaleSupported(axis)
1311
+ && ((horizontal && shouldZoomX) || (!horizontal && shouldZoomY));
1312
+ if (!shouldZoom) {
1313
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1314
+ }
1315
+ var scale = state.scales.find(function (item) { return item.field === axis.field && item.placement === axis.placement; });
1316
+ if (!scale || typeof scale.scale.invert !== 'function') {
1317
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1318
+ }
1319
+ var rawDomain = horizontal
1320
+ ? [scale.scale.invert(minX), scale.scale.invert(maxX)]
1321
+ : [scale.scale.invert(maxY), scale.scale.invert(minY)];
1322
+ var domain = rawDomain.map(function (value) { return axis.type === 'time'
1323
+ ? value instanceof Date ? value : new Date(value)
1324
+ : Number(value); });
1325
+ if (horizontal) {
1326
+ xDomain = domain;
1327
+ }
1328
+ else {
1329
+ yDomain = domain;
1330
+ }
1331
+ return __assign(__assign({}, axis), { domain: domain });
1332
+ });
1333
+ return {
1334
+ axes: axes,
1335
+ xDomain: xDomain,
1336
+ yDomain: yDomain,
1337
+ transform: d3_zoom_1.zoomIdentity
1338
+ };
1339
+ };
1340
+ var applySelectionZoom = function (state) {
1341
+ var _a, _b;
1342
+ var selection = state.zoomSelection;
1343
+ if (!(selection === null || selection === void 0 ? void 0 : selection.active)) {
1344
+ return;
1345
+ }
1346
+ var nextZoom = resolveSelectionZoomAxes(state, selection);
1347
+ state.zoomSelection = undefined;
1348
+ drawZoomSelection(state);
1349
+ if (!nextZoom) {
1350
+ return;
1351
+ }
1352
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
1353
+ state.axes = nextZoom.axes;
1354
+ state.baseAxes = cloneAxes(nextZoom.axes);
1355
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1356
+ (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.onZoom) === null || _b === void 0 ? void 0 : _b.call(_a, nextZoom);
1357
+ render(state);
1358
+ };
1359
+ var renderZoom = function (state) {
1360
+ var _a, _b, _c;
1361
+ var overlay = state.layers.overlayGroup.select('rect.kchart-tooltip-overlay');
1362
+ var overlayNode = overlay.node();
1363
+ if (!overlayNode) {
1364
+ return;
1365
+ }
1366
+ var mode = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.mode) !== null && _b !== void 0 ? _b : 'wheel';
1367
+ var zoomConfig = state.config.zoom;
1368
+ var inputOptionsConfigured = hasZoomInputOptions(zoomConfig);
1369
+ var selectionEnabled = isZoomEnabled(state) && (mode === 'select' || mode === 'both');
1370
+ var wheelEnabled = isZoomEnabled(state)
1371
+ && (mode === 'wheel' || mode === 'both')
1372
+ && isZoomOptionEnabled(zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.wheelZoom, true);
1373
+ var panEnabled = isZoomEnabled(state) && mode === 'wheel';
1374
+ var desktopPanEnabled = panEnabled && isZoomOptionEnabled(zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.wheelZoom, true);
1375
+ var gestureEnabled = isZoomEnabled(state)
1376
+ && isZoomOptionEnabled(zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.gestureZoom, !inputOptionsConfigured && panEnabled);
1377
+ overlay
1378
+ .style('cursor', selectionEnabled ? 'crosshair' : isZoomEnabled(state) ? 'grab' : null)
1379
+ .on('dblclick.kchart-zoom-reset', null)
1380
+ .on('mousedown.kchart-select-zoom', null)
1381
+ .on('mousemove.kchart-select-zoom', null)
1382
+ .on('mouseup.kchart-select-zoom', null)
1383
+ .on('mouseleave.kchart-select-zoom', null);
1384
+ if (!isZoomEnabled(state)) {
1385
+ overlay.on('.zoom', null);
1386
+ state.layers.overlayGroup.selectAll('rect.kchart-zoom-selection').remove();
1387
+ return;
1388
+ }
1389
+ if (wheelEnabled || desktopPanEnabled || gestureEnabled) {
1390
+ var wheelSensitivity_1 = getZoomWheelSensitivity(zoomConfig);
1391
+ var zoomBehavior = (0, d3_zoom_1.zoom)()
1392
+ .scaleExtent((_c = zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.scaleExtent) !== null && _c !== void 0 ? _c : [1, 40])
1393
+ .extent([[0, 0], [state.plotSize.width, state.plotSize.height]])
1394
+ .translateExtent([[0, 0], [state.plotSize.width, state.plotSize.height]])
1395
+ .touchable(function () { return gestureEnabled; })
1396
+ .wheelDelta(function (event) { return -event.deltaY
1397
+ * (event.deltaMode === 1 ? 0.05 : event.deltaMode ? 1 : 0.002)
1398
+ * (event.ctrlKey ? 10 : 1)
1399
+ * wheelSensitivity_1; })
1400
+ .filter(function (event) {
1401
+ if (event.type === 'wheel') {
1402
+ return wheelEnabled && (!inputOptionsConfigured
1403
+ || matchesZoomInputDevice(getZoomWheelDevice(zoomConfig), event));
1404
+ }
1405
+ if (event.type === 'mousedown') {
1406
+ return desktopPanEnabled && (!inputOptionsConfigured
1407
+ || matchesZoomInputDevice(getZoomWheelDevice(zoomConfig), event));
1408
+ }
1409
+ if (event.type === 'touchstart' || event.type === 'touchmove') {
1410
+ var touchCount = getEventTouchCount(event);
1411
+ return gestureEnabled
1412
+ && touchCount >= getZoomGestureMinTouches(zoomConfig)
1413
+ && (!inputOptionsConfigured
1414
+ || matchesZoomInputDevice(getZoomGestureDevice(zoomConfig), event));
1415
+ }
1416
+ return false;
1417
+ })
1418
+ .on('zoom', function (event) {
1419
+ var _a;
1420
+ state.zoomTransform = event.transform;
1421
+ var nextZoom = resolveZoomedAxes(state, event.transform);
1422
+ state.axes = nextZoom.axes;
1423
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1424
+ (_a = zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.onZoom) === null || _a === void 0 ? void 0 : _a.call(zoomConfig, nextZoom);
1425
+ render(state);
1426
+ });
1427
+ overlay.call(zoomBehavior);
1428
+ overlayNode.__zoom = state.zoomTransform;
1429
+ }
1430
+ else {
1431
+ overlay.on('.zoom', null);
1432
+ }
1433
+ if (selectionEnabled) {
1434
+ overlay
1435
+ .on('mousedown.kchart-select-zoom', function (event) {
1436
+ if (event.button !== 0) {
1437
+ return;
1438
+ }
1439
+ event.preventDefault();
1440
+ event.stopPropagation();
1441
+ var _a = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _a[0], mouseY = _a[1];
1442
+ state.zoomSelection = {
1443
+ active: true,
1444
+ startX: mouseX,
1445
+ startY: mouseY,
1446
+ currentX: mouseX,
1447
+ currentY: mouseY
1448
+ };
1449
+ drawZoomSelection(state);
1450
+ })
1451
+ .on('mousemove.kchart-select-zoom', function (event) {
1452
+ var _a;
1453
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
1454
+ return;
1455
+ }
1456
+ var _b = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _b[0], mouseY = _b[1];
1457
+ state.zoomSelection.currentX = mouseX;
1458
+ state.zoomSelection.currentY = mouseY;
1459
+ drawZoomSelection(state);
1460
+ })
1461
+ .on('mouseup.kchart-select-zoom', function (event) {
1462
+ var _a;
1463
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
1464
+ return;
1465
+ }
1466
+ var _b = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _b[0], mouseY = _b[1];
1467
+ state.zoomSelection.currentX = mouseX;
1468
+ state.zoomSelection.currentY = mouseY;
1469
+ applySelectionZoom(state);
1470
+ })
1471
+ .on('mouseleave.kchart-select-zoom', function () {
1472
+ var _a;
1473
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
1474
+ return;
1475
+ }
1476
+ state.zoomSelection = undefined;
1477
+ drawZoomSelection(state);
1478
+ });
1479
+ }
1480
+ if ((zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.resetOnDoubleClick) !== false) {
1481
+ overlay.on('dblclick.kchart-zoom-reset', function () { return resetZoom(state); });
1482
+ }
1483
+ };
1063
1484
  var renderTooltip = function (state) {
1064
1485
  var _a, _b;
1065
1486
  var tooltipEnabled = ((_a = state.config.tooltip) === null || _a === void 0 ? void 0 : _a.visible) === true;
1066
1487
  var cursorGuide = getCursorGuide(state);
1067
1488
  var guideEnabled = (cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.visible) === true;
1068
- var enabled = tooltipEnabled || guideEnabled;
1489
+ var enabled = tooltipEnabled || guideEnabled || isZoomEnabled(state);
1069
1490
  var overlay = state.layers.overlayGroup.selectAll('rect.kchart-tooltip-overlay')
1070
1491
  .data(enabled ? [undefined] : [])
1071
1492
  .join('rect')
@@ -1342,6 +1763,7 @@ var render = function (state) {
1342
1763
  renderSeries(state);
1343
1764
  renderLegend(state);
1344
1765
  renderTooltip(state);
1766
+ renderZoom(state);
1345
1767
  };
1346
1768
  var createCustomSeries = function (series) { return series; };
1347
1769
  exports.createCustomSeries = createCustomSeries;
@@ -1512,6 +1934,131 @@ var createCanvasPointSeries = function (configuration) { return (0, exports.crea
1512
1934
  }
1513
1935
  }); };
1514
1936
  exports.createCanvasPointSeries = createCanvasPointSeries;
1937
+ var createCanvasCandlestickSeries = function (configuration) { return (0, exports.createCustomSeries)({
1938
+ selector: configuration.selector,
1939
+ displayName: configuration.displayName,
1940
+ xField: configuration.xField,
1941
+ yField: configuration.closeField,
1942
+ color: configuration.neutralColor,
1943
+ render: function (_a) {
1944
+ var _b, _c;
1945
+ var getCanvas = _a.getCanvas, data = _a.data, xScale = _a.xScale, yScale = _a.yScale, color = _a.color, plotSize = _a.plotSize;
1946
+ if (!xScale || !yScale) {
1947
+ return;
1948
+ }
1949
+ var canvas = getCanvas((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector);
1950
+ var context = canvas.getContext('2d');
1951
+ if (!context) {
1952
+ return;
1953
+ }
1954
+ var candleWidth = resolveCanvasCandlestickWidth(data, xScale, configuration.xField, plotSize, configuration);
1955
+ var halfWidth = candleWidth / 2;
1956
+ context.clearRect(0, 0, canvas.width, canvas.height);
1957
+ context.lineCap = 'round';
1958
+ context.lineJoin = 'round';
1959
+ context.lineWidth = (_c = configuration.strokeWidth) !== null && _c !== void 0 ? _c : 1.4;
1960
+ data.forEach(function (point, index) {
1961
+ var _a, _b;
1962
+ var open = Number(point[configuration.openField]);
1963
+ var high = Number(point[configuration.highField]);
1964
+ var low = Number(point[configuration.lowField]);
1965
+ var close = Number(point[configuration.closeField]);
1966
+ if (![open, high, low, close].every(Number.isFinite) || point[configuration.xField] === undefined) {
1967
+ return;
1968
+ }
1969
+ var x = resolveScalePosition(xScale, point[configuration.xField]);
1970
+ var openY = resolveScalePosition(yScale, open);
1971
+ var highY = resolveScalePosition(yScale, high);
1972
+ var lowY = resolveScalePosition(yScale, low);
1973
+ var closeY = resolveScalePosition(yScale, close);
1974
+ if (![x, openY, highY, lowY, closeY].every(Number.isFinite)) {
1975
+ return;
1976
+ }
1977
+ var candleColor = resolveCandlestickColor(point, data[index - 1], configuration, color);
1978
+ var bodyTop = Math.min(openY, closeY);
1979
+ var bodyHeight = Math.max(Math.abs(closeY - openY), 1);
1980
+ context.strokeStyle = (_a = configuration.wickColor) !== null && _a !== void 0 ? _a : candleColor;
1981
+ context.beginPath();
1982
+ context.moveTo(x, highY);
1983
+ context.lineTo(x, lowY);
1984
+ context.stroke();
1985
+ context.fillStyle = candleColor;
1986
+ context.strokeStyle = (_b = configuration.borderColor) !== null && _b !== void 0 ? _b : candleColor;
1987
+ context.beginPath();
1988
+ context.rect(x - halfWidth, bodyTop, candleWidth, bodyHeight);
1989
+ context.fill();
1990
+ context.stroke();
1991
+ });
1992
+ },
1993
+ tooltip: function (_a) {
1994
+ var _b, _c, _d;
1995
+ var data = _a.data, scales = _a.scales, mouseX = _a.mouseX, mouseY = _a.mouseY, color = _a.color, plotSize = _a.plotSize;
1996
+ var xScale = scales.find(function (scale) { return scale.field === configuration.xField; });
1997
+ var yScale = (_b = scales.find(function (scale) { return scale.field === configuration.closeField; })) !== null && _b !== void 0 ? _b : scales.find(function (scale) { return scale.placement === 'left' || scale.placement === 'right'; });
1998
+ if (!xScale || !yScale) {
1999
+ return null;
2000
+ }
2001
+ var candleWidth = resolveCanvasCandlestickWidth(data, xScale, configuration.xField, plotSize, configuration);
2002
+ var hitRadius = Math.max(candleWidth * 0.7, 14);
2003
+ var nearest = null;
2004
+ data.forEach(function (point, index) {
2005
+ if (point[configuration.xField] === undefined || point[configuration.closeField] === undefined) {
2006
+ return;
2007
+ }
2008
+ var x = resolveScalePosition(xScale, point[configuration.xField]);
2009
+ var y = resolveScalePosition(yScale, point[configuration.closeField]);
2010
+ var distance = Math.abs(mouseX - x);
2011
+ if (!Number.isFinite(x) || !Number.isFinite(y) || distance > hitRadius) {
2012
+ return;
2013
+ }
2014
+ var verticalDistance = Math.abs(mouseY - y);
2015
+ var totalDistance = distance + verticalDistance * 0.08;
2016
+ if (!nearest || totalDistance < nearest.distance) {
2017
+ nearest = {
2018
+ data: point,
2019
+ previousData: data[index - 1],
2020
+ x: x,
2021
+ y: y,
2022
+ distance: totalDistance
2023
+ };
2024
+ }
2025
+ });
2026
+ if (!nearest) {
2027
+ return null;
2028
+ }
2029
+ var point = nearest.data;
2030
+ var candleColor = resolveCandlestickColor(point, nearest.previousData, configuration, color);
2031
+ var label = (_c = configuration.displayName) !== null && _c !== void 0 ? _c : configuration.selector;
2032
+ var xValue = String(point[configuration.xField]);
2033
+ var previousClose = configuration.previousCloseField
2034
+ ? point[configuration.previousCloseField]
2035
+ : (_d = nearest.previousData) === null || _d === void 0 ? void 0 : _d[configuration.closeField];
2036
+ return {
2037
+ data: point,
2038
+ x: nearest.x,
2039
+ y: nearest.y,
2040
+ color: candleColor,
2041
+ distance: nearest.distance,
2042
+ html: [
2043
+ "<strong style=\"color:".concat(candleColor, "\">").concat(label, "</strong>"),
2044
+ "x: ".concat(xValue),
2045
+ configuration.colorMode === 'previous-close'
2046
+ ? "previous close: ".concat(formatCandlestickTooltipValue(previousClose))
2047
+ : '',
2048
+ "open: ".concat(formatCandlestickTooltipValue(point[configuration.openField])),
2049
+ "high: ".concat(formatCandlestickTooltipValue(point[configuration.highField])),
2050
+ "low: ".concat(formatCandlestickTooltipValue(point[configuration.lowField])),
2051
+ "close: ".concat(formatCandlestickTooltipValue(point[configuration.closeField]))
2052
+ ].filter(Boolean).join('<br/>')
2053
+ };
2054
+ },
2055
+ destroy: function (_a) {
2056
+ var _b;
2057
+ var svg = _a.svg;
2058
+ destroyCanvasByClass(svg, "kchart-2d-canvas-".concat((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector));
2059
+ }
2060
+ }); };
2061
+ exports.createCanvasCandlestickSeries = createCanvasCandlestickSeries;
1515
2062
  var createWebglPointSeries = function (configuration) { return (0, exports.createCustomSeries)({
1516
2063
  selector: configuration.selector,
1517
2064
  displayName: configuration.displayName,
@@ -1637,7 +2184,9 @@ var createKChart = function (config) {
1637
2184
  config: config,
1638
2185
  container: container,
1639
2186
  data: config.data,
1640
- axes: config.axes,
2187
+ axes: cloneAxes(config.axes),
2188
+ initialAxes: cloneAxes(config.axes),
2189
+ baseAxes: cloneAxes(config.axes),
1641
2190
  series: config.series,
1642
2191
  size: sizes.size,
1643
2192
  plotSize: sizes.plotSize,
@@ -1645,7 +2194,8 @@ var createKChart = function (config) {
1645
2194
  colors: (_a = config.colors) !== null && _a !== void 0 ? _a : d3_scale_chromatic_1.schemeCategory10.slice(),
1646
2195
  scales: [],
1647
2196
  layers: undefined,
1648
- hiddenSeries: new Set()
2197
+ hiddenSeries: new Set(),
2198
+ zoomTransform: d3_zoom_1.zoomIdentity
1649
2199
  };
1650
2200
  state.layers = createLayers(container, config, function () { return state.size; }, function () { return state.margin; });
1651
2201
  var controller = {
@@ -1655,10 +2205,15 @@ var createKChart = function (config) {
1655
2205
  },
1656
2206
  updateData: function (data) {
1657
2207
  state.data = data;
2208
+ state.config = __assign(__assign({}, state.config), { data: data });
1658
2209
  return controller.render();
1659
2210
  },
1660
2211
  updateAxes: function (axes) {
1661
- state.axes = axes;
2212
+ state.axes = cloneAxes(axes);
2213
+ state.initialAxes = cloneAxes(axes);
2214
+ state.baseAxes = cloneAxes(axes);
2215
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
2216
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1662
2217
  return controller.render();
1663
2218
  },
1664
2219
  updateSeries: function (series) {