如何在Chart.js中显示工具提示?

时间:2014-07-02 08:02:26

标签: javascript charts

在本教程之后,此代码绘制折线图,​​但没有工具提示。我在这里错过了一些配置选项吗?在教程中有工具提示出现。

var chartData = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.2)",
            strokeColor: "rgba(151,187,205,1)",
            pointColor: "rgba(151,187,205,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};

var ctx = document.getElementById("chart").getContext("2d");
var myNewChart = new Chart(ctx).Line(chartData, {
    showTooltip: true,
    tooltipTemplate: "<%= value %>"
});

3 个答案:

答案 0 :(得分:11)

您使用的是什么版本的chart.js?

我可以确认tooltips work using v1.0.1-beta2

<script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1-beta.2/Chart.min.js"></script>

但不起作用using v0.2.0

版本1.0.1-beta2可从cdnjs获得。

答案 1 :(得分:1)

它正常工作并正确显示工具提示..你在控制台中是否收到任何错误?

以下是我使用您的代码的方式:

var chartData = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.2)",
            strokeColor: "rgba(151,187,205,1)",
            pointColor: "rgba(151,187,205,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};

window.onload = function(){
    var ctx = document.getElementById("chart").getContext("2d");
    window.myNewChart = new Chart(ctx).Line(chartData, {
        showTooltip: true,
        tooltipTemplate: "<%= value %>"
    });
};

答案 2 :(得分:0)

你只需要将backgroundColor用一个值而不是数组:

datasets: [{
    label: "# of beauty womens",
    data: [12, 5, 3],
    backgroundColor: "#FC940B",
    fill: false,
    borderColor: "#FC940B"
}]

...拥抱