Highchart Line Chart MySQL数据

时间:2013-03-12 12:54:32

标签: php mysql highcharts

<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"     type="text/javascript"></script>    
<script src='http://code.highcharts.com/highcharts.js' type='text/javascript'> </script>       
<script src='http://code.highcharts.com/modules/exporting.js' type='text/javascript'> </script>    

</head>

<body>

<?php
$con = mysql_connect('localhost', 'root', '123456') or die('Error connecting to server');
mysql_select_db("aplikace", $con); 

$SQL1 =     "SELECT * FROM data";

$result1 = mysql_query($SQL1);
$data1 = array();
while ($row = mysql_fetch_array($result1)) {
   $data1[] = $row['cas'];
}

$result2 = mysql_query($SQL1);
$data2 = array();
while ($row = mysql_fetch_array($result2)) {
   $data2[] = hexdec($row['pars_data']);
}


?>

<script type="text/javascript">
$(document).ready(function() {
    var chart = new Highcharts.Chart({
          chart: {
             renderTo: 'container',
             type: 'line'
          },

        title:  {
                    text: 'Comming Data'
                },

        xAxis:  {
                    categories: ['<?php echo join($data1, "','") ?>'],
                },

        yAxis:  {
                    min:0,

                },

        legend: {
                    layout: 'vertical',
                    backgroundColor: '#FFFFFF',
                    align: 'left',
                    verticalAlign: 'top',
                    x: 50,
                    y: 35,
                    floating: true,
                    shadow: true
                },

        plotOptions: {
                        column: {
                                    pointPadding: 0.2,
                                    borderWidth: 0
                                }
                    },

        series: [   {
                        name: 'Data',
                        data: ['<?php echo join($data2, "','") ?>'],
                       // pointStart: 0
                        //pointInterval
                    },


                ]
    });
});
</script>

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

大家好,我不知道我哪里有错,你能帮帮我吗?在X轴上应该是来自列CAS->的日期时间。它工作但它不显示列PARS_DATA。谢谢你的帮助,谢谢。

MySQL TABLE屏幕:

我看到的图表:

1 个答案:

答案 0 :(得分:0)

您应该将日期时间字段转换为其上的进程的unix时间戳。 所以strtotime功能是个好主意。

在您的代码中,请按照以下步骤操作:

 $result=mysql_query($sql)or die(mysql_error());
 if(mysql_num_rows($result)>0){
    while($row=mysql_fetch_array($result))
    {
        $uts=strtotime($row['time']); //convert to Unix Timestamp
        $date=date("l, F j, Y H:i:s",$uts); //standard template for draw chart

        echo $date . "\t" . $row['new_cost']. "\n";  //only this template work 
    }
  

$ sql:你的SQL查询文本。   $结果:反馈。   $ row ['new_cost']:我的费用字段。

了解更多详情,请点击this链接

祝你好运