Remove padding or margins from Google Charts

138,118

Solution 1

By adding and tuning some configuration options listed in the API documentation, you can create a lot of different styles. For instance, here is a version that removes most of the extra blank space by setting the chartArea.width to 100% and chartArea.height to 80% and moving the legend.position to bottom:

// Set chart options
var options = {'title': 'How Much Pizza I Ate Last Night',
               'width': 350,
               'height': 400,
               'chartArea': {'width': '100%', 'height': '80%'},
               'legend': {'position': 'bottom'}
    };

If you want to tune it more, try changing these values or using other properties from the link above.

Solution 2

I am quite late but any user searching for this can get help from it. Inside the options you can pass a new parameter called chartArea.

        var options = {
        chartArea:{left:10,top:20,width:"100%",height:"100%"}
    };

Left and top options will define the amount of padding from left and top. Hope this will help.

Solution 3

I arrived here like most people with this same issue, and left shocked that none of the answer even remotely worked.

For anyone interested, here is the actual solution:

... //rest of options
width: '100%',
height: '350',
chartArea:{
    left:5,
    top: 20,
    width: '100%',
    height: '350',
}
... //rest of options

The key here has nothing to do with the "left" or "top" values. But rather that the:

Dimensions of both the chart and chart-area are SET and set to the SAME VALUE

As an amendment to my answer. The above will indeed solve the "excessive" padding/margin/whitespace problem. However, if you wish to include axes labels and/or a legend you will need to reduce the height & width of the chart area so something slightly below the outer width/height. This will "tell" the chart API that there is sufficient room to display these properties. Otherwise it will happily exclude them.

Solution 4

It's missing in the docs (I'm using version 43), but you can actually use the right and bottom property of the chart area:

var options = {
  chartArea:{
    left:10,
    right:10, // !!! works !!!
    bottom:20,  // !!! works !!!
    top:20,
    width:"100%",
    height:"100%"
  }
};

So it's possible to use full responsive width & height and prevent any axis labels or legends from being cropped.

Solution 5

There's a theme available specifically for this

options: {
  theme: 'maximized'
}

from the Google chart docs:

Currently only one theme is available:

'maximized' - Maximizes the area of the chart, and draws the legend and all of the labels inside the chart area. Sets the following options:

chartArea: {width: '100%', height: '100%'},
legend: {position: 'in'},
titlePosition: 'in', axisTitlesPosition: 'in',
hAxis: {textPosition: 'in'}, vAxis: {textPosition: 'in'}
Share:
138,118

Related videos on Youtube

Paul Armdam
Author by

Paul Armdam

Updated on August 20, 2020

Comments

  • Paul Armdam
    Paul Armdam over 3 years

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1.0', {'packages':['corechart']});
    
    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);
    
    // Callback that creates and populates a data table,
    // instantiates the pie chart, passes in the data and
    // draws it.
    function drawChart() {
    
      // Create the data table.
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Topping');
      data.addColumn('number', 'Slices');
    
      var myData = {
        'Mushrooms': 3,
        'Onions': 1,
        'Olives': 1,
        'Zucchini': 1,
        'Pepperoni': 2
      };
    
      var rows = [];
      for (element in myData) {
          rows.push([element + " (" + myData[element] + ")", myData[element]])
      }
      data.addRows(rows);
    
      // Set chart options
      var options = {'title':'How Much Pizza I Ate Last Night',
                     'width':450,
                     'height':300};
    
      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    
    <div id="chart_div"></div>

    Example fiddle

    How do I remove padding or margins in this example?

  • pim
    pim almost 9 years
    As an amendment to my answer. This will indeed solve the "excessive" padding/margin/whitespace problem. However, if you wish to include axes labels and/or a legend you will need to reduce the height & width of the chart area so something slightly below the outer width/height. This will "tell" the chart API that there is sufficient room to display these properties. Otherwise it will happily exclude them.
  • Blamkin86
    Blamkin86 almost 9 years
    the chartArea.top did the trick for my dynamically-sized div. Thanks.
  • user3800174
    user3800174 over 8 years
    Being able to use right is definitely a nice addition, however in the case of a column chart that requires numbers for the vertical axis it will get tricky when the values fluctuate by a good bit - for instance a chart with values that range from 0 - 100 would require a left value of 20 pixels, but 0 - 10 mil would need 100 pixels, and using 100 pixels then would leave a fairly large margin which is what I think all of us are trying to get rid of :) Unless there is an option that allows the chart to adjust its own width that I am missing. Any thoughts on how one might be able to solve this?
  • Dave Burton
    Dave Burton about 8 years
    I think the reason the other solutions didn't work for you is probably that they assumed the chart was being inserted into a DIV which already had preset width and height attributes, but you hadn't done that. It's a good idea to preset the height and width in the HTML so that the layout of the page doesn't change when graph gets filled it. That will prevent things from "jumping around" on the page as it loads.
  • pim
    pim about 8 years
    @Dave thanks for the suggestion, but I don't necessarily agree. There are plenty of situations where you just simply don't know what those values are going to be. And it looks like at least 15 other people feel as though my offering is adequate.
  • Dave Burton
    Dave Burton about 8 years
    I agree, Pim. When you don't know the width and/or height in advance (perhaps because you want the page to adapt responsively), you can't preset the width & height in the DIV. I was just pointing out why I think the solutions given in the other answers worked for them but didn't work for you, and why it's a good idea to preset the width and height in the DIV if you can.
  • Dave Burton
    Dave Burton about 8 years
    It looks like they documented it Oct. 2, 2015, here: archive.is/lwbQY#selection-2997.0-3011.1
  • Pirate X
    Pirate X almost 8 years
    @PimBrouwers I'm curious how did you find out that both values should be same only then it would work. (It worked, thanks !!)
  • pim
    pim almost 8 years
    @PirateX -- that's a great question, it's been so long I don't recall exactly. I would say, like most of my discoveries, just a s&^%-load of trial and error haha. Thanks for the upvote and more importantly, I'm glad it helped you!
  • Stephen R
    Stephen R almost 6 years
    For reasons @DaveBurton points out, this is likely the correct answer if working with CSS Grid (which I am)
  • Jiri Kriz
    Jiri Kriz about 4 years
    This seems the best option. After setting theme: 'maximized' it is still possible to tune the appearance, e.g. height:' 90%', textPosition: 'out', etc.