使用php mysql创建jqplot图

时间:2016-04-03 11:06:26

标签: php jquery mysql ajax jqplot

我想使用mysql中的数据显示日期图表。我使用库jqplot来生成图形。图表应在X轴上有一个日期,在Y轴上应有一些数值。

的MySQL

在MySQL中我有游泳池:ID,温度,日期

CREATE TABLE IF NOT EXISTS `temperature_monitor` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `temperature` float NOT NULL
  PRIMARY KEY (`id`),
  UNIQUE KEY `counter_UNIQUE` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT= 4 ;

我使用php放入数组的mysql日期

$date = array();
while ($row = mysqli_fetch_array($result)) {
    $date = array(
        "date"  => $row['date'],
        "temperature"   => $row['temperature']
    );
    $date_array[] = $date;

}

echo ('['.json_encode($date_array).']');

这是一个显示图表的脚本

$(document).ready(function () {
$.ajax({
    type: "GET", 
    url: "temperature.php", 
    contentType: "application/json; charset=utf-8", 
    dataType: 'json', 

    success: function (json) {
        console.log(json);

        var plot2 = $.jqplot('chart3', [json], {
            title:'Customized Date Axis',
            axes:{
                xaxis:{
                    renderer:$.jqplot.DateAxisRenderer,
                    tickOptions:{formatString:'%Y-%m-%d'},
                    min:'2016-03-01',
                    tickInterval:'1 day'
                }
            },
            series:[{lineWidth:4, markerOptions:{style:'square'}}]
        });

    },
    error:function (json){
    }
});});

显示图表的数据格式应为:[['2008-08-12 4:00 PM',4],[.......,...],...... ..]

文档示例:

`var line1=[['2008-08-12 4:00PM',4], ['2008-09-12 4:00PM',6.5], ['2008-10-12 4:00PM',5.7], ['2008-11-12 4:00PM',9], ['2008-12-12 4:00P`M',8.2]];

当我从mysql中获取数据时,生成一个没有任何值的空白图表。

我不知道出了什么问题。

  

解决:

代码php应该改变

    $date = array();
while ($row = mysqli_fetch_array($result)) {
    $date = array($row['date'],$row['temperature']);
    $date_array[] = $date;

}

echo (json_encode($date_array));

0 个答案:

没有答案