javascript - date type not matches google chart requirements -
it really hard me run project state right now. @ beginning, had no clue how can read file csv , push array. works right now, mismatching date value adding rows.
i think code simple , shows problem. im stuck.
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script src="jquery.csv.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['timeline']}); google.charts.setonloadcallback(drawchart); function csvarray(filename, separator, callback) { $.get(filename, function(filecontent){ var result = []; var textarray = filecontent.split(/(\r\n|\n|\r)/gm); (var = 0; < textarray.length; i++) { if (textarray[i].length > 1) { var elementarray = textarray[i].split(separator); elementarray.splice(elementarray.length ,1 ); result.push(elementarray); } } callback(result); }); } csvarray("data.csv", ";", function(result) { arrayresult = result; }); function drawchart() { var container = document.getelementbyid('timeline'); var chart = new google.visualization.timeline(container); var datatable = new google.visualization.datatable(); datatable.addcolumn({ type: 'string', id: 'job'}) datatable.addcolumn({ type: 'string', id: 'status' }); datatable.addcolumn({ type: 'date', id: 'start' }); datatable.addcolumn({ type: 'date', id: 'end' }); datatable.addrows(arrayresult); chart.draw(datatable); } </script> </head> <body> <div id="timeline" style="height: 180px;"></div> </body> </html>
data.csv
'zs142770-win dateien dd---btd woche';'passed';new date(2014, 10, 15);new date(2014, 10, 15)
i guess problem is, array contain data string , not object - right?
errorcode:
error: type mismatch. value new date(2014, 10, 15) not match type date in column index 2
best regards paul
you correct problem,
need convert string real date
first, recommend changing date values in csv format
see more on here --> converting string date in js
then create date string, like...
elementarray[2] = new date(elementarray[2]);
but if can't change values in csv,
can try using eval
, pretty never recommended
(but should work)
elementarray[2] = eval(elementarray[2]);
next, have timing issues layout of code,
recommend waiting google load, before loading csv
draw chart...
recommend setup similar following...
google.charts.load('current', { callback: getdata, packages: ['timeline'] }); function getdata() { $.get("data.csv", function (filecontent) { var result = []; var textarray = filecontent.split(/(\r\n|\n|\r)/gm); (var = 0; < textarray.length; i++) { if (textarray[i].length > 1) { var elementarray = textarray[i].split(";"); // convert date elementarray[2] = eval(elementarray[2]); elementarray[3] = eval(elementarray[3]); // or different format elementarray[2] = new date(elementarray[2]); elementarray[3] = new date(elementarray[3]); result.push(elementarray); } } drawchart(result); }); } function drawchart(arrayresult) { var container = document.getelementbyid('timeline'); var chart = new google.visualization.timeline(container); var datatable = new google.visualization.datatable(); datatable.addcolumn({ type: 'string', id: 'job'}) datatable.addcolumn({ type: 'string', id: 'status' }); datatable.addcolumn({ type: 'date', id: 'start' }); datatable.addcolumn({ type: 'date', id: 'end' }); datatable.addrows(arrayresult); chart.draw(datatable); }
Comments
Post a Comment