ChartJS:获取有关悬停点的点信息 [英] ChartJS: get points information on hovering the points

查看:128
本文介绍了ChartJS:获取有关悬停点的点信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过 onClick 函数成功获取了信息。但是有什么方法可以获取关于悬停在该点上的点信息。目前,文档中提到的 onHover 不能解决问题。这是我的 jsFiddle

I have successfully got the information by onClick function. But is there any method i can get the point information on Hovering over the point. Right now onHover as mentioned in docs is not working to get the point. This is my jsFiddle.

推荐答案

在Chart.js的早期版本(例如2.6)中,onHover处理程序必须配置如下:

In previous versions of Chart.js (for example 2.6) the onHover handler has to be configured as below:

  hover: { 
     onHover: function(evt, item) { 
        if (item.length) {
            console.log("onHover", item, evt.type);
            console.log(">data", item[0]._index, data.datasets[0].data[item[0]._index]);
        }
     }
  },

itme [0] ._ index 属性指向目标项目的数据

itme[0]._index property points to data of target item

所以您更新的小提琴(chart.js 2.6)是: https://jsfiddle.net/beaver71/440L5661/

So your fiddle (chart.js 2.6) updated is: https://jsfiddle.net/beaver71/440L5661/

使用chart.js 2.7: https://jsfiddle.net/beaver71/ttrak7sj/

With chart.js 2.7: https://jsfiddle.net/beaver71/ttrak7sj/

这篇关于ChartJS:获取有关悬停点的点信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