【问题标题】:How to plot date/time on X but display only dates in Chart.js?如何在 X 上绘制日期/时间,但在 Chart.js 中仅显示日期?
【发布时间】:2018-03-22 00:35:36
【问题描述】:

我有一系列数据点,每个数据点对应一个特定的日期/时间。每个点之间的时间各不相同,范围涵盖数天(最多 30 天)。每天可能有多个点,也可能有几天没有数据点。

我希望 X 刻度显示日期(包括星期几),并根据日期和时间将数据点绘制到它们应该去的地方。例如,2018-02-15 12:00:00 将介于 02-15 和 02-16 的标签之间。

这里是一些示例代码/数据:

var chartData = {
    type:'line',
    data: {
        datasets: [
            {
                label:'Depression',
                backgroundColor:'#00f',
                borderColor:'#00f',
                data:[
                    {y:3,t:Date.parse('2018/02/20 05:01:16')},
                    {y:4,t:Date.parse('2018/02/20 15:03:09')},
                    {y:5,t:Date.parse('2018/02/21 05:04:09')},
                    {y:1,t:Date.parse('2018/02/21 08:06:09')},
                    {y:5,t:Date.parse('2018/02/22 05:05:09')},
                ],
                fill:false,
            },
            {
                label:'Anxiety',
                backgroundColor:'#d0d',
                borderColor:'#d0d',
                data:[
                    {y:6,t:Date.parse('2018/02/20 05:01:16')},
                    {y:2,t:Date.parse('2018/02/20 15:03:09')},
                    {y:4,t:Date.parse('2018/02/21 05:04:09')},
                    {y:6,t:Date.parse('2018/02/21 08:06:09')},
                    {y:3,t:Date.parse('2018/02/22 05:05:09')},
                ],
                fill:false,
            },
            {
                label:'Activity',
                backgroundColor:'#f90',
                borderColor:'#f90',
                data:[
                    {y:4,t:Date.parse('2018/02/20 05:01:16')},
                    {y:1,t:Date.parse('2018/02/20 15:03:09')},
                    {y:4,t:Date.parse('2018/02/21 05:04:09')},
                    {y:7,t:Date.parse('2018/02/21 08:06:09')},
                    {y:3,t:Date.parse('2018/02/22 05:05:09')},
                ],
                fill:true,
            },
            {
                label:'Physical Health',
                backgroundColor:'#ffc',
                borderColor:'#cc0',
                data:[
                    {y:-2,t:Date.parse('2018/02/20 05:01:16')},
                    {y:-3,t:Date.parse('2018/02/20 15:03:09')},
                    {y:-2,t:Date.parse('2018/02/21 05:04:09')},
                    {y:-6,t:Date.parse('2018/02/21 08:06:09')},
                    {y:-5,t:Date.parse('2018/02/22 05:05:09')},
                ],
                fill:true,
            },
        ],
        fill:false,
    },
    'options': {
        responsive:true,
        title:{
            display:true,
            text:'Recent History',
        },
        scales: {
            xAxes:[{
                display:true,
                time: {
                    min:Date.parse('2018/02/01 00:00:00'),
                    max:Date.parse('2018/03/01 00:00:00'),
                    displayFormats:{
                        day:'ddd MM/DD',
                    },
                    unit:'day',
                    round:'day',
                },
                distribution:'linear',
                scaleLabel: {
                    display:true,
                    labelString:'Date'
                },
                bounds:'data',
                ticks: {
                    source:'data',
                },
            }],
            yAxes:[{
                display:true,
                scaleLabel:'Rating'
            }]
        },
    }
};

上面的代码只显示每个系列的前两个数据点。

谢谢!

【问题讨论】:

    标签: javascript chart.js


    【解决方案1】:

    http://www.chartjs.org/docs/latest/axes/cartesian/time.html#ticks-source http://www.chartjs.org/docs/latest/axes/labelling.html#scale-title-configuration

    你可以尝试改变

    ticks: {
       source:'data',
    },
    

    ...到source:'auto',它会为您自动截断刻度标签,然后看看它是什么样子...

    X 轴is described here 的时间刻度显示格式,因此您可以将其格式化为您想要的任何 Moment.js 时间格式,例如'YYYY-mm-dd:

     xAxes: [{
                    type: 'time',
                    time: {
                        displayFormats: {
                            quarter: 'MMM YYYY'
                        }
                    }
                }]
    

    ...虽然看起来你可能已经在里面了。它没有显示任何 x 轴刻度吗?它是否显示任何错误?

    或者,如果您想包含日期/时间以外的内容,您可以尝试关注this example 来覆盖自定义“tick”的回调方法。他们的示例是在所有值之前附加一个美元符号。如果您只想处理日期时间,那么上述解决方案应该就足够了。

    【讨论】:

    • 感谢您的建议。我试过了。添加type:'time' 导致浏览器要求停止代码需要很长时间,并且它从未显示图表。将“自动”更改为“数据”似乎没有什么不同。它只显示前两个数据点,X 轴上的标签没有显示任何内容。我确定我在 options.scales 部分有问题,但找不到此用例的任何参考示例。还有其他想法吗?
    • 除了将 type:'time' 添加到 xAxes 之外,我还尝试了另一件事。我还将发行版更改为“系列”,现在它似乎可以工作了!感谢@mc01 为我指明了正确的方向!!
    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多