简体   繁体   English

当线未从第一个标签开始时,Chart.js 折线图工具提示显示错误的标签

[英]Chart.js line chart tooltip shows wrong label when line doesn't start at first label

I have a simple line chart with 5 labels on x-axis: 'Oct 2018', 'Jan 2019', 'Apr 2019', 'Jul 2019', 'Oct 2019' and a line which has points only on 3 middle labels 'Jan 2019', 'Apr 2019', 'Jul 2019' .我有与X轴5层的标签的简单线图: 'Oct 2018', 'Jan 2019', 'Apr 2019', 'Jul 2019', 'Oct 2019' ,并且具有仅在3个中间标签的点的线'Jan 2019', 'Apr 2019', 'Jul 2019'

The problem is that when line data doesn't start from first label (like in my case), the tooltip shows incorrect label on hover.问题是当行数据不是从第一个标签开始时(就像我的例子),工具提示在悬停时显示不正确的标签。 As you can see from below image, when I hover on 'Apr 2019' data point, tooltip shows 'Jan 2019' .如下图所示,当我将鼠标悬停在'Apr 2019'数据点上时,工具提示显示'Jan 2019' Similarly, when I hover on 'Jul 2019' data point, tooltip shows 'Apr 2019' .同样,当我将鼠标悬停在'Jul 2019'数据点上时,工具提示显示'Apr 2019' Can you tell me what am I missing?你能告诉我我错过了什么吗?

错误的工具提示标签

Here's the code:这是代码:

<canvas id="canvas" ></canvas>
<script>
    var lineChartData = {
        labels: ['Oct 2018', 'Jan 2019', 'Apr 2019', 'Jul 2019', 'Oct 2019'],
        datasets: [
            {
                label: 'Oranges',
                borderColor: window.chartColors.blue,
                backgroundColor: window.chartColors.blue,
                fill: false,
                data: 
                [
                    {y: 30, x: 'Jan 2019'},
                    {y: 20, x: 'Apr 2019'},
                    {y: 25, x: 'Jul 2019'},
                ],              
            }           
        ]
    };

    window.onload = function() {
        var ctx = document.getElementById('canvas').getContext('2d');
        window.myLine = Chart.Line(ctx, {
            data: lineChartData,
            options: {
                responsive: true,
                hoverMode: 'index',
                stacked: false,
                scales: {
                    yAxes: [{
                        type: 'linear',
                        display: true,
                        position: 'left'
                    }],
                }
            }
        });
    };
</script>

Chart.js doesn't like date-labels as string. Chart.js不喜欢将日期标签作为字符串。 You should use a Date() or momentjs() object to give a date.您应该使用Date()momentjs()对象来给出日期。


Using MomentJS, your graph could look something like this;使用 MomentJS,您的图表可能如下所示;

 const newDate = (mdy) => moment(mdy, "MM-DD-YYYY"); var lineChartData = { labels: [newDate('9-1-2018'), newDate('1-1-2019'), newDate('4-1-2019'), newDate('6-1-2019'), newDate('9-1-2019')], datasets: [{ label: 'Oranges', borderColor: 'blue', backgroundColor: 'orange', fill: false, data: [ { y: 30, x: newDate('1-1-2019') }, { y: 20, x: newDate('4-1-2019') }, { y: 25, x: newDate('6-1-2019') } ], }] }; window.onload = function() { var ctx = document.getElementById('canvas').getContext('2d'); window.myLine = Chart.Line(ctx, { data: lineChartData, options: { hover: { mode: 'new mode' }, responsive: true, hoverMode: 'index', stacked: false, scales: { yAxes: [{ type: 'linear', display: true, position: 'left' }], xAxes: [{ type: 'time', time: { displayFormats: { 'millisecond': 'MMM YY', 'second': 'MMM YY', 'minute': 'MMM YY', 'hour': 'MMM YY', 'day': 'MMM YY', 'week': 'MMM YY', 'month': 'MMM YY', 'quarter': 'MMM YY', 'year': 'MMM YY', } } }] } } }); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script> <canvas id="canvas" ></canvas>

(Note; the extra empty data could me removed, please take a look at the Chartjs time docs ) (注意;我可以删除多余的空数据,请查看Chartjs 时间文档


EDIT: Based on comments, a more complete solution, with the original data being parsed as the OP intended.编辑:基于评论,一个更完整的解决方案,原始数据被解析为 OP 的意图。

 const newDate = (mdy) => moment(mdy, "MMM YYYY"); const data = { labels: ['Oct 2018', 'Jan 2019', 'Apr 2019', 'Jul 2019', 'Oct 2019'], points: [ {y: 30, x: 'Jan 2019'}, {y: 20, x: 'Apr 2019'}, {y: 25, x: 'Jul 2019'} ] }; var lineChartData = { labels: data.labels.map(l => newDate(l)), datasets: [{ label: 'Oranges', borderColor: 'blue', backgroundColor: 'orange', fill: false, data: data.points.map(p => { return { ...p, x: newDate(px) } }) }] }; window.onload = function() { var ctx = document.getElementById('canvas').getContext('2d'); window.myLine = Chart.Line(ctx, { data: lineChartData, options: { hover: { mode: 'new mode' }, responsive: true, hoverMode: 'index', stacked: false, scales: { yAxes: [{ type: 'linear', display: true, position: 'left' }], xAxes: [{ type: 'time', time: { parser: 'MMM YYYY', tooltipFormat: 'MMM YYYY', displayFormats: { 'millisecond': 'MMM YYYY', 'second': 'MMM YYYY', 'minute': 'MMM YYYY', 'hour': 'MMM YYYY', 'day': 'MMM YYYY', 'week': 'MMM YYYY', 'month': 'MMM YYYY', 'quarter': 'MMM YYYY', 'year': 'MMM YYYY', } } }] } } }); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script> <canvas id="canvas" ></canvas>

Basically, the answer is to replace this:基本上,答案是替换这个:

[
    {y: 30, x: 'Jan 2019'},
    {y: 20, x: 'Apr 2019'},
    {y: 25, x: 'Jul 2019'},
]

with this:有了这个:

[null, 30, 20, 25, null]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM