How export google chart into pdf

64 views
Skip to first unread message

Cleciana Alves de Oliveira Rangel

unread,
Mar 6, 2020, 12:29:37 PM3/6/20
to Google Visualization API
 
I need help to export the google chart graph to pdf or put a button that generates the pdf of the graph. Does anyone know how to do?
My code looks like this:



<script type="text/javascript">
    google.charts.load("current", {packages:["corechart"]});
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ["Element", "Quantidade de Mudas", { role: "style" } ],
         <?php
         foreach ($relatorios as $value) {
            echo '["'.$value['cidade'].'" , '.$value['quantidade'].' , ""],'; 
            //echo 'R$' . number_format($num, 2, ',', '.');
         }
          ?>
          
      ]);

      var view = new google.visualization.DataView(data);
      view.setColumns([0, 1,
                       { calc: "stringify",
                         sourceColumn: 1,
                         type: "string",
                         role: "annotation" },
                       2]);
          
      var formatter = new google.visualization.NumberFormat(
              {negativeColor: 'red', negativeParens: true, pattern: '###.###'}); formatter.format(data, 1); formatter.format(data, 1);     

      var options = {
        title: "Total de mudas por cidade",
        width: 1560,
        height: 2550,
        bar: {groupWidth: "40%"},
        legend: { position: "none" },
        pieSliceText: 'value'
      };
      var chart = new google.visualization.BarChart(document.getElementById("barchart_values", "myChart"));
      chart.draw(view, options);
     
      $('button').on('click', function() {
    html2canvas($('#myChart'), {
    onrendered: function(canvas) {
      var imgData = canvas.toDataURL('image/png');
      var pdf = new jsPDF('p', 'mm');
      pdf.addImage(imgData, 'PNG', 10, 10);
      pdf.save('test.pdf');
    }
  });
});
Reply all
Reply to author
Forward
0 new messages