-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstretchScale.js
More file actions
69 lines (62 loc) · 1.94 KB
/
stretchScale.js
File metadata and controls
69 lines (62 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* stretchScale.js
* Custom radial-stretch scale plugin for Chart.js
*/
(() => {
const Chart = window.Chart;
const { RadialLinearScale } = Chart;
class RadialStretchScale extends RadialLinearScale {
static id = 'radialStretch';
static defaults = RadialLinearScale.defaults;
constructor(cfg) {
super(cfg);
}
/**
* Interpolate between linear and exponential radius mapping
* based on `options.stretch` (0–1) and `options.K`.
*/
getDistanceFromCenterForValue(value) {
const { stretch = 0, K = 10 } = this.options;
// clamp the value within the configured min/max
const L = Math.max(this.min, Math.min(this.max, value));
// linear component
const lin = L;
// exponential component scaled by max
const exp = this.max * (Math.pow(K, L / this.max) - 1) / (K - 1);
// mix linear and exponential based on stretch factor
const mix = (1 - stretch) * lin + stretch * exp;
// normalize and scale to drawing area
return (mix / this.max) * this.drawingArea;
}
}
// register the new scale
Chart.register(RadialStretchScale);
// preserve custom grid & tick styling
Chart.defaults.scales.radialStretch.grid = {
borderDash: [4, 4],
color: '#ccc'
};
Chart.defaults.scales.radialStretch.ticks = {
backdropColor: 'transparent',
showLabelBackdrop: false,
color: '#888'
};
// ensure point labels render and inherit correct callback
// Chart.defaults.scales.radialStretch.pointLabels = {
// display: true,
// callback: label => label,
// font: { size: 12, weight: '300' },
// color: '#333',
// padding: 8
// };
// plugin to redraw point labels on top of dataset fills
Chart.register({
id: 'bringPointLabelsToFront',
afterDatasetsDraw(chart) {
const scale = chart.scales.r;
if (scale && typeof scale.drawPointLabels === 'function') {
scale.drawPointLabels(chart.ctx);
}
}
});
})();