【问题标题】:Showing graphs with php from Mysql tables用 php 从 Mysql 表中显示图表
【发布时间】:2013-01-17 07:43:24
【问题描述】:

如何使用 PHP 图像函数动态显示存储在 MySQL 表中的数据的统计信息?我可以使用静态表上的计数查询以条形图的形式显示计数,但如给出的图像所示。我有动态存储服务器日志的数据库。

图像由 3 个表格和 php 的 image & draw 函数生成

【问题讨论】:

    标签: php mysql image dynamic-data


    【解决方案1】:

    我不知道你是如何尝试的。但是有一个很好的 Jquery 插件,叫做 Highcharts。您可以使用它来创建各种类型的图表。有兴趣的请参考以下链接

    http://www.highcharts.com/

    【讨论】:

      【解决方案2】:

      Rgraph 也是一个非常好的 HTML5/Javascript 图形库,易于使用且有很多选项。

      http://www.rgraph.net/

      var data = ['<?php echo implode("','", $average); ?>']; 
      
      function drawGraph(){
          RGraph.Clear(document.getElementById('myRadar'));
          var radar = new RGraph.Radar('myRadar', data);
      

      这是添加 PHP 数组的方法,$average 是带有数字的数组,在本例中是雷达图。 implode 函数在数组的值之间放置一个逗号。

      【讨论】:

        【解决方案3】:

        我用下面的代码用php创建了一个简单的条形图。 您必须从数据库中获取 bar 值并将其放入 $data 数组中。 接下来必须为图像大小设置 $height 和 $width。 根据您的图像大小,您必须定位聊天。

        <?php
        
        $data = array('400', '2570', '245', '473', '1000', '3456', '780', '5000', '30', '420');
        $sum = array_sum($data);
        
        $height = 480;
        $width  = 640;
        
        $im         = imagecreate($width, $height);
        $background = imagecolorallocate($im, 255, 255, 255);
        
        $white  = imagecolorallocate($im, 255, 255, 255);
        $black  = imagecolorallocate($im, 0, 0, 0);
        $red    = imagecolorallocate($im, 255, 0, 0);
        $green  = imagecolorallocate($im, 51, 153, 0);
        $yellow = imagecolorallocate($im, 255, 255, 0);
        
        imageline($im, 10, 5, 10, $height - 20, $black);
        imageline($im, 10, $height - 20, 620, $height - 20, $black);
        
        header("Content-type: image/png");
        
        $x       = 11;
        $y       = 459;
        $x_width = 20;
        $y_ht    = 0;
        
        $max_i = count($data);
        for ($i     = 0; $i < $max_i; $i++) {
            $y_ht = ($data[$i] / $sum) * $height;
            if ($data[$i] > 1000) {
                imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $green);
            } else if ($data[$i] > 500) {
                imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $yellow);
            } else {
                imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $red);
            }
            imagestring($im, 2, $x - 1, ($y - $y_ht - 15), $data[$i], $black);
            $x += ($x_width + 2);
        }
        imagepng($im);
        imagedestroy($im);
        ?>
        

        参考:http://www.talkphp.com/advanced-php-programming/1629-bar-chart-php.html

        【讨论】:

          猜你喜欢
          • 2012-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-13
          • 1970-01-01
          • 1970-01-01
          • 2013-01-23
          • 1970-01-01
          相关资源
          最近更新 更多