// 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);
}
Example fiddle
例如小提琴
How do I remove padding or margins in this example?
在这个例子中如何删除填充或边距?
5 个解决方案
#1
208
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.
如果您想对它进行更多的优化,可以尝试更改这些值或使用上面链接中的其他属性。
#2
61
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.
我很晚了,但是任何用户搜索这个都可以得到帮助。在选项中可以传递一个名为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.
左侧和顶部选项将定义从左侧和顶部填充的数量。希望这将帮助。
#3
37
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:
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.
There is this possibility like Aman Virk mentioned:
有一种可能性就像安曼·威克提到的那样:
var optiOns= {
chartArea:{left:10,top:20,width:"100%",height:"100%"}
};
But keep in mind that the padding and margin aren't there to bother you. If you have the possibility to switch between different types of charts like a ColumnChart and the one with vertical columns then you need some margin for displaying the labels of those lines.