简体   繁体   中英

how to add buttons to switch between two types of google charts in a dashboard

i have a linechart and a daterangefilter applied to that line chart ,this is my present code ...

function go(){

 window.setTimeout( function(){
var p=0;
var table = document.getElementById('mytable');
var rows = table.getElementsByTagName('tr');
var i, j, cells;
var distance=[];
var date=[];
var entryid=[];
j=rows.length;
for (i = 1; i < j; i++) {
cells = rows[i].getElementsByTagName('td');
if (!cells.length) {
    continue;
}
distance[i-1] = parseInt(cells[1].innerHTML);
date[i-1]=new Date(reverse(cells[2].innerHTML));
entryid[i-1]=cells[0].innerHTML;
}
var c=[];
for (var z=0;z<distance.length;z++){
c.push([date[z],distance[z]]);
}
/*document.getElementById('cvs').innerHTML=c;*/
function reverse(f){
var g=f.split("/");
var goodstr=g[1]+"/"+g[2]+"/"+g[0];
return goodstr;
}
google.charts.load('44', {
callback: drawBackgroundColor,
packages: ['corechart','controls']
});
function drawBackgroundColor() {

var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Distance');

data.addRows(c);

var options = {
hAxis: {
  title: 'Date'
},
vAxis: {
  title: 'Distance Ran'
},
backgroundColor: '#f1f8e9'
};
var control;
var chartWrapper;
var dash;
dash = new google.visualization.Dashboard(
document.getElementById('dashboard_div'));
var controlOptions = {
    filterColumnIndex: 0
};
///////////////////////////


control = new google.visualization.ControlWrapper({
controlType: 'DateRangeFilter',
containerId: 'control_div',
options: controlOptions
});


chartWrapper = new google.visualization.ChartWrapper({
chartType:'LineChart',//'LineChart', //'LineChart',
containerId: 'cvs',
options: options
});

dash.bind([control], [chartWrapper]);

dash.draw(data);

}
}, 5 * 1000 );

i want to modify this code to add buttons to toggle between two type of charts with the same data , linechart and barchart . may be i need to add another chartWrapper with chartType="BarChart" and use some if ,else statement ,but i cant seem to figure it out . can somebody plz help me achieve this . thanks in advance :) .

take a look at this example in the doc: https://developers.google.com/chart/interactive/docs/gallery/controls#7-draw-your-dashboard

It seem exactly what you need

Something like this should work

document.getElementById('b2').addEventListener("click", changeType);
          function changeType(){
            chartWrapper.setChartType('ColumnChart');
            chartWrapper.draw();
          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM