gpt4 book ai didi

javascript - 如何在一页中加载两个 Google 折线图?

转载 作者:行者123 更新时间:2023-11-27 22:55:49 25 4
gpt4 key购买 nike

我深入研究了一些类似的问题herehere 。我尝试了他们的实现。没成功!

然后我查看了 Google 提供的以下链接:tow charts in one page 。遵循相同的过程,但最终仍然一次只显示一张图表。

下面是我的代码:

 <script type = "text/javascript" src = "https://www.google.com/jsapi" ></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!--Load the Ajax API-->

<script type="text/javascript">

google.load("visualization", "1", {packages:["line"]});
google.setOnLoadCallback(drawChart);
google.setOnLoadCallback(drawOutputChart);

function drawChart() {
var data = new google.visualization.DataTable(<?= $jsonAnalogTable ?>);
var options = {
chart: {
title: ' Data ',
is3D: 'true'
},
width: 550,
height: 350,

};
var chart = new google.charts.Line(document.getElementById('analogchart'));
chart.draw(data, options);
}

function drawOutputChart(){
var data2 = new google.visualization.DataTable(<?= $jsonOutputTable ?>);
var options2 = {
chart: {
title: ' Data ',
is3D: 'true'
},
width: 550,
height: 350,

};

var chart2 = new google.charts.Line(document.getElementById('outputChart'));
chart2.draw(data2, options2);
}


</script>

**调用它的 HTML 代码:**

<table>
<tr>
<td>
<div id="analogchart" >

</div>
</td>
<td>
<div id="outputChart" >

</div>
</td>

</tr>
</table>

我真的不知道出了什么问题,因为它一次只能显示一张图表,但不能同时显示两者!

最佳答案

建议使用 loader.js 与旧库 jsapi
对于 Line 图表而言,两者都不需要

此外,setOnLoadCallback 每个页面只能调用一次
可以包含在 load 语句中,如以下示例所示

尝试这样的事情......

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">

google.charts.load('current', {
callback: function () {
drawChart();
drawOutputChart();
},
packages: ['line']
});

function drawChart() {
var data = new google.visualization.DataTable(<?= $jsonAnalogTable ?>);
var options = {
chart: {
title: ' Data ',
is3D: 'true'
},
width: 550,
height: 350,
};
var chart = new google.charts.Line(document.getElementById('analogchart'));
chart.draw(data, options);
}

function drawOutputChart(){
var data2 = new google.visualization.DataTable(<?= $jsonOutputTable ?>);
var options2 = {
chart: {
title: ' Data ',
is3D: 'true'
},
width: 550,
height: 350,
};

var chart2 = new google.charts.Line(document.getElementById('outputChart'));
chart2.draw(data2, options2);
}

</script>

关于javascript - 如何在一页中加载两个 Google 折线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com