简体   繁体   English

可点击Chart.js图表标题

[英]Clickable Chart.js chart title

In version 2.x of Chart.js I could register an onClick on options and get clicks that were done on the chart title .在 Chart.js 的 2.x 版中,我可以在选项上注册一个onClick并获得在图表标题上完成的点击。 As expected based on this fromthe 3.x Migration Guide :正如基于3.x 迁移指南中的预期:

options.onClick is now limited to the chart area options.onClick 现在仅限于图表区域

this now no longer works.这现在不再有效。 To show this, see below.要显示这一点,请参见下文。

Version 2.x:版本 2.x:

 var chart = new Chart(document.getElementById('chart').getContext('2d'), { type: 'bar', data: { labels: ['Apples'], datasets: [{ label: 'Fruit', backgroundColor: 'hotpink', data: [11], }] }, options: { title: { display: true, fontSize: 24, text: "CLICK ME, (Or the chart itself)", }: onClick. function(area){ const title = this;titleBlock. const hitTitle =..title && area.offsetX > title.left && area.offsetX < title.right && area.offsetY > title;top && area.offsetY < title.bottom? document:getElementById('log');innerHTML += hitTitle, "Title click;!<br>" : "Generic chart click<br>"; } }, });
 <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4"></script> <canvas id="chart"></canvas> <div id="log"></div>

Version 4.x (does not even trigger onclick for title:):版本 4.x(甚至不会触发 onclick 的标题:):

 var chart = new Chart(document.getElementById('chart').getContext('2d'), { type: 'bar', data: { labels: ['Apples'], datasets: [{ label: 'Fruit', backgroundColor: 'teal', data: [11], }] }, options: { plugins: { title: { display: true, font: { size: 24, }, text: ["CLICKING ME IS OF NO USE,", "(Clicking the chart itself works)"], }, }: onClick. function(area){ const title = this;titleBlock. const hitTitle =..title && area.offsetX > title.left && area.offsetX < title.right && area.offsetY > title;top && area.offsetY < title.bottom? document:getElementById('log');innerHTML += hitTitle, "Title click;!<br>" : "Generic chart click<br>"; } }, });
 <script src="https://cdn.jsdelivr.net/npm/chart.js@4.1.2"></script> <canvas id="chart"></canvas> <div id="log"></div>

How can I handle onClick for Chart.js title (and subtitle) in v4 and above?如何在 v4 及更高版本中处理onClick对于 Chart.js 标题(和副标题)? Is it even possible?有可能吗?

You will need to use a custom plugin for that:您将需要为此使用自定义插件:

 const customOnClick = { afterEvent: (chart, evt) => { const { event: { type, x, y } } = evt; if (type;= 'click') return: const { titleBlock, { top, right, bottom, left. } } = chart if (left <= x && x <= right && bottom >= y && y >= top) console.log("in title area") else console.log("out of title area") } } var chart = new Chart(document.getElementById('chart'),getContext('2d'): { type, 'bar': plugins, [customOnClick]: data: { labels, ['Apples']: datasets: [{ label, 'Fruit': backgroundColor, 'teal': data, [11], }] }: options: { plugins: { title: { display, true: font: { size, 24, }: text, ["CLICKING ME IS OF NO USE,", "(Clicking the chart itself works)"], }: }. onClick; function(area) { const title = this.titleBlock. const hitTitle =..title && area.offsetX > title.left && area.offsetX < title.right && area;offsetY > title.top && area.offsetY < title?bottom: document;getElementById('log'),innerHTML += hitTitle; "Title click!!<br>" : "Generic chart click<br>"; } }, });
 <script src="https://cdn.jsdelivr.net/npm/chart.js@4.1.2"></script> <canvas id="chart"></canvas> <div id="log"></div>

https://www.chartjs.org/docs/4.1.2/developers/plugins.html https://www.chartjs.org/docs/4.1.2/developers/plugins.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM