简体   繁体   English

为什么我的HighCharts图表无法呈现?

[英]Why won't my HighCharts chart render?

I'm trying to plot a chart to show time against temperature using highcharts. 我正在尝试绘制图表以使用高图显示时间与温度的关系。

I have three sensors, and data is recorded every 5 minutes (which I believe is 5*60 in JS). 我有三个传感器,每5分钟记录一次数据(我相信在JS中为5 * 60)。

For some reason, it won't render a scale for the x axis, or any data on the graph. 由于某些原因,它不会为x轴或图形上的任何数据绘制比例。 However the barebones of the graph and the y axis scale render fine. 但是,图形的准系统和y轴比例尺效果很好。

I also get the following error in the console: 我也在控制台中收到以下错误:

Error: Invalid value for <circle> attribute cx="NaN"

If you can spot anything missing or incorrect with my chart code that would be good. 如果您可以通过我的图表代码发现任何缺失或不正确的地方,那将是很好的。

My code is below - I've commented in any external references (they're from a JSON data source, and I've checked that it's all valid data when it comes through). 我的代码在下面-我在任何外部引用中都进行了注释(它们来自JSON数据源,并且检查了它们通过时是否均为有效数据)。

tempChart = new Highcharts.Chart({
        chart: {
            renderTo:'tempChart', // just a div with id of tempChart
            zoomType:'x',
            type:'line'
        },
        title: {
            text: 'Server Temperature'
        },
        xAxis: {
            type: 'datetime',
            maxZoom: 5*60*1000, // limit zoom to one record (5 minutes)
            title: {
                text: 'Timestamp'
            }
        },
        yAxis: {
            title: {
                text: 'Temperature'
            },
        },
        tooltip: {
            shared: true                    
        },
        series:[{
            type: 'line',
            name: 'Sensor 1',
            pointInterval:5*60*1000,
            pointStart:startDate, // this is a js date object
            data:data.temp1 // this is a javascript array object
        }, {
            type: 'line',
            name: 'Sensor 2',
            pointInterval:5*60*1000,
            pointStart:startDate, // same js date object
            data:data.temp2 // javascript array object
        }, {
            type: 'line',
            name: 'Sensor 3',
            pointInterval:5*60*1000,
            pointStart:startDate, // same
            data:data.temp3 // javascript array object
        }]
    });

Update 更新

I construct the date by using the following function to convert a MySQL date to a JS one ( source ) 我通过使用以下函数将MySQL日期转换为JS日期来构造日期(

function sql2js(timestamp) {
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}

So at the beginning of my function to create the chart, I use that function on the MySQL date from my JSON data, like so: 因此,在创建图表的函数开始时,我在JSON日期从JSON数据中使用该函数,如下所示:

startDate = sql2js(data.startDate);

Just to make sure that the dates were correct, I logged them both: 为了确保日期正确,我将它们都记录了下来:

MySQL date: 2012-01-03 17:05:01
JS date:    Tue Jan 03 2012 17:05:01 GMT+0000 (GMT)

How do you construct your Date object ? 您如何构造Date对象?

I've played with your example on a JSFiddle , and saw that if I made a bad declaration of the date object (for example, var startDate = new Date() ), I got the same error you mentioned. 我在JSFiddle上处理了您的示例,发现如果对日期对象进行了错误的声明(例如, var startDate = new Date() ),则会遇到与您提到的相同的错误。

But with a proper Date object, all works fine 但是使用适当的Date对象,一切正常

EDITED : 编辑:

In fact, HighCharts asks for a millisecond UTC date, so you'll have to do 实际上,HighCharts要求提供毫秒级的UTC日期,因此您必须

var startDate = sql2js('2012-01-03 17:05:01').getUTCMilliseconds();

as updated in the JSFiddle JSFiddle中所更新

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

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