简体   繁体   English

在同一页面上显示多个Google图表

[英]Displaying multiple Google charts on same page

I have looked into multiple google charts api, on same web page and couple of other URLs but invain. 在同一网页和其他几个URL 研究了多个Google Charts API,但这些都是无效的。 I am not able to load multiple / even single google chart on my page. 我无法在页面上加载多个甚至单个Google图表。 Single chart would come easily but as soon as I enter other charts all of them will go. 单张图表会很容易实现,但是一旦我输入其他图表,所有图表都会消失。 My google chart is inside the php script. 我的Google图表位于php脚本中。 The values are pulled successfully from the database, however google charts won't load. 这些值已成功从数据库中拉出,但是不会加载Google图表。 Dont know the error. 不知道错误。 Any help is appreciated. 任何帮助表示赞赏。 The code is as below: 代码如下:

<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:'gauge','table', 'corechart']});
</script>

<script type='text/javascript'>
google.setOnLoadCallback(initialize);
function initialize()   
{

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

function drawA() {
    var data_PUE = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['PUE',<?php echo $r_PUE['PUE']; ?> ]  ]);

var options_PUE =   {
          width : 800,
          height :  240,
          redFrom: 2.5, redTo: 5,
          yellowFrom:1.5, yellowTo: 2.5,
          greenFrom:0, greenTo: 1.5,
          minorTicks: 1,
          max:5
                };
var chart = new google.visualization.Gauge(document.getElementById('chart_div'));   
chart.draw(data_PUE, options_PUE);
        }

function drawB() {  
    var data_CEC = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['CEC',<?php echo $r_EDF_CEC['CEC']; ?> ]   ]);

var options_CEC =   {
          width : 120,
          height :  120,
          redFrom: 1, redTo: 3,
          yellowFrom:.5, yellowTo: 1,
          greenFrom:0, greenTo: .5,
          minorTicks: .5,
          max:3
                };
    var chart1 = new google.visualization.Gauge(document.getElementById('chart_div1')); 
    chart1.draw(data_CEC, options_CEC);
        }

function drawC() {
    var data_LEC = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['LEC',<?php echo $r_EDF_LEC['LEC']; ?> ]   ]);

var options_LEC =   {
          width : 120,
          height :  120,
          redFrom: .05, redTo: .1,
          yellowFrom:.01, yellowTo: .05,
          greenFrom:0, greenTo: .01,
          minorTicks: .01,
          max:.05
                };
    var chart2 = new google.visualization.Gauge(document.getElementById('chart_div2'));    
    chart2.draw(data_LEC,options_LEC);
        }
}

    </script>

<div id="chart_div" style="width: 200px; height: 150px;"></div>
<p></p>
<div id="chart_div" style="width: 200px; height: 150px;"></div>
<p></p>
<div id="chart_div" style="width: 200px; height: 150px;"></div>
<p></p>

There is an error here: 这里有一个错误:

<div id="chart_div" style="width: 200px; height: 150px;"></div>
<p></p>
<div id="chart_div" style="width: 200px; height: 150px;"></div>
<p></p>
<div id="chart_div" style="width: 200px; height: 150px;"></div>

I'm pretty sure you mean chart_div , chart_div1 , chart_div2 我很确定你的意思是chart_divchart_div1chart_div2

Debugging in firebug brings another error: 在Firebug中进行调试会带来另一个错误:

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

It should be 它应该是

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

And lastly, the function initialize() will do nothing because there is another function called drawVisualization() that was made which would do the drawing for you. 最后,函数initialize()不会执行任何操作,因为制作了另一个名为drawVisualization()函数,它将为您绘制图形。 Either call drawVisualization() or just remove that function declaration. 调用drawVisualization()或仅删除该函数声明。

http://jsbin.com/xerewuva/1/edit http://jsbin.com/xerewuva/1/edit

Your div id's at the bottom of your code are all called "chart_div" but your document.getElementById's are set to: 您在代码底部的div ID都称为“ chart_div”,但您的document.getElementById设置为:

document.getElementById('chart_div')
document.getElementById('chart_div1')
document.getElementById('chart_div2')

change your div id's to match: 更改您的div ID以匹配:

<div id="chart_div" style="width: 200px; height: 150px;"></div>
<div id="chart_div1" style="width: 200px; height: 150px;"></div>
<div id="chart_div2" style="width: 200px; height: 150px;"></div>

:-) :-)

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

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