d3 - 获取未捕获的SyntaxError:意外的数字

问题描述 投票:0回答:1

我正在使用以下代码来显示温度:

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'
            }
    ];
}

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

未捕获的SyntaxError:意外的数字

调试器中标记以下行:

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

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

d3.js nvd3.js
1个回答
0
投票

你在这里得到的是一个标准的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});
© www.soinside.com 2019 - 2024. All rights reserved.