想要显示数据集标签 [英] Want to show the dataset labels

查看:254
本文介绍了想要显示数据集标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一组数据的ChartJS折线图,其中有多条线.我想显示数据的工具提示.但是我做不到. 我的代码是

I am trying ChartJS line chart with a set of data.I have multiple lines in it. I want to show the tooltip for the data. but I am not able to. My code is

new Chart(canvas).Line(data,{
        responsive : true,
        animation: true,
        barValueSpacing : 5,
        barDatasetSpacing : 1,
        tooltipFillColor: "rgba(0,0,0,0.8)",
        multiTooltipTemplate: "<%= datasetLabel %> - <%= value %>",
        showTooltips:true
    });

目前我对折线图的看法是

currently my view of line chart is

我想要的就像是在此处输入图像描述

推荐答案

您可以使用自定义工具提示选项.以下代码是根据 https:/的示例改编而成的/github.com/nnnick/Chart.js/blob/master/samples/line-customTooltips.html

You can use the custom tooltips option. The below code is adapted from the example at https://github.com/nnnick/Chart.js/blob/master/samples/line-customTooltips.html

预览

CSS

#canvas-holder {
    width: 500px;
    height: 300px;
}
#chartjs-tooltip {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, .7);
    font-size: 12px;
    color: white;
    padding: 5px;
    border-radius: 3px;
    -webkit-transition: all .1s ease;
    transition: all .1s ease;
    pointer-events: none;
}
.chartjs-tooltip-section{
    padding: 1px;
}

HTML

<div id="canvas-holder">
    <canvas id="chart" />
</div>
<div id="chartjs-tooltip"></div>

脚本

function CustomLabel(short, long) {
    this.short = short;
    this.long = long;
}
CustomLabel.prototype.toString = function () {
    return this.short;
}

var data = {
    labels: [
        new CustomLabel("January", "January 11"),
        new CustomLabel("February", "February 12"),
        new CustomLabel("March", "March 13"),
        new CustomLabel("April", "April 14"),
        new CustomLabel("May", "May 15"),
        new CustomLabel("June", "June 16"),
        new CustomLabel("July", "July 17")],
    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]
        },
        {
            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");
new Chart(ctx).Line(data, {
    responsive: true,
    customTooltips: function (tooltip) {
        var tooltipEl = $('#chartjs-tooltip');
        if (!tooltip) {
            tooltipEl.css({
                opacity: 0
            });
            return;
        }

        tooltipEl.removeClass('above below');
        tooltipEl.addClass(tooltip.yAlign);

        var innerHtml = ['<div class="chartjs-tooltip-section">',
            '   <span>' + tooltip.title.long + '</span>',
            '</div>'
        ].join('');
        for (var i = tooltip.labels.length - 1; i >= 0; i--) {
            innerHtml += [
                '<div class="chartjs-tooltip-section">',
                '   <span style="color:' + tooltip.legendColors[i].fill + '">' + data.datasets[i].label + ': ' + tooltip.labels[i] + '</span>',
                '</div>'
            ].join('');
        }
        tooltipEl.html(innerHtml);

        tooltipEl.css({
            opacity: 1,
            left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px',
            top: tooltip.chart.canvas.offsetTop + tooltip.y + 'px',
            fontFamily: tooltip.fontFamily,
            fontStyle: tooltip.fontStyle,
        });
    }
});

小提琴- http://jsfiddle.net/fLm8c5ee/

这篇关于想要显示数据集标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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