duanke6249 2013-06-17 19:52
浏览 61
已采纳

JPGraph + mysql数组没有读取值

I'm connecting to mySQL and running a simple query and returning the query into an array, then imploding the array and attempting to graph it using jpgraph, but I don't get any data points.

<?php
// content="text/plain; charset=utf-8"
        include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph.php");  
        include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph_line.php");
        $monday1 = strtotime("this monday");
        $monday2 = date("Y-m-d",$monday1);
//connect to Database and return graph values
        $con=mysql_connect("localhost:3306","root","","mysql");
        $db_selected = mysql_select_db('mysql', $con);
        if (!$db_selected)
        {
            die ('Can\'t use mysql : ' . mysql_error());
        }
        $mondayquery = "SELECT invoicetotal
                FROM thermdata
                WHERE CAST( datestamp AS DATE ) =  '$monday2'
                ORDER BY datehour";
        $mondayresult = mysql_query($mondayquery,$con);
            while($mondayinfo = mysql_fetch_array( $mondayresult )) 
                { 
                    $monimp[] = $mondayinfo['invoicetotal'];
                }

// Convert the array 
        $mondayfinal = implode(",", $monimp);

//set variables for graph values as array
        $datay1 = array($mondayfinal);

// Setup the graph
        $graph = new Graph(1200,600);
        $graph->SetScale("intint");
        $theme_class=new UniversalTheme;
        $graph->SetTheme($theme_class);
        $graph->img->SetAntiAliasing(false);
        $graph->SetMargin(10,10,-500,10);
        $graph->SetBox(false);
        $graph->legend->SetPos(.01,0,'left','top');
        $graph->legend->SetColumns(7);
        $graph->legend->SetFont(FF_FONT2,FS_NORMAL, 72);
        $graph->legend->SetHColMargin(10);
        $graph->legend->SetLineWeight(5);

        $graph->img->SetAntiAliasing();

        $graph->yaxis->HideZeroLabel();
        $graph->yaxis->HideLine(true);
        $graph->yaxis->HideTicks(true,true);
        $graph->yaxis->SetColor('white','white');

        $graph->xgrid->Show();
        $graph->xgrid->SetLineStyle("solid");
        $graph->xaxis->SetTickLabels(array('00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24'));
        $graph->xgrid->SetColor('#E3E3E3');

// Create the first line
        $p1 = new LinePlot($datay1);
        $graph->Add($p1);
        $p1->SetColor("#ff0000");
        $p1->SetLegend('Sunday');

// Output line
        $graph->Stroke();
                mysql_close($con);
?>

Basically the database contains a new invoicetotal every hour, and I want to graph them over 24 hours per day of the current week.

The variable $mondayfinal resolves as "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,278,627,1235,1919,2015", but I don't see what I'm doing wrong that's preventing jpgraph from plotting them.

Any help would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dongshao8566 2013-08-26 17:49
    关注

    You need to make sure your $datay1 array has individual points at each element, ie:

    Array ( [0] => a [1] => b [2] => c ) 
    

    Currently, the code would create a single element array with all values at element 0:

    Array ( [0] => a,b,c )
    

    This won't work in jpgraph.

    Instead, Try explode() on $mondayfinal in csv form to create the proper array or just use $monimp instead as it appears to be in the proper format.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3