谷歌饼图,不能为2个不同的图表设置不同的值

时间:2015-04-20 16:23:21

标签: javascript html charts highcharts pie-chart

我正在尝试重新使用谷歌饼图功能在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);
});

因此,当我设置单个值时,我有以下好看的图表:

looking ok

当我设置两个不同的值时,结果很奇怪:

looking bad

信息:我有一个

  

1:1 XMLHttpRequest无法加载   https://www.google.com/uds/api/visualization/1.0/dca88b1ff7033fac80178eb526cb263e/ui+en.css。   No&#39; Access-Control-Allow-Origin&#39;标题出现在请求的上   资源。起源&#39; http://foodmeup.dev&#39;因此是不允许的   访问。

在这两种情况下都有错误,但在第一种情况下它是否存在错误,因此我不确定这是否存在问题。

1 个答案:

答案 0 :(得分:0)

.attr(&#39; data-completeness&#39;)返回字符串&#34; 35&#34;和&#34; 80&#34;所以你可以使用parseInt获取实际数字,或者你可以像这样使用.data(&#34;完整性&#34;)

var completeness = $(this).data('completeness');
             // or parseInt($(this).attr('data-completeness'));

完整代码

function drawChart(){
    var completeness = $(this).data('completeness');
    // or parseInt($(this).attr('data-completeness'))

    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);
});    

https://jsfiddle.net/sjsaa2xf/