Problem in drawing Charts in Web app Html page
Hello, I have hard time understanding the Google chart API error "All series on a given axis must be of the same data type"
My App script function, I am returning the values like this
function chart_data(){
function draw_chart(){
var ss = SpreadsheetApp.openById('XXXXXXX');
var metrics_sheet = ss.getSheetByName('data_s');
var lastrow = metrics_sheet.getLastRow();
var lastcolumn = metrics_sheet.getLastColumn();
var values = metrics_sheet.getRange("A1:X").getValues();
/* Find Last Index of the Non Blank cells */
const range = metrics_sheet.getRange("A1:X"+lastrow).getValues();
var index_values = lastrow - range.reverse().findIndex(c=>c[0]!='');
var temp = "A1:X"+index_values;
var values = metrics_sheet.getRange(temp).getValues();
var pat_data1 = JSON.stringfy(values);
return pat_data1;
}
And its gives me the output like this
[["Region","date","volume"],
["All","2024-04-30T18:30:00.000Z",100],
["All","2024-05-30T18:30:00.000Z",403],
["All","2024-06-30T18:30:00.000Z",678],
["All","2024-07-30T18:30:00.000Z",700],
["North","2024-04-30T18:30:00.000Z",90],
["North","2024-05-31T18:30:00.000Z",200],
["North","2024-06-30T18:30:00.000Z",500],
["North","2024-07-31T18:30:00.000Z",300],
["South","2024-04-30T18:30:00.000Z",10],
["South","2024-05-31T18:30:00.000Z",203],
["South","2024-06-30T18:30:00.000Z",178],
["South","2024-07-31T18:30:00.000Z",400]]
My JS Script code to draw the chart in HTML page
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current',{'packages':['corechart']});
google.charts.setOnLoadCallback(drawchart);
function drawchart(){
google.script.run.withSuccessHandler(displaychart).draw_linechart();
}
function displaychart(c_data1){
var chartdata = new google.visualization.arrayToDataTable(JSON.parse(c_data1),false);
console.log(chartdata);
//Setting Chart Options
var options={
title: 'Line Chart Example',
is3D: true
}
// Draw chart passing some options
var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
chart.draw(chartdata,options);
}
</script>
<select id="city">
<option value="National">National</option>
<option value="North">North</option>
<option value="South">South</option>
</select>
<div id="line_chart" class="line_cont"></div>
In Browser I am getting error
All series on a given axis must be of the same data type
On searching I found to add data to DataTable mentioning types, so I did this
var data = new google.visualization.DataTable();
data.addColumn('string','region' );
data.addColumn('date','date');
data.addColumn('number','volume');
var arrMain=[];
for(i=0;i<c_data1.length;i++){
console.log(c_data1[i].region) // returns undefined
arrMain.push([c_data1[i].region,new Date(c_data1[i].t_date),c_data1[i].pat]);
}
data.addRows(arrMain);
Still the same error
My App script returns 2D-Array and using JSON.stringify to get data in my JS code, in which type of my data becomes String. I learnt JSON.parse() will return JS array. I tried passing JSON.parse(c_data1) to function arrayToDataTable function & used DataTable, both the methods throws me the error All series on a given axis must be of the same data type
In below function
var arrMain=[];
for(i=0;i<c_data1.length;i++){
console.log(c_data1[i].region) // returns undefined
arrMain.push([c_data1[i].region,new Date(c_data1[i].t_date),c_data1[i].pat]);
}
Why values are undefined in the loop? What is the correct way to overcome this error. Thanks in advance.
Please or to participate in this conversation.