回答編集履歴
1
追加
test
CHANGED
@@ -177,3 +177,293 @@
|
|
177
177
|
</script>
|
178
178
|
|
179
179
|
```
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
---------
|
184
|
+
|
185
|
+
ツールチップを固定表示するのであれば、よくみかける下記の手法で対応できます。(ツールチップの形を質問記載の画像に似せるところまでは回答範囲外と考えるので対応していません)
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
<body>
|
192
|
+
|
193
|
+
<canvas id="chart"></canvas>
|
194
|
+
|
195
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.js"></script>
|
196
|
+
|
197
|
+
<script>
|
198
|
+
|
199
|
+
"use strict";
|
200
|
+
|
201
|
+
const type = "bar";
|
202
|
+
|
203
|
+
const data = {
|
204
|
+
|
205
|
+
labels: [0, 1, 2, 3],
|
206
|
+
|
207
|
+
datasets: [
|
208
|
+
|
209
|
+
{
|
210
|
+
|
211
|
+
label: "AAA",
|
212
|
+
|
213
|
+
data: [100, 200, 221, 350],
|
214
|
+
|
215
|
+
backgroundColor: "green",
|
216
|
+
|
217
|
+
},
|
218
|
+
|
219
|
+
{
|
220
|
+
|
221
|
+
label: "BBB",
|
222
|
+
|
223
|
+
data: [100, 200, 221, 350],
|
224
|
+
|
225
|
+
backgroundColor: "skyblue",
|
226
|
+
|
227
|
+
},
|
228
|
+
|
229
|
+
{
|
230
|
+
|
231
|
+
label: "CCC",
|
232
|
+
|
233
|
+
data: [100, 200, 221, 350],
|
234
|
+
|
235
|
+
backgroundColor: "red",
|
236
|
+
|
237
|
+
},
|
238
|
+
|
239
|
+
],
|
240
|
+
|
241
|
+
};
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
Chart.pluginService.register({
|
246
|
+
|
247
|
+
beforeRender: chart => {
|
248
|
+
|
249
|
+
if (chart.config.options.showAllTooltips) {
|
250
|
+
|
251
|
+
chart.pluginTooltips = [];
|
252
|
+
|
253
|
+
chart.config.data.datasets.forEach((dataset, i) => {
|
254
|
+
|
255
|
+
chart.getDatasetMeta(i).data.forEach(sector => {
|
256
|
+
|
257
|
+
chart.pluginTooltips.push(new Chart.Tooltip({
|
258
|
+
|
259
|
+
_chart: chart.chart,
|
260
|
+
|
261
|
+
_chartInstance: chart,
|
262
|
+
|
263
|
+
_data: chart.data,
|
264
|
+
|
265
|
+
_options: chart.options.tooltips,
|
266
|
+
|
267
|
+
_active: [sector]
|
268
|
+
|
269
|
+
}, chart));
|
270
|
+
|
271
|
+
});
|
272
|
+
|
273
|
+
});
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
chart.options.tooltips.enabled = false;
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
},
|
282
|
+
|
283
|
+
afterDraw: (chart, easing) => {
|
284
|
+
|
285
|
+
if (chart.config.options.showAllTooltips) {
|
286
|
+
|
287
|
+
if (!chart.allTooltipsOnce) {
|
288
|
+
|
289
|
+
if (easing !== 1)
|
290
|
+
|
291
|
+
return;
|
292
|
+
|
293
|
+
chart.allTooltipsOnce = true;
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
let cnt = 1;
|
298
|
+
|
299
|
+
chart.options.tooltips.enabled = true;
|
300
|
+
|
301
|
+
Chart.helpers.each(chart.pluginTooltips, tooltip => {
|
302
|
+
|
303
|
+
if (cnt == chart.pluginTooltips.length) { // 最後のスタック描画時だけツールチップを描く
|
304
|
+
|
305
|
+
tooltip.initialize();
|
306
|
+
|
307
|
+
tooltip.update();
|
308
|
+
|
309
|
+
tooltip.pivot();
|
310
|
+
|
311
|
+
tooltip.transition(easing).draw();
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
cnt = cnt + 1;
|
316
|
+
|
317
|
+
});
|
318
|
+
|
319
|
+
chart.options.tooltips.enabled = false;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
})
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
const options = {
|
330
|
+
|
331
|
+
scales: {
|
332
|
+
|
333
|
+
xAxes: [
|
334
|
+
|
335
|
+
{
|
336
|
+
|
337
|
+
stacked: true,
|
338
|
+
|
339
|
+
scaleLabel: {
|
340
|
+
|
341
|
+
display: true,
|
342
|
+
|
343
|
+
},
|
344
|
+
|
345
|
+
},
|
346
|
+
|
347
|
+
],
|
348
|
+
|
349
|
+
yAxes: [
|
350
|
+
|
351
|
+
{
|
352
|
+
|
353
|
+
stacked: true,
|
354
|
+
|
355
|
+
scaleLabel: {
|
356
|
+
|
357
|
+
display: true,
|
358
|
+
|
359
|
+
},
|
360
|
+
|
361
|
+
},
|
362
|
+
|
363
|
+
],
|
364
|
+
|
365
|
+
},
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
tooltips: {
|
370
|
+
|
371
|
+
mode: 'index',
|
372
|
+
|
373
|
+
caretSize: 10,
|
374
|
+
|
375
|
+
titleFontSize: 24,
|
376
|
+
|
377
|
+
xAlign: "center",
|
378
|
+
|
379
|
+
yAlign: "bottom",
|
380
|
+
|
381
|
+
bodySpacing: 100,
|
382
|
+
|
383
|
+
backgroundColor: "yellowgreen",
|
384
|
+
|
385
|
+
position: "customMode",
|
386
|
+
|
387
|
+
cornerRadius: 20,
|
388
|
+
|
389
|
+
callbacks: {
|
390
|
+
|
391
|
+
label: (tooltipItems, data) => {
|
392
|
+
|
393
|
+
return;
|
394
|
+
|
395
|
+
},
|
396
|
+
|
397
|
+
title: (tooltipItems, data) => {
|
398
|
+
|
399
|
+
return ' 最新 ';
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
},
|
406
|
+
|
407
|
+
showAllTooltips: true,
|
408
|
+
|
409
|
+
responsive: true,
|
410
|
+
|
411
|
+
hover: { mode: null },
|
412
|
+
|
413
|
+
};
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
Chart.Tooltip.positioners.customMode = (elements, eventPosition) => {
|
418
|
+
|
419
|
+
let x = 0;
|
420
|
+
|
421
|
+
let y = 0;
|
422
|
+
|
423
|
+
let count = 0;
|
424
|
+
|
425
|
+
for (let el of elements) {
|
426
|
+
|
427
|
+
if (el && el.hasValue()) {
|
428
|
+
|
429
|
+
let pos = el.tooltipPosition();
|
430
|
+
|
431
|
+
x += pos.x;
|
432
|
+
|
433
|
+
y = pos.y;
|
434
|
+
|
435
|
+
++count;
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
return {
|
442
|
+
|
443
|
+
x: Math.round(x / count),
|
444
|
+
|
445
|
+
y: Math.round(y / count) - 20
|
446
|
+
|
447
|
+
};
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
const ctx = document.getElementById("chart").getContext("2d");
|
454
|
+
|
455
|
+
const myChart = new Chart(ctx, {
|
456
|
+
|
457
|
+
type: type,
|
458
|
+
|
459
|
+
data: data,
|
460
|
+
|
461
|
+
options: options,
|
462
|
+
|
463
|
+
});
|
464
|
+
|
465
|
+
</script>
|
466
|
+
|
467
|
+
</body>
|
468
|
+
|
469
|
+
```
|