Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show an Empty Google Chart when there is no data?

Consider drawing a column chart and I don't get any data from the data source, How do we draw an empty chart instead of showing up a red colored default message saying "Table has no columns"?

like image 353
Vamshi Vangapally Avatar asked Jun 07 '12 18:06

Vamshi Vangapally


People also ask

Why does my Google chart say no data?

Empty Rows If your form has blank rows, it may be because these rows used to have data in them but they were deleted. To truly delete rows, highlight the rows by clicking on the "2" and dragging to "5".

How do you make a Google pie chart blank?

to make the chart look empty, use transparent as the color and add a border... colors: ['transparent'], pieSliceBorderColor: '#9e9e9e', see following working snippet...

Can Google charts be used offline?

Can I use charts offline? Your users' computers must have access to https://www.gstatic.com/charts/loader.js in order to use the interactive features of Google Charts. This is because the visualization libraries that your page requires are loaded dynamically before you use them.


2 Answers

What I do is initialize my chart with 1 column and 1 data point (set to 0). Then whenever data gets added I check if there is only 1 column and that it is the dummy column, then I remove it. I also hide the legend to begin so that it doesn't appear with the dummy column, then I add it when the new column gets added.

Here is some sample code you can plug in to the Google Visualization Playground that does what I am talking about. You should see the empty chart for 2 seconds, then data will get added and the columns will appear.

var data, options, chart;  function drawVisualization() {    data = google.visualization.arrayToDataTable([     ['Time', 'dummy'],     ['', 0],    ]);    options = {     title:"My Chart",     width:600, height:400,     hAxis: {title: "Time"},     legend : {position: 'none'}   };    // Create and draw the visualization.   chart = new google.visualization.ColumnChart(document.getElementById('visualization'));   chart.draw(data,options);   setTimeout('addData("12:00",10)',2000);   setTimeout('addData("12:10",20)',3000); }  function addData(x,y) {   if(data.getColumnLabel(1) == 'dummy') {     data.addColumn('number', 'Your Values', 'col_id');     data.removeColumn(1);     options.legend = {position: 'right'};   }    data.addRow([x,y]);   chart.draw(data,options); }​ 
like image 108
Matt Dodge Avatar answered Oct 02 '22 22:10

Matt Dodge


A even better solution for this problem might be to use a annotation column instead of a data column as shown below. With this solution you do not need to use any setTimeout or custom function to remove or hide your column. Give it a try by pasting the given code below into Google Code Playground.

function drawVisualization() {   var data = google.visualization.arrayToDataTable([       ['', { role: 'annotation' }],       ['', '']   ]);    var ac = new google.visualization.ColumnChart(document.getElementById('visualization'));   ac.draw(data, {       title : 'Just a title...',       width: 600,       height: 400   }); } 

like image 20
AntonSjoberg Avatar answered Oct 02 '22 22:10

AntonSjoberg