简体   繁体   English

chart.js 绘制时间序列

[英]chart.js plotting timeseries

Attempting to pass through data from django to a webpage to render a responsive chart.试图将 django 中的数据传递到网页以呈现响应式图表。 The data are being passed correctly to js, but I am driving myself crazy trying to understand why charts.js is throwing an error.数据被正确地传递给 js,但我让自己发疯,试图理解为什么charts.js 会抛出错误。

I have hardcoded some data for example:我已经硬编码了一些数据,例如:

function setLineChart() {
    var ctx = document.getElementById("myLineChart").getContext('2d');
    var dat_1 = {
        label: 'things',
        borderColor: 'blue',
        data: [
            {t: new Date("04/01/2020"), y: 310},
            {t: new Date("04/02/2020"), y: 315},
            {t: new Date("04/03/2020"), y: 320},
            ]
    };
    var myLineChart = new Chart(ctx, {
        type: 'line',
        data: {
            datasets: [dat_1]
        },
        options: {
            scales: {
                xAxes: [{
                    type: 'time',
                    time: {
                        unit: 'day'
                    },
                }],
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
            }]
            }
        }
    })
}
<canvas id="myLineChart" width="600" height="600"></canvas>

And this returns a Uncaught TypeError: Cannot read property 'skip' of undefined error that I can't debug.这会返回一个Uncaught TypeError: Cannot read property 'skip' of undefined error that I can debug。 setLineChart() gets called as part of an ajax response on a form update. setLineChart()在表单更新时作为 ajax 响应的一部分被调用。 When I comment out the options section, it does render a chart, but misses off the last data point, and has undefined as the x-axis marker.当我注释掉options部分时,它确实呈现了一个图表,但错过了最后一个数据点,并且undefined为 x 轴标记。

Any help would be appreciated.任何帮助,将不胜感激。

Chart.js internally uses Moment.js for the functionality of the time axis . Chart.js 内部使用 Moment.js 来实现时间轴的功能。 Therefore you should use the bundled version of Chart.js that includes Moment.js in a single file.因此,您应该使用在单个文件中包含 Moment.js 的 Chart.js 的捆绑版本

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>

This will solve your problem as the following amended code snippet illustrates.这将解决您的问题,如下面的修改代码片段所示。

 var ctx = document.getElementById("myLineChart").getContext('2d'); var dat_1 = { label: 'things', borderColor: 'blue', data: [ { t: new Date("04/01/2020"), y: 310 }, { t: new Date("04/02/2020"), y: 315 }, { t: new Date("04/03/2020"), y: 320 }, ] }; var myLineChart = new Chart(ctx, { type: 'line', data: { datasets: [dat_1] }, options: { scales: { xAxes: [{ type: 'time', time: { unit: 'day' }, }], yAxes: [{ ticks: { beginAtZero: true } }] } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script> <canvas id="myLineChart" height="90"></canvas>

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

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