how to get imageUri for google chart

杀马特。学长 韩版系。学妹 提交于 2019-11-29 12:33:54

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!