简体   繁体   English

如何将多个重要的Google图表添加到一页?

[英]How to add multiple material google charts to one page?

This question has been asked before , but in regards to the old corechart API, which I haven't had a problem with, not the new Material charts. 之前曾有人问过这个问题 ,但是关于旧的corechart API(不是我遇到的问题),不是新的Material图表。 For instance, the following code will create two charts as expected: 例如,以下代码将按预期创建两个图表:

var data = [
    ['Year', 'Sales', 'Expenses', 'Profit'],
    ['2014', 1000, 400, 200],
    ['2015', 1170, 460, 250],
    ['2016', 660, 1120, 300],
    ['2017', 1030, 540, 350]
];

google.load('visualization', '1', {
    'packages': ['corechart']
});

google.setOnLoadCallback(drawChart);

function drawChart() {

    new google.visualization.ColumnChart(document.getElementById('groupedBar')).draw(
        new google.visualization.arrayToDataTable(data), {});

    new google.visualization.ColumnChart(document.getElementById('stackedBar')).draw(
        new google.visualization.arrayToDataTable(data), {isStacked:true});
}

http://jsfiddle.net/crclayton/r67ega11/10/ http://jsfiddle.net/crclayton/r67ega11/10/

However, the updated version: 但是,更新的版本:

google.load('visualization', '1.1', {      // note version 1.1 and bar package 
    'packages': ['bar']                     
});

google.setOnLoadCallback(drawChart);

function drawChart() {

    new google.charts.Bar(document.getElementById('groupedBar')).draw(
    new google.visualization.arrayToDataTable(data),
    google.charts.Bar.convertOptions({}));

    new google.charts.Bar(document.getElementById('stackedBar')).draw(
    new google.visualization.arrayToDataTable(data),
    google.charts.Bar.convertOptions({
        isStacked: true
    }));
}

http://jsfiddle.net/crclayton/r67ega11/6/ http://jsfiddle.net/crclayton/r67ega11/6/

... will only render one of the charts, sometimes the first, sometimes the second. ...将仅呈现其中一个图表,有时是第一个,有时是第二个。 It won't throw an error, it will simply ignore the other. 它不会抛出错误,只会忽略另一个错误。 I've tried breaking them up into individual functions, assigning everything to variables, resetting google.setOnLoadCallback with the same results. 我尝试将它们分解为单个函数,将所有内容分配给变量,并以相同的结果重置google.setOnLoadCallback

I've also found that when rendering different types of charts , I don't have that problem. 我还发现,在呈现不同类型的图表时 ,我没有这个问题。

Any ideas? 有任何想法吗?

It's most likely the same issue that was reported in google-visualization-issues repository: 这很可能是google-visualization-issues存储库中报告的同一问题

The problems people have seen with the sizing of multiple instances of material charts should be resolved with this new release. 人们在使用物料图的多个实例确定大小时所遇到的问题应通过此新版本解决。 You can change your code to load "1.1" now so that when the candidate release becomes available, you will be using it. 您可以更改代码以立即加载“ 1.1”,以便在候选版本可用时使用。

There are at least two solutions available at the moment: 目前至少有两种解决方案可用:

Option 1. Render charts synchronously 选项1.同步渲染图表

The general idea is to render chart synchronously. 总体思路是同步绘制图表。 Since draw function is asynchronous, we utilize ready event handler for that purpose. 由于绘图功能是异步的,因此我们为此目的使用了ready事件处理程序。

Replace: 更换:

function drawChart_() {
    new google.charts.Bar(document.getElementById('groupedBar')).draw(
    new google.visualization.arrayToDataTable(data),
    google.charts.Bar.convertOptions({}));



    new google.charts.Bar(document.getElementById('stackedBar')).draw(
    new google.visualization.arrayToDataTable(data),
    google.charts.Bar.convertOptions({
        isStacked: true
    }));
}

with: 有:

function drawChart() {

    var groupedBarChart = new google.charts.Bar(document.getElementById('groupedBar'));
    google.visualization.events.addOneTimeListener(groupedBarChart, 'ready', function(){
       var stackedBarChart = new google.charts.Bar(document.getElementById('stackedBar'));
       stackedBarChart.draw(new google.visualization.arrayToDataTable(data),google.charts.Bar.convertOptions({isStacked: true}));
    });
    groupedBarChart.draw(new google.visualization.arrayToDataTable(data),google.charts.Bar.convertOptions({}));
}

Example

 var data = [ ['Year', 'Sales', 'Expenses', 'Profit'], ['2014', 1000, 400, 200], ['2015', 1170, 460, 250], ['2016', 660, 1120, 300], ['2017', 1030, 540, 350] ]; google.load('visualization', '1.1', { 'packages': ['bar'] }); google.setOnLoadCallback(drawChart); function drawChart() { var groupedBarChart = new google.charts.Bar(document.getElementById('groupedBar')); google.visualization.events.addOneTimeListener(groupedBarChart, 'ready', function(){ var stackedBarChart = new google.charts.Bar(document.getElementById('stackedBar')); stackedBarChart.draw(new google.visualization.arrayToDataTable(data),google.charts.Bar.convertOptions({isStacked: true})); }); groupedBarChart.draw(new google.visualization.arrayToDataTable(data),google.charts.Bar.convertOptions({})); } function drawChart_() { new google.charts.Bar(document.getElementById('groupedBar')).draw( new google.visualization.arrayToDataTable(data), google.charts.Bar.convertOptions({})); new google.charts.Bar(document.getElementById('stackedBar')).draw( new google.visualization.arrayToDataTable(data), google.charts.Bar.convertOptions({ isStacked: true })); } 
 <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="chart.js"></script> <div class="chart" id="groupedBar" style></div> <div class="chart" id="stackedBar" style></div> 

Option 2. Using the frozen version loader. 选项2.使用冻结的版本加载器。

Since 以来

The rollout of the v43 candidate release that would fix this problem v43候选版本的推出将解决此问题

switch to using the frozen version loader. 切换到使用冻结版本加载器。

Steps: 脚步:

1)Add a reference to loader: <script src="//www.gstatic.com/charts/loader.js"></script> 1)添加对加载程序的引用: <script src="//www.gstatic.com/charts/loader.js"></script>

2)Then load a 43 version of library: google.charts.load("43",{packages:["bar"]}); 2)然后加载43版本的库: google.charts.load("43",{packages:["bar"]});

3)Replace: 3)更换:

google.setOnLoadCallback(drawChart);

with

google.charts.setOnLoadCallback(drawChart);

Example

 var data = [ ['Year', 'Sales', 'Expenses', 'Profit'], ['2014', 1000, 400, 200], ['2015', 1170, 460, 250], ['2016', 660, 1120, 300], ['2017', 1030, 540, 350] ]; google.charts.load("43",{packages:["bar","corechart"]}); google.charts.setOnLoadCallback(drawChart); function drawChart() { new google.charts.Bar(document.getElementById('groupedBar')).draw( new google.visualization.arrayToDataTable(data), google.charts.Bar.convertOptions({})); new google.charts.Bar(document.getElementById('stackedBar')).draw( new google.visualization.arrayToDataTable(data), google.charts.Bar.convertOptions({ isStacked: true })); } 
 <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script src="http://www.gstatic.com/charts/loader.js"></script> <div class="chart" id="groupedBar" style></div> <div class="chart" id="stackedBar" style></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM