js の chart.js プラグインへの質問です。
ページ内に複数あるグラフの内、特定のグラフに絞ってツールチップ常時表示する方法を模索しています。
以下のように、ページ内にある全てのグラフにツールチップの表示をさせる事が実現できたのですが、
var data = { labels: ['18/01', '18/02', '18/03', '18/04'], datasets: [ { label: 'hoge', fill: false, backgroundColor: "#000", borderColor: '#000', pointBorderColor: '#000', tension: 0, data: [10, 15, 13, 12] }, { label: 'fuga', fill: false, backgroundColor: "#00f", borderColor: '#00f', pointBorderColor: '#00f', tension: 0, data: [12, 13, 16, 13] }, ] }; Chart.pluginService.register({ beforeRender: function (chart) { console.log(chart); if (chart.config.options.showAllTooltips) { chart.pluginTooltips = []; chart.config.data.datasets.forEach(function (dataset, i) { chart.getDatasetMeta(i).data.forEach(function (sector, j) { chart.pluginTooltips.push(new Chart.Tooltip({ _chart: chart.chart, _chartInstance: chart, _data: chart.data, _options: chart.options, _active: [sector], no: i }, chart)); }); }); chart.options.tooltips.enabled = false; } }, afterDraw: function (chart, easing) { console.log(chart); if (chart.config.options.showAllTooltips) { if (!chart.allTooltipsOnce) { if (easing !== 1) return; chart.allTooltipsOnce = true; } chart.options.tooltips.enabled = true; Chart.helpers.each(chart.pluginTooltips, function (tooltip) { tooltip.initialize(); tooltip.update(); // we don't actually need this since we are not animating tooltips tooltip.pivot(); tooltip.transition(easing).draw(); }); chart.options.tooltips.enabled = false; } } }) var ctx = $('#hogehoge').get(0).getContext('2d'); var myChart = new Chart(ctx, { type: 'line', data: data, options: { showAllTooltips: true, responsive: true, maintainAspectRatio: false, tooltips: { mode: 'single', }, }, }); ctx = $('#fugafuga').get(0).getContext('2d'); myChart = new Chart(ctx, { type: 'line', data: data, options: { showAllTooltips: true, responsive: true, maintainAspectRatio: false, tooltips: { mode: 'single', }, }, });
hogehoge、fugafuga、両方のグラフにツールチップが表示されます。
以下のように個別プラグイン化することで、個別のグラフに対応できるのではと考えたのですが、
どうもプラグインへのアクセス自体が確認できないような状態です。
var data = { labels: ['18/01', '18/02', '18/03', '18/04'], datasets: [ { label: 'hoge', fill: false, backgroundColor: "#000", borderColor: '#000', pointBorderColor: '#000', tension: 0, data: [10, 15, 13, 12] }, { label: 'fuga', fill: false, backgroundColor: "#00f", borderColor: '#00f', pointBorderColor: '#00f', tension: 0, data: [12, 13, 16, 13] }, ] }; var plugin = { beforeRender: function (chart) { console.log(chart); if (chart.config.options.showAllTooltips) { chart.pluginTooltips = []; chart.config.data.datasets.forEach(function (dataset, i) { chart.getDatasetMeta(i).data.forEach(function (sector, j) { chart.pluginTooltips.push(new Chart.Tooltip({ _chart: chart.chart, _chartInstance: chart, _data: chart.data, _options: chart.options, _active: [sector], no: i }, chart)); }); }); chart.options.tooltips.enabled = false; } }, afterDraw: function (chart, easing) { if (chart.config.options.showAllTooltips) { if (!chart.allTooltipsOnce) { if (easing !== 1) return; chart.allTooltipsOnce = true; } chart.options.tooltips.enabled = true; Chart.helpers.each(chart.pluginTooltips, function (tooltip) { tooltip.initialize(); tooltip.update(); tooltip.pivot(); tooltip.transition(easing).draw(); }); chart.options.tooltips.enabled = false; } } } var ctx = $('#hogehoge').get(0).getContext('2d'); var myChart = new Chart(ctx, { type: 'line', data: data, plugins: [plugin], options: { showAllTooltips: true, responsive: true, maintainAspectRatio: false, tooltips: { mode: 'single', }, }, }); ctx = $('#fugafuga').get(0).getContext('2d'); myChart = new Chart(ctx, { type: 'line', data: data, options: { showAllTooltips: true, responsive: true, maintainAspectRatio: false, tooltips: { mode: 'single', }, }, });
そもそものアプローチが間違っていたりしますでしょうか。
ご存知の方、いらっしゃればアドバイスをいただけましたら幸いです。
あなたの回答
tips
プレビュー