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?
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?
2 Answers
Reset to default 11Try 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)}]);
}
I spotted the same issue.
not working:
data.addRow([v, obd[v].engine_rpm]);
working:
data.addRow([v, Number(obd[v].engine_rpm)]);
Value
as anumber
? Did you check if it is astring
by any chance? This is one of the most common errors. – Fr0zenFyr Commented Jun 23, 2014 at 13:18data.addColumn('string', 'Value'); for (i = 0; i < myArrayCreated.length; i++){ data.addRow(myArrayCreated[i][0] , parseFloat(myArrayCreated[i][1]).toFixed(6)); }
however get this errorIf argument is given to addRow, it must be an array, or null
– user3362533 Commented Jun 23, 2014 at 13:20for (var i=0;i<myArrayCreated.length;i++){ myVal = parseFloat($.trim(myArrayCreated[i][0])); data.addRow([i, {v: myVal, f: myval.toFixed(6)}]); }
– Fr0zenFyr Commented Jun 23, 2014 at 13:28myVal = parseFloat($.trim(myArrayCreated[i][1]));
... my bad.. – Fr0zenFyr Commented Jun 23, 2014 at 13:36data.addRow([ myArrayCreated[i][0], { v : myVal, f : myVal.toFixed(6) } ]);
if yes I get the same error.. thank you! – user3362533 Commented Jun 23, 2014 at 13:54