@datarailsshared/dr_renderer 1.2.454 → 1.2.455
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/package.json +1 -1
- package/src/charts/dr_gauge_chart.js +131 -118
- package/src/dr_chart_tooltip.js +28 -30
- package/src/highcharts_renderer.js +1 -2
- package/tests/dr_gauge_chart.test.js +9 -10
- package/src/charts/dr_chart.js +0 -34
package/package.json
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
const { DrChart } = require("./dr_chart");
|
2
1
|
const { DrChartTooltip } = require("../dr_chart_tooltip");
|
3
2
|
const helpers = require("../dr-renderer-helpers");
|
4
3
|
|
5
|
-
|
4
|
+
const GAUGE_OPTIONS_DEFAULT = {
|
6
5
|
gauge: {
|
7
6
|
background: "#fff",
|
8
7
|
startAngle: -90,
|
@@ -51,39 +50,40 @@ export const GAUGE_OPTIONS_DEFAULT = {
|
|
51
50
|
],
|
52
51
|
};
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
53
|
+
function DrGaugeChart(pivotData, opts) {
|
54
|
+
this.render = function () {
|
55
|
+
return DrGaugeChart.highchartsRenderer.ptCreateElementAndDraw(this.configChart(), opts);
|
56
|
+
};
|
57
|
+
|
58
|
+
this.formatValue = function (data_type, number_format, value, widget_values_format) {
|
59
|
+
return DrGaugeChart.highchartsRenderer.formatValue(data_type, number_format, value, widget_values_format);
|
60
|
+
};
|
61
|
+
|
62
|
+
this.getDefaultValueForChart = function (type, existing_options) {
|
63
|
+
return DrGaugeChart.highchartsRenderer.getDefaultValueForChart(type, existing_options);
|
64
|
+
};
|
65
|
+
|
66
|
+
this.ptCreateBasicLineSeries = function (pivotData, colors, onlyNumbers, isUniqueVals, additionOptions, opts, chartOptions) {
|
67
|
+
return DrGaugeChart.highchartsRenderer.ptCreateBasicLineSeries(
|
68
|
+
pivotData,
|
69
|
+
colors,
|
70
|
+
onlyNumbers,
|
71
|
+
isUniqueVals,
|
72
|
+
additionOptions,
|
73
|
+
opts,
|
74
|
+
chartOptions
|
75
|
+
);
|
76
|
+
};
|
77
|
+
|
78
|
+
this.getSingleValueAgg = function (opts, aggfunc, base) {
|
79
|
+
return DrGaugeChart.highchartsRenderer.getSingleValueAgg(opts, aggfunc, base);
|
80
|
+
};
|
81
|
+
|
82
|
+
this.isLeftQuarter = function (value, max = this.max) {
|
83
83
|
return value < max / 2;
|
84
|
-
}
|
84
|
+
};
|
85
85
|
|
86
|
-
createPlotBands(options) {
|
86
|
+
this.createPlotBands = function (options) {
|
87
87
|
const {
|
88
88
|
segments,
|
89
89
|
isAbsoluteValue,
|
@@ -106,41 +106,41 @@ export class DrGaugeChart extends DrChart {
|
|
106
106
|
bands[bands.length - 1].to = Math.max(bands[bands.length - 1].to, goalValue);
|
107
107
|
|
108
108
|
return bands;
|
109
|
-
}
|
109
|
+
};
|
110
110
|
|
111
|
-
createTicks(plotBands, options) {
|
111
|
+
this.createTicks = function (plotBands, options) {
|
112
112
|
return [...new Set([0, ...plotBands.map((b) => b.to), options.goal.value])].sort((a, b) => a - b);
|
113
|
-
}
|
113
|
+
};
|
114
114
|
|
115
|
-
mergeOptions(options) {
|
115
|
+
this.mergeOptions = function (options) {
|
116
116
|
return helpers.mergeDeep(
|
117
117
|
JSON.parse(JSON.stringify(GAUGE_OPTIONS_DEFAULT)),
|
118
|
-
this.getDefaultValueForChart(
|
118
|
+
this.getDefaultValueForChart(DrGaugeChart.highchartsRenderer.CHART_TYPES.GAUGE_CHART_ENHANCED),
|
119
119
|
options
|
120
120
|
);
|
121
|
-
}
|
121
|
+
};
|
122
122
|
|
123
|
-
getAngleForValue(
|
123
|
+
this.getAngleForValue = function (
|
124
124
|
value,
|
125
125
|
min = 0,
|
126
|
-
max = this
|
127
|
-
startAngle = this
|
128
|
-
endAngle = this
|
126
|
+
max = this.max,
|
127
|
+
startAngle = this.options.gauge.startAngle,
|
128
|
+
endAngle = this.options.gauge.endAngle
|
129
129
|
) {
|
130
130
|
const degrees = ((value - min) / (max - min)) * (endAngle - startAngle);
|
131
131
|
const radians = degrees * (Math.PI / 180);
|
132
132
|
return radians;
|
133
|
-
}
|
133
|
+
};
|
134
134
|
|
135
|
-
formatValue(value, format = this
|
136
|
-
return helpers.isNumber(value) ?
|
137
|
-
}
|
135
|
+
this.formatValue = function (value, format = this.format) {
|
136
|
+
return helpers.isNumber(value) ? DrGaugeChart.highchartsRenderer.formatValue("n", format, value).value : value;
|
137
|
+
};
|
138
138
|
|
139
|
-
toPercent(value) {
|
140
|
-
return `${Math.round((value * 100) / (this
|
141
|
-
}
|
139
|
+
this.toPercent = function (value) {
|
140
|
+
return `${Math.round((value * 100) / (this.options.isAbsoluteValue ? this.max : this.goal.value))}%`;
|
141
|
+
};
|
142
142
|
|
143
|
-
formatValueLabel(value, options) {
|
143
|
+
this.formatValueLabel = function (value, options) {
|
144
144
|
return `<span style="display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: ${
|
145
145
|
options.label.font_size
|
146
146
|
}px;">
|
@@ -158,9 +158,9 @@ export class DrGaugeChart extends DrChart {
|
|
158
158
|
options.gauge.colors.meta
|
159
159
|
};">Current status</span>
|
160
160
|
</span>`;
|
161
|
-
}
|
161
|
+
};
|
162
162
|
|
163
|
-
formatTickLabel(value, options) {
|
163
|
+
this.formatTickLabel = function (value, options) {
|
164
164
|
const isGoal = value === options.goal.value;
|
165
165
|
const isLeftQuarter = this.isLeftQuarter(value);
|
166
166
|
const formattedValue = this.formatValue(value);
|
@@ -191,9 +191,9 @@ export class DrGaugeChart extends DrChart {
|
|
191
191
|
${goalTitle}
|
192
192
|
${percentage}
|
193
193
|
</span>`;
|
194
|
-
}
|
194
|
+
};
|
195
195
|
|
196
|
-
getValue(pivotData, opts) {
|
196
|
+
this.getValue = function (pivotData, opts) {
|
197
197
|
const lineSeries = this.ptCreateBasicLineSeries(pivotData, null, true, null, null, opts, {});
|
198
198
|
|
199
199
|
let total = lineSeries
|
@@ -220,9 +220,9 @@ export class DrGaugeChart extends DrChart {
|
|
220
220
|
const value = total.length > 0 ? total.reduce(aggfunc, base) : aggregator.value();
|
221
221
|
|
222
222
|
return value;
|
223
|
-
}
|
223
|
+
};
|
224
224
|
|
225
|
-
getBorderPosition(chart, options, position) {
|
225
|
+
this.getBorderPosition = function (chart, options, position) {
|
226
226
|
const { center, size } = chart.pane[0].options;
|
227
227
|
const {
|
228
228
|
gauge: { tickLength, tickWidth },
|
@@ -232,17 +232,18 @@ export class DrGaugeChart extends DrChart {
|
|
232
232
|
x: position === "start" ? center[0] - size / 2 + tickLength / 2 : center[0] + size / 2 - tickLength / 2,
|
233
233
|
y: center[1] + tickWidth / 2,
|
234
234
|
};
|
235
|
-
}
|
235
|
+
};
|
236
236
|
|
237
|
-
createBorder(chart, options, position) {
|
237
|
+
this.createBorder = function (chart, options, position) {
|
238
238
|
return chart.renderer
|
239
|
-
.arc(
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
239
|
+
.arc(
|
240
|
+
Object.assign(this.getBorderPosition(chart, options, position), {
|
241
|
+
r: options.gauge.thickness / 2,
|
242
|
+
innerR: 0,
|
243
|
+
start: 0,
|
244
|
+
end: Math.PI,
|
245
|
+
})
|
246
|
+
)
|
246
247
|
.attr({
|
247
248
|
fill:
|
248
249
|
position === "start"
|
@@ -251,9 +252,9 @@ export class DrGaugeChart extends DrChart {
|
|
251
252
|
})
|
252
253
|
.add()
|
253
254
|
.toFront();
|
254
|
-
}
|
255
|
+
};
|
255
256
|
|
256
|
-
getGoalIconPosition(chart, options) {
|
257
|
+
this.getGoalIconPosition = function (chart, options) {
|
257
258
|
const { center, size } = chart.pane[0].options;
|
258
259
|
const radius = size / 2;
|
259
260
|
const {
|
@@ -265,26 +266,26 @@ export class DrGaugeChart extends DrChart {
|
|
265
266
|
x: center[0] - radius * Math.sin(Math.PI / 2 - this.getAngleForValue(value)) - goalIconSize[0] / 2,
|
266
267
|
y: center[1] - radius * Math.cos(Math.PI / 2 - this.getAngleForValue(value)) - goalIconSize[1] / 2,
|
267
268
|
};
|
268
|
-
}
|
269
|
+
};
|
269
270
|
|
270
|
-
createGoalIcon(chart, options) {
|
271
|
+
this.createGoalIcon = function (chart, options) {
|
271
272
|
const point = this.getGoalIconPosition(chart, options);
|
272
273
|
return chart.renderer
|
273
274
|
.image(options.gauge.goalIcon, point.x, point.y, options.gauge.goalIconSize[0], options.gauge.goalIconSize[1])
|
274
275
|
.add()
|
275
276
|
.toFront();
|
276
|
-
}
|
277
|
+
};
|
277
278
|
|
278
|
-
getValueLabelPosition(chart, options) {
|
279
|
+
this.getValueLabelPosition = function (chart, options) {
|
279
280
|
const { center } = chart.pane[0].options;
|
280
281
|
return {
|
281
282
|
x: center[0],
|
282
283
|
y: center[1] + options.gauge.valueOffset[0],
|
283
284
|
};
|
284
|
-
}
|
285
|
+
};
|
285
286
|
|
286
|
-
createValueLabel(chart, options) {
|
287
|
-
const label = chart.renderer.text(this.formatValueLabel(this
|
287
|
+
this.createValueLabel = function (chart, options) {
|
288
|
+
const label = chart.renderer.text(this.formatValueLabel(this.value, options), 0, 0, true).add().toFront();
|
288
289
|
|
289
290
|
helpers.removeSVGTextCorrection(label, "yCorr");
|
290
291
|
|
@@ -293,11 +294,11 @@ export class DrGaugeChart extends DrChart {
|
|
293
294
|
});
|
294
295
|
|
295
296
|
return label;
|
296
|
-
}
|
297
|
+
};
|
297
298
|
|
298
|
-
getPaneDimensions(chart, options) {
|
299
|
+
this.getPaneDimensions = function (chart, options) {
|
299
300
|
const { renderer } = chart;
|
300
|
-
const valueLabel = this.createValueLabel(chart, this
|
301
|
+
const valueLabel = this.createValueLabel(chart, this.options);
|
301
302
|
const { offset } = options.gauge;
|
302
303
|
const { height: labelH } = valueLabel.getBBox();
|
303
304
|
|
@@ -306,7 +307,7 @@ export class DrGaugeChart extends DrChart {
|
|
306
307
|
|
307
308
|
const radiuses = [chart.chartWidth / 2 - Math.max(offset[1], offset[3]), chart.chartHeight - offsetBottom - offset[0]];
|
308
309
|
if (options.label.show) {
|
309
|
-
this
|
310
|
+
this.ticks.forEach((tick) => {
|
310
311
|
const label = renderer.label(this.formatTickLabel(tick, options), 0, 0, null, null, null, true).add();
|
311
312
|
const angle = this.getAngleForValue(tick);
|
312
313
|
// depends on label width
|
@@ -333,9 +334,9 @@ export class DrGaugeChart extends DrChart {
|
|
333
334
|
radius: Math.min(...radiuses),
|
334
335
|
center: [chart.chartWidth / 2, chart.chartHeight - offsetBottom],
|
335
336
|
};
|
336
|
-
}
|
337
|
+
};
|
337
338
|
|
338
|
-
setTicksStyles(chart, options) {
|
339
|
+
this.setTicksStyles = function (chart, options) {
|
339
340
|
Object.keys(chart.yAxis[0].ticks).forEach((i) => {
|
340
341
|
const tick = chart.yAxis[0].ticks[i];
|
341
342
|
const isLeftQuarter = this.isLeftQuarter(tick.pos);
|
@@ -355,9 +356,9 @@ export class DrGaugeChart extends DrChart {
|
|
355
356
|
transform: `translate(${isLeftQuarter ? "-100%" : 0}, 0)`,
|
356
357
|
});
|
357
358
|
});
|
358
|
-
}
|
359
|
+
};
|
359
360
|
|
360
|
-
addTooltips(chart, options) {
|
361
|
+
this.addTooltips = function (chart, options) {
|
361
362
|
if (!options.tooltips.show) {
|
362
363
|
return false;
|
363
364
|
}
|
@@ -380,7 +381,7 @@ export class DrGaugeChart extends DrChart {
|
|
380
381
|
|
381
382
|
// value label tooltip
|
382
383
|
if (options.tooltips.show_percentage_in_value && !options.label.show_percentage_in_value) {
|
383
|
-
drTooltip.add(this.toPercent(this
|
384
|
+
drTooltip.add(this.toPercent(this.value), chart.label.element, {
|
384
385
|
direction: "top",
|
385
386
|
});
|
386
387
|
}
|
@@ -410,43 +411,43 @@ export class DrGaugeChart extends DrChart {
|
|
410
411
|
});
|
411
412
|
}
|
412
413
|
});
|
413
|
-
}
|
414
|
+
};
|
414
415
|
|
415
|
-
setPane(chart, options) {
|
416
|
+
this.setPane = function (chart, options) {
|
416
417
|
const { radius, center } = this.getPaneDimensions(chart, options);
|
417
418
|
chart.pane[0].options.size = 2 * radius;
|
418
419
|
chart.pane[0].options.center = center;
|
419
420
|
chart.yAxis[0].options.plotBands.forEach((band) => {
|
420
421
|
band.outerRadius = radius - (options.gauge.tickLength - options.gauge.thickness) / 2;
|
421
422
|
});
|
422
|
-
chart.series[0].options.dial.radius = Math.round(100 * (radius - options.gauge.tickLength - 10) / radius) +
|
423
|
-
}
|
423
|
+
chart.series[0].options.dial.radius = Math.round((100 * (radius - options.gauge.tickLength - 10)) / radius) + "%";
|
424
|
+
};
|
424
425
|
|
425
|
-
setCustomElements(chart, options) {
|
426
|
+
this.setCustomElements = function (chart, options) {
|
426
427
|
chart.label = this.createValueLabel(chart, options);
|
427
428
|
chart.startBorder = this.createBorder(chart, options, "start");
|
428
429
|
chart.endBorder = this.createBorder(chart, options, "end");
|
429
430
|
chart.goalIcon = this.createGoalIcon(chart, options);
|
430
|
-
}
|
431
|
+
};
|
431
432
|
|
432
|
-
updateCustomElements(chart, options) {
|
433
|
+
this.updateCustomElements = function (chart, options) {
|
433
434
|
chart.startBorder.attr(this.getBorderPosition(chart, options, "start"));
|
434
435
|
chart.endBorder.attr(this.getBorderPosition(chart, options, "end"));
|
435
436
|
chart.goalIcon.attr(this.getGoalIconPosition(chart, options));
|
436
437
|
chart.label.attr(this.getValueLabelPosition(chart, options));
|
437
|
-
}
|
438
|
+
};
|
438
439
|
|
439
|
-
moveCustomElementsToFront(chart) {
|
440
|
+
this.moveCustomElementsToFront = function (chart) {
|
440
441
|
chart.startBorder.toFront();
|
441
442
|
chart.endBorder.toFront();
|
442
443
|
chart.goalIcon.toFront();
|
443
|
-
}
|
444
|
+
};
|
444
445
|
|
445
|
-
clampValueToPane(value, max = this
|
446
|
+
this.clampValueToPane = function (value, max = this.max) {
|
446
447
|
return helpers.clamp(-0.02 * max, value, 1.02 * max);
|
447
|
-
}
|
448
|
+
};
|
448
449
|
|
449
|
-
configChart() {
|
450
|
+
this.configChart = function () {
|
450
451
|
return {
|
451
452
|
title: {
|
452
453
|
text: null,
|
@@ -460,7 +461,7 @@ export class DrGaugeChart extends DrChart {
|
|
460
461
|
},
|
461
462
|
chart: {
|
462
463
|
type: "gauge",
|
463
|
-
backgroundColor: this
|
464
|
+
backgroundColor: this.options.gauge.background,
|
464
465
|
plotBackgroundColor: null,
|
465
466
|
plotBackgroundImage: null,
|
466
467
|
plotBorderWidth: 0,
|
@@ -468,18 +469,18 @@ export class DrGaugeChart extends DrChart {
|
|
468
469
|
events: {
|
469
470
|
render: ({ target: chart }) => {
|
470
471
|
this.moveCustomElementsToFront(chart);
|
471
|
-
this.setTicksStyles(chart, this
|
472
|
+
this.setTicksStyles(chart, this.options);
|
472
473
|
},
|
473
474
|
beforeRedraw: ({ target: chart }) => {
|
474
|
-
this.setPane(chart, this
|
475
|
-
this.updateCustomElements(chart, this
|
475
|
+
this.setPane(chart, this.options);
|
476
|
+
this.updateCustomElements(chart, this.options);
|
476
477
|
},
|
477
478
|
beforeRender: ({ target: chart }) => {
|
478
|
-
this.setPane(chart, this
|
479
|
-
this.setCustomElements(chart, this
|
479
|
+
this.setPane(chart, this.options);
|
480
|
+
this.setCustomElements(chart, this.options);
|
480
481
|
},
|
481
482
|
load: ({ target: chart }) => {
|
482
|
-
this.addTooltips(chart, this
|
483
|
+
this.addTooltips(chart, this.options);
|
483
484
|
},
|
484
485
|
},
|
485
486
|
margin: [0, 0, 0, 0],
|
@@ -496,16 +497,16 @@ export class DrGaugeChart extends DrChart {
|
|
496
497
|
// the value axis
|
497
498
|
yAxis: {
|
498
499
|
min: 0,
|
499
|
-
max: this
|
500
|
-
tickPositions: this
|
500
|
+
max: this.max,
|
501
|
+
tickPositions: this.ticks,
|
501
502
|
tickPosition: "inside",
|
502
|
-
tickColor: this
|
503
|
-
tickLength: this
|
504
|
-
tickWidth: this
|
503
|
+
tickColor: this.options.gauge.background,
|
504
|
+
tickLength: this.options.gauge.tickLength,
|
505
|
+
tickWidth: this.options.gauge.tickWidth,
|
505
506
|
minorTickInterval: null,
|
506
507
|
|
507
508
|
labels: {
|
508
|
-
enabled: !!this
|
509
|
+
enabled: !!this.options.label.show,
|
509
510
|
distance: 0,
|
510
511
|
verticalAlign: "middle",
|
511
512
|
allowOverlap: true,
|
@@ -515,18 +516,18 @@ export class DrGaugeChart extends DrChart {
|
|
515
516
|
width: "auto",
|
516
517
|
},
|
517
518
|
formatter: ({ value }) => {
|
518
|
-
return this.formatTickLabel(value, this
|
519
|
+
return this.formatTickLabel(value, this.options);
|
519
520
|
},
|
520
521
|
useHTML: true,
|
521
522
|
},
|
522
523
|
lineWidth: 0,
|
523
|
-
plotBands: this
|
524
|
+
plotBands: this.plotBands,
|
524
525
|
},
|
525
526
|
|
526
527
|
series: [
|
527
528
|
{
|
528
529
|
name: null,
|
529
|
-
data: [this.clampValueToPane(this
|
530
|
+
data: [this.clampValueToPane(this.value)],
|
530
531
|
dataLabels: [
|
531
532
|
{
|
532
533
|
enabled: false,
|
@@ -534,17 +535,29 @@ export class DrGaugeChart extends DrChart {
|
|
534
535
|
],
|
535
536
|
dial: {
|
536
537
|
radius: "70%",
|
537
|
-
backgroundColor: this
|
538
|
-
baseWidth: 2 * this
|
538
|
+
backgroundColor: this.options.gauge.pivot.color,
|
539
|
+
baseWidth: 2 * this.options.gauge.pivot.radius,
|
539
540
|
baseLength: "0%",
|
540
541
|
rearLength: "0%",
|
541
542
|
},
|
542
543
|
pivot: {
|
543
|
-
backgroundColor: this
|
544
|
-
radius: this
|
544
|
+
backgroundColor: this.options.gauge.pivot.color,
|
545
|
+
radius: this.options.gauge.pivot.radius,
|
545
546
|
},
|
546
547
|
},
|
547
548
|
],
|
548
549
|
};
|
549
|
-
}
|
550
|
+
};
|
551
|
+
|
552
|
+
this.originalOptions = opts;
|
553
|
+
this.options = this.mergeOptions(opts.chartOptions);
|
554
|
+
this.aggregation = pivotData.getAggregator([], []);
|
555
|
+
this.format = this.aggregation.widget_values_format;
|
556
|
+
this.goal = this.options.goal;
|
557
|
+
this.plotBands = this.createPlotBands(this.options);
|
558
|
+
this.ticks = this.createTicks(this.plotBands, this.options);
|
559
|
+
this.value = this.getValue(pivotData, opts);
|
560
|
+
this.max = this.ticks[this.ticks.length - 1];
|
550
561
|
}
|
562
|
+
|
563
|
+
module.exports = { DrGaugeChart, GAUGE_OPTIONS_DEFAULT };
|
package/src/dr_chart_tooltip.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
const helpers = require("./dr-renderer-helpers");
|
2
2
|
|
3
|
-
|
3
|
+
const DR_TOOLTIP_OPTIONS_DEFAULT = {
|
4
4
|
anchorOffset: 10,
|
5
5
|
arrowRadius: 3,
|
6
6
|
arrowSize: 10,
|
@@ -16,21 +16,13 @@ export const DR_TOOLTIP_OPTIONS_DEFAULT = {
|
|
16
16
|
showArrow: true,
|
17
17
|
};
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
constructor(chart, options = {}) {
|
24
|
-
this.#chart = chart;
|
25
|
-
this.#options = { ...DR_TOOLTIP_OPTIONS_DEFAULT, ...options };
|
26
|
-
}
|
27
|
-
|
28
|
-
getOptions() {
|
29
|
-
return this.#options;
|
19
|
+
function DrChartTooltip(chart, options) {
|
20
|
+
this.getOptions = function() {
|
21
|
+
return this.options;
|
30
22
|
}
|
31
23
|
|
32
|
-
add(content, anchor, options = {}) {
|
33
|
-
const localOptions = {
|
24
|
+
this.add = function(content, anchor, options = {}) {
|
25
|
+
const localOptions = Object.assign({}, this.options, options );
|
34
26
|
let tooltip;
|
35
27
|
|
36
28
|
if (!content || !anchor) {
|
@@ -64,8 +56,8 @@ export class DrChartTooltip {
|
|
64
56
|
}
|
65
57
|
}
|
66
58
|
|
67
|
-
create(content, anchor, options) {
|
68
|
-
const tooltip = this
|
59
|
+
this.create = function(content, anchor, options) {
|
60
|
+
const tooltip = this.chart.renderer
|
69
61
|
.text(`${content}${options.showArrow ? `<span class="dr-renderer-tooltip_arrow" style="position: absolute"></span>` : ""}`, 0, 0, true)
|
70
62
|
.attr({
|
71
63
|
class: "dr-renderer-tooltip",
|
@@ -93,7 +85,7 @@ export class DrChartTooltip {
|
|
93
85
|
return tooltip;
|
94
86
|
}
|
95
87
|
|
96
|
-
setTooltipPosition(tooltip, anchor, options) {
|
88
|
+
this.setTooltipPosition = function(tooltip, anchor, options) {
|
97
89
|
const position = this.getPosition(tooltip, anchor, options);
|
98
90
|
|
99
91
|
tooltip.attr({
|
@@ -107,7 +99,7 @@ export class DrChartTooltip {
|
|
107
99
|
}
|
108
100
|
}
|
109
101
|
|
110
|
-
setArrowPosition(tooltip, position, anchor, options) {
|
102
|
+
this.setArrowPosition = function(tooltip, position, anchor, options) {
|
111
103
|
const arrowEl = tooltip.element.querySelector(".dr-renderer-tooltip_arrow");
|
112
104
|
const anchorBox = this.getAnchorBox(anchor);
|
113
105
|
const tooltipBox = tooltip.getBBox();
|
@@ -190,7 +182,7 @@ export class DrChartTooltip {
|
|
190
182
|
);
|
191
183
|
}
|
192
184
|
|
193
|
-
getCoords(direction, tooltip, anchor, options) {
|
185
|
+
this.getCoords = function(direction, tooltip, anchor, options) {
|
194
186
|
const tooltipBox = tooltip.getBBox();
|
195
187
|
const anchorBox = this.getAnchorBox(anchor);
|
196
188
|
switch (direction) {
|
@@ -199,7 +191,7 @@ export class DrChartTooltip {
|
|
199
191
|
x: helpers.clamp(
|
200
192
|
options.outerOffset,
|
201
193
|
anchorBox.x + anchorBox.width / 2 - tooltipBox.width / 2,
|
202
|
-
this
|
194
|
+
this.chart.chartWidth - tooltipBox.width - options.outerOffset
|
203
195
|
),
|
204
196
|
y: anchorBox.y - tooltipBox.height - options.anchorOffset,
|
205
197
|
};
|
@@ -208,7 +200,7 @@ export class DrChartTooltip {
|
|
208
200
|
x: helpers.clamp(
|
209
201
|
options.outerOffset,
|
210
202
|
anchorBox.x + anchorBox.width / 2 - tooltipBox.width / 2,
|
211
|
-
this
|
203
|
+
this.chart.chartWidth - tooltipBox.width - options.outerOffset
|
212
204
|
),
|
213
205
|
y: anchorBox.y + anchorBox.height + options.anchorOffset,
|
214
206
|
};
|
@@ -218,7 +210,7 @@ export class DrChartTooltip {
|
|
218
210
|
y: helpers.clamp(
|
219
211
|
options.outerOffset,
|
220
212
|
anchorBox.y + anchorBox.height / 2 - tooltipBox.height / 2,
|
221
|
-
this
|
213
|
+
this.chart.chartHeight - tooltipBox.height - options.outerOffset
|
222
214
|
),
|
223
215
|
};
|
224
216
|
case "right":
|
@@ -227,18 +219,18 @@ export class DrChartTooltip {
|
|
227
219
|
y: helpers.clamp(
|
228
220
|
options.outerOffset,
|
229
221
|
anchorBox.y + anchorBox.height / 2 - tooltipBox.height / 2,
|
230
|
-
this
|
222
|
+
this.chart.chartHeight - tooltipBox.height - options.outerOffset
|
231
223
|
),
|
232
224
|
};
|
233
225
|
}
|
234
226
|
}
|
235
227
|
|
236
|
-
getAnchorBox(el) {
|
228
|
+
this.getAnchorBox = function(el) {
|
237
229
|
if (el.getBBox) {
|
238
230
|
return el.getBBox();
|
239
231
|
}
|
240
232
|
|
241
|
-
const containerRect = this
|
233
|
+
const containerRect = this.chart.container.getBoundingClientRect();
|
242
234
|
|
243
235
|
if (el.getBoundingClientRect) {
|
244
236
|
const elRect = el.getBoundingClientRect();
|
@@ -257,7 +249,7 @@ export class DrChartTooltip {
|
|
257
249
|
};
|
258
250
|
}
|
259
251
|
|
260
|
-
getPosition(tooltip, anchor, options) {
|
252
|
+
this.getPosition = function(tooltip, anchor, options) {
|
261
253
|
const bbox = tooltip.getBBox();
|
262
254
|
|
263
255
|
for (let direction of [options.direction, ...["top", "right", "bottom", "left"].filter((d) => d !== options.direction)]) {
|
@@ -265,15 +257,21 @@ export class DrChartTooltip {
|
|
265
257
|
|
266
258
|
if (
|
267
259
|
coords.x >= 0 &&
|
268
|
-
coords.x <= this
|
260
|
+
coords.x <= this.chart.chartWidth - bbox.width &&
|
269
261
|
coords.y >= 0 &&
|
270
|
-
coords.y <= this
|
262
|
+
coords.y <= this.chart.chartHeight - bbox.height
|
271
263
|
) {
|
272
264
|
return {
|
273
|
-
|
274
|
-
|
265
|
+
x: coords.x,
|
266
|
+
y: coords.y,
|
267
|
+
direction: direction,
|
275
268
|
};
|
276
269
|
}
|
277
270
|
}
|
278
271
|
}
|
272
|
+
|
273
|
+
this.chart = chart;
|
274
|
+
this.options = Object.assign({}, DR_TOOLTIP_OPTIONS_DEFAULT, options);
|
279
275
|
}
|
276
|
+
|
277
|
+
module.exports = { DrChartTooltip, DR_TOOLTIP_OPTIONS_DEFAULT };
|
@@ -1,5 +1,4 @@
|
|
1
1
|
const helpers = require('./dr-renderer-helpers');
|
2
|
-
const { DrChart } = require('./charts/dr_chart');
|
3
2
|
const { DrGaugeChart, GAUGE_OPTIONS_DEFAULT } = require('./charts/dr_gauge_chart');
|
4
3
|
const seriesPointStylesHelper= require('./seriesPointStyles-helper');
|
5
4
|
|
@@ -198,7 +197,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
|
|
198
197
|
if (!highchartsRenderer) {
|
199
198
|
highchartsRenderer = {};
|
200
199
|
}
|
201
|
-
|
200
|
+
DrGaugeChart.highchartsRenderer = highchartsRenderer;
|
202
201
|
lodash.assign(highchartsRenderer, HIGHCHARTS_CONSTANTS);
|
203
202
|
|
204
203
|
highchartsRenderer.enabledNewWidgetValueFormatting = false;
|
@@ -1,5 +1,4 @@
|
|
1
1
|
const { DrGaugeChart, GAUGE_OPTIONS_DEFAULT } = require("../src/charts/dr_gauge_chart");
|
2
|
-
const { DrChart } = require("../src/charts/dr_chart");
|
3
2
|
const helpers = require("../src/dr-renderer-helpers");
|
4
3
|
const { DrChartTooltip } = require("../src/dr_chart_tooltip");
|
5
4
|
|
@@ -7,7 +6,7 @@ jest.mock("../src/dr_chart_tooltip"); // Mock the tooltip class
|
|
7
6
|
|
8
7
|
const mockFormattedValue = "16,549";
|
9
8
|
|
10
|
-
|
9
|
+
DrGaugeChart.highchartsRenderer = {
|
11
10
|
CHART_TYPES: {
|
12
11
|
GAUGE_CHART_ENHANCE: "gauge-chart-enhanced",
|
13
12
|
},
|
@@ -401,21 +400,20 @@ describe("DrGaugeChart", () => {
|
|
401
400
|
describe("formatValue", () => {
|
402
401
|
it("should format number to provided format", () => {
|
403
402
|
expect(chart.formatValue(16549.1234, "#,###")).toBe(mockFormattedValue);
|
404
|
-
expect(
|
403
|
+
expect(DrGaugeChart.highchartsRenderer.formatValue).toHaveBeenCalledWith("n", "#,###", 16549.1234);
|
405
404
|
});
|
406
405
|
|
407
406
|
it("should not format value unless it is a number", () => {
|
408
407
|
expect(chart.formatValue("one", "#,###")).toBe("one");
|
409
|
-
expect(
|
408
|
+
expect(DrGaugeChart.highchartsRenderer.formatValue).not.toHaveBeenCalled();
|
410
409
|
});
|
411
410
|
|
412
411
|
it("should use default value for format", () => {
|
413
412
|
expect(chart.formatValue(16549.1234)).toBe(mockFormattedValue);
|
414
|
-
expect(
|
413
|
+
expect(DrGaugeChart.highchartsRenderer.formatValue).toHaveBeenCalledWith(
|
415
414
|
"n",
|
416
415
|
mockAggregation.widget_values_format,
|
417
|
-
16549.1234
|
418
|
-
undefined
|
416
|
+
16549.1234
|
419
417
|
);
|
420
418
|
});
|
421
419
|
});
|
@@ -704,17 +702,17 @@ describe("DrGaugeChart", () => {
|
|
704
702
|
|
705
703
|
describe("getValue", () => {
|
706
704
|
it("should return aggregator value unledss the total is empty", () => {
|
707
|
-
|
705
|
+
DrGaugeChart.highchartsRenderer.ptCreateBasicLineSeries.mockReturnValue([]);
|
708
706
|
expect(chart.getValue(mockPivotData, {})).toBe(mockAggregationValue);
|
709
707
|
});
|
710
708
|
|
711
709
|
it("should return value to display", () => {
|
712
|
-
|
710
|
+
DrGaugeChart.highchartsRenderer.ptCreateBasicLineSeries.mockReturnValue([{ data: [2000] }]);
|
713
711
|
expect(chart.getValue(mockPivotData, {})).toBe(2000);
|
714
712
|
});
|
715
713
|
|
716
714
|
it("should calculate total", () => {
|
717
|
-
|
715
|
+
DrGaugeChart.highchartsRenderer.ptCreateBasicLineSeries.mockReturnValue([
|
718
716
|
{ data: [2000] },
|
719
717
|
{ data: [3000] },
|
720
718
|
{ data: [5000] },
|
@@ -1903,6 +1901,7 @@ describe("DrGaugeChart", () => {
|
|
1903
1901
|
});
|
1904
1902
|
|
1905
1903
|
it("sets series", () => {
|
1904
|
+
chart.value = 204;
|
1906
1905
|
const series = chart.configChart().series[0];
|
1907
1906
|
|
1908
1907
|
expect(series.name).toBe(null);
|
package/src/charts/dr_chart.js
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
export class DrChart {
|
2
|
-
// A highchartsRenderer injection.
|
3
|
-
// Could be removed later when each chart type mirgate to class syntax
|
4
|
-
static highchartsRenderer;
|
5
|
-
|
6
|
-
constructor() {
|
7
|
-
if (!DrChart.highchartsRenderer) {
|
8
|
-
console.error('The highchartsRenderer instance is not provided!');
|
9
|
-
}
|
10
|
-
}
|
11
|
-
|
12
|
-
render(chartOptions, opts) {
|
13
|
-
return DrChart.highchartsRenderer.ptCreateElementAndDraw(
|
14
|
-
chartOptions,
|
15
|
-
opts
|
16
|
-
);
|
17
|
-
}
|
18
|
-
|
19
|
-
formatValue(data_type, number_format, value, widget_values_format) {
|
20
|
-
return DrChart.highchartsRenderer.formatValue(data_type, number_format, value, widget_values_format)
|
21
|
-
}
|
22
|
-
|
23
|
-
getDefaultValueForChart(type, existing_options) {
|
24
|
-
return DrChart.highchartsRenderer.getDefaultValueForChart(type, existing_options);
|
25
|
-
}
|
26
|
-
|
27
|
-
ptCreateBasicLineSeries(pivotData, colors, onlyNumbers, isUniqueVals, additionOptions, opts, chartOptions) {
|
28
|
-
return DrChart.highchartsRenderer.ptCreateBasicLineSeries(pivotData, colors, onlyNumbers, isUniqueVals, additionOptions, opts, chartOptions);
|
29
|
-
}
|
30
|
-
|
31
|
-
getSingleValueAgg(opts, aggfunc, base) {
|
32
|
-
return DrChart.highchartsRenderer.getSingleValueAgg(opts, aggfunc, base);
|
33
|
-
}
|
34
|
-
}
|