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