如何向chart.js图添加工具提示

时间:2021-12-14 10:06:29

I'm working on a webapp and I recently swapped google charts with chart.js because it is visually more appealing. However, the one loss that I took is that I can no longer get tooltips above the data points. I was wondering if anyone knows how this can be done as I am a novice with javascript. Here is the code for the graph and the settings:

我正在开发一个webapp,最近我用map.js交换了谷歌图表,因为它在视觉上更具吸引力。但是,我所遭受的一个损失就是我无法再获得数据点之上的工具提示。我想知道是否有人知道如何做到这一点,因为我是一个新手javascript。以下是图表和设置的代码:

    var data = {
            labels : graphData[0],
            datasets : [
                {
                    fillColor : "rgba(200,160,100,0.5)",
                    strokeColor : "rgba(80,240,70,1)",
                    pointColor : "rgba(80,240,70,1)",
                    pointStrokeColor : "#fff",
                    data : graphData[3]
                },
                {
                    fillColor : "rgba(220,220,220,0.5)",
                    strokeColor : "rgba(220,220,220,1)",
                    pointColor : "rgba(220,220,220,1)",
                    pointStrokeColor : "#fff",
                    data : graphData[1]
                },
                {
                    fillColor : "rgba(151,187,205,0.5)",
                    strokeColor : "rgba(151,187,205,1)",
                    pointColor : "rgba(151,187,205,1)",
                    pointStrokeColor : "#fff",
                    data : graphData[2]
                }
            ]
        };

        var options = {
            scaleShowGridLines : true,
            scaleShowLabels : true,
            animationSteps : 150,
            scaleOverride: true,
            scaleSteps : Math.max.apply(Math, graphData[3]),
            scaleStepWidth : 1,
            scaleStartValue : 1
        };

        var ctx = document.getElementById("chart_div").getContext("2d");
        ctx.canvas.width  = Math.max(graphData[0].length * 60, 600);
        var myNewChart = new Chart(ctx).Line(data,options);
        $('#chart_area').fadeIn();
        $('html, body').animate({
             scrollTop: $("#picture_area").offset().top
         }, 1000);

3 个解决方案

#1


8  

There is a chartjs version available along with tooltip you can get it in this github page https://github.com/Regaddi/Chart.js/tree/tooltips

有一个chartjs版本以及工具提示你可以在这个github页面中获取它https://github.com/Regaddi/Chart.js/tree/tooltips

Looks like the above link is no longer available

看起来上面的链接不再可用

However its available here in Github

但是它可以在Github上找到

You can see the documentation here

您可以在此处查看文档

#2


5  

You just have to copy Chart.min.js from https://github.com/nnnick/Chart.js/blob/master/Chart.min.js.

您只需要从https://github.com/nnnick/Chart.js/blob/master/Chart.min.js复制Chart.min.js。

#3


1  

var ctx = document.getElementById("canvas").getContext("2d");
    window.myLine = new Chart(ctx).Line(lineChartData, {
        responsive: true,
        showTooltips: true,
        multiTooltipTemplate: "<%= value %>",
    });

use this to set a gloabl settings in chartjs.

用它来设置chartjs中的gloabl设置。

#1


8  

There is a chartjs version available along with tooltip you can get it in this github page https://github.com/Regaddi/Chart.js/tree/tooltips

有一个chartjs版本以及工具提示你可以在这个github页面中获取它https://github.com/Regaddi/Chart.js/tree/tooltips

Looks like the above link is no longer available

看起来上面的链接不再可用

However its available here in Github

但是它可以在Github上找到

You can see the documentation here

您可以在此处查看文档

#2


5  

You just have to copy Chart.min.js from https://github.com/nnnick/Chart.js/blob/master/Chart.min.js.

您只需要从https://github.com/nnnick/Chart.js/blob/master/Chart.min.js复制Chart.min.js。

#3


1  

var ctx = document.getElementById("canvas").getContext("2d");
    window.myLine = new Chart(ctx).Line(lineChartData, {
        responsive: true,
        showTooltips: true,
        multiTooltipTemplate: "<%= value %>",
    });

use this to set a gloabl settings in chartjs.

用它来设置chartjs中的gloabl设置。