我正在使用 Google 图表(饼图)并希望在新行上开始每个图例 - 不像下图所示:
这是我的代码:
var data = new google.visualization.arrayToDataTable(chartData, false);
var optionsErrorDetails = {
title: 'My Piechart',
fontSize: 10
,
pieHole: 0.3,
legend:
{
position: 'top',
alignment: 'start',
textStyle: {
fontSize: 8
},
},
};
var chartErrorDetails = new google.visualization.PieChart(document.getElementById('errorDetails'));
chartErrorDetails.draw(data, optionsErrorDetails);
最佳答案
保证每个图例标签显示在新行上的唯一方法是将图例放置在右侧,
请参阅以下工作片段...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var chartData = [
['Category', 'Value'],
['Category Alpha', 46.7],
['Category Beta', 53.3],
];
var data = google.visualization.arrayToDataTable(chartData, false);
var optionsErrorDetails = {
title: 'My Piechart',
fontSize: 10,
pieHole: 0.3,
legend: {
position: 'right',
textStyle: {
fontSize: 8
},
},
};
var chartErrorDetails = new google.visualization.PieChart(document.getElementById('errorDetails'));
chartErrorDetails.draw(data, optionsErrorDetails);
});
#errorDetails {
display: inline-block;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="errorDetails"></div>
maxLines
选项在 legend.position
为 'top'
时有效。
但这只允许额外的行,它不会强制它们,
来自 docs ...
legend.maxLines - Maximum number of lines in the legend. Set this to a number greater than one to add lines to your legend. Note: The exact logic used to determine the actual number of lines rendered is still in flux.
注意:arrayToDataTable
是一个静态方法,不需要 new
关键字...
var data = google.visualization.arrayToDataTable(chartData, false);
编辑
你可以尝试手动移动标签,
有关粗略示例,请参见以下工作片段...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var chartData = [
['Category', 'Value'],
['Category C', 50],
['Category B', 50],
['Category A', 50],
];
var data = google.visualization.arrayToDataTable(chartData, false);
var optionsErrorDetails = {
chartArea: {
bottom: 72
},
title: 'My Piechart',
fontSize: 10,
pieHole: 0.3,
legend: {
position: 'bottom',
alignment: 'middle',
textStyle: {
fontSize: 8
},
},
};
var container = document.getElementById('errorDetails');
var chartErrorDetails = new google.visualization.PieChart(container);
google.visualization.events.addListener(chartErrorDetails, 'ready', function () {
var circles = container.getElementsByTagName('circle');
var labels = container.getElementsByTagName('text');
var labelIndex = -1;
var fontSize;
var radius;
var xCoordCircle;
var xCoordLabel;
var yCoordLabel;
var yCoordCircle;
Array.prototype.forEach.call(labels, function(label) {
if ((label.getAttribute('text-anchor') === 'start') && (label.getAttribute('fill') !== '#ffffff')) {
labelIndex++;
if (labelIndex === 0) {
radius = parseFloat(circles[labelIndex].getAttribute('r'));
xCoordCircle = circles[labelIndex].getAttribute('cx');
xCoordLabel = label.getAttribute('x');
} else {
fontSize = parseFloat(label.getAttribute('font-size')) * labelIndex;
yCoordLabel = parseFloat(label.getAttribute('y'));
label.setAttribute('x', xCoordLabel);
label.setAttribute('y', (yCoordLabel - fontSize - (radius * labelIndex)));
yCoordCircle = parseFloat(circles[labelIndex].getAttribute('cy'));
circles[labelIndex].setAttribute('cx', xCoordCircle);
circles[labelIndex].setAttribute('cy', yCoordCircle - fontSize - (radius * labelIndex));
}
}
});
});
chartErrorDetails.draw(data, optionsErrorDetails);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="errorDetails"></div>
关于javascript - 谷歌图表(饼图)中图例的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52183282/