简体   繁体   English

多个Google图表无法在同一页面上正确显示

[英]Multiple Google charts not displaying properly on the same page

I've got 4 charts displayed on a single page. 我在单个页面上显示了4个图表。 When drawn individually they work perfectly fine, but when I try to have more than 1 they're not all displayed. 当单独绘制时,它们工作得很好,但是当我尝试超过1个时,它们并不会全部显示。 I have also noticed that when resizing the window (hence refreshing the charts), the "active" chart can change. 我还注意到,在调整窗口大小(从而刷新图表)时,“活动”图表可以更改。

Here's my method that draws the charts: 这是我绘制图表的方法:

function drawChart() {
    // Occurrences per step
    var data_occ =  new google.visualization.DataTable();
    data_occ.addColumn('string', 'Step');
    data_occ.addColumn('number', 'Number');
    data_occ.addRows([
        ['NO_STOP_DEP', 2057],
        ['FIND_STOPS_DEP', 795],
        ['FIND_STOPS_ARR', 423],
        ['FIND_ROUTES', 416],
        ['FIND_PATHS_0', 416],
        ['NO_STOP_ARR', 371],
        ['FIND_PATHS_1', 359],
        ['JOURNEY_GET_1CONNECTION_FAILED', 274],
        ['FIND_PATHS_2', 274],
        ['JOURNEY_GET_1CONNECTION_t1d', 185],
        ['OK', 147],
        ['JOURNEY_GET_2CONNECTION_t1d', 145],
        ['JOURNEY_GET_1CONNECTION_t1a', 138],
        ['NO_PATH', 129],
        ['JOURNEY_GET_2CONNECTION_FAILED', 118],
        ['NO_JOURNEY', 118],
        ['JOURNEY_GET_1CONNECTION_cs1', 117],
        ['JOURNEY_GET_1CONNECTION_t2d', 115],
        ['JOURNEY_GET_DIRECT_t1d', 111],
        ['JOURNEY_GET_2CONNECTION_t1a', 79],
        ['JOURNEY_GET_2CONNECTION_cs1', 75],
        ['JOURNEY_GET_2CONNECTION_t2d', 73],
        ['JOURNEY_GET_2CONNECTION_t2a', 66],
        ['JOURNEY_GET_2CONNECTION_cs2', 66],
        ['JOURNEY_GET_2CONNECTION_t3d', 66],
        ['JOURNEY_GET_1CONNECTION', 65],
        ['JOURNEY_GET_DIRECT', 56],
        ['JOURNEY_GET_DIRECT_FAILED', 54],
        ['JOURNEY_GET_2CONNECTION', 26],
        ['NO_ROUTE_ARR', 4],
        ['NO_ROUTE_DEP', 3]
    ]);
    var opt_occ = {
        chart: {
            title: 'Occurrences of each step'
        }
    };
    var chart_occ = new google.charts.Bar(document.getElementById('chart_occ'));
    chart_occ.draw(data_occ, opt_occ);

    // Sum of durations per step
    var data_dur =  new google.visualization.DataTable();
    data_dur.addColumn('string', 'Step');
    data_dur.addColumn('number', 'Duration');
    data_dur.addRows([
        ['JOURNEY_GET_2CONNECTION_t2d', 4271651.423530579],
        ['NO_STOP_DEP', 954578.8992916346],
        ['FIND_STOPS_DEP', 711477.470664978],
        ['JOURNEY_GET_DIRECT_t1d', 604728.3424301147],
        ['JOURNEY_GET_1CONNECTION_t2d', 483084.8451423645],
        ['JOURNEY_GET_1CONNECTION_t1d', 399811.6393585205],
        ['JOURNEY_GET_2CONNECTION_t3d', 391471.8716468811],
        ['FIND_PATHS_1', 173883.78058815002],
        ['FIND_STOPS_ARR', 164751.4531224966],
        ['JOURNEY_GET_2CONNECTION_t1d', 158291.4034690857],
        ['FIND_PATHS_2', 154918.55130004883],
        ['FIND_ROUTES', 125470.71777877212],
        ['NO_STOP_ARR', 82222.14379951358],
        ['JOURNEY_GET_1CONNECTION_cs1', 45374.44926452637],
        ['JOURNEY_GET_1CONNECTION_t1a', 29688.884063720703],
        ['JOURNEY_GET_2CONNECTION_cs2', 21626.706924438477],
        ['JOURNEY_GET_2CONNECTION_cs1', 13983.793979644775],
        ['JOURNEY_GET_2CONNECTION_t2a', 13081.894062042236],
        ['JOURNEY_GET_1CONNECTION', 11718.449104309082],
        ['JOURNEY_GET_2CONNECTION_FAILED', 9777.992935180664],
        ['JOURNEY_GET_1CONNECTION_FAILED', 9182.082992315292],
        ['JOURNEY_GET_DIRECT', 8991.909969329834],
        ['JOURNEY_GET_2CONNECTION_t1a', 8132.20499420166],
        ['NO_ROUTE_ARR', 5709.329235076904],
        ['JOURNEY_GET_DIRECT_FAILED', 5620.268951416016],
        ['FIND_PATHS_0', 4501.938883662224],
        ['JOURNEY_GET_2CONNECTION', 3359.796012878418],
        ['NO_PATH', 1778.0850104540586],
        ['OK', 1419.4850099533796],
        ['NO_JOURNEY', 1267.5709964483976],
        ['NO_ROUTE_DEP', 334.49600982666016]
    ]);
    var opt_dur = {
        chart: {
            title: 'Cumulative duration of each step (ms)'
        }
    };
    var chart_dur = new google.charts.Bar(document.getElementById('chart_dur'));
    chart_dur.draw(data_dur, opt_dur);

    // Average of durations per step
    var data_dur_avg =  new google.visualization.DataTable();
    data_dur_avg.addColumn('string', 'Step');
    data_dur_avg.addColumn('number', 'Duration');
    data_dur_avg.addRows([
        ['NO_ROUTE_DEP', 111.49866994222005],
        ['NO_ROUTE_ARR', 1427.332308769226],
        ['JOURNEY_GET_DIRECT', 160.5698208808899],
        ['NO_PATH', 13.783604732202004],
        ['JOURNEY_GET_1CONNECTION_cs1', 387.8158056797125],
        ['JOURNEY_GET_1CONNECTION_t2d', 4200.737783846648],
        ['JOURNEY_GET_1CONNECTION', 180.2838323739859],
        ['JOURNEY_GET_2CONNECTION_t1a', 102.93930372407165],
        ['JOURNEY_GET_2CONNECTION_cs1', 186.45058639526368],
        ['JOURNEY_GET_2CONNECTION_t2d', 58515.77292507642],
        ['JOURNEY_GET_2CONNECTION_t2a', 198.21051609154904],
        ['JOURNEY_GET_2CONNECTION_cs2', 327.6773776430072],
        ['JOURNEY_GET_2CONNECTION_t3d', 5931.3919946497135],
        ['JOURNEY_GET_2CONNECTION', 129.22292357224686],
        ['OK', 9.656360611927752],
        ['NO_STOP_ARR', 221.623029109201],
        ['JOURNEY_GET_1CONNECTION_t1a', 215.13684104145437],
        ['NO_STOP_DEP', 464.06363601926813],
        ['FIND_STOPS_DEP', 894.9402146729284],
        ['FIND_STOPS_ARR', 389.483340715122],
        ['FIND_ROUTES', 301.6123023528176],
        ['FIND_PATHS_0', 10.821968470341885],
        ['JOURNEY_GET_DIRECT_t1d', 5448.003084955989],
        ['JOURNEY_GET_DIRECT_FAILED', 104.07905465585215],
        ['FIND_PATHS_1', 484.35593478593324],
        ['JOURNEY_GET_1CONNECTION_t1d', 2161.1439965325435],
        ['JOURNEY_GET_1CONNECTION_FAILED', 33.51125179677114],
        ['FIND_PATHS_2', 565.3961726279155],
        ['JOURNEY_GET_2CONNECTION_t1d', 1091.6648515109357],
        ['JOURNEY_GET_2CONNECTION_FAILED', 82.86434690831071],
        ['NO_JOURNEY', 10.742127088545743]
    ]);
    var opt_dur_avg = {
        chart: {
            title: 'Average duration of each step (ms)'
        }
    };
    var chart_dur_avg = new google.charts.Bar(document.getElementById('chart_dur_avg'));
    chart_dur_avg.draw(data_dur_avg, opt_dur_avg);

    // Average duration comparison today vs yesterday
    var data_dur_avg_cmp =  new google.visualization.DataTable();
    data_dur_avg_cmp.addColumn('string', 'Step');
    data_dur_avg_cmp.addColumn('number', 'Yesterday');
    data_dur_avg_cmp.addColumn('number', 'Today');
    data_dur_avg_cmp.addRows([
    ]);
    var opt_dur_avg_cmp = {
        chart: {
            title: 'Average duration of each step (ms)'
        }
    };
    var chart_dur_avg_cmp = new google.charts.Bar(document.getElementById('chart_dur_avg_cmp'));
    chart_dur_avg_cmp.draw(data_dur_avg_cmp, opt_dur_avg_cmp);
}

