【问题标题】:PHP generating PNG bar chartPHP生成PNG条形图
【发布时间】:2012-02-06 04:03:42
【问题描述】:

我有一个包含订单的数据库,需要生成一个 PNG 图表来显示每个月的订单总数。我可以从普通数组中使它工作,但是我似乎无法从我的数据库查询中使它工作。

我认为需要更改的代码是第三个和第四个 for 循环,因为这是我认为生成月份名称和条形的代码。

这是通过数组工作的代码,我需要使用在 $values 数组下声明的 SQL 查询:

<?php

require_once(DATABASE CONNECTOR REMOVED);

//Set the values
$values=array(
    "Jan" => 1100,
    "Feb" => 1300,
    "Mar" => 2150,
    "Apr" => 810,
    "May" => 3100,
    "Jun" => 1100,
    "Jul" => 1900,
    "Aug" => 1750,
    "Sep" => 3900,
    "Oct" => 2860,
    "Nov" => 1500,
    "Dec" => 1960
); 

$query = mysql_query("SELECT SUM(order_total) AS total, MONTHNAME(FROM_UNIXTIME(order_date)) AS month FROM orders GROUP BY YEAR (order_date), MONTH(order_date)") or die ('Error: '.mysql_error());

$img_width=600;
$img_height=400;
$margins=35;
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2; 

$img=imagecreatetruecolor($img_width,$img_height);

$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); 

$bar_color=imagecolorallocate($img,70,85,96);
$background_color=imagecolorallocate($img,255,255,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,70,85,96);

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);

$max_value=max($values);
$ratio= $graph_height/$max_value;

$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;
for($i=1;$i<=$horizontal_lines;$i++){
    $y=$img_height - $margins - $horizontal_gap * $i ;
    imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
    $v='£'.intval($horizontal_gap * $i /$ratio);
    imagettftext($img, 8, 0, 2 ,$y+5, $bar_color, 'includes/trebuc.ttf', $v);
}

for($i=1;$i<=$horizontal_lines;$i++){
    $y=$img_height - $margins - $horizontal_gap * $i ;
    imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
    $v='£'.intval($horizontal_gap * $i /$ratio);
    imagettftext($img, 8, 0, 2 ,$y+5, $bar_color, 'includes/trebuc.ttf', $v);
}


    for($i=0;$i< $total_bars; $i++){
        list($key,$value)=each($values);
        $x1= $margins + $gap + $i * ($gap+$bar_width) ;
        $x2= $x1 + $bar_width;
        $y1=$margins +$graph_height- intval($value * $ratio) ;
        $y2=$img_height-$margins;
        imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
        imagettftext($img, 10, 0, $x1,$img_height - 14, $bar_color, 'includes/trebuc.ttf', $key);
}



for($i=0;$i< $total_bars; $i++){
    list($key,$value)=each($values);
    $x1= $margins + $gap + $i * ($gap+$bar_width) ;
    $x2= $x1 + $bar_width;
    $y1=$margins +$graph_height- intval($value * $ratio) ;
    $y2=$img_height-$margins;
    imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
    imagettftext($img, 10, 0, $x1,$img_height - 14, $bar_color, 'includes/trebuc.ttf', $key);
}

header("Content-type:image/png");
imagepng($img); 

?>

编辑

我知道这不仅限于过去 12 个月,我的目标是在我通过数据库中的值构建图表后解决这个问题

【问题讨论】:

  • 您不需要查看从数据库中获取的内容而不是发布该代码吗?
  • 我使用了 while($fetch = mysql_fetch_array($query)){ echo 'Month vs total: '.$fetch["month"].' - '.$fetch["total"].'
    '; } 检查输出并按预期输出“Month vs total: November - 1155.50
    ”等。
  • 起初,您的代码被 gd 调用弄乱了,这似乎与您的问题无关:代码正在使用给定的内联数组,但不适用于数据库数据。正确的?所以,检查数据库数据。像在代码中一样将其提取到数组中,然后重试。你可以调试你的代码,我们其他人在这里 - 不是。 (+ 一些建议:看看图形库:jpgraph

标签: php png php-gd


【解决方案1】:

您必须获取结果并填充$values 数组:

mysql_query()之后:

$values = array();
while ($row = mysql_fetch($query) {
  $values[substr($row['month'], 0, 3)] = $row['total'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2021-12-18
    • 2023-03-28
    相关资源
    最近更新 更多