@datarailsshared/dr_renderer 1.2.453 → 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 +563 -0
- package/src/dr-renderer-helpers.js +45 -0
- package/src/dr_chart_tooltip.js +277 -0
- package/src/highcharts_renderer.js +210 -4
- package/tests/dr-renderer-helpers.test.js +98 -0
- package/tests/dr_chart_tooltip.test.js +739 -0
- package/tests/dr_gauge_chart.test.js +1931 -0
- package/tests/highcharts_renderer.test.js +195 -0
package/package.json
CHANGED
@@ -0,0 +1,563 @@
|
|
1
|
+
const { DrChartTooltip } = require("../dr_chart_tooltip");
|
2
|
+
const helpers = require("../dr-renderer-helpers");
|
3
|
+
|
4
|
+
const GAUGE_OPTIONS_DEFAULT = {
|
5
|
+
gauge: {
|
6
|
+
background: "#fff",
|
7
|
+
startAngle: -90,
|
8
|
+
endAngle: 90,
|
9
|
+
thickness: 16,
|
10
|
+
tickLength: 40,
|
11
|
+
tickWidth: 2,
|
12
|
+
valueOffset: [20, 0, 8, 0],
|
13
|
+
offset: [8, 8, 8, 8],
|
14
|
+
goalIcon:
|
15
|
+
"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNSIgdmlld0JveD0iMCAwIDE2IDE1IiBmaWxsPSJub25lIj4KICAgIDxwYXRoIGQ9Ik04IDBMOS43OTYxMSA1LjUyNzg2SDE1LjYwODVMMTAuOTA2MiA4Ljk0NDI3TDEyLjcwMjMgMTQuNDcyMUw4IDExLjA1NTdMMy4yOTc3MiAxNC40NzIxTDUuMDkzODMgOC45NDQyN0wwLjM5MTU0OCA1LjUyNzg2SDYuMjAzODlMOCAwWiIgZmlsbD0iIzQ2NDZDRSIvPgo8L3N2Zz4=",
|
16
|
+
goalIconSize: [16, 16],
|
17
|
+
pivot: {
|
18
|
+
radius: 5,
|
19
|
+
color: "#808080",
|
20
|
+
},
|
21
|
+
colors: {
|
22
|
+
meta: "#6D6E6F",
|
23
|
+
goal: "#4646CE",
|
24
|
+
},
|
25
|
+
},
|
26
|
+
goal: {
|
27
|
+
title: "Goal",
|
28
|
+
value: 180,
|
29
|
+
},
|
30
|
+
isAbsoluteValue: false,
|
31
|
+
segments: [
|
32
|
+
{
|
33
|
+
from: 0,
|
34
|
+
to: 60,
|
35
|
+
title: "Low",
|
36
|
+
color: "#BF1D30",
|
37
|
+
},
|
38
|
+
{
|
39
|
+
from: 61,
|
40
|
+
to: 80,
|
41
|
+
title: "Medium",
|
42
|
+
color: "#FFA310",
|
43
|
+
},
|
44
|
+
{
|
45
|
+
from: 81,
|
46
|
+
to: 100,
|
47
|
+
title: "High",
|
48
|
+
color: "#037C5A",
|
49
|
+
},
|
50
|
+
],
|
51
|
+
};
|
52
|
+
|
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
|
+
return value < max / 2;
|
84
|
+
};
|
85
|
+
|
86
|
+
this.createPlotBands = function (options) {
|
87
|
+
const {
|
88
|
+
segments,
|
89
|
+
isAbsoluteValue,
|
90
|
+
goal: { value: goalValue },
|
91
|
+
gauge: { thickness },
|
92
|
+
} = options;
|
93
|
+
|
94
|
+
const bands = segments.map((item) => {
|
95
|
+
return {
|
96
|
+
from: isAbsoluteValue ? Math.max(0, item.from - 1) : (Math.max(0, item.from - 1) * goalValue) / 100,
|
97
|
+
to: isAbsoluteValue ? item.to : (item.to * goalValue) / 100,
|
98
|
+
color: item.color,
|
99
|
+
thickness: thickness,
|
100
|
+
title: item.title,
|
101
|
+
};
|
102
|
+
});
|
103
|
+
|
104
|
+
// clamp segments
|
105
|
+
bands[0].from = 0;
|
106
|
+
bands[bands.length - 1].to = Math.max(bands[bands.length - 1].to, goalValue);
|
107
|
+
|
108
|
+
return bands;
|
109
|
+
};
|
110
|
+
|
111
|
+
this.createTicks = function (plotBands, options) {
|
112
|
+
return [...new Set([0, ...plotBands.map((b) => b.to), options.goal.value])].sort((a, b) => a - b);
|
113
|
+
};
|
114
|
+
|
115
|
+
this.mergeOptions = function (options) {
|
116
|
+
return helpers.mergeDeep(
|
117
|
+
JSON.parse(JSON.stringify(GAUGE_OPTIONS_DEFAULT)),
|
118
|
+
this.getDefaultValueForChart(DrGaugeChart.highchartsRenderer.CHART_TYPES.GAUGE_CHART_ENHANCED),
|
119
|
+
options
|
120
|
+
);
|
121
|
+
};
|
122
|
+
|
123
|
+
this.getAngleForValue = function (
|
124
|
+
value,
|
125
|
+
min = 0,
|
126
|
+
max = this.max,
|
127
|
+
startAngle = this.options.gauge.startAngle,
|
128
|
+
endAngle = this.options.gauge.endAngle
|
129
|
+
) {
|
130
|
+
const degrees = ((value - min) / (max - min)) * (endAngle - startAngle);
|
131
|
+
const radians = degrees * (Math.PI / 180);
|
132
|
+
return radians;
|
133
|
+
};
|
134
|
+
|
135
|
+
this.formatValue = function (value, format = this.format) {
|
136
|
+
return helpers.isNumber(value) ? DrGaugeChart.highchartsRenderer.formatValue("n", format, value).value : value;
|
137
|
+
};
|
138
|
+
|
139
|
+
this.toPercent = function (value) {
|
140
|
+
return `${Math.round((value * 100) / (this.options.isAbsoluteValue ? this.max : this.goal.value))}%`;
|
141
|
+
};
|
142
|
+
|
143
|
+
this.formatValueLabel = function (value, options) {
|
144
|
+
return `<span style="display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: ${
|
145
|
+
options.label.font_size
|
146
|
+
}px;">
|
147
|
+
<span style="font-weight: 600; font-size: 1.5em; line-height: 1; color: ${options.label.font_color}">
|
148
|
+
${this.formatValue(value)}
|
149
|
+
${
|
150
|
+
options.label.show_percentage_in_value
|
151
|
+
? `<span style="font-size: 0.5833em; font-weight: 600; color: ${
|
152
|
+
options.gauge.colors.meta
|
153
|
+
};">(${this.toPercent(value)})</span>`
|
154
|
+
: ""
|
155
|
+
}
|
156
|
+
</span>
|
157
|
+
<span style="font-weight: 500; font-size: 0.875em; line-height: 1; color: ${
|
158
|
+
options.gauge.colors.meta
|
159
|
+
};">Current status</span>
|
160
|
+
</span>`;
|
161
|
+
};
|
162
|
+
|
163
|
+
this.formatTickLabel = function (value, options) {
|
164
|
+
const isGoal = value === options.goal.value;
|
165
|
+
const isLeftQuarter = this.isLeftQuarter(value);
|
166
|
+
const formattedValue = this.formatValue(value);
|
167
|
+
|
168
|
+
const goalStyles = isGoal ? `padding-${isLeftQuarter ? "right" : "left"}: 12px;` : "";
|
169
|
+
const goalValue = isGoal
|
170
|
+
? `<span style="font-size: 1.125em; color: ${options.gauge.colors.goal};">${formattedValue}</span>`
|
171
|
+
: `<span style="color: ${options.label.font_color};">${formattedValue}</span>`;
|
172
|
+
const goalTitle =
|
173
|
+
isGoal && options.label.show_goal_name && options.goal.title
|
174
|
+
? `<span style="font-size: 0.75em; color: ${options.gauge.colors.goal};">${options.goal.title}</span>`
|
175
|
+
: "";
|
176
|
+
const percentage = options.label.show_percentage_in_segments
|
177
|
+
? `<span style="font-size: 0.75em; color: ${options.gauge.colors.meta}; font-weight: 400;">(${this.toPercent(
|
178
|
+
value
|
179
|
+
)})</span>`
|
180
|
+
: "";
|
181
|
+
|
182
|
+
return `<span style="
|
183
|
+
display: flex;
|
184
|
+
align-items: center;
|
185
|
+
gap: 4px;
|
186
|
+
font-weight: 600;
|
187
|
+
font-size: ${options.label.font_size}px;
|
188
|
+
${goalStyles}
|
189
|
+
">
|
190
|
+
${goalValue}
|
191
|
+
${goalTitle}
|
192
|
+
${percentage}
|
193
|
+
</span>`;
|
194
|
+
};
|
195
|
+
|
196
|
+
this.getValue = function (pivotData, opts) {
|
197
|
+
const lineSeries = this.ptCreateBasicLineSeries(pivotData, null, true, null, null, opts, {});
|
198
|
+
|
199
|
+
let total = lineSeries
|
200
|
+
.map((s) => s.data.map((v) => v))
|
201
|
+
.filter((v) => !isNaN(v))
|
202
|
+
.flat();
|
203
|
+
|
204
|
+
let aggfunc = (a, b) => a + b;
|
205
|
+
let base = 0;
|
206
|
+
let singleValueAgg = this.getSingleValueAgg(
|
207
|
+
{
|
208
|
+
value: {
|
209
|
+
value: "Sum",
|
210
|
+
},
|
211
|
+
},
|
212
|
+
aggfunc,
|
213
|
+
base
|
214
|
+
);
|
215
|
+
|
216
|
+
aggfunc = singleValueAgg.aggfunc;
|
217
|
+
base = singleValueAgg.base;
|
218
|
+
|
219
|
+
const aggregator = pivotData.getAggregator([], []);
|
220
|
+
const value = total.length > 0 ? total.reduce(aggfunc, base) : aggregator.value();
|
221
|
+
|
222
|
+
return value;
|
223
|
+
};
|
224
|
+
|
225
|
+
this.getBorderPosition = function (chart, options, position) {
|
226
|
+
const { center, size } = chart.pane[0].options;
|
227
|
+
const {
|
228
|
+
gauge: { tickLength, tickWidth },
|
229
|
+
} = options;
|
230
|
+
|
231
|
+
return {
|
232
|
+
x: position === "start" ? center[0] - size / 2 + tickLength / 2 : center[0] + size / 2 - tickLength / 2,
|
233
|
+
y: center[1] + tickWidth / 2,
|
234
|
+
};
|
235
|
+
};
|
236
|
+
|
237
|
+
this.createBorder = function (chart, options, position) {
|
238
|
+
return chart.renderer
|
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
|
+
)
|
247
|
+
.attr({
|
248
|
+
fill:
|
249
|
+
position === "start"
|
250
|
+
? chart.yAxis[0].options.plotBands[0].color
|
251
|
+
: chart.yAxis[0].options.plotBands.slice(-1)[0].color,
|
252
|
+
})
|
253
|
+
.add()
|
254
|
+
.toFront();
|
255
|
+
};
|
256
|
+
|
257
|
+
this.getGoalIconPosition = function (chart, options) {
|
258
|
+
const { center, size } = chart.pane[0].options;
|
259
|
+
const radius = size / 2;
|
260
|
+
const {
|
261
|
+
gauge: { goalIconSize },
|
262
|
+
goal: { value },
|
263
|
+
} = options;
|
264
|
+
|
265
|
+
return {
|
266
|
+
x: center[0] - radius * Math.sin(Math.PI / 2 - this.getAngleForValue(value)) - goalIconSize[0] / 2,
|
267
|
+
y: center[1] - radius * Math.cos(Math.PI / 2 - this.getAngleForValue(value)) - goalIconSize[1] / 2,
|
268
|
+
};
|
269
|
+
};
|
270
|
+
|
271
|
+
this.createGoalIcon = function (chart, options) {
|
272
|
+
const point = this.getGoalIconPosition(chart, options);
|
273
|
+
return chart.renderer
|
274
|
+
.image(options.gauge.goalIcon, point.x, point.y, options.gauge.goalIconSize[0], options.gauge.goalIconSize[1])
|
275
|
+
.add()
|
276
|
+
.toFront();
|
277
|
+
};
|
278
|
+
|
279
|
+
this.getValueLabelPosition = function (chart, options) {
|
280
|
+
const { center } = chart.pane[0].options;
|
281
|
+
return {
|
282
|
+
x: center[0],
|
283
|
+
y: center[1] + options.gauge.valueOffset[0],
|
284
|
+
};
|
285
|
+
};
|
286
|
+
|
287
|
+
this.createValueLabel = function (chart, options) {
|
288
|
+
const label = chart.renderer.text(this.formatValueLabel(this.value, options), 0, 0, true).add().toFront();
|
289
|
+
|
290
|
+
helpers.removeSVGTextCorrection(label, "yCorr");
|
291
|
+
|
292
|
+
label.attr(this.getValueLabelPosition(chart, options)).css({
|
293
|
+
transform: "translateX(-50%)",
|
294
|
+
});
|
295
|
+
|
296
|
+
return label;
|
297
|
+
};
|
298
|
+
|
299
|
+
this.getPaneDimensions = function (chart, options) {
|
300
|
+
const { renderer } = chart;
|
301
|
+
const valueLabel = this.createValueLabel(chart, this.options);
|
302
|
+
const { offset } = options.gauge;
|
303
|
+
const { height: labelH } = valueLabel.getBBox();
|
304
|
+
|
305
|
+
const offsetBottom = labelH + options.gauge.valueOffset[0] + options.gauge.valueOffset[2] + offset[2];
|
306
|
+
valueLabel.destroy();
|
307
|
+
|
308
|
+
const radiuses = [chart.chartWidth / 2 - Math.max(offset[1], offset[3]), chart.chartHeight - offsetBottom - offset[0]];
|
309
|
+
if (options.label.show) {
|
310
|
+
this.ticks.forEach((tick) => {
|
311
|
+
const label = renderer.label(this.formatTickLabel(tick, options), 0, 0, null, null, null, true).add();
|
312
|
+
const angle = this.getAngleForValue(tick);
|
313
|
+
// depends on label width
|
314
|
+
radiuses.push(
|
315
|
+
(chart.chartWidth / 2 - label.bBox.width - Math.max(offset[1], offset[3])) /
|
316
|
+
Math.sin(Math.abs(Math.PI / 2 - angle))
|
317
|
+
);
|
318
|
+
// depends on label height
|
319
|
+
radiuses.push(
|
320
|
+
(chart.chartHeight - offsetBottom - label.bBox.height / 2 - offset[0]) /
|
321
|
+
Math.cos(Math.abs(Math.PI / 2 - angle))
|
322
|
+
);
|
323
|
+
label.destroy();
|
324
|
+
});
|
325
|
+
} else {
|
326
|
+
// reserve space for the goal icon
|
327
|
+
const angle = this.getAngleForValue(options.goal.value);
|
328
|
+
const [iconW, iconH] = options.gauge.goalIconSize;
|
329
|
+
radiuses.push((chart.chartWidth / 2 - iconW / 2 - offset[1]) / Math.sin(Math.abs(Math.PI / 2 - angle)));
|
330
|
+
radiuses.push((chart.chartHeight - offsetBottom - iconH / 2 - offset[0]) / Math.cos(Math.abs(Math.PI / 2 - angle)));
|
331
|
+
}
|
332
|
+
|
333
|
+
return {
|
334
|
+
radius: Math.min(...radiuses),
|
335
|
+
center: [chart.chartWidth / 2, chart.chartHeight - offsetBottom],
|
336
|
+
};
|
337
|
+
};
|
338
|
+
|
339
|
+
this.setTicksStyles = function (chart, options) {
|
340
|
+
Object.keys(chart.yAxis[0].ticks).forEach((i) => {
|
341
|
+
const tick = chart.yAxis[0].ticks[i];
|
342
|
+
const isLeftQuarter = this.isLeftQuarter(tick.pos);
|
343
|
+
|
344
|
+
if (tick.pos === options.goal.value) {
|
345
|
+
tick.mark.attr({
|
346
|
+
stroke: options.gauge.colors.goal,
|
347
|
+
"stroke-dasharray": 2,
|
348
|
+
"pointer-events": "none",
|
349
|
+
});
|
350
|
+
}
|
351
|
+
|
352
|
+
if (!tick.label) return;
|
353
|
+
|
354
|
+
// align left querter's labels
|
355
|
+
tick.label.css({
|
356
|
+
transform: `translate(${isLeftQuarter ? "-100%" : 0}, 0)`,
|
357
|
+
});
|
358
|
+
});
|
359
|
+
};
|
360
|
+
|
361
|
+
this.addTooltips = function (chart, options) {
|
362
|
+
if (!options.tooltips.show) {
|
363
|
+
return false;
|
364
|
+
}
|
365
|
+
|
366
|
+
const drTooltip = new DrChartTooltip(chart, {
|
367
|
+
fontSize: options.tooltips.font_size,
|
368
|
+
fontFamily: options.tooltips.font_style,
|
369
|
+
color: options.tooltips.font_color,
|
370
|
+
});
|
371
|
+
|
372
|
+
// segment title tooltip
|
373
|
+
if (options.tooltips.show_segment_name) {
|
374
|
+
chart.yAxis[0].plotLinesAndBands.forEach((band, i) => {
|
375
|
+
drTooltip.add(band.options.title, band.svgElem.element, {
|
376
|
+
direction: "top",
|
377
|
+
followPointer: true,
|
378
|
+
});
|
379
|
+
});
|
380
|
+
}
|
381
|
+
|
382
|
+
// value label tooltip
|
383
|
+
if (options.tooltips.show_percentage_in_value && !options.label.show_percentage_in_value) {
|
384
|
+
drTooltip.add(this.toPercent(this.value), chart.label.element, {
|
385
|
+
direction: "top",
|
386
|
+
});
|
387
|
+
}
|
388
|
+
|
389
|
+
// segment name tooltips
|
390
|
+
Object.keys(chart.yAxis[0].ticks).forEach((i) => {
|
391
|
+
const tick = chart.yAxis[0].ticks[i];
|
392
|
+
const isLeftQuarter = this.isLeftQuarter(tick.pos);
|
393
|
+
|
394
|
+
// goal tooltip if lebels are hidden
|
395
|
+
if (tick.pos === options.goal.value && !options.label.show) {
|
396
|
+
drTooltip.add(
|
397
|
+
`${
|
398
|
+
options.label.show_goal_name ? options.goal.title || "" : ""
|
399
|
+
}<span style="font-weight: 600">${this.formatValue(options.goal.value)}</span>`,
|
400
|
+
chart.goalIcon.element,
|
401
|
+
{
|
402
|
+
direction: isLeftQuarter ? "left" : "right",
|
403
|
+
}
|
404
|
+
);
|
405
|
+
}
|
406
|
+
|
407
|
+
// segment label percentage tooltips
|
408
|
+
if (tick.label && options.tooltips.show_percentage_in_segments && !options.label.show_percentage_in_segments) {
|
409
|
+
drTooltip.add(this.toPercent(tick.pos), tick.label.element, {
|
410
|
+
direction: isLeftQuarter ? "left" : "right",
|
411
|
+
});
|
412
|
+
}
|
413
|
+
});
|
414
|
+
};
|
415
|
+
|
416
|
+
this.setPane = function (chart, options) {
|
417
|
+
const { radius, center } = this.getPaneDimensions(chart, options);
|
418
|
+
chart.pane[0].options.size = 2 * radius;
|
419
|
+
chart.pane[0].options.center = center;
|
420
|
+
chart.yAxis[0].options.plotBands.forEach((band) => {
|
421
|
+
band.outerRadius = radius - (options.gauge.tickLength - options.gauge.thickness) / 2;
|
422
|
+
});
|
423
|
+
chart.series[0].options.dial.radius = Math.round((100 * (radius - options.gauge.tickLength - 10)) / radius) + "%";
|
424
|
+
};
|
425
|
+
|
426
|
+
this.setCustomElements = function (chart, options) {
|
427
|
+
chart.label = this.createValueLabel(chart, options);
|
428
|
+
chart.startBorder = this.createBorder(chart, options, "start");
|
429
|
+
chart.endBorder = this.createBorder(chart, options, "end");
|
430
|
+
chart.goalIcon = this.createGoalIcon(chart, options);
|
431
|
+
};
|
432
|
+
|
433
|
+
this.updateCustomElements = function (chart, options) {
|
434
|
+
chart.startBorder.attr(this.getBorderPosition(chart, options, "start"));
|
435
|
+
chart.endBorder.attr(this.getBorderPosition(chart, options, "end"));
|
436
|
+
chart.goalIcon.attr(this.getGoalIconPosition(chart, options));
|
437
|
+
chart.label.attr(this.getValueLabelPosition(chart, options));
|
438
|
+
};
|
439
|
+
|
440
|
+
this.moveCustomElementsToFront = function (chart) {
|
441
|
+
chart.startBorder.toFront();
|
442
|
+
chart.endBorder.toFront();
|
443
|
+
chart.goalIcon.toFront();
|
444
|
+
};
|
445
|
+
|
446
|
+
this.clampValueToPane = function (value, max = this.max) {
|
447
|
+
return helpers.clamp(-0.02 * max, value, 1.02 * max);
|
448
|
+
};
|
449
|
+
|
450
|
+
this.configChart = function () {
|
451
|
+
return {
|
452
|
+
title: {
|
453
|
+
text: null,
|
454
|
+
},
|
455
|
+
subtitle: null,
|
456
|
+
exporting: {
|
457
|
+
allowHTML: true,
|
458
|
+
},
|
459
|
+
tooltip: {
|
460
|
+
enabled: false,
|
461
|
+
},
|
462
|
+
chart: {
|
463
|
+
type: "gauge",
|
464
|
+
backgroundColor: this.options.gauge.background,
|
465
|
+
plotBackgroundColor: null,
|
466
|
+
plotBackgroundImage: null,
|
467
|
+
plotBorderWidth: 0,
|
468
|
+
plotShadow: false,
|
469
|
+
events: {
|
470
|
+
render: ({ target: chart }) => {
|
471
|
+
this.moveCustomElementsToFront(chart);
|
472
|
+
this.setTicksStyles(chart, this.options);
|
473
|
+
},
|
474
|
+
beforeRedraw: ({ target: chart }) => {
|
475
|
+
this.setPane(chart, this.options);
|
476
|
+
this.updateCustomElements(chart, this.options);
|
477
|
+
},
|
478
|
+
beforeRender: ({ target: chart }) => {
|
479
|
+
this.setPane(chart, this.options);
|
480
|
+
this.setCustomElements(chart, this.options);
|
481
|
+
},
|
482
|
+
load: ({ target: chart }) => {
|
483
|
+
this.addTooltips(chart, this.options);
|
484
|
+
},
|
485
|
+
},
|
486
|
+
margin: [0, 0, 0, 0],
|
487
|
+
spacing: [0, 0, 0, 0],
|
488
|
+
},
|
489
|
+
|
490
|
+
pane: {
|
491
|
+
startAngle: -90,
|
492
|
+
endAngle: 90,
|
493
|
+
background: null,
|
494
|
+
center: [0, 0],
|
495
|
+
},
|
496
|
+
|
497
|
+
// the value axis
|
498
|
+
yAxis: {
|
499
|
+
min: 0,
|
500
|
+
max: this.max,
|
501
|
+
tickPositions: this.ticks,
|
502
|
+
tickPosition: "inside",
|
503
|
+
tickColor: this.options.gauge.background,
|
504
|
+
tickLength: this.options.gauge.tickLength,
|
505
|
+
tickWidth: this.options.gauge.tickWidth,
|
506
|
+
minorTickInterval: null,
|
507
|
+
|
508
|
+
labels: {
|
509
|
+
enabled: !!this.options.label.show,
|
510
|
+
distance: 0,
|
511
|
+
verticalAlign: "middle",
|
512
|
+
allowOverlap: true,
|
513
|
+
align: "left",
|
514
|
+
style: {
|
515
|
+
whiteSpace: "nowrap",
|
516
|
+
width: "auto",
|
517
|
+
},
|
518
|
+
formatter: ({ value }) => {
|
519
|
+
return this.formatTickLabel(value, this.options);
|
520
|
+
},
|
521
|
+
useHTML: true,
|
522
|
+
},
|
523
|
+
lineWidth: 0,
|
524
|
+
plotBands: this.plotBands,
|
525
|
+
},
|
526
|
+
|
527
|
+
series: [
|
528
|
+
{
|
529
|
+
name: null,
|
530
|
+
data: [this.clampValueToPane(this.value)],
|
531
|
+
dataLabels: [
|
532
|
+
{
|
533
|
+
enabled: false,
|
534
|
+
},
|
535
|
+
],
|
536
|
+
dial: {
|
537
|
+
radius: "70%",
|
538
|
+
backgroundColor: this.options.gauge.pivot.color,
|
539
|
+
baseWidth: 2 * this.options.gauge.pivot.radius,
|
540
|
+
baseLength: "0%",
|
541
|
+
rearLength: "0%",
|
542
|
+
},
|
543
|
+
pivot: {
|
544
|
+
backgroundColor: this.options.gauge.pivot.color,
|
545
|
+
radius: this.options.gauge.pivot.radius,
|
546
|
+
},
|
547
|
+
},
|
548
|
+
],
|
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];
|
561
|
+
}
|
562
|
+
|
563
|
+
module.exports = { DrGaugeChart, GAUGE_OPTIONS_DEFAULT };
|
@@ -7,7 +7,52 @@ function capitalize(string) {
|
|
7
7
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
8
8
|
}
|
9
9
|
|
10
|
+
function clamp(min, v, max) {
|
11
|
+
return Math.min(max, Math.max(v, min));
|
12
|
+
}
|
13
|
+
|
14
|
+
function isNumber(n) {
|
15
|
+
return !isNaN(parseFloat(n)) && isFinite(n);
|
16
|
+
}
|
17
|
+
|
18
|
+
function mergeDeep(target, ...sources) {
|
19
|
+
const isObject = (obj) => obj && typeof obj === 'object' && !Array.isArray(obj);
|
20
|
+
|
21
|
+
if (!isObject(target)) return target;
|
22
|
+
|
23
|
+
sources.forEach((source) => {
|
24
|
+
if (!isObject(source)) return;
|
25
|
+
|
26
|
+
Object.keys(source).forEach((key) => {
|
27
|
+
const targetValue = target[key];
|
28
|
+
const sourceValue = source[key];
|
29
|
+
|
30
|
+
if (isObject(targetValue) && isObject(sourceValue)) {
|
31
|
+
target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue);
|
32
|
+
} else {
|
33
|
+
target[key] = sourceValue;
|
34
|
+
}
|
35
|
+
});
|
36
|
+
});
|
37
|
+
|
38
|
+
return target;
|
39
|
+
}
|
40
|
+
|
41
|
+
function removeSVGTextCorrection(svgEl, corr = 'yCorr') {
|
42
|
+
Object.defineProperty(svgEl, corr, {
|
43
|
+
set: function() {},
|
44
|
+
get: function() {
|
45
|
+
return 0;
|
46
|
+
}
|
47
|
+
});
|
48
|
+
return svgEl;
|
49
|
+
}
|
50
|
+
|
10
51
|
module.exports = {
|
11
52
|
backendSortingKeysAreNotEmpty,
|
12
53
|
capitalize,
|
54
|
+
clamp,
|
55
|
+
isNumber,
|
56
|
+
mergeDeep,
|
57
|
+
removeSVGTextCorrection,
|
13
58
|
}
|