Search code examples
javascripthtmlchartshighchartspie-chart

Google Pie chart, can't set different values for 2 different graphs


I am trying to re-use a google pie chart function to draw a chart in 2 different divs.

My html :

<div class="piechart" style="height: 220px;" data-completeness="35"></div>
<div class="piechart" style="height: 220px;" data-completeness="80"></div>

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

So when I set a single value, I have the following good looking graph:

looking ok

When I set two different values, the result in weird:

looking bad

Info : I have a

1:1 XMLHttpRequest cannot load https://www.google.com/uds/api/visualization/1.0/dca88b1ff7033fac80178eb526cb263e/ui+en.css. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://foodmeup.dev' is therefore not allowed access.

error in both cases, but it's diplaying well in the first case so I'm not sure it's the issue here.


Solution

  • .attr('data-completeness') return the string "35" and "80" so you can use parseInt to get the actual numbers or you can use .data("completeness") like this

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

    full code

    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/