I have also uploaded the code to a jsfiddle so that you can see for yourself. 我还将代码上传到jsfiddle中,以便您自己查看。 You can play with the integer at the top, to select which graphs will be displayed. 您可以在顶部播放整数,以选择要显示的图形。 Having a value that is not a power of 2 (that is, display more than one graph) will cause the following: 具有一个非2的幂的值(即,显示多个图形)将导致以下情况:

  • the div will be sized, I mean the layout indicates that it is present div的大小将被确定,我的意思是布局表明存在
  • nothing will be drawn in the div 不会在div中绘制任何内容

Here's an example with 2 graphs drawn, one is properly drawn and the second one is blank: 这是一个示例,其中绘制了2个图形,一个图形正确绘制,第二个图形为空白:

一个图不起作用的示例

We can clearly see that the Average duration div has a size, but is blank. 我们可以清楚地看到“ 平均持续时间” div有一个大小,但为空白。

It's the known 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. Using the frozen version loader. 选项1.使用冻结的版本加载器。

Since the rollout of the v43 candidate release that would fix this problem switch to using the frozen version loader. 由于v43候选版本的发布将解决此问题,因此切换到使用冻结版本加载程序。

Steps: 脚步:

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

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

3)Replace google.setOnLoadCallback(drawChart); 3)替换google.setOnLoadCallback(drawChart); with google.charts.setOnLoadCallback(drawChart); google.charts.setOnLoadCallback(drawChart);

Modified jsfiddle 修改后的jsfiddle

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

Since a draw function is asynchronous , we utilize ready event handler to draw charts sequentially, in that case multiple chart should be rendered properly as demonstrated below. 由于draw函数是异步的 ,因此我们利用ready事件处理程序按顺序绘制图表,在这种情况下,应正确呈现多个图表,如下所示。

function drawChart(chartsQueue,index) {
    index = index || 0;
    if (index === chartsQueue.length)
        return;



    var chart = new google.charts.Bar(document.getElementById(chartsQueue[index].id));
    google.visualization.events.addOneTimeListener(chart, 'ready', function() {
        drawChart(chartsQueue, index + 1); //draw next chart
    });
    chart.draw(chartsQueue[index].data, chartsQueue[index].options);
}

Modified jsfiddle 修改后的jsfiddle

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

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