-
Notifications
You must be signed in to change notification settings - Fork 37
/
controller.gauge.js
252 lines (229 loc) · 7.53 KB
/
controller.gauge.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import Chart from 'chart.js';
// default options
Chart.defaults._set('gauge', {
needle: {
// Needle circle radius as the percentage of the chart area width
radiusPercentage: 2,
// Needle width as the percentage of the chart area width
widthPercentage: 3.2,
// Needle length as the percentage of the interval between inner radius (0%) and outer radius (100%) of the arc
lengthPercentage: 80,
// The color of the needle
color: 'rgba(0, 0, 0, 1)',
},
valueLabel: {
// fontSize: undefined
display: true,
formatter: null,
color: 'rgba(255, 255, 255, 1)',
backgroundColor: 'rgba(0, 0, 0, 1)',
borderRadius: 5,
padding: {
top: 5,
right: 5,
bottom: 5,
left: 5,
},
bottomMarginPercentage: 5,
},
animation: {
duration: 1000,
animateRotate: true,
animateScale: false,
},
// The percentage of the chart that we cut out of the middle.
cutoutPercentage: 50,
// The rotation of the chart, where the first data arc begins.
rotation: -Math.PI,
// The total circumference of the chart.
circumference: Math.PI,
legend: {
display: false,
},
tooltips: {
enabled: false,
},
});
const GaugeController = Chart.controllers.doughnut.extend({
getValuePercent({ minValue, data }, value) {
const min = minValue || 0;
const max = data[data.length - 1] || 1;
const length = max - min;
const percent = (value - min) / length;
return percent;
},
getWidth(chart) {
return chart.chartArea.right - chart.chartArea.left;
},
getTranslation(chart) {
const { chartArea, offsetX, offsetY } = chart;
const centerX = (chartArea.left + chartArea.right) / 2;
const centerY = (chartArea.top + chartArea.bottom) / 2;
const dx = (centerX + offsetX);
const dy = (centerY + offsetY);
return { dx, dy };
},
getAngle({ chart, valuePercent }) {
const { rotation, circumference } = chart.options;
return rotation + (circumference * valuePercent);
},
/* TODO set min padding, not applied until chart.update() (also chartArea must have been set)
setBottomPadding(chart) {
const needleRadius = this.getNeedleRadius(chart);
const padding = this.chart.config.options.layout.padding;
if (needleRadius > padding.bottom) {
padding.bottom = needleRadius;
return true;
}
return false;
},
*/
drawNeedle(ease) {
if (!this.chart.animating) { // triggered when hovering
ease = 1;
}
const {
ctx,
config,
innerRadius,
outerRadius,
} = this.chart;
const dataset = config.data.datasets[this.index];
const { previous } = this.getMeta();
const {
radiusPercentage,
widthPercentage,
lengthPercentage,
color,
} = config.options.needle;
const width = this.getWidth(this.chart);
const needleRadius = (radiusPercentage / 100) * width;
const needleWidth = (widthPercentage / 100) * width;
const needleLength = (lengthPercentage / 100) * (outerRadius - innerRadius) + innerRadius;
// center
const { dx, dy } = this.getTranslation(this.chart);
// interpolate
const origin = this.getAngle({ chart: this.chart, valuePercent: previous.valuePercent });
// TODO valuePercent is in current.valuePercent also
const target = this.getAngle({ chart: this.chart, valuePercent: this.getValuePercent(dataset, dataset.value) });
const angle = origin + (target - origin) * ease;
// draw
ctx.save();
ctx.translate(dx, dy);
ctx.rotate(angle);
ctx.fillStyle = color;
// draw circle
ctx.beginPath();
ctx.ellipse(0, 0, needleRadius, needleRadius, 0, 0, 2 * Math.PI);
ctx.fill();
// draw needle
ctx.beginPath();
ctx.moveTo(0, needleWidth / 2);
ctx.lineTo(needleLength, 0);
ctx.lineTo(0, -needleWidth / 2);
ctx.fill();
ctx.restore();
},
drawValueLabel(ease) { // eslint-disable-line no-unused-vars
if (!this.chart.config.options.valueLabel.display) {
return;
}
const { ctx, config } = this.chart;
const {
defaultFontFamily,
} = config.options;
const dataset = config.data.datasets[this.index];
const {
formatter,
fontSize,
color,
backgroundColor,
borderRadius,
padding,
bottomMarginPercentage,
} = config.options.valueLabel;
const width = this.getWidth(this.chart);
const bottomMargin = (bottomMarginPercentage / 100) * width;
const fmt = formatter || (value => value);
const valueText = fmt(dataset.value).toString();
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
if (fontSize) {
ctx.font = `${fontSize}px ${defaultFontFamily}`;
}
// const { width: textWidth, actualBoundingBoxAscent, actualBoundingBoxDescent } = ctx.measureText(valueText);
// const textHeight = actualBoundingBoxAscent + actualBoundingBoxDescent;
const { width: textWidth } = ctx.measureText(valueText);
// approximate height until browsers support advanced TextMetrics
const textHeight = Math.max(ctx.measureText('m').width, ctx.measureText('\uFF37').width);
const x = -(padding.left + textWidth / 2);
const y = -(padding.top + textHeight / 2);
const w = (padding.left + textWidth + padding.right);
const h = (padding.top + textHeight + padding.bottom);
// center
let { dx, dy } = this.getTranslation(this.chart);
// add rotation
const rotation = this.chart.options.rotation % (Math.PI * 2.0);
dx += bottomMargin * Math.cos(rotation + Math.PI / 2);
dy += bottomMargin * Math.sin(rotation + Math.PI / 2);
// draw
ctx.save();
ctx.translate(dx, dy);
// draw background
ctx.beginPath();
Chart.helpers.canvas.roundedRect(ctx, x, y, w, h, borderRadius);
ctx.fillStyle = backgroundColor;
ctx.fill();
// draw value text
ctx.fillStyle = color || config.options.defaultFontColor;
const magicNumber = 0.075; // manual testing
ctx.fillText(valueText, 0, textHeight * magicNumber);
ctx.restore();
},
// overrides
update(reset) {
const dataset = this.chart.config.data.datasets[this.index];
dataset.minValue = dataset.minValue || 0;
const meta = this.getMeta();
const initialValue = {
valuePercent: 0,
};
// animations on will call update(reset) before update()
if (reset) {
meta.previous = null;
meta.current = initialValue;
} else {
dataset.data.sort((a, b) => a - b);
meta.previous = meta.current || initialValue;
meta.current = {
valuePercent: this.getValuePercent(dataset, dataset.value),
};
}
Chart.controllers.doughnut.prototype.update.call(this, reset);
},
updateElement(arc, index, reset) {
// TODO handle reset and options.animation
Chart.controllers.doughnut.prototype.updateElement.call(this, arc, index, reset);
const dataset = this.getDataset();
const { data } = dataset;
// const { options } = this.chart.config;
// scale data
const previousValue = index === 0 ? dataset.minValue : data[index - 1];
const value = data[index];
const startAngle = this.getAngle({ chart: this.chart, valuePercent: this.getValuePercent(dataset, previousValue) });
const endAngle = this.getAngle({ chart: this.chart, valuePercent: this.getValuePercent(dataset, value) });
const circumference = endAngle - startAngle;
arc._model = {
...arc._model,
startAngle,
endAngle,
circumference,
};
},
draw(ease) {
Chart.controllers.doughnut.prototype.draw.call(this, ease);
this.drawNeedle(ease);
this.drawValueLabel(ease);
},
});
export default GaugeController;