Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

how to get imageUri for google chart

This is my sample material bar graph and i want the image uri for the plotted graph

google.charts.load('current', {'packages':['bar']});
      google.charts.setOnLoadCallback(drawStuff);

      function drawStuff() {
        var data = new google.visualization.arrayToDataTable([
          ['Galaxy', 'Distance', 'Brightness'],
          ['Canis Major Dwarf', 8000, 23.3],
          ['Sagittarius Dwarf', 24000, 4.5],
          ['Ursa Major II Dwarf', 30000, 14.3],
          ['Lg. Magellanic Cloud', 50000, 0.9],
          ['Bootes I', 60000, 13.1]
        ]);

        var options = {
          width: 800,
          chart: {
            title: 'Nearby galaxies',
            subtitle: 'distance on the left, brightness on the right'
          },
          bars: 'vertical', // Required for Material Bar Charts.
          series: {
            0: { axis: 'distance' }, // Bind series 0 to an axis named 'distance'.
            1: { axis: 'brightness' } // Bind series 1 to an axis named 'brightness'.
          },
          axes: {
            x: {
              distance: {label: 'parsecs'}, // Bottom x-axis.
              brightness: {side: 'top', label: 'apparent magnitude'} // Top x-axis.
            }
          }
        };

      var chart = new google.charts.Bar(document.getElementById('dual_x_div'));
      chart.draw(data, options);
      console.log(chart.getImageURI());
    };
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
       <div id="dual_x_div" style="width: 900px; height: 500px;"></div>
   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use html2canvas

you'll need the following two files from the build

<script src="html2canvas.js"></script>
<script src="html2canvas.svg.js"></script>

then on the chart's 'ready' event...

  google.visualization.events.addListener(chart, 'ready', function () {
    // add svg namespace to chart
    $(chartContainer).find('svg').attr('xmlns', 'http://www.w3.org/2000/svg');

    // get image uri
    html2canvas(chartContainer, {
      allowTaint: true,
      taintTest: false
    }).then(function(canvas) {
      console.log(canvas.toDataURL('image/png'));
    });
  });

UPDATE

another method is to convert the svg to an image and draw it on a canvas,
then pull the uri from the canvas...

google.charts.load('current', {
  packages:['bar']
}).then(function () {
  var data = new google.visualization.arrayToDataTable([
    ['Galaxy', 'Distance', 'Brightness'],
    ['Canis Major Dwarf', 8000, 23.3],
    ['Sagittarius Dwarf', 24000, 4.5],
    ['Ursa Major II Dwarf', 30000, 14.3],
    ['Lg. Magellanic Cloud', 50000, 0.9],
    ['Bootes I', 60000, 13.1]
  ]);

  var options = {
    width: 800,
    chart: {
      title: 'Nearby galaxies',
      subtitle: 'distance on the left, brightness on the right'
    },
    bars: 'vertical', // Required for Material Bar Charts.
    series: {
      0: { axis: 'distance' }, // Bind series 0 to an axis named 'distance'.
      1: { axis: 'brightness' } // Bind series 1 to an axis named 'brightness'.
    },
    axes: {
      x: {
        distance: {label: 'parsecs'}, // Bottom x-axis.
        brightness: {side: 'top', label: 'apparent magnitude'} // Top x-axis.
      }
    }
  };

  var chartContainer = document.getElementById('dual_x_div');
  var chart = new google.charts.Bar(chartContainer);

  google.visualization.events.addListener(chart, 'ready', function () {
    var canvas;
    var domURL;
    var imageNode;
    var imageURL;
    var svgParent;

    // add svg namespace to chart
    domURL = window.URL || window.webkitURL || window;
    svgParent = chartContainer.getElementsByTagName('svg')[0];
    svgParent.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
    imageNode = chartContainer.cloneNode(true);
    imageURL = domURL.createObjectURL(new Blob([svgParent.outerHTML], {type: 'image/svg+xml'}));
    image = new Image();
    image.onload = function() {
      canvas = document.getElementById('canvas');
      canvas.setAttribute('width', parseFloat(svgParent.getAttribute('width')));
      canvas.setAttribute('height', parseFloat(svgParent.getAttribute('height')));
      canvas.getContext('2d').drawImage(image, 0, 0);
      console.log(canvas.toDataURL('image/png'));
    }
    image.src = imageURL;
  });

  chart.draw(data, options);
});
.hidden {
  display: none;
  visibility: hidden;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="dual_x_div"></div>
<canvas class="hidden" id="canvas"></canvas>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...