从Google图表中删除填充或边距

时间:2012-03-12 03:37:31

标签: javascript html charts google-visualization

// 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

如何在此示例中删除填充或边距?

6 个答案:

答案 0 :(得分:228)

通过添加和调整API documentation中列出的一些配置选项,您可以创建许多不同的样式。例如,here is a version通过将chartArea.width设置为 100%并将chartArea.height设置为 80%来删除大部分额外空白区域并将legend.position移至底部

// 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'}
    };

如果您想更多地调整它,请尝试更改这些值或使用上面链接中的其他属性。

答案 1 :(得分:73)

我已经很晚了,但是搜索此内容的任何用户都可以从中获得帮助。在选项中,您可以传递一个名为 chartArea 的新参数。

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

左侧和顶部选项将定义从左侧和顶部填充的数量。希望这会有所帮助。

答案 2 :(得分:50)

我像大多数人一样来到这里同样的问题,并且震惊的是没有任何答案甚至远程工作。

对于任何有兴趣的人,这是实际的解决方案:

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

此处的关键字 与“left”或“top”值有关。而是那个:

  

图表图表区域的尺寸均已设置并设置为相同值

作为我答案的修正案。以上将确实解决“过度”填充/边距/空白问题。但是,如果您希望包含轴标签和/或图例,则需要降低高度和高度。图表区域的宽度,使其略低于外部宽度/高度。这将“告诉”图表API有足够的空间来显示这些属性。否则它会很乐意排除它们。

答案 3 :(得分:13)

有这种可能性like Aman Virk mentioned

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

但请记住,填充和边距不会打扰你。 如果您可以在不同类型的图表(如ColumnChart)和具有垂直列的图表之间切换,则需要一些余量来显示这些行的标签。

如果你拿掉那个边距,那么你最终只会显示部分标签或者根本没有标签。

因此,如果您只有一种图表类型,那么您可以像Arman所说的那样更改边距和填充。但如果有可能切换不改变它们。

答案 4 :(得分:12)

文档中缺少(我使用的是版本43),但您实际上可以使用图表的正确底部属性地区:

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

因此可以使用完整的响应宽度&amp;高度并防止任何轴标签或图例被裁剪。

答案 5 :(得分:5)

有一个专门针对此主题

options: {
  theme: 'maximized'
}

来自Google图表文档:

当前只有一个主题可用:

“最大化”-最大化图表区域,并在图表区域内绘制图例和所有标签。设置以下选项:

chartArea: {width: '100%', height: '100%'},
legend: {position: 'in'},
titlePosition: 'in', axisTitlesPosition: 'in',
hAxis: {textPosition: 'in'}, vAxis: {textPosition: 'in'}
相关问题