Type mismatch for number in Google Chart API
Asked Answered
P

2

9

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

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 1 however it cannot be a string type as well, why do I get this error and how to fix it?

Pustulate answered 23/6, 2014 at 12:17 Comment(7)
Are you sure that you are getting Value as a number? Did you check if it is a string by any chance? This is one of the most common errors.Altar
I changed for string as you said like this data.addColumn('string', 'Value'); for (i = 0; i < myArrayCreated.length; i++){ data.addRow(myArrayCreated[i][0] , parseFloat(myArrayCreated[i][1]).toFixed(6)); } however get this error If argument is given to addRow, it must be an array, or nullPustulate
Do this instead: for (var i=0;i<myArrayCreated.length;i++){ myVal = parseFloat($.trim(myArrayCreated[i][0])); data.addRow([i, {v: myVal, f: myval.toFixed(6)}]); }Altar
it should be myVal = parseFloat($.trim(myArrayCreated[i][1]));... my bad..Altar
thank you very much!! it really helped me! since I'm new with javascript I don't know where should I put my myArrayCreated[i][0] should it be data.addRow([ myArrayCreated[i][0], { v : myVal, f : myVal.toFixed(6) } ]); if yes I get the same error.. thank you!Pustulate
Oh sorry it was my error... I forgot to change it to number again! Thank you very much again!Pustulate
No problem.. Happy learning.Altar
A
11

Try passing the Value as string and then later do a parseFloat. Something like this:

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)}]); 
}
Altar answered 23/6, 2014 at 14:1 Comment(0)
G
7

I spotted the same issue.

not working:

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

working:

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

wtf

Gereron answered 25/7, 2017 at 11:18 Comment(2)
Thank you friend !! the Number() saved me :)Undaunted
Nice dude. it is work for meOrsino

© 2022 - 2024 — McMap. All rights reserved.