我试图重新使用谷歌饼图功能,以绘制一个图表在2个不同的div。
我的html:
<div class="piechart" style="height: 220px;" data-completeness="35"></div>
<div class="piechart" style="height: 220px;" data-completeness="80"></div>
我的javascript:
function drawChart()
{
{#var completeness = 30;#} //this is working and setting same value to the two different charts
var completeness = $(this).attr('data-completeness'); //this is not working and charts are rendered very small
console.log(completeness); //this is dumping the right values in both cases, either the same, or two different ones as I expect
var data = google.visualization.arrayToDataTable([
['Nom', 'Valeur'],
["Profil rempli à ", completeness],
['Manque', 100 - completeness]
]);
var options = {
backgroundColor: { fill:'transparent'},
pieSliceBorderColor : 'transparent',
pieHole: 0.8,
legend: {position: 'top'},
width: 220,
height: 220,
tooltip: {trigger: 'none'},
pieStartAngle: -90,
pieSliceTextStyle :{fontsize : 16, color: 'transparent'},
slices: {
0: { color: '#09b4ff'},
1: { color: '#444'}
},
chartArea : {width: '90%', height: '90%'}
};
var chart = new google.visualization.PieChart(this);
chart.draw(data, options);
}
google.load('visualization', '1', {packages:['corechart']});
google.setOnLoadCallback(function(){
$('.piechart').each(drawChart);
});
因此,当我设置一个值时,我会得到以下漂亮的图形:
当我设置两个不同的值时,结果很奇怪:
信息:我有一个
1:1 XMLHttpRequest无法加载https://www.google.com/uds/api/visualization/1.0/dca88b1ff7033fac80178eb526cb263e/ui+en.css。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“http://foodmeup.dev”。
这两种情况下都有错误,但在第一种情况下显示得很好,所以我不确定这是这里的问题。
2条答案
按热度按时间bpzcxfmw1#
.attr('data-completeness')返回字符串“35”和“80”,这样您就可以使用parseInt来获取实际数字,或者您也可以使用.data(“completeness”),如下所示
全码
https://jsfiddle.net/sjsaa2xf/
vmdwslir2#
enter image description here