charts - How to hide a bar if Value is null? -


how remove empty gap on chart if chart value null (or 0)?

see example:

http://jsfiddle.net/x13o8lgj/

you can see empty space on "mon 21 oct". how remove white gap?

  google.load("visualization", "1.1", {packages:["bar"]});   google.setonloadcallback(drawchart);   function drawchart() {     var data = google.visualization.arraytodatatable([       ['day', 'bmw', 'ford', 'toyota'],       ['mon 19 oct', 4, 8, 3],       ['mon 20 oct', 11, 4, 7],       ['mon 21 oct', 6, null,6],       ['mon 22 oct', 10, 5,2]     ]);      var options = {       chart: {         title: 'statistics',         subtitle: 'car',       },     };      var chart = new google.charts.bar(document.getelementbyid('columnchart_material'));      chart.draw(data, options); 

it seems feature not supported consider following solution remove empty gap in bar group. idea move remaining bars in group fill gap between them.

note: every bar rendered in google chart path element. in provided example supported fill gap group consisting of 3 bars.

google.load("visualization", "1.1", { packages: ["bar"] });  google.setonloadcallback(drawchart);  function drawchart() {      var data = google.visualization.arraytodatatable([        ['day', 'bmw', 'ford', 'toyota'],        ['mon 19 oct', 6, null, 8],        ['mon 20 oct', 11, 5, 7],        ['mon 21 oct', 6, 6,null],        ['mon 22 oct', 4, 5, 2]      ]);                var options = {          chart: {              title: 'statistics',              subtitle: 'car',          },      };            var chart = new google.charts.bar(document.getelementbyid('columnchart_material'));      google.visualization.events.addlistener(chart, 'ready', function () {          configurebaroptions(chart,data);      });      chart.draw(data, options);  }      function configurebaroptions(chart,data){            var chartdiv = chart.getcontainer();      var bars = chartdiv.getelementsbytagname('path');      if(bars.length == 0)        return;          var w = bars[0].getboundingclientrect().width;      var numofemptybars = 0;      foreach(data,function(row,col,val){            if(val == null || val == 0){                 var pos = ((data.getnumberofcolumns() - 1) * row) + col - 1 - numofemptybars;                  if(col == 1) { /* first empty bar in group? */                    bars[pos].setattribute('transform', 'translate(-' + w/2 + ', 0)');                    bars[pos+1].setattribute('transform', 'translate(-' + w/2 + ', 0)');                     numofemptybars++;                 }                  else if (col == 2) { /* middle empty bar in group? */                    bars[pos-1].setattribute('transform', 'translate(' + w/2 + ', 0)');                    bars[pos].setattribute('transform', 'translate(-' + w/2 + ', 0)');                    numofemptybars++;                 }                   else { /*last empty bar in series? */                    bars[pos-1].setattribute('transform', 'translate(' + w/2 + ', 0)');                    bars[pos-2].setattribute('transform', 'translate(' + w/2 + ', 0)');                    numofemptybars++;                 }            }      });                  }      function foreach(data,func){      for(var = 0; < data.getnumberofrows(); i++){          for(var j = 0; j < data.getnumberofcolumns(); j++){              var val = data.getvalue(i,j);                 func(i,j,val);          }      }  }
<script type="text/javascript" src="https://www.google.com/jsapi"></script>  <div id="columnchart_material" style="width: 900px; height: 500px;"></div>


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -