php中的条形图和ajax并发症

时间:2016-08-02 11:58:02

标签: php mysql ajax bar-chart

你好朋友我试图在php中创建条形图。我在显示月度数据时遇到问题,日期数据显示正确。

另一个问题是我为每日和每月条形图选择了选项。当我每天选择它显示单个图表时,但是当我选择每月它显示每日和另一个月有错误。我在sql查询中写了什么

这是代码

revenue.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
 <link rel="stylesheet" href="./css/w3-theme-blue-grey.css">
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Open+Sans'>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">

 <link rel="stylesheet" href="./css/w3.css">
 <script type="text/javascript">
function getdata(str)
{
    //document.write("hi");
var xmlhttp;
if (str==0)
  { 
 alert("please select Table no");
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //  var dat=xmlhttp.responseText;
    //document.write(dat[0]);
    document.getElementById("show").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","fetchgraph.php?dt="+str,true);
xmlhttp.send();
}
</script>

</head>

<body class="w3-theme-l5">
<?php 
include("./header.php");
?>
<div class="w3-container w3-content" style="margin-top:100px; margin-bottom:100px; border:none">
<select onchange="getdata(this.value)">

<option value="" selected="selected">Select
</option>
<option value="daily">Daily
</option>

<option value="month">Monthly
</option>
</select>
<div id="show">
</div>
</div>
<?php 
include("./footer.php");
?>
</body>
</html>

fetchgraph.php

<?php 
include("./config.php");
$val=$_GET['dt'];
echo $val;
if($val="daily")
{
$sql="SELECT date,revenue FROM revennue_master where date BETWEEN CURDATE()-INTERVAL 10 DAY and CURDATE() group by date"; // Give Your field Name  
echo $sql;
$result = mysqli_query($con,$sql);
$graphtitle='BarChart';//Graph Title

$xname='Date'; //X-axis Name

$yname='Revenue';//y-axis name

$img_width=600;//image height

$img_height=400;//image width 

$margins=100;

$ymargin=6;

$count=mysqli_affected_rows($con);

$graph_width=$img_width - $margins * 2;

$graph_height=$img_height - $margins * 2; 
$bar_width=50;

$total_bars=$count;

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

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

include './barcolor.php';
imagefilledrectangle($img,0,0,0,0,$bag_color);
imageline($img,$margins,$img_height-45,$img_width-20,$img_height-45,$xyline_color);
imageline($img,$margins,$ymargin,$margins,$img_height-45,$xyline_color);

$maxvalue="select max(ceil(revenue)) as amount from revennue_master";//Give your field name for Y axis 
$max=mysqli_query($con,$maxvalue);
while($inf1= mysqli_fetch_array($max)) 
  {
      //$n=number_format($inf1[0],0,'',',');
      //echo $n;
   $ratio=1000;
  }
  $v=0;
$horizontal_lines=8;
$horizontal_gap=($img_height+20)/$horizontal_lines;
echo $horizontal_gap." ";
for($j=1;$j<=$horizontal_lines;$j++)
{
        $y=($img_height-48) - $horizontal_gap * $j ;
        //imageline($img,$margins+1,$y,$img_width-20,$y,$hline_color);
        $v+=intval($ratio);
      imagestring($img,2,$margins-30,$y-5,$v,$values_color);
}
$i=0;
while($inf = mysqli_fetch_array($result)) 
  {

      $x1=($margins+10)+($gap+5)+$i*($gap+$bar_width);
      //$y1=($img_height-46)-ceil($inf[1]/600);
      $y1=($img_height-48)-ceil($inf[1]*$horizontal_gap/$ratio);
      echo $y1;
      $x2=$x1+$bar_width;
      $y2=($img_height-46);
      //echo $y2;
    imagestring($img,2,$x1+1,$y1-15,$inf[1],$values_color); 
    imagestring($img,2,$x2-46,$img_height-43,$inf[0],$values_color);  
    imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); // Draw bar
   $i++;   
  }
imagestring($img,8, ($img_width-$margins)/2, 0, $graphtitle, $txt_color);
imagestring($img,5, ($img_width-$margins)/2, $img_height-($ymargin+10), $xname, $txt_color);
imagestringup($img,5,10,($img_height-$ymargin)/2, $yname, $txt_color);
//header('Content-type: image/png');
imagepng($img, 'barchart.jpg');
echo "<div style='width:$img_width'><img src='barchart.jpg'></div>";
}

if($val="month")
{
$sql="SELECT MONTH(date),sum(revenue) FROM revennue_master where MONTH(date) BETWEEN MONTH(DATEADD(mm,-5,date)) and MONTH(date) group by MONTH(date)"; // Give Your field Name  
echo $sql;
$result = mysqli_query($con,$sql);
$graphtitle='BarChart';//Graph Title

$xname='Date'; //X-axis Name

$yname='Revenue';//y-axis name

$img_width=600;//image height

$img_height=400;//image width 

$margins=100;

$ymargin=6;

$count=mysqli_affected_rows($con);

$graph_width=$img_width - $margins * 2;

$graph_height=$img_height - $margins * 2; 
$bar_width=50;

$total_bars=$count;

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

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

include './barcolor.php';
imagefilledrectangle($img,0,0,0,0,$bag_color);
imageline($img,$margins,$img_height-45,$img_width-20,$img_height-45,$xyline_color);
imageline($img,$margins,$ymargin,$margins,$img_height-45,$xyline_color);

$maxvalue="select max(sum(revenue)) as amount from revennue_master where MONTH(date) BETWEEN MONTH(DATEADD(mm,-5,date)) and MONTH(date)";//Give your field name for Y axis 
$max=mysqli_query($con,$maxvalue);
while($inf1= mysqli_fetch_array($max)) 
  {
      //$n=number_format($inf1[0],0,'',',');
      //echo $n;
   $ratio=1000;
  }
  $v=0;
$horizontal_lines=8;
$horizontal_gap=($img_height+20)/$horizontal_lines;
echo $horizontal_gap." ";
for($j=1;$j<=$horizontal_lines;$j++)
{
        $y=($img_height-48) - $horizontal_gap * $j ;
        //imageline($img,$margins+1,$y,$img_width-20,$y,$hline_color);
        $v+=intval($ratio);
      imagestring($img,2,$margins-30,$y-5,$v,$values_color);
}
$i=0;
while($inf = mysqli_fetch_array($result)) 
  {

     // echo $getx;
      //$x1=($margins+10) + ($gap+5) + $i * ($gap+$bar_width) ;
      //$x2=$x1+$bar_width;
      //$y1=($img_height+46)-floor($inf[1]/10); 
      //echo $y1."</br>";
      //$y2=($img_height-46); 
      $x1=($margins+10)+($gap+5)+$i*($gap+$bar_width);
      //$y1=($img_height-46)-ceil($inf[1]/600);
      $y1=($img_height-48)-ceil($inf[1]*$horizontal_gap/$ratio);
      echo $y1;
      $x2=$x1+$bar_width;
      $y2=($img_height-46);
      //echo $y2;
    imagestring($img,2,$x1+1,$y1-15,$inf[1],$values_color); 
    imagestring($img,2,$x2-46,$img_height-43,$inf[0],$values_color);  
    imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); // Draw bar
   $i++;   
  }
imagestring($img,8, ($img_width-$margins)/2, 0, $graphtitle, $txt_color);
imagestring($img,5, ($img_width-$margins)/2, $img_height-($ymargin+10), $xname, $txt_color);
imagestringup($img,5,10,($img_height-$ymargin)/2, $yname, $txt_color);
//header('Content-type: image/png');
imagepng($img, 'barchart1.jpg');
echo "<div style='width:$img_width'><img src='barchart1.jpg'></div>";
}

?>

这是附加的输出: enter image description here

0 个答案:

没有答案