简体   繁体   English

一页可以有多个谷歌图表吗?

[英]is it possible to have multiple google charts in one page?

I am using Google visualization api.我正在使用谷歌可视化 api。 Following is my sample code.以下是我的示例代码。 Either of the two charts can be shown if it is the only one to be drawn.如果是唯一要绘制的,则可以显示两个图表中的任何一个。 But I can not get both working.但我不能同时工作。 Thanks for your advice.谢谢你的建议。

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <title>Home Page</title>
        <!--Load the AJAX API-->
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
            //Load the Visualization API and the ready-made Google table visualization
            google.load('visualization', '1', {'packages':['corechart']});
        </script>

        <script type='text/javascript'>

            function drawA() {
                // Define the chart using setters:
                var wrap = new google.visualization.ChartWrapper();
                wrap.setChartType('ColumnChart');
                wrap.setDataSourceUrl('dataurl');
                wrap.setContainerId('checkin-column');
                wrap.setOptions({'title':'Daily Check-in Numbers', 'width':500,'height':400});
                wrap.draw();
                wrap.getChart();
            }

            function drawB() {
                // Define the chart using setters:
                var wrap = new google.visualization.ChartWrapper();
                wrap.setChartType('ColumnChart');
                wrap.setDataSourceUrl('dataurl2');
                wrap.setContainerId('redemption-table');
                wrap.setOptions({'title':'Redemption History', 'width':500,'height':400});
                wrap.draw();
            }



            function drawVisualization() {
               drawA();
                drawB();

            }


            google.setOnLoadCallback(drawVisualization);
        </script>
    </head>
    <body>



        <div id="checkin-column"/>
        <p/>
        <div id="redemption-table"/>

    </body>
</html>

The problem is in your divs.问题出在您的 div 中。

Replace this:替换这个:

<div id="checkin-column"/>
<p/>
<div id="redemption-table"/>

With this:有了这个:

<div id="checkin-column"></div>
<p></p>
<div id="redemption-table"></div>

Just throwing down some information that relates to a problem I had (not the OP's exact Q).只是扔掉一些与我遇到的问题有关的信息(不是 OP 的确切 Q)。

When you want to draw multiple charts on one page, you must use different ids for the divs your targeting (duh.), So if you have a page that should have multiple charts but only 1 (or N < expected) is rendering.当您想在一个页面上绘制多个图表时,您必须为您的目标 div 使用不同的 id (duh.),因此,如果您的页面应该有多个图表但只有 1 个(或 N < 预期)正在呈现。 double check that there is a unique div id for each chart and that the JavaScript targets a unique div id.仔细检查每个图表是否有唯一的 div id,并且 JavaScript 的目标是唯一的 div id。

Eg, the following will render only one chart:例如,以下将仅呈现一个图表:

<div id="chart"></div>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load('visualization', '1', {packages: ['corechart']});
    google.setOnLoadCallback(drawChart);
    google.setOnLoadCallback(drawChart2);

    function drawChart() {
        var data = google.visualization.arrayToDataTable([
            ['', 'Stuff', 'Stuff2'],
            ['', 1.0, 2.0]
        ]);

        var options = {
            title: 'MyChart',
            pointSize: 3,
            vAxis: {
                baseline: 0.1,
                title: 'vAxis'
            },
            hAxis: {
                title: 'hAxis'
            }
        };
        var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
        chart.draw(data, options);
    }

        function drawChart2() {
            var data = google.visualization.arrayToDataTable([
                ['', 'Stuff', 'Stuff2'],
                ['', 4.0, 5.0]
            ]);

            var options = {
                title: 'MyChart2',
                pointSize: 3,
                vAxis: {
                    baseline: 0.1,
                    title: 'vAxis'
                },
                hAxis: {
                    title: 'hAxis'
                }
            };
            var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
            chart.draw(data, options);
        }
    </script>

Probably not your problem, but on a related note, I have found that if you have lots of Google Visualizations on the same page ( I believe that it was in the neighborhood of 50), Google will throttle the usage, and stop rendering properly.可能不是您的问题,但在相关说明中,我发现如果您在同一页面上有很多 Google 可视化(我相信它在 50 左右),Google 将限制使用,并停止正确渲染。

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

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