Google图表:如何触发2' ready'事件处理功能是否同步?

时间:2018-03-08 07:55:01

标签: charts google-visualization linechart

我正在使用Google图表绘制折线图并使用2个就绪事件处理函数,如下所示:

  1. AlignTitle:将标题与中心对齐
  2. CreateImageToExport:将图形转换为图像(使用getImageURI())
  3. 问题是如此形成的图像包含左对齐的标题(不反映按功能1的更改)。

    同样的jsfiddle link

    javascript代码:

        function drawBackgroundColor() {
          var data = new google.visualization.DataTable();
          data.addColumn('number', 'X');
          data.addColumn('number', 'Dogs');
    
          data.addRows([
            [0, 0],   [1, 10],  [2, 23],  [3, 17],  [4, 18],  [5, 9],
            [6, 11],  [7, 27],  [8, 33],  [9, 40],  [10, 32], [11, 35],
            [12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
            [18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
            [24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
            [30, 55], [31, 60], [32, 61], [33, 59], [34, 62], [35, 65],
            [36, 62], [37, 58], [38, 55], [39, 61], [40, 64], [41, 65],
            [42, 63], [43, 66], [44, 67], [45, 69], [46, 69], [47, 70],
            [48, 72], [49, 68], [50, 66], [51, 65], [52, 67], [53, 70],
            [54, 71], [55, 72], [56, 73], [57, 75], [58, 70], [59, 68],
            [60, 64], [61, 60], [62, 65], [63, 67], [64, 68], [65, 69],
            [66, 70], [67, 72], [68, 75], [69, 80]
          ]);
    
          var options = {
            title: 'Popularity',
            hAxis: {
              title: 'Time'
            },
            vAxis: {
              title: 'Popularity'
            },
            backgroundColor: '#f1f8e9'
          };
    
          var vLeftMargin = '400';
          var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    
          google.visualization.events.addListener(chart, 'ready', AlignTitle);
    
          //Function to tilt the label
          function AlignTitle()
          {
              $('#chart_div').find("text:contains(" + options.title + ")").attr({'x': vLeftMargin});
          }
    
          //Add function to export
          CreateImageToExport   (chart,
                                'chart_div',
                                1000,
                                300); 
    
          chart.draw(data, options);
        }
    
    //Function to create image to export 
    function CreateImageToExport(pChart,
                                 pDiv,
                                 pWidth,
                                 pHeight)
    {
        google.visualization.events.addListener(pChart, 'ready', function () 
        {   
            $('#' + pDiv + '_Export').remove();
            var vExportedImage = document.createElement("img");
            vExportedImage.src = pChart.getImageURI();
            vExportedImage.width = pWidth;
            vExportedImage.height = pHeight;
            vExportedImage.id = pDiv + '_Export';
            document.getElementById('divImageCharts').appendChild(vExportedImage);
        });
    }
    

    我希望在标题对齐后将图形转换为图像。任何的意见都将会有帮助。感谢

1 个答案:

答案 0 :(得分:1)

您可以在同一'ready'事件中合并两者,以确保首先对齐。

google.visualization.events.addListener(chart, 'ready', function () {
  AlignTitle();
  CreateImageToExport(chart, 'chart_div', 1000, 300);
});

然而,这不会解决问题,
getImageURI不包括在绘制图表后对图表进行的修改...

要使用自定义模式获取图像,您可以使用html2canvas

function CreateImageToExport(chartContainer, pWidth, pHeight) {
  html2canvas(chartContainer, {
    allowTaint: true,
    taintTest: false
  }).then(function(canvas) {
    var browserIsIE = false || !!document.documentMode;
    var domURL = window.URL || window.webkitURL || window;
    var imageURI;
    if (browserIsIE) {
      imageURI = domURL.createObjectURL(canvas.msToBlob(), {type: 'image/png'});
    } else {
      imageURI = canvas.toDataURL('image/png');
    }

    $('#' + chartContainer.id + '_Export').remove();
    var vExportedImage = document.createElement('img');
    vExportedImage.src = imageURI;
    vExportedImage.width = pWidth;
    vExportedImage.height = pHeight;
    vExportedImage.id = chartContainer.id + '_Export';
    document.getElementById('divImageCharts').appendChild(vExportedImage);
  });
}

看到这个小提琴的工作实例......
https://jsfiddle.net/274qjomw/11/
(出于某种原因,html2canvas在stackoverflow片段中不起作用)