phantomjs and google chart

198 views
Skip to first unread message

oscaroxy

unread,
Jan 27, 2018, 10:34:09 AM1/27/18
to Google Visualization API
Why before phantomjs worked with google chart and now not?

the sources is the same but now it response me:

ReferenceError: Can't find variable: google

if I run the js into a browser the variable google exists and the chart is rendered 

thanks for any advice

<html>

  <head>

    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    <script type="text/javascript">

    console.log(google);

    google.charts.load('current', {'packages':['corechart'], 'language': 'it'});

    google.charts.setOnLoadCallback(drawChart);

     

      function formatMoney(number){

    if (number == 0){

    return '0';

    }

    var numberStr = parseFloat(number).toFixed(2).toString();

    var numFormatDec = numberStr.slice(-2); /*decimal 00*/

    numberStr = numberStr.substring(0, numberStr.length-3); /*cut last 3 strings*/

    var numFormat = new Array;

    while (numberStr.length > 3) {

    numFormat.unshift(numberStr.slice(-3));

    numberStr = numberStr.substring(0, numberStr.length-3);

    }

    numFormat.unshift(numberStr);

    return numFormat.join('.')+','+numFormatDec; /*format 000.000.000,00 */

    }

      

      function drawChart() {

var i,n;

        var input = [["E.M.","Prezzo"],["Importo E.M. in oggetto",0.23999999999999999],["Importo ancora da evadere",117499.75999999999],["Importo evaso",0.01]];

        var data = google.visualization.arrayToDataTable(input);

        var formatter = new google.visualization.NumberFormat({pattern: '€ #,###.##'});

        formatter.format(data, 1);

// console.log(JSON.stringify(input))


  n = input.length;

  var count=0;

        for (i = 0; i < n; i++) {

        if (input[i][1] == 0){

        count += 1;

        }

        }

        var is3D = count < 2;

        

        var options = {

                title: 'Pippo',

                backgroundColor: '#fff',

                is3D: is3D,

                pieSliceText: 'value',

                chartArea: {

                    left: 0,

                    top: 0,

                    width: '100%',

                    height: '100%'

                },

                height: 550,

                width: 750,

                legend:{ 

                position: 'right', //labeled | right | ...

                    labeledValueText: 'value'

                },

                pieSliceText:'value',

                slices: { 0: {offset: 0.2}},

                sliceVisibilityThreshold:0,

                colors:['#f29606', '#12b301', '#eb0101']

         };

//         labeledValueText: value | both | none |  percent

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

        google.visualization.events.addListener(chart, 'ready', onReadyChart);

        chart.draw(data, options);

        //** set legend

        var legendBar = document.getElementsByTagName('g')[0]; //get legend container 

        var labels = legendBar.getElementsByTagName('text');

        n = labels.length;

        for (i = 0; i < n; i++) {

//    console.log(Object.keys(labels[i]))

//    console.log(Object.keys(data2.rows))

  labels[i].textContent += ' € ' +formatMoney(input[i+1][1]);

        }

        //**----

        

      }

      

      function onReadyChart(){

      //** l'ho commentato perché viene eseguito prima di eseguire le istruzioni dopo il chart.draw

//        window.callPhantom();

      }

    </script>

  </head>

  <body style="background-color: #fff">

    <div id="piechart" style="width: 500px; height: 550px;"></div>

  </body>

</html>


Reply all
Reply to author
Forward
0 new messages