1

我正在创建谷歌饼图,但在这里我必须使用 iframe 来显示此图表,但我想通过 ajax 而不使用 iframe 来显示它......有没有办法在没有 iframe 的情况下显示谷歌饼图?

4

2 回答 2

1

@Grrbrr404 提供的链接不再起作用,所以你可以试试,

脚本

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {
  var jsonData = $.ajax({
      url: "getData.php",
      dataType:"json",
      async: false
      }).responseText;

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240});
}

</script>

chart_div在添加上述脚本之前,在您的 html 中添加一个具有 id 的 div,例如,

<div id="chart_div"></div>

显示这个php 示例

于 2014-09-01T06:48:45.470 回答
0

您可以使用 google chart api 用纯 JavaScript 显示饼图。

有关示例代码,请参见此页面

于 2011-12-19T07:51:33.740 回答