当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《多个Google图表无法在同一页面上正常显示》经验,为你挑选了1个好方法。

我在一个页面上显示了4个图表.当单独绘制时它们完美地工作,但是当我尝试超过1时它们并不是全部显示出来.我还注意到,在调整窗口大小(因此刷新图表)时,"活动"图表可能会发生变化.

这是绘制图表的方法:

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);
}

我还将代码上传到jsfiddle,以便您可以自己查看.您可以使用顶部的整数来选择要显示的图形.如果值不是2的幂(即显示多个图形),则会导致以下情况:

div的大小,我的意思是布局表明它存在

div中没有​​任何内容

这是一个示例,其中绘制了2个图形,一个是正确绘制的,第二个是空白的:

一个图表不起作用的示例

我们可以清楚地看到平均持续时间 div有一个大小,但是是空白的.



1> Vadim Gremya..:

这是google-visualization-issues存储库中报告的已知问题:

应该通过这个新版本解决人们在查看多个实体图表实例时遇到的问题.您可以将代码更改为现在加载"1.1",以便在候选版本可用时,您将使用它.

目前至少有两种解决方案可供使用.

选项1.使用冻结版本加载器.

由于将解决此问题的v43候选版本的推出 切换到使用冻结版本加载器.

脚步:

1)添加对loader的引用:

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

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

修改了jsfiddle

选项2.同步渲染图表

由于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);
}

修改了jsfiddle

推荐阅读
可爱的天使keven_464
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有