Chart.js v2:如何使工具提示始终显示在折线图上?
我已经搜索了解决方案,但我只找到了饼图的解决方案。例如,这个: 我只想让工具提示在不悬停的情况下可见。我试着注册新的pluginService,但它不工作,并且产生了这个错误 未捕获的TypeError:无法读取未定义的属性“call” 这就是我目前所拥有的Chart.js v2:如何使工具提示始终显示在折线图上?,chart.js,Chart.js,我已经搜索了解决方案,但我只找到了饼图的解决方案。例如,这个: 我只想让工具提示在不悬停的情况下可见。我试着注册新的pluginService,但它不工作,并且产生了这个错误 未捕获的TypeError:无法读取未定义的属性“call” 这就是我目前所拥有的 Chart.pluginService.register({ beforeRender: function (chart) { if (chart.config.options.sh
Chart.pluginService.register({
beforeRender: function (chart) {
if (chart.config.options.showAllTooltips) {
// create an array of tooltips
// we can't use the chart tooltip because there is only one tooltip per chart
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]
}, chart));
});
});
// turn off normal tooltips
chart.options.tooltips.enabled = false;
}
},
afterDraw: function (chart, easing) {
if (chart.config.options.showAllTooltips) {
// we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
if (!chart.allTooltipsOnce) {
if (easing !== 1)
return;
chart.allTooltipsOnce = true;
}
// turn on tooltips
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 myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
showAllTooltips: true
}
});
感谢所有帮助tq当使用最新稳定版本的Chart.js(2.9.3)时,它可以进行以下更改
更改\u选项:图表.选项
写入\u选项:图表.选项.工具提示
Chart.plugins.register({
渲染前:函数(图表){
if(chart.config.options.showAllTooltips){
//创建一组工具提示,
//我们无法使用图表工具提示,因为每个图表只有一个工具提示
chart.pluginTooltips=[];
chart.config.data.datasets.forEach(函数(dataset,i){
chart.getDatasetMeta(i).data.forEach(函数(扇区,j){
chart.pluginTooltips.push(新图表工具提示({
_查特:查特,查特,
_chartInstance:chart,
_数据:图表数据,
_选项:chart.options.tooltips,
_活跃:[行业]
});
});
});
chart.options.tooltips.enabled=false;//关闭常规工具提示
}
},
后拉伸:功能(图表、缓和){
if(chart.config.options.showAllTooltips){
如果(!chart.allTooltipsOnce){
如果(缓和!==1){
返回;
}
chart.allTooltipsOnce=true;
}
chart.options.tooltips.enabled=true;
Chart.helpers.each(Chart.pluginTooltips,函数(工具提示){
tooltip.initialize();
tooltip.update();
tooltip.pivot();
工具提示.transition(缓和).draw();
});
chart.options.tooltips.enabled=false;
}
}
});
新图表(“图表”{
键入:“行”,
数据:{
标签:[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”],
数据集:[{
标签:“我的第一个数据集”,
填充颜色:“rgba(220220,0.2)”,
strokeColor:“rgba(2201)”,
点颜色:“rgba(220220,1)”,
pointStrokeColor:“fff”,
pointHighlightFill:“fff”,
pointHighlightStroke:“rgba(2201)”,
数据:[65,59,80,81,56,55,40]
}]
},
选项:{
比例:{
雅克斯:[{
滴答声:{
贝吉纳泽罗:是的
}
}]
},
showAllTooltips:true
}
});代码>
我们如何在特定数据点上显示它?例如,仅适用于装载的April
。