javascript Google Chart API 中的数字类型不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24365731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:39:08  来源:igfitidea点击:

Type mismatch for number in Google Chart API

javascriptgoogle-visualization

提问by user3362533

I have an array and the second column with values like this 2050.878456and inside my javascript function to create a Area Chart I made the following

我有一个数组和第二列这样的值2050.878456,在我的 javascript 函数中创建了一个面积图我做了以下

function drawVisualization() {
    var data = null;
    data = new google.visualization.DataTable();
    data.addColumn('string', 'Date');
    data.addColumn('number', 'Value');
    data.addRows(myArrayCreated);
    // Create and draw the visualization.
    var ac = new google.visualization.AreaChart(document
            .getElementById('visualization_chart'));
    ac.draw(data, {
        title : 'Results',
        isStacked : true,
        width : 700,
        height : 400,
        vAxis : {title : "kW"},
        hAxis : {title : "Day"}
    });

}

however I get this error Type mismatch. Value 2050.878456 does not match type number in column index 1however it cannot be a string type as well, why do I get this error and how to fix it?

但是我收到此错误,Type mismatch. Value 2050.878456 does not match type number in column index 1但它也不能是字符串类型,为什么我会收到此错误以及如何修复它?

回答by Fr0zenFyr

Try passing the Valueas stringand then later do a parseFloat. Something like this:

尝试传递Valueasstring然后稍后执行parseFloat. 像这样的东西:

data.addColumn('string', 'Value');

for (var i=0;i<myArrayCreated.length;i++){
    myVal = parseFloat($.trim(myArrayCreated[i][1])); 
    data.addRow([i, {v: myVal, f: myval.toFixed(6)}]); 
}

回答by Martin Pfeffer

I spotted the same issue.

我发现了同样的问题。

not working:

不工作:

data.addRow([v, obd[v].engine_rpm]);

working:

在职的:

data.addRow([v, Number(obd[v].engine_rpm)]);

wtf

武侠