gpt4 book ai didi

google-visualization - 使用谷歌图表在 donut 饼图中心显示总数?

转载 作者:行者123 更新时间:2023-12-02 08:25:50 25 4
gpt4 key购买 nike

我是 angularjs 和谷歌图表的新手,我制作了一个饼图,现在我想关注:

1- display total components inside the centre of the pie charts.
2- display pieSliceText outer side of the pie slice

你可以看到这就是我想要的:enter image description here

如何做到这一点我的示例代码:

<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}">                    </script>
<div id="piechart" style="width: 900px; height: 500px;"></div>

JavaScript:

google.setOnLoadCallback(drawChart);
function drawChart() {

var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);

var options = {
height: 360,
width: 360,
pieHole: 0.5,
showLables: 'true',
pieSliceText: 'value',
pieSliceTextStyle: {
color: 'white',
fontSize:18
},
legend: {
position: 'right',
alignment: 'center'
},
chartArea: {
left: 10,
top: 10,
width: '130%',
height: '65%'
},
tooltip: {
trigger:'none'
}
};

var chart = new google.visualization.PieChart(document.getElementById('piechart'));

chart.draw(data, options);
}

最佳答案

好的,试试这个 http://jsfiddle.net/L1tct3Lv/3/ ...这是一个快速的 hack,但您当然可以通过根据大小等计算位置来极大地改进它。

google.setOnLoadCallback(drawChart);

<!-- This generates the google chart -->
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);

var options = {
height: 360,
width: 360,
pieHole: 0.8,
showLables: 'true',
pieSliceText: 'value',
pieSliceTextStyle: {
color: 'white',
fontSize:18
},
legend: {
position: 'right',
alignment: 'center'
},
chartArea: {
left: 10,
top: 10,
width: '130%',
height: '65%'
},
tooltip: {
trigger:'none'
}
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
jQuery('#cnt').text(data.Gf.length);
chart.draw(data, options);

像这样设置一些叠加层:

<div id="JSFiddle">
<div id="chart_div"></div>
<div id="cnt" class="overlay"></div>
<div class="overlay-label">total components</div>
</div>

使用这个 CSS:

#JSFiddle {
position: relative;
}

.overlay {
display:block;
width:240px;
height:240px;
text-align:center;
vertical-align: middle;
position: absolute;
top: 0px; /* chartArea top */
left: 0px; /* chartArea left */
font-size: 80px;
margin-top: 60px;
}
.overlay-label{
display:block;
width:240px;
height:240px;
text-align:center;
vertical-align: middle;
position: absolute;
top: 0px; /* chartArea top */
left: 0px; /* chartArea left */
margin-top: 140px;
font-size: 20px;
}

结果是这样的:

enter image description here

关于google-visualization - 使用谷歌图表在 donut 饼图中心显示总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32256527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com