@keneth80/k-chart 1.1.0 → 1.3.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,19 +20,34 @@ 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.downsampleLTTB = void 0;
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;
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,
33
34
  bottom: 36,
34
35
  left: 44
35
36
  };
37
+ var transferredCanvases = new WeakSet();
38
+ var asyncCanvasEntries = new WeakMap();
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
+ };
36
51
  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()); };
37
52
  var hasTopOptionLabels = function (config) {
38
53
  var _a, _b, _c, _d, _e;
@@ -150,6 +165,8 @@ var createLayers = function (container, config, getSize, getMargin) {
150
165
  };
151
166
  var ensureCanvas = function (container, name, size, margin, renderer) {
152
167
  var className = "kchart-".concat(renderer, "-canvas-").concat(name);
168
+ var width = Math.max(size.width - margin.left - margin.right, 0);
169
+ var height = Math.max(size.height - margin.top - margin.bottom, 0);
153
170
  var canvas = container.selectAll("canvas.".concat(className))
154
171
  .data([undefined])
155
172
  .join('canvas')
@@ -159,11 +176,15 @@ var ensureCanvas = function (container, name, size, margin, renderer) {
159
176
  .style('pointer-events', 'none')
160
177
  .style('left', "".concat(margin.left, "px"))
161
178
  .style('top', "".concat(margin.top, "px"))
162
- .style('width', "".concat(Math.max(size.width - margin.left - margin.right, 0), "px"))
163
- .style('height', "".concat(Math.max(size.height - margin.top - margin.bottom, 0), "px"))
179
+ .style('width', "".concat(width, "px"))
180
+ .style('height', "".concat(height, "px"))
181
+ .attr('data-kchart-width', String(width))
182
+ .attr('data-kchart-height', String(height))
164
183
  .node();
165
- canvas.width = Math.max(size.width - margin.left - margin.right, 0);
166
- canvas.height = Math.max(size.height - margin.top - margin.bottom, 0);
184
+ if (!transferredCanvases.has(canvas)) {
185
+ canvas.width = width;
186
+ canvas.height = height;
187
+ }
167
188
  return canvas;
168
189
  };
169
190
  var resolveAxisDomain = function (axis, data) {
@@ -391,6 +412,135 @@ var resolveSeriesRenderData = function (state, series, xScale, yScale) {
391
412
  var threshold = resolveDownsampleThreshold(downsample, context);
392
413
  return (0, exports.downsampleLTTB)(state.data, threshold, xAccessor, yAccessor);
393
414
  };
415
+ var getAsyncCanvasEntry = function (canvas, renderer, asyncRender) {
416
+ if (!(asyncRender === null || asyncRender === void 0 ? void 0 : asyncRender.enabled) || !asyncRender.workerFactory || typeof canvas.transferControlToOffscreen !== 'function') {
417
+ return null;
418
+ }
419
+ var existing = asyncCanvasEntries.get(canvas);
420
+ if (existing) {
421
+ return existing.failed || existing.renderer !== renderer ? null : existing;
422
+ }
423
+ try {
424
+ var worker = asyncRender.workerFactory();
425
+ var canvasId = "kchart-offscreen-".concat(asyncCanvasId += 1);
426
+ var offscreenCanvas = canvas.transferControlToOffscreen();
427
+ var entry_1 = {
428
+ worker: worker,
429
+ renderer: renderer,
430
+ canvasId: canvasId,
431
+ failed: false
432
+ };
433
+ worker.onerror = function () {
434
+ entry_1.failed = true;
435
+ };
436
+ worker.postMessage({
437
+ type: 'kchart:init-canvas',
438
+ canvasId: canvasId,
439
+ renderer: renderer,
440
+ canvas: offscreenCanvas
441
+ }, [offscreenCanvas]);
442
+ transferredCanvases.add(canvas);
443
+ asyncCanvasEntries.set(canvas, entry_1);
444
+ return entry_1;
445
+ }
446
+ catch (_error) {
447
+ return null;
448
+ }
449
+ };
450
+ var destroyAsyncCanvas = function (canvas) {
451
+ var entry = asyncCanvasEntries.get(canvas);
452
+ if (!entry) {
453
+ return;
454
+ }
455
+ try {
456
+ entry.worker.postMessage({
457
+ type: 'kchart:destroy-canvas',
458
+ canvasId: entry.canvasId
459
+ });
460
+ entry.worker.terminate();
461
+ }
462
+ catch (_error) {
463
+ entry.worker.terminate();
464
+ }
465
+ asyncCanvasEntries.delete(canvas);
466
+ };
467
+ var destroyCanvasByClass = function (svg, className) {
468
+ var _a;
469
+ var parent = (_a = svg.node()) === null || _a === void 0 ? void 0 : _a.parentElement;
470
+ if (!parent) {
471
+ return;
472
+ }
473
+ (0, d3_selection_1.select)(parent).selectAll("canvas.".concat(className))
474
+ .each(function destroyCanvas() {
475
+ destroyAsyncCanvas(this);
476
+ })
477
+ .remove();
478
+ };
479
+ var resolveLinePoints = function (data, xScale, yScale, xField, yField) {
480
+ var points = new Float32Array(data.length * 2);
481
+ var pointIndex = 0;
482
+ data.forEach(function (point) {
483
+ if (point[xField] === undefined || point[yField] === undefined) {
484
+ return;
485
+ }
486
+ var x = resolveScalePosition(xScale, point[xField]);
487
+ var y = resolveScalePosition(yScale, point[yField]);
488
+ if (Number.isFinite(x) && Number.isFinite(y)) {
489
+ points[pointIndex] = x;
490
+ points[pointIndex + 1] = y;
491
+ pointIndex += 2;
492
+ }
493
+ });
494
+ return pointIndex === points.length
495
+ ? points
496
+ : points.slice(0, pointIndex);
497
+ };
498
+ var resolveWebglLineVertices = function (points, width, height) {
499
+ var vertices = new Float32Array(points.length);
500
+ for (var index = 0; index < points.length; index += 2) {
501
+ vertices[index] = (points[index] / width) * 2 - 1;
502
+ vertices[index + 1] = 1 - (points[index + 1] / height) * 2;
503
+ }
504
+ return vertices;
505
+ };
506
+ var resolveWebglPointInterleavedData = function (data, xScale, yScale, xField, yField, width, height, resolveSize) {
507
+ var buffer = new Float32Array(data.length * 3);
508
+ var pointCount = 0;
509
+ data.forEach(function (point) {
510
+ if (point[xField] === undefined || point[yField] === undefined) {
511
+ return;
512
+ }
513
+ var x = resolveScalePosition(xScale, point[xField]);
514
+ var y = resolveScalePosition(yScale, point[yField]);
515
+ if (!Number.isFinite(x) || !Number.isFinite(y)) {
516
+ return;
517
+ }
518
+ var offset = pointCount * 3;
519
+ buffer[offset] = (x / width) * 2 - 1;
520
+ buffer[offset + 1] = 1 - (y / height) * 2;
521
+ buffer[offset + 2] = resolveSize(point);
522
+ pointCount += 1;
523
+ });
524
+ return {
525
+ buffer: pointCount * 3 === buffer.length
526
+ ? buffer
527
+ : buffer.subarray(0, pointCount * 3),
528
+ count: pointCount
529
+ };
530
+ };
531
+ var resolveCanvasPixelSize = function (canvas) { return ({
532
+ width: Number(canvas.dataset.kchartWidth) || canvas.width,
533
+ height: Number(canvas.dataset.kchartHeight) || canvas.height
534
+ }); };
535
+ var renderLineWithWorker = function (canvas, renderer, asyncRender, payload) {
536
+ var entry = getAsyncCanvasEntry(canvas, renderer, asyncRender);
537
+ if (!entry) {
538
+ return false;
539
+ }
540
+ var message = __assign({ type: 'kchart:render-line', canvasId: entry.canvasId, renderer: renderer }, payload);
541
+ entry.worker.postMessage(message, [message.points.buffer]);
542
+ return true;
543
+ };
394
544
  var parseColor = function (color) {
395
545
  if (color.startsWith('#')) {
396
546
  var value = color.slice(1);
@@ -439,6 +589,119 @@ var createProgram = function (gl, vertexSource, fragmentSource) {
439
589
  }
440
590
  return program;
441
591
  };
592
+ var startKChartRenderWorker = function (workerScope) {
593
+ if (workerScope === void 0) { workerScope = self; }
594
+ var canvases = new Map();
595
+ var resolveWorkerProgram = function (entry) {
596
+ var _a, _b;
597
+ if (entry.program) {
598
+ return entry.program;
599
+ }
600
+ if (!entry.gl) {
601
+ return null;
602
+ }
603
+ var vertexSource = "\n attribute vec2 a_position;\n\n void main() {\n gl_Position = vec4(a_position, 0.0, 1.0);\n }\n ";
604
+ var fragmentSource = "\n precision mediump float;\n uniform vec4 u_color;\n\n void main() {\n gl_FragColor = u_color;\n }\n ";
605
+ entry.program = (_a = createProgram(entry.gl, vertexSource, fragmentSource)) !== null && _a !== void 0 ? _a : undefined;
606
+ return (_b = entry.program) !== null && _b !== void 0 ? _b : null;
607
+ };
608
+ var drawCanvasLine = function (entry, message) {
609
+ var context = entry.context2d;
610
+ if (!context) {
611
+ return;
612
+ }
613
+ entry.canvas.width = message.width;
614
+ entry.canvas.height = message.height;
615
+ context.clearRect(0, 0, message.width, message.height);
616
+ context.beginPath();
617
+ context.lineCap = 'round';
618
+ context.lineJoin = 'round';
619
+ context.lineWidth = message.lineWidth;
620
+ context.strokeStyle = message.color;
621
+ for (var index = 0; index < message.points.length; index += 2) {
622
+ var x = message.points[index];
623
+ var y = message.points[index + 1];
624
+ if (index === 0) {
625
+ context.moveTo(x, y);
626
+ }
627
+ else {
628
+ context.lineTo(x, y);
629
+ }
630
+ }
631
+ context.stroke();
632
+ };
633
+ var drawWebglLine = function (entry, message) {
634
+ var gl = entry.gl;
635
+ if (!gl) {
636
+ return;
637
+ }
638
+ entry.canvas.width = message.width;
639
+ entry.canvas.height = message.height;
640
+ var vertices = new Float32Array(message.points.length);
641
+ for (var index = 0; index < message.points.length; index += 2) {
642
+ vertices[index] = (message.points[index] / message.width) * 2 - 1;
643
+ vertices[index + 1] = 1 - (message.points[index + 1] / message.height) * 2;
644
+ }
645
+ var program = resolveWorkerProgram(entry);
646
+ if (!program) {
647
+ return;
648
+ }
649
+ gl.viewport(0, 0, message.width, message.height);
650
+ gl.clearColor(0, 0, 0, 0);
651
+ gl.clear(gl.COLOR_BUFFER_BIT);
652
+ gl.useProgram(program);
653
+ gl.enable(gl.BLEND);
654
+ gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
655
+ gl.lineWidth(message.lineWidth);
656
+ var positionBuffer = gl.createBuffer();
657
+ gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
658
+ gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
659
+ var positionLocation = gl.getAttribLocation(program, 'a_position');
660
+ gl.enableVertexAttribArray(positionLocation);
661
+ gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
662
+ var colorLocation = gl.getUniformLocation(program, 'u_color');
663
+ gl.uniform4fv(colorLocation, new Float32Array(parseColor(message.color)));
664
+ gl.drawArrays(gl.LINE_STRIP, 0, vertices.length / 2);
665
+ gl.deleteBuffer(positionBuffer);
666
+ };
667
+ workerScope.onmessage = function (event) {
668
+ var _a, _b;
669
+ var message = event.data;
670
+ if (message.type === 'kchart:init-canvas') {
671
+ var canvas = message.canvas;
672
+ var renderer = message.renderer;
673
+ var entry = {
674
+ canvas: canvas,
675
+ renderer: renderer
676
+ };
677
+ if (renderer === '2d') {
678
+ entry.context2d = (_a = canvas.getContext('2d')) !== null && _a !== void 0 ? _a : undefined;
679
+ }
680
+ else {
681
+ entry.gl = (_b = canvas.getContext('webgl', { alpha: true })) !== null && _b !== void 0 ? _b : undefined;
682
+ }
683
+ canvases.set(message.canvasId, entry);
684
+ return;
685
+ }
686
+ if (message.type === 'kchart:destroy-canvas') {
687
+ canvases.delete(message.canvasId);
688
+ return;
689
+ }
690
+ if (message.type === 'kchart:render-line') {
691
+ var entry = canvases.get(message.canvasId);
692
+ if (!entry) {
693
+ return;
694
+ }
695
+ if (message.renderer === '2d') {
696
+ drawCanvasLine(entry, message);
697
+ }
698
+ else {
699
+ drawWebglLine(entry, message);
700
+ }
701
+ }
702
+ };
703
+ };
704
+ exports.startKChartRenderWorker = startKChartRenderWorker;
442
705
  var renderTitle = function (state) {
443
706
  var _a, _b, _c, _d, _e;
444
707
  var title = state.config.title;
@@ -809,12 +1072,249 @@ var getCursorGuide = function (state) {
809
1072
  }
810
1073
  return (_c = state.config.cursorGuide) !== null && _c !== void 0 ? _c : state.config.guideLine;
811
1074
  };
1075
+ var resolveZoomedAxes = function (state, transform) {
1076
+ var _a, _b;
1077
+ var direction = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.direction) !== null && _b !== void 0 ? _b : 'x';
1078
+ var baseScales = resolveScales(state.data, state.baseAxes, state.plotSize);
1079
+ var xDomain;
1080
+ var yDomain;
1081
+ var axes = state.baseAxes.map(function (axis) {
1082
+ var horizontal = isHorizontalAxis(axis);
1083
+ var shouldZoom = isZoomScaleSupported(axis)
1084
+ && ((horizontal && (direction === 'x' || direction === 'xy'))
1085
+ || (!horizontal && (direction === 'y' || direction === 'xy')));
1086
+ if (!shouldZoom) {
1087
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1088
+ }
1089
+ var scale = baseScales.find(function (item) { return item.field === axis.field && item.placement === axis.placement; });
1090
+ if (!scale) {
1091
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1092
+ }
1093
+ var zoomedScale = horizontal
1094
+ ? transform.rescaleX(scale.scale)
1095
+ : transform.rescaleY(scale.scale);
1096
+ var domain = zoomedScale.domain().map(function (value) { return axis.type === 'time'
1097
+ ? value instanceof Date ? value : new Date(value)
1098
+ : Number(value); });
1099
+ if (horizontal) {
1100
+ xDomain = domain;
1101
+ }
1102
+ else {
1103
+ yDomain = domain;
1104
+ }
1105
+ return __assign(__assign({}, axis), { domain: domain });
1106
+ });
1107
+ return {
1108
+ axes: axes,
1109
+ xDomain: xDomain,
1110
+ yDomain: yDomain,
1111
+ transform: transform
1112
+ };
1113
+ };
1114
+ var resetZoom = function (state) {
1115
+ var _a, _b;
1116
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
1117
+ state.axes = cloneAxes(state.initialAxes);
1118
+ state.baseAxes = cloneAxes(state.initialAxes);
1119
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1120
+ (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.onZoom) === null || _b === void 0 ? void 0 : _b.call(_a, {
1121
+ axes: state.axes,
1122
+ transform: state.zoomTransform
1123
+ });
1124
+ render(state);
1125
+ };
1126
+ var drawZoomSelection = function (state) {
1127
+ var selection = state.zoomSelection;
1128
+ var rect = state.layers.overlayGroup.selectAll('rect.kchart-zoom-selection')
1129
+ .data((selection === null || selection === void 0 ? void 0 : selection.active) ? [undefined] : [])
1130
+ .join('rect')
1131
+ .attr('class', 'kchart-zoom-selection')
1132
+ .style('fill', 'rgba(93, 184, 255, 0.16)')
1133
+ .style('stroke', 'rgba(148, 214, 255, 0.9)')
1134
+ .style('stroke-width', 1)
1135
+ .style('stroke-dasharray', '4 4')
1136
+ .style('pointer-events', 'none');
1137
+ if (!(selection === null || selection === void 0 ? void 0 : selection.active)) {
1138
+ return;
1139
+ }
1140
+ rect
1141
+ .attr('x', Math.min(selection.startX, selection.currentX))
1142
+ .attr('y', Math.min(selection.startY, selection.currentY))
1143
+ .attr('width', Math.abs(selection.currentX - selection.startX))
1144
+ .attr('height', Math.abs(selection.currentY - selection.startY));
1145
+ };
1146
+ var resolveSelectionZoomAxes = function (state, selection) {
1147
+ var _a, _b;
1148
+ var direction = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.direction) !== null && _b !== void 0 ? _b : 'x';
1149
+ var minX = Math.max(0, Math.min(selection.startX, selection.currentX));
1150
+ var maxX = Math.min(state.plotSize.width, Math.max(selection.startX, selection.currentX));
1151
+ var minY = Math.max(0, Math.min(selection.startY, selection.currentY));
1152
+ var maxY = Math.min(state.plotSize.height, Math.max(selection.startY, selection.currentY));
1153
+ var width = maxX - minX;
1154
+ var height = maxY - minY;
1155
+ var shouldZoomX = direction === 'x' || direction === 'xy';
1156
+ var shouldZoomY = direction === 'y' || direction === 'xy';
1157
+ if ((shouldZoomX && width < 8) || (shouldZoomY && height < 8)) {
1158
+ return null;
1159
+ }
1160
+ var xDomain;
1161
+ var yDomain;
1162
+ var axes = state.axes.map(function (axis) {
1163
+ var horizontal = isHorizontalAxis(axis);
1164
+ var shouldZoom = isZoomScaleSupported(axis)
1165
+ && ((horizontal && shouldZoomX) || (!horizontal && shouldZoomY));
1166
+ if (!shouldZoom) {
1167
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1168
+ }
1169
+ var scale = state.scales.find(function (item) { return item.field === axis.field && item.placement === axis.placement; });
1170
+ if (!scale || typeof scale.scale.invert !== 'function') {
1171
+ return __assign(__assign({}, axis), { domain: cloneAxisDomain(axis.domain) });
1172
+ }
1173
+ var rawDomain = horizontal
1174
+ ? [scale.scale.invert(minX), scale.scale.invert(maxX)]
1175
+ : [scale.scale.invert(maxY), scale.scale.invert(minY)];
1176
+ var domain = rawDomain.map(function (value) { return axis.type === 'time'
1177
+ ? value instanceof Date ? value : new Date(value)
1178
+ : Number(value); });
1179
+ if (horizontal) {
1180
+ xDomain = domain;
1181
+ }
1182
+ else {
1183
+ yDomain = domain;
1184
+ }
1185
+ return __assign(__assign({}, axis), { domain: domain });
1186
+ });
1187
+ return {
1188
+ axes: axes,
1189
+ xDomain: xDomain,
1190
+ yDomain: yDomain,
1191
+ transform: d3_zoom_1.zoomIdentity
1192
+ };
1193
+ };
1194
+ var applySelectionZoom = function (state) {
1195
+ var _a, _b;
1196
+ var selection = state.zoomSelection;
1197
+ if (!(selection === null || selection === void 0 ? void 0 : selection.active)) {
1198
+ return;
1199
+ }
1200
+ var nextZoom = resolveSelectionZoomAxes(state, selection);
1201
+ state.zoomSelection = undefined;
1202
+ drawZoomSelection(state);
1203
+ if (!nextZoom) {
1204
+ return;
1205
+ }
1206
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
1207
+ state.axes = nextZoom.axes;
1208
+ state.baseAxes = cloneAxes(nextZoom.axes);
1209
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1210
+ (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.onZoom) === null || _b === void 0 ? void 0 : _b.call(_a, nextZoom);
1211
+ render(state);
1212
+ };
1213
+ var renderZoom = function (state) {
1214
+ var _a, _b, _c;
1215
+ var overlay = state.layers.overlayGroup.select('rect.kchart-tooltip-overlay');
1216
+ var overlayNode = overlay.node();
1217
+ if (!overlayNode) {
1218
+ return;
1219
+ }
1220
+ var mode = (_b = (_a = state.config.zoom) === null || _a === void 0 ? void 0 : _a.mode) !== null && _b !== void 0 ? _b : 'wheel';
1221
+ var selectionEnabled = isZoomEnabled(state) && (mode === 'select' || mode === 'both');
1222
+ var wheelEnabled = isZoomEnabled(state) && (mode === 'wheel' || mode === 'both');
1223
+ var panEnabled = isZoomEnabled(state) && mode === 'wheel';
1224
+ overlay
1225
+ .style('cursor', selectionEnabled ? 'crosshair' : isZoomEnabled(state) ? 'grab' : null)
1226
+ .on('dblclick.kchart-zoom-reset', null)
1227
+ .on('mousedown.kchart-select-zoom', null)
1228
+ .on('mousemove.kchart-select-zoom', null)
1229
+ .on('mouseup.kchart-select-zoom', null)
1230
+ .on('mouseleave.kchart-select-zoom', null);
1231
+ if (!isZoomEnabled(state)) {
1232
+ overlay.on('.zoom', null);
1233
+ state.layers.overlayGroup.selectAll('rect.kchart-zoom-selection').remove();
1234
+ return;
1235
+ }
1236
+ var zoomConfig = state.config.zoom;
1237
+ if (wheelEnabled || panEnabled) {
1238
+ var zoomBehavior = (0, d3_zoom_1.zoom)()
1239
+ .scaleExtent((_c = zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.scaleExtent) !== null && _c !== void 0 ? _c : [1, 40])
1240
+ .extent([[0, 0], [state.plotSize.width, state.plotSize.height]])
1241
+ .translateExtent([[0, 0], [state.plotSize.width, state.plotSize.height]])
1242
+ .filter(function (event) { return event.type === 'wheel'
1243
+ || (panEnabled && (event.type === 'mousedown'
1244
+ || event.type === 'touchstart'
1245
+ || event.type === 'touchmove')); })
1246
+ .on('zoom', function (event) {
1247
+ var _a;
1248
+ state.zoomTransform = event.transform;
1249
+ var nextZoom = resolveZoomedAxes(state, event.transform);
1250
+ state.axes = nextZoom.axes;
1251
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1252
+ (_a = zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.onZoom) === null || _a === void 0 ? void 0 : _a.call(zoomConfig, nextZoom);
1253
+ render(state);
1254
+ });
1255
+ overlay.call(zoomBehavior);
1256
+ overlayNode.__zoom = state.zoomTransform;
1257
+ }
1258
+ else {
1259
+ overlay.on('.zoom', null);
1260
+ }
1261
+ if (selectionEnabled) {
1262
+ overlay
1263
+ .on('mousedown.kchart-select-zoom', function (event) {
1264
+ if (event.button !== 0) {
1265
+ return;
1266
+ }
1267
+ event.preventDefault();
1268
+ event.stopPropagation();
1269
+ var _a = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _a[0], mouseY = _a[1];
1270
+ state.zoomSelection = {
1271
+ active: true,
1272
+ startX: mouseX,
1273
+ startY: mouseY,
1274
+ currentX: mouseX,
1275
+ currentY: mouseY
1276
+ };
1277
+ drawZoomSelection(state);
1278
+ })
1279
+ .on('mousemove.kchart-select-zoom', function (event) {
1280
+ var _a;
1281
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
1282
+ return;
1283
+ }
1284
+ var _b = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _b[0], mouseY = _b[1];
1285
+ state.zoomSelection.currentX = mouseX;
1286
+ state.zoomSelection.currentY = mouseY;
1287
+ drawZoomSelection(state);
1288
+ })
1289
+ .on('mouseup.kchart-select-zoom', function (event) {
1290
+ var _a;
1291
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
1292
+ return;
1293
+ }
1294
+ var _b = (0, d3_selection_1.pointer)(event, overlayNode), mouseX = _b[0], mouseY = _b[1];
1295
+ state.zoomSelection.currentX = mouseX;
1296
+ state.zoomSelection.currentY = mouseY;
1297
+ applySelectionZoom(state);
1298
+ })
1299
+ .on('mouseleave.kchart-select-zoom', function () {
1300
+ var _a;
1301
+ if (!((_a = state.zoomSelection) === null || _a === void 0 ? void 0 : _a.active)) {
1302
+ return;
1303
+ }
1304
+ state.zoomSelection = undefined;
1305
+ drawZoomSelection(state);
1306
+ });
1307
+ }
1308
+ if ((zoomConfig === null || zoomConfig === void 0 ? void 0 : zoomConfig.resetOnDoubleClick) !== false) {
1309
+ overlay.on('dblclick.kchart-zoom-reset', function () { return resetZoom(state); });
1310
+ }
1311
+ };
812
1312
  var renderTooltip = function (state) {
813
1313
  var _a, _b;
814
1314
  var tooltipEnabled = ((_a = state.config.tooltip) === null || _a === void 0 ? void 0 : _a.visible) === true;
815
1315
  var cursorGuide = getCursorGuide(state);
816
1316
  var guideEnabled = (cursorGuide === null || cursorGuide === void 0 ? void 0 : cursorGuide.visible) === true;
817
- var enabled = tooltipEnabled || guideEnabled;
1317
+ var enabled = tooltipEnabled || guideEnabled || isZoomEnabled(state);
818
1318
  var overlay = state.layers.overlayGroup.selectAll('rect.kchart-tooltip-overlay')
819
1319
  .data(enabled ? [undefined] : [])
820
1320
  .join('rect')
@@ -1091,6 +1591,7 @@ var render = function (state) {
1091
1591
  renderSeries(state);
1092
1592
  renderLegend(state);
1093
1593
  renderTooltip(state);
1594
+ renderZoom(state);
1094
1595
  };
1095
1596
  var createCustomSeries = function (series) { return series; };
1096
1597
  exports.createCustomSeries = createCustomSeries;
@@ -1169,12 +1670,23 @@ var createCanvasLineSeries = function (configuration) { return (0, exports.creat
1169
1670
  color: configuration.color,
1170
1671
  downsample: configuration.downsample,
1171
1672
  render: function (_a) {
1172
- var _b, _c, _d;
1673
+ var _b, _c, _d, _e, _f;
1173
1674
  var getCanvas = _a.getCanvas, data = _a.data, xScale = _a.xScale, yScale = _a.yScale, color = _a.color;
1174
1675
  if (!xScale || !yScale) {
1175
1676
  return;
1176
1677
  }
1177
1678
  var canvas = getCanvas((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector);
1679
+ var canvasSize = resolveCanvasPixelSize(canvas);
1680
+ var points = resolveLinePoints(data, xScale, yScale, configuration.xField, configuration.yField);
1681
+ if (renderLineWithWorker(canvas, '2d', configuration.asyncRender, {
1682
+ width: canvasSize.width,
1683
+ height: canvasSize.height,
1684
+ color: (_c = configuration.color) !== null && _c !== void 0 ? _c : color,
1685
+ lineWidth: (_d = configuration.lineWidth) !== null && _d !== void 0 ? _d : 2,
1686
+ points: points
1687
+ })) {
1688
+ return;
1689
+ }
1178
1690
  var context = canvas.getContext('2d');
1179
1691
  if (!context) {
1180
1692
  return;
@@ -1184,8 +1696,8 @@ var createCanvasLineSeries = function (configuration) { return (0, exports.creat
1184
1696
  context.beginPath();
1185
1697
  context.lineCap = 'round';
1186
1698
  context.lineJoin = 'round';
1187
- context.lineWidth = (_c = configuration.lineWidth) !== null && _c !== void 0 ? _c : 2;
1188
- context.strokeStyle = (_d = configuration.color) !== null && _d !== void 0 ? _d : color;
1699
+ context.lineWidth = (_e = configuration.lineWidth) !== null && _e !== void 0 ? _e : 2;
1700
+ context.strokeStyle = (_f = configuration.color) !== null && _f !== void 0 ? _f : color;
1189
1701
  data.forEach(function (point) {
1190
1702
  if (point[configuration.xField] === undefined || point[configuration.yField] === undefined) {
1191
1703
  return;
@@ -1202,12 +1714,9 @@ var createCanvasLineSeries = function (configuration) { return (0, exports.creat
1202
1714
  context.stroke();
1203
1715
  },
1204
1716
  destroy: function (_a) {
1205
- var _b, _c;
1717
+ var _b;
1206
1718
  var svg = _a.svg;
1207
- var parent = (_b = svg.node()) === null || _b === void 0 ? void 0 : _b.parentElement;
1208
- if (parent) {
1209
- (0, d3_selection_1.select)(parent).selectAll("canvas.kchart-2d-canvas-".concat((_c = configuration.canvasName) !== null && _c !== void 0 ? _c : configuration.selector)).remove();
1210
- }
1719
+ destroyCanvasByClass(svg, "kchart-2d-canvas-".concat((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector));
1211
1720
  }
1212
1721
  }); };
1213
1722
  exports.createCanvasLineSeries = createCanvasLineSeries;
@@ -1247,12 +1756,9 @@ var createCanvasPointSeries = function (configuration) { return (0, exports.crea
1247
1756
  });
1248
1757
  },
1249
1758
  destroy: function (_a) {
1250
- var _b, _c;
1759
+ var _b;
1251
1760
  var svg = _a.svg;
1252
- var parent = (_b = svg.node()) === null || _b === void 0 ? void 0 : _b.parentElement;
1253
- if (parent) {
1254
- (0, d3_selection_1.select)(parent).selectAll("canvas.kchart-2d-canvas-".concat((_c = configuration.canvasName) !== null && _c !== void 0 ? _c : configuration.selector)).remove();
1255
- }
1761
+ destroyCanvasByClass(svg, "kchart-2d-canvas-".concat((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector));
1256
1762
  }
1257
1763
  }); };
1258
1764
  exports.createCanvasPointSeries = createCanvasPointSeries;
@@ -1279,49 +1785,36 @@ var createWebglPointSeries = function (configuration) { return (0, exports.creat
1279
1785
  if (!program) {
1280
1786
  return;
1281
1787
  }
1282
- var vertices = [];
1283
- var sizes = [];
1284
- data.forEach(function (point) {
1788
+ var _d = resolveWebglPointInterleavedData(data, xScale, yScale, configuration.xField, configuration.yField, canvas.width, canvas.height, function (point) {
1285
1789
  var _a;
1286
- if (point[configuration.xField] === undefined || point[configuration.yField] === undefined) {
1287
- return;
1288
- }
1289
- var x = resolveScalePosition(xScale, point[configuration.xField]);
1290
- var y = resolveScalePosition(yScale, point[configuration.yField]);
1291
- vertices.push((x / canvas.width) * 2 - 1, 1 - (y / canvas.height) * 2);
1292
- sizes.push(typeof configuration.pointSize === 'function'
1790
+ return typeof configuration.pointSize === 'function'
1293
1791
  ? configuration.pointSize(point)
1294
- : (_a = configuration.pointSize) !== null && _a !== void 0 ? _a : 8);
1295
- });
1792
+ : (_a = configuration.pointSize) !== null && _a !== void 0 ? _a : 8;
1793
+ }), buffer = _d.buffer, count = _d.count;
1296
1794
  gl.viewport(0, 0, canvas.width, canvas.height);
1297
1795
  gl.clearColor(0, 0, 0, 0);
1298
1796
  gl.clear(gl.COLOR_BUFFER_BIT);
1299
1797
  gl.useProgram(program);
1300
1798
  gl.enable(gl.BLEND);
1301
1799
  gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
1302
- var positionBuffer = gl.createBuffer();
1303
- gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
1304
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
1800
+ var interleavedBuffer = gl.createBuffer();
1801
+ gl.bindBuffer(gl.ARRAY_BUFFER, interleavedBuffer);
1802
+ gl.bufferData(gl.ARRAY_BUFFER, buffer, gl.STATIC_DRAW);
1803
+ var stride = 3 * Float32Array.BYTES_PER_ELEMENT;
1305
1804
  var positionLocation = gl.getAttribLocation(program, 'a_position');
1306
1805
  gl.enableVertexAttribArray(positionLocation);
1307
- gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
1308
- var sizeBuffer = gl.createBuffer();
1309
- gl.bindBuffer(gl.ARRAY_BUFFER, sizeBuffer);
1310
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(sizes), gl.STATIC_DRAW);
1806
+ gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, stride, 0);
1311
1807
  var sizeLocation = gl.getAttribLocation(program, 'a_size');
1312
1808
  gl.enableVertexAttribArray(sizeLocation);
1313
- gl.vertexAttribPointer(sizeLocation, 1, gl.FLOAT, false, 0, 0);
1809
+ gl.vertexAttribPointer(sizeLocation, 1, gl.FLOAT, false, stride, 2 * Float32Array.BYTES_PER_ELEMENT);
1314
1810
  var colorLocation = gl.getUniformLocation(program, 'u_color');
1315
1811
  gl.uniform4fv(colorLocation, new Float32Array(parseColor((_c = configuration.color) !== null && _c !== void 0 ? _c : color)));
1316
- gl.drawArrays(gl.POINTS, 0, sizes.length);
1812
+ gl.drawArrays(gl.POINTS, 0, count);
1317
1813
  },
1318
1814
  destroy: function (_a) {
1319
- var _b, _c;
1815
+ var _b;
1320
1816
  var svg = _a.svg;
1321
- var parent = (_b = svg.node()) === null || _b === void 0 ? void 0 : _b.parentElement;
1322
- if (parent) {
1323
- (0, d3_selection_1.select)(parent).selectAll("canvas.kchart-webgl-canvas-".concat((_c = configuration.canvasName) !== null && _c !== void 0 ? _c : configuration.selector)).remove();
1324
- }
1817
+ destroyCanvasByClass(svg, "kchart-webgl-canvas-".concat((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector));
1325
1818
  }
1326
1819
  }); };
1327
1820
  exports.createWebglPointSeries = createWebglPointSeries;
@@ -1333,12 +1826,23 @@ var createWebglLineSeries = function (configuration) { return (0, exports.create
1333
1826
  color: configuration.color,
1334
1827
  downsample: configuration.downsample,
1335
1828
  render: function (_a) {
1336
- var _b, _c, _d;
1829
+ var _b, _c, _d, _e, _f;
1337
1830
  var getWebglCanvas = _a.getWebglCanvas, data = _a.data, xScale = _a.xScale, yScale = _a.yScale, color = _a.color;
1338
1831
  if (!xScale || !yScale) {
1339
1832
  return;
1340
1833
  }
1341
1834
  var canvas = getWebglCanvas((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector);
1835
+ var canvasSize = resolveCanvasPixelSize(canvas);
1836
+ var points = resolveLinePoints(data, xScale, yScale, configuration.xField, configuration.yField);
1837
+ if (renderLineWithWorker(canvas, 'webgl', configuration.asyncRender, {
1838
+ width: canvasSize.width,
1839
+ height: canvasSize.height,
1840
+ color: (_c = configuration.color) !== null && _c !== void 0 ? _c : color,
1841
+ lineWidth: (_d = configuration.lineWidth) !== null && _d !== void 0 ? _d : 1,
1842
+ points: points
1843
+ })) {
1844
+ return;
1845
+ }
1342
1846
  var gl = canvas.getContext('webgl', { alpha: true });
1343
1847
  if (!gl) {
1344
1848
  return;
@@ -1349,39 +1853,28 @@ var createWebglLineSeries = function (configuration) { return (0, exports.create
1349
1853
  if (!program) {
1350
1854
  return;
1351
1855
  }
1352
- var vertices = [];
1353
- data.forEach(function (point) {
1354
- if (point[configuration.xField] === undefined || point[configuration.yField] === undefined) {
1355
- return;
1356
- }
1357
- var x = resolveScalePosition(xScale, point[configuration.xField]);
1358
- var y = resolveScalePosition(yScale, point[configuration.yField]);
1359
- vertices.push((x / canvas.width) * 2 - 1, 1 - (y / canvas.height) * 2);
1360
- });
1856
+ var vertices = resolveWebglLineVertices(points, canvas.width, canvas.height);
1361
1857
  gl.viewport(0, 0, canvas.width, canvas.height);
1362
1858
  gl.clearColor(0, 0, 0, 0);
1363
1859
  gl.clear(gl.COLOR_BUFFER_BIT);
1364
1860
  gl.useProgram(program);
1365
1861
  gl.enable(gl.BLEND);
1366
1862
  gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
1367
- gl.lineWidth((_c = configuration.lineWidth) !== null && _c !== void 0 ? _c : 1);
1863
+ gl.lineWidth((_e = configuration.lineWidth) !== null && _e !== void 0 ? _e : 1);
1368
1864
  var positionBuffer = gl.createBuffer();
1369
1865
  gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
1370
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
1866
+ gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
1371
1867
  var positionLocation = gl.getAttribLocation(program, 'a_position');
1372
1868
  gl.enableVertexAttribArray(positionLocation);
1373
1869
  gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
1374
1870
  var colorLocation = gl.getUniformLocation(program, 'u_color');
1375
- gl.uniform4fv(colorLocation, new Float32Array(parseColor((_d = configuration.color) !== null && _d !== void 0 ? _d : color)));
1871
+ gl.uniform4fv(colorLocation, new Float32Array(parseColor((_f = configuration.color) !== null && _f !== void 0 ? _f : color)));
1376
1872
  gl.drawArrays(gl.LINE_STRIP, 0, vertices.length / 2);
1377
1873
  },
1378
1874
  destroy: function (_a) {
1379
- var _b, _c;
1875
+ var _b;
1380
1876
  var svg = _a.svg;
1381
- var parent = (_b = svg.node()) === null || _b === void 0 ? void 0 : _b.parentElement;
1382
- if (parent) {
1383
- (0, d3_selection_1.select)(parent).selectAll("canvas.kchart-webgl-canvas-".concat((_c = configuration.canvasName) !== null && _c !== void 0 ? _c : configuration.selector)).remove();
1384
- }
1877
+ destroyCanvasByClass(svg, "kchart-webgl-canvas-".concat((_b = configuration.canvasName) !== null && _b !== void 0 ? _b : configuration.selector));
1385
1878
  }
1386
1879
  }); };
1387
1880
  exports.createWebglLineSeries = createWebglLineSeries;
@@ -1394,7 +1887,9 @@ var createKChart = function (config) {
1394
1887
  config: config,
1395
1888
  container: container,
1396
1889
  data: config.data,
1397
- axes: config.axes,
1890
+ axes: cloneAxes(config.axes),
1891
+ initialAxes: cloneAxes(config.axes),
1892
+ baseAxes: cloneAxes(config.axes),
1398
1893
  series: config.series,
1399
1894
  size: sizes.size,
1400
1895
  plotSize: sizes.plotSize,
@@ -1402,7 +1897,8 @@ var createKChart = function (config) {
1402
1897
  colors: (_a = config.colors) !== null && _a !== void 0 ? _a : d3_scale_chromatic_1.schemeCategory10.slice(),
1403
1898
  scales: [],
1404
1899
  layers: undefined,
1405
- hiddenSeries: new Set()
1900
+ hiddenSeries: new Set(),
1901
+ zoomTransform: d3_zoom_1.zoomIdentity
1406
1902
  };
1407
1903
  state.layers = createLayers(container, config, function () { return state.size; }, function () { return state.margin; });
1408
1904
  var controller = {
@@ -1412,10 +1908,15 @@ var createKChart = function (config) {
1412
1908
  },
1413
1909
  updateData: function (data) {
1414
1910
  state.data = data;
1911
+ state.config = __assign(__assign({}, state.config), { data: data });
1415
1912
  return controller.render();
1416
1913
  },
1417
1914
  updateAxes: function (axes) {
1418
- state.axes = axes;
1915
+ state.axes = cloneAxes(axes);
1916
+ state.initialAxes = cloneAxes(axes);
1917
+ state.baseAxes = cloneAxes(axes);
1918
+ state.zoomTransform = d3_zoom_1.zoomIdentity;
1919
+ state.config = __assign(__assign({}, state.config), { axes: state.axes });
1419
1920
  return controller.render();
1420
1921
  },
1421
1922
  updateSeries: function (series) {