d3 - 获取未捕获的语法错误:意外数字



>我使用以下代码来可视化温度:

nv.addGraph(function() {
var chart = nv.models.lineChart()
            .margin({left: 100})        // Margin space
    .margin({bottom: 130})
            .useInteractiveGuideline(true)  // Activate tooltips
            .transitionDuration(500)    // Transition delay
            .showLegend(true)           // Show the legend (Turn on/off line series)
;

chart.xAxis    
        .axisLabel('Datum')
        .rotateLabels(-45)
        .tickFormat(function(d) { 
        // 01/12/2013 12:00 Uhr
        return d3.time.format('%Y-%m-%d %H:%M:%S.%L')(new Date(d))
    });

chart.yAxis     
        .axisLabel('Temperatur')
        .tickFormat(d3.format('.01f'))
;

var tempHumidData = getTempHumidData();

d3.select('#chart svg') 
        .datum(tempHumidData) 
        .call(chart) 
;

nv.utils.windowResize(function() { chart.update() });
return chart;
});

function getTempHumidData() {
    var tempOutside  = []
    // Data is represented as an array of {x,y} pairs
    // Hint: The UNIX timestamp from the DB must be multiplied by 1000
    //       as the JS date datatype works with milliseconds
    <?php
            for($i=0; $i < count($tempOutside); $i++)
    {
            echo "tempOutside.push({x: $tempOutsideDate[$i], y: $tempOutside[$i]});n";
    }
   ?>

    // Line chart data should be sent as an array of series objects.
    return [
            {
                    values: tempOutside,
                    key: 'Temperatur Aussen',
                    area: true
                    //color: '#FFa02c'
            }
    ];
}

但是当我在浏览器中执行此操作时,我收到以下错误:

未捕获的语法错误:意外的数字

调试器中标记了以下行:

tempOutside.push({x: 2018-02-27 21:00:09.779805, y: -10.9}(;

所以我不明白我的问题在哪里。在哪里可以为此行指定正确的格式?

你在这里得到的是一个标准的JavaScript错误,这是因为你的代码无效。我想它失败了,因为它试图将2018-02-27 21:00:09.779805视为一个数字。以下是此错误的重现:

const myArray = [];
myArray.push({ x: 2018-02-27 21:00:09.779805, y: 3 });

相反,这应该是一个string,或者很可能是一个date。尝试从中构建一个日期,如下所示:

tempOutside.push({x: new Date("2018-02-27 21:00:09.779805"), y: -10.9});

最新更新