如何从mysql数据创建图表? (使用谷歌可视化API)

发布于 2024-09-01 02:19:05 字数 1213 浏览 1 评论 0原文

我有一些数据,想要创建一些动态图表。我看过 Google 可视化 api ..它看起来很棒,但问题是我对它不是很熟悉。任何想法,我如何从 mysql 数据设置 data.setValue

  <script type='text/javascript'>
   google.load('visualization', '1', {'packages': ['geomap']});
   google.setOnLoadCallback(drawMap);

    function drawMap() {
      var data = new google.visualization.DataTable();
      data.addRows(6);
      data.addColumn('string', 'Country');
      data.addColumn('number', 'Popularity');
      data.setValue(0, 0, 'Germany');
      data.setValue(0, 1, 200);
      data.setValue(1, 0, 'United States');
      data.setValue(1, 1, 300);
      data.setValue(2, 0, 'Brazil');
      data.setValue(2, 1, 400);
      data.setValue(3, 0, 'Canada');
      data.setValue(3, 1, 500);
      data.setValue(4, 0, 'France');
      data.setValue(4, 1, 600);
      data.setValue(5, 0, 'RU');
      data.setValue(5, 1, 700);

      var options = {};
      options['dataMode'] = 'regions';

      var container = document.getElementById('map_canvas');
      var geomap = new google.visualization.GeoMap(container);
      geomap.draw(data, options);
  };
  </script>

我可以使用其他一些方法创建图表,但只是对使用 Google Visualization API 感兴趣。

谢谢。

I have some data and want to create some dynamic charts. I have looked on Google visualisation api .. It looks great but the problem is I am not very familiar with it. Any ideas, how I can set the data.setValue from mysql data.

  <script type='text/javascript'>
   google.load('visualization', '1', {'packages': ['geomap']});
   google.setOnLoadCallback(drawMap);

    function drawMap() {
      var data = new google.visualization.DataTable();
      data.addRows(6);
      data.addColumn('string', 'Country');
      data.addColumn('number', 'Popularity');
      data.setValue(0, 0, 'Germany');
      data.setValue(0, 1, 200);
      data.setValue(1, 0, 'United States');
      data.setValue(1, 1, 300);
      data.setValue(2, 0, 'Brazil');
      data.setValue(2, 1, 400);
      data.setValue(3, 0, 'Canada');
      data.setValue(3, 1, 500);
      data.setValue(4, 0, 'France');
      data.setValue(4, 1, 600);
      data.setValue(5, 0, 'RU');
      data.setValue(5, 1, 700);

      var options = {};
      options['dataMode'] = 'regions';

      var container = document.getElementById('map_canvas');
      var geomap = new google.visualization.GeoMap(container);
      geomap.draw(data, options);
  };
  </script>

I can create chart using some other methods but just interested in using Google Visualisation API.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城空念 2024-09-08 02:19:06

我推荐 http://pchart.sourceforge.net/ pchart 进行绘图。它工作得很好。

i recommend http://pchart.sourceforge.net/ pchart for graphing. it works perfectly.

暖阳 2024-09-08 02:19:05

更新:

了解如何向图表添加数据。您可以添加 JSON 格式的数据

您唯一要做的就是准备一个相应的 PHP 数组。然后你可以序列化这个数组并设置数据。例如,

<?php 
// $data is an array and already has the correct structure...
$jdata = json_encode($data);

?>

<!-- later ... -->

<script type='text/javascript'>
   google.load('visualization', '1', {'packages': ['geomap']});
   google.setOnLoadCallback(drawMap);

   function drawMap() {
      var data = new google.visualization.DataTable(<?php echo $jdata ?>);
      var options = {};
      options['dataMode'] = 'regions';

      var container = document.getElementById('map_canvas');
      var geomap = new google.visualization.GeoMap(container);
      geomap.draw(data, options);
   };
</script>

我建议阅读文档/API 参考。我基本上只是通过搜索发现了这一点...


没有进一步的信息,我们无法给出具体的答案,但一般方法是:

假设您已经从结果集中的数据库中获取了记录 $results ,而不是您可以循环它:

<?php foreach($results as $row): ?>

    data.setValue(<?php echo $row['column1']; ?>, <?php echo $row['column2']; ?>);
    // depends on what type of char you want to create, on your actual data etc.

<?php endforeach; ?>

Update:

Have a look at how you can add data to the chart. You have the possibility to add data in JSON.

The only thing you have to do is to prepare a corresponding PHP array. Then you can serialize this array and set the data. E.g.

<?php 
// $data is an array and already has the correct structure...
$jdata = json_encode($data);

?>

<!-- later ... -->

<script type='text/javascript'>
   google.load('visualization', '1', {'packages': ['geomap']});
   google.setOnLoadCallback(drawMap);

   function drawMap() {
      var data = new google.visualization.DataTable(<?php echo $jdata ?>);
      var options = {};
      options['dataMode'] = 'regions';

      var container = document.getElementById('map_canvas');
      var geomap = new google.visualization.GeoMap(container);
      geomap.draw(data, options);
   };
</script>

I recommend to read the documentation / API reference. I basically found this just by searching...


Without further information we cannot give a specific answer but a general approach is:

Assuming that you already fetched the records from your DB in a result set $results than you can just loop over it:

<?php foreach($results as $row): ?>

    data.setValue(<?php echo $row['column1']; ?>, <?php echo $row['column2']; ?>);
    // depends on what type of char you want to create, on your actual data etc.

<?php endforeach; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文