Highcharts异步服务器加载多个系列 [英] Highcharts async Server Loading with multiple series

查看:97
本文介绍了Highcharts异步服务器加载多个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用下面例子中的Highcharts的延迟加载方式


http://www.highcharts.com/stock/demo/lazy-loading
blockquote>

及其php代码


https://github.com/highslide-software/highcharts.com/blob/master/ samples / data / from-sql.php


但我唯一得到的是一张空白的空白图表,其中有2个2011年的天数(示例数据)。
我的php代码:

 <?php 


//获取参数

$ callback = $ _GET ['callback'];
if(!preg_match('/ ^ [a-zA-Z0-9 _] + $ /',$ callback)){
die('Invalid callback name');
}

$ start = $ _GET ['start'];
if($ start&&!preg_match('/ ^ [0-9] + $ /',$ start)){
die(Invalid start parameter:$ start);
}

$ end = $ _GET ['end'];
if($ end&&!preg_match('/ ^ [0-9] + $ /',$ end)){
die(无效的结束参数:$ end);
}
if(!$ end)$ end = mktime()* 1000;


$ b //连接到MySQL
$ link = mysqli_connect('localhost:3306','root','elektra','telegestione');
//设置UTC + 1次
// mysql_query(SET time_zone ='+01:00');

//设置一些实用变量
$ range = $ end - $ start;
// $ startTime = gmstrftime('%Y-%m-%d%H:%M:%S',$ start / 1000);
$ startTime = date('Ymd H:i:s',strtotime(gmstrftime('%Y-%m-%d%H:%M:%S',$ start / 1000)。' - 1天'));
// $ endTime = gmstrftime('%Y-%m-%d%H:%M:%S',$ end / 1000);
$ endTime = date('Ymd H:i:s',strtotime(gmstrftime('%Y-%m-%d%H:%M:%S',$ end / 1000)。'+ 0天'));

//找到正确的表
//两天范围内加载分钟数据
if($ range <2 * 24 * 3600 * 1000){
if (!$ link){
die('Could not connect:'。mysqli_error());};
回显分钟数据2;
$ result = mysqli_query($ link,select 1000 * unix_timestamp(q1.time)as time,q1.kwhg,q1.kwhc,q1.kwhi,q1.kwhfm,q2.test from(
选择
(t1.dt)AS时间,(当t1.value> t2.value AND t2.value> 0
时,
则abs(t1.value - t2.value)
when t1.value> t2.value AND t2.value = 0
then t2.value
else t1.value END)kwhg,
(case
when t1.value1> ; t2.value1 AND t2.value1> 0
then abs(t1.value1 - t2.value1)
when t1.value1> t2.value1 AND t2.value1 = 0
then t2。 value1
当t1.value1 = t2.value1 AND t1.value1> 0
然后abs(t1.value1 - t2.value1)
else 0 END)kwhc,
(case
当t1.value2> t2.value2 AND t2.value2> 0
则abs(t1.value2 - t2.value2)
当t1.value2> t2.value2 AND t2.value2 = 0
then t2.value2
当t1.value2 = t2.value2 AND t1.value2> 0
然后abs(t1.value2 - t2.value2)
else 0 END)kwhi,
(case t $ value
t1.value3> t2.value3 AND t2.value3> 0
然后abs(t1.value3 - t2.value3)
当t1.value3> t2.value3 AND t2.value3 = 0
then t2.value3
当t1.value3 = t2 .value3 AND t1.value3> 0
then abs(t1.value3 - t2.value3)
else 0 END)kwhfm
FROM
(SELECT(dataora)dt,MAX kwhg)value,MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'and'$ endTime'GROUP BY date(dt),hour(dt))t1
JOIN
(SELECT(dataora)dt,MAX(kwhg)value,MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'and'$ endTime '选择dataora,AVG(测试)',然后点击'GROUP BY date(dt),hour(dt))t2
on t1.dt = t2.dt + interval 15 minutes)作为dataora b的温度测试etween'$ startTime'和'$ endTime'GROUP BY date(dataora),hour(dataora))as q2
ON q1.time = q2.dataora);

//一周范围加载小时数据
elseif($范围<7 * 24 * 3600 * 1000){
if(!$ link){
die('Could not connect:'。mysqli_error());};
回显hourly data1;
$ result = mysqli_query($ link,select 1000 * unix_timestamp(q1.time)as time,q1.kwhg,q1.kwhc,q1.kwhi,q1.kwhfm,q2.test from(
选择
(t1.dt)AS时间,(当t1.value> t2.value AND t2.value> 0
时,
则abs(t1.value - t2.value)
when t1.value> t2.value AND t2.value = 0
then t2.value
else t1.value END)kwhg,
(case
when t1.value1> ; t2.value1 AND t2.value1> 0
then abs(t1.value1 - t2.value1)
when t1.value1> t2.value1 AND t2.value1 = 0
then t2。 value1
当t1.value1 = t2.value1 AND t1.value1> 0
然后abs(t1.value1 - t2.value1)
else 0 END)kwhc,
(case
当t1.value2> t2.value2 AND t2.value2> 0
则abs(t1.value2 - t2.value2)
当t1.value2> t2.value2 AND t2.value2 = 0
then t2.value2
当t1.value2 = t2.value2 AND t1.value2> 0
然后abs(t1.value2 - t2.value2)
else 0 END)kwhi,
(case
when t1.value3> t2.value3 AND t2.value3> 0
then abs(t1.value3 - t2.value3)
当t1.value3> t2.value3 AND t2.value3 = 0
then t2.value3
当t1.value3 = t2.value3 AND t1 .value3> 0
then abs(t1.value3 - t2.value3)
else 0 END)kwhfm
FROM
(SELECT(dataora)dt,MAX(kwhg) MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'and'$ endTime'GROUP BY date(dt),hour(dt))t1
JOIN
(SELECT(dataora)dt,MAX(kwhg)value,MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'和'$ endTime'GROUP BY date (dt),小时(dt))t2
on t1.dt = t2.dt + interval 1小时)as q1
JOIN
(选择dataora,AVG(Test)作为温度测试dataora之间'$ startTime'和'$ endTime'GROUP BY date(dataora),hour(dataora))as q2
ON q1.time = q2.dataora);

//一个月范围加载小时数据
} elseif($范围<31 * 24 * 3600 * 1000){
if(!$ link){
die('Could not connect:'。mysqli_error());};
回显小时数据2;
$ result = mysqli_query($ link,select 1000 * unix_timestamp(q1.time)as time,q1.kwhg,q1.kwhc,q1.kwhi,q1.kwhfm,q2.test from(
选择
(t1.dt)AS时间,(当t1.value> t2.value AND t2.value> 0
时,
则abs(t1.value - t2.value)
when t1.value> t2.value AND t2.value = 0
then t2.value
else t1.value END)kwhg,
(case
when t1.value1> ; t2.value1 AND t2.value1> 0
then abs(t1.value1 - t2.value1)
when t1.value1> t2.value1 AND t2.value1 = 0
then t2。 value1
当t1.value1 = t2.value1 AND t1.value1> 0
然后abs(t1.value1 - t2.value1)
else 0 END)kwhc,
(case
当t1.value2> t2.value2 AND t2.value2> 0
则abs(t1.value2 - t2.value2)
当t1.value2> t2.value2 AND t2.value2 = 0
then t2.value2
当t1.value2 = t2.value2 AND t1.value2> 0
然后abs(t1.value2 - t2.value2)
else 0 END)kwhi,
(case
when t1.value3> t2.value3 AND t2.value3> 0
then abs(t1.value3 - t2.value3)
当t1.value3> t2.value3 AND t2.value3 = 0
then t2.value3
当t1.value3 = t2.value3 AND t1 .value3> 0
then abs(t1.value3 - t2.value3)
else 0 END)kwhfm
FROM
(SELECT(dataora)dt,MAX(kwhg) MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'and'$ endTime'GROUP BY date(dt),hour(dt))t1
JOIN
(SELECT(dataora)dt,MAX(kwhg)value,MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'和'$ endTime'GROUP BY date (dt),小时(dt))t2
on t1.dt = t2.dt + interval 1小时)as q1
JOIN
(选择dataora,AVG(Test)作为温度测试dataora之间'$ startTime'和'$ endTime'GROUP BY date(dataora),hour(dataora))as q2
ON q1.time = q2.dataora);

//一年范围加载每日数据
} elseif($范围<15 * 31 * 24 * 3600 * 1000){
if(!$ link){
die('Could not connect:'。mysqli_error());};
回显每日数据2;
$ result = mysqli_query($ link,select 1000 * unix_timestamp(q1.time)as time,q1.kwhg,q1.kwhc,q1.kwhi,q1.kwhfm,q2.test from(
选择
(t1.dt)AS时间,(当t1.value> t2.value AND t2.value> 0
时,
则abs(t1.value - t2.value)
when t1.value> t2.value AND t2.value = 0
then t2.value
else t1.value END)kwhg,
(case
when t1.value1> ; t2.value1 AND t2.value1> 0
then abs(t1.value1 - t2.value1)
when t1.value1> t2.value1 AND t2.value1 = 0
then t2。 value1
当t1.value1 = t2.value1 AND t1.value1> 0
然后abs(t1.value1 - t2.value1)
else 0 END)kwhc,
(case
当t1.value2> t2.value2 AND t2.value2> 0
则abs(t1.value2 - t2.value2)
当t1.value2> t2.value2 AND t2.value2 = 0
then t2.value2
当t1.value2 = t2.value2 AND t1.value2> 0
然后abs(t1.value2 - t2.value2)
else 0 END)kwhi,
(case
when t1.value3> t2.value3 AND t2.value3> 0
then abs(t1.value3 - t2.value3)
当t1.value3> t2.value3 AND t2.value3 = 0
then t2.value3
当t1.value3 = t2.value3 AND t1 .value3> 0
then abs(t1.value3 - t2.value3)
else 0 END)kwhfm
FROM
(SELECT(dataora)dt,MAX(kwhg) MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'and'$ endTime'GROUP BY date(dt))t1
JOIN
(SELECT (dataora)dt,MAX(kwhg)value,MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'和'$ endTime'GROUP BY date(dt))t2
on t1.dt = t2.dt + interval 1 day)as q1
JOIN
(选择dataora,AVG(Test)作为测试温度,其中dataora位于'$ startTime'和$ ENDTIME GROUP BY日期(dataora))作为Q2
关于q1.time = q2.dataora);

//更大范围加载每月数据
} else {
if(!$ link){
die('Could not connect:'。mysqli_error()) ;};
回显每月数据2;
$ result = mysqli_query($ link,select 1000 * unix_timestamp(q1.time)as time,q1.kwhg,q1.kwhc,q1.kwhi,q1.kwhfm,q2.test from(
选择
(t1.dt)AS时间,(当t1.value> t2.value AND t2.value> 0
时,
则abs(t1.value - t2.value)
when t1.value> t2.value AND t2.value = 0
then t2.value
else t1.value END)kwhg,
(case
when t1.value1> ; t2.value1 AND t2.value1> 0
then abs(t1.value1 - t2.value1)
when t1.value1> t2.value1 AND t2.value1 = 0
then t2。 value1
当t1.value1 = t2.value1 AND t1.value1> 0
然后abs(t1.value1 - t2.value1)
else 0 END)kwhc,
(case
当t1.value2> t2.value2 AND t2.value2> 0
则abs(t1.value2 - t2.value2)
当t1.value2> t2.value2 AND t2.value2 = 0
then t2.value2
当t1.value2 = t2.value2 AND t1.value2> 0
然后abs(t1.value2 - t2.value2)
else 0 END)kwhi,
(case
when t1.value3> t2.value3 AND t2.value3> 0
then abs(t1.value3 - t2.value3)
当t1.value3> t2.value3 AND t2.value3 = 0
then t2.value3
当t1.value3 = t2.value3 AND t1 .value3> 0
then abs(t1.value3 - t2.value3)
else 0 END)kwhfm
FROM
(SELECT(dataora)dt,MAX(kwhg) MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'and'$ endTime'GROUP BY date(dt),hour(dt))t1
JOIN
(SELECT(dataora)dt,MAX(kwhg)value,MAX(kwhc)value1,MAX(kwhi)value2,MAX(kwhfm)value3 FROM misure where'$ startTime'和'$ endTime'GROUP BY date (dt),小时(dt))t2
on t1.dt = t2.dt +间隔1个月)为q1
JOIN
(选择dataora,AVG数据之间' $ startTime'和'$ endTime'GROUP BY date(dataora),hour(dataora))as q2
ON q1.time = q2.dataora);
};

$ rows = array();
while($ row = mysqli_fetch_assoc($ result)){
// echo $ a =hello;
提取($ row);

$ rows [] =[$ time,$ test,$ kwhg,$ kwhc,$ kwhi,$ kwhfm];
}

//打印
header('Content-Type:text / javascript');
// echo $ range;
// echo $ startTime;
// echo $ end;
echo/ * console.log('range = $ range,start = $ start,end = $ end,startTime = $ startTime,endTime = $ endTime'); * /; $($,$,$,$,$,$,$,$,$,$,$,$)。

?>

输出数据对于高位图应该是正确的,如演示所示,但我不能初始化图表或显示任何系列...

  $(function(){

//请参阅https://github.com/highslide-software/highcharts.com/blob/master/samples/data/from-sql.php上的JSONP处理程序源代码。
$ .getJSON('grafico_nuovo.php? callback =?',函数(data){

//为结束日期添加空值
data = [] .concat(data,[[Date.GMT(2013,9 ,14,19,59),null,null,null,null]]);

//创建图表
$('#container')。highcharts('StockChart', {
图:{
类型:'spline',
zoomType:'xy'
},

导航器:{
adaptToUpdatedData:假,
系列:{
数据:数据
}
},

滚动条:{
liveRedraw:false
},

title:{
text:'AAPL history从1998年到2011年的分钟


副标题:{
text:'通过异步服务器加载在$ {
}显示Highcharts股票中的170万个数据点 ,

rangeSelector:{
buttons:[{
type:'hour',
count:1,
text:'1h'
},{
type:'day',
count:1,
text:'1d'
},{
类型:'month',
count:1,
text:'1m'
},{
类型:'year',
count:1,
text:'1y'
},{
type:'all',
text:'All'
}],
inputEnabled:true,//它支持只有天
被选中:2 //天
},

xAxis:{
events:{
afterSetExtremes:afterSetExtremes
},
minRange:3600 * 1000 //一小时
},

系列:[{
data:data,
dataGrouping:{
enabled :false
}
}]
});
});
});

$ b $ / **
*根据所选的最小值和最大值加载新数据
* /
函数afterSetExtremes(e){

var currentExtremes = this.getExtremes(),
range = e.max - e.min,
chart = $('#container')。highcharts();

chart.showLoading('从服务器加载数据...');
$ .getJSON('grafico_nuovo.php?start ='+ Math.round(e.min)+
'& end ='+ Math.round(e.max)+'& callback ='',函数(数据){

chart.series [0] .setData(data [0]);
chart.hideLoading();
});


$ / code>

感谢您的任何帮助或建议
Giorgio

解决方案

回显 $ callback。([\\\
。join(,\\\
,$ rows)。\\\
]);;
返回?我建议通过json_encode()将你的数组转换成json,然后在javascript中使用。



编辑:

你无法访问你的图表,更多你可以使用setData只在现有的serie上,而不是像chart.series [2]那样不存在的系列。您应该致电 addSeries

  val1 = []; 
val2 = [];
val3 = [];
val4 = [];
val5 = [];
$ .each(data,function(key,value){
val1.push([value [0],value [1]]);
val2.push([value [0 ],value [2]]);
val3.push([value [0],value [3]]);
val4.push([value [0],value [4]]) ;
val5.push([value [0],value [5]]);
});
chart.series [0] .setData(val1);
chart.addSeries({
data:val2
});

chart.addSeries({
data:val3
});

chart.addSeries({
data:val4
});

chart.addSeries({
data:val5
});



chart.hideLoading();

http://jsfiddle.net/4knAX/2/


i'm trying to use a the lazy loading of Highcharts following its example

http://www.highcharts.com/stock/demo/lazy-loading

and their php code

https://github.com/highslide-software/highcharts.com/blob/master/samples/data/from-sql.php

but the only thing that i get is a blank empty chart with 2 days of 2011(the example data). My php code :

<?php


// get the parameters

$callback = $_GET['callback'];
if (!preg_match('/^[a-zA-Z0-9_]+$/', $callback)) {
        die('Invalid callback name');
}

$start = $_GET['start'];
if ($start && !preg_match('/^[0-9]+$/', $start)) {
        die("Invalid start parameter: $start");
}

$end = $_GET['end'];
if ($end && !preg_match('/^[0-9]+$/', $end)) {
        die("Invalid end parameter: $end");
}
if (!$end) $end = mktime() * 1000;



// connect to MySQL
$link = mysqli_connect('localhost:3306', 'root', 'elektra','telegestione');
// set UTC+1 time
//mysql_query("SET time_zone = '+01:00'");

// set some utility variables
$range = $end - $start;
//$startTime = gmstrftime('%Y-%m-%d %H:%M:%S', $start / 1000);
$startTime= date('Y-m-d H:i:s', strtotime(gmstrftime('%Y-%m-%d %H:%M:%S', $start / 1000) . ' - 1 day'));
//$endTime = gmstrftime('%Y-%m-%d %H:%M:%S', $end / 1000);
$endTime= date('Y-m-d H:i:s', strtotime(gmstrftime('%Y-%m-%d %H:%M:%S', $end / 1000) . ' + 0 day'));

// find the right table
// two days range loads minute data
if ($range < 2 * 24 * 3600 * 1000) {
        if (!$link)     {
                        die('Could not connect: ' . mysqli_error());};  
                        echo "minute data2" ;
            $result = mysqli_query($link,"select 1000*unix_timestamp(q1.time) as time, q1.kwhg, q1.kwhc, q1.kwhi, q1.kwhfm, q2.test from (
SELECT
      (t1.dt) AS time,(case
     when t1.value>t2.value AND t2.value>0
          then abs(t1.value - t2.value)
     when t1.value>t2.value AND t2.value=0
          then t2.value
          else t1.value END)kwhg, 
     (case
     when t1.value1>t2.value1 AND t2.value1>0
          then abs(t1.value1 - t2.value1)
     when t1.value1>t2.value1 AND t2.value1=0
          then t2.value1
     when t1.value1=t2.value1 AND t1.value1>0 
        then abs(t1.value1 - t2.value1)
      else 0 END) kwhc, 
      (case
     when t1.value2>t2.value2 AND t2.value2>0
          then abs(t1.value2 - t2.value2)
     when t1.value2>t2.value2 AND t2.value2=0
          then t2.value2
     when t1.value2=t2.value2 AND t1.value2>0 
        then abs(t1.value2 - t2.value2)
      else 0 END)kwhi,
    (case
     when t1.value3>t2.value3 AND t2.value3>0
          then abs(t1.value3 - t2.value3)
     when t1.value3>t2.value3 AND t2.value3=0
          then t2.value3
     when t1.value3=t2.value3 AND t1.value3>0 
        then abs(t1.value3 - t2.value3)
      else 0 END) kwhfm
    FROM 
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t1
        JOIN
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t2
        on t1.dt = t2.dt + interval 15 minute) as q1
JOIN
    (select dataora, AVG(Test) as test from temperature where dataora between '$startTime' and '$endTime' GROUP BY date(dataora),hour(dataora))  as q2
    ON q1.time=q2.dataora");

 //one week range loads hourly data
} elseif ($range < 7 * 24 * 3600 * 1000) {
        if (!$link)     {
            die('Could not connect: ' . mysqli_error());};  
            echo "hourly data1" ;
    $result = mysqli_query($link,"select 1000*unix_timestamp(q1.time) as time, q1.kwhg, q1.kwhc, q1.kwhi, q1.kwhfm, q2.test from (
SELECT
      (t1.dt) AS time,(case
     when t1.value>t2.value AND t2.value>0
          then abs(t1.value - t2.value)
     when t1.value>t2.value AND t2.value=0
          then t2.value
          else t1.value END)kwhg, 
     (case
     when t1.value1>t2.value1 AND t2.value1>0
          then abs(t1.value1 - t2.value1)
     when t1.value1>t2.value1 AND t2.value1=0
          then t2.value1
     when t1.value1=t2.value1 AND t1.value1>0 
        then abs(t1.value1 - t2.value1)
      else 0 END) kwhc, 
      (case
     when t1.value2>t2.value2 AND t2.value2>0
          then abs(t1.value2 - t2.value2)
     when t1.value2>t2.value2 AND t2.value2=0
          then t2.value2
     when t1.value2=t2.value2 AND t1.value2>0 
        then abs(t1.value2 - t2.value2)
      else 0 END)kwhi,
    (case
     when t1.value3>t2.value3 AND t2.value3>0
          then abs(t1.value3 - t2.value3)
     when t1.value3>t2.value3 AND t2.value3=0
          then t2.value3
     when t1.value3=t2.value3 AND t1.value3>0 
        then abs(t1.value3 - t2.value3)
      else 0 END) kwhfm
    FROM 
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t1
        JOIN
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t2
        on t1.dt = t2.dt + interval 1 hour) as q1
JOIN
    (select dataora, AVG(Test) as test from temperature where dataora between '$startTime' and '$endTime' GROUP BY date(dataora),hour(dataora))  as q2
    ON q1.time=q2.dataora");

// one month range loads hourly data
} elseif ($range < 31 * 24 * 3600 * 1000) {
        if (!$link)     {
            die('Could not connect: ' . mysqli_error());};  
            echo "hourly data2" ;
    $result = mysqli_query($link,"select 1000*unix_timestamp(q1.time) as time, q1.kwhg, q1.kwhc, q1.kwhi, q1.kwhfm, q2.test from (
SELECT
      (t1.dt) AS time,(case
     when t1.value>t2.value AND t2.value>0
          then abs(t1.value - t2.value)
     when t1.value>t2.value AND t2.value=0
          then t2.value
          else t1.value END)kwhg, 
     (case
     when t1.value1>t2.value1 AND t2.value1>0
          then abs(t1.value1 - t2.value1)
     when t1.value1>t2.value1 AND t2.value1=0
          then t2.value1
     when t1.value1=t2.value1 AND t1.value1>0 
        then abs(t1.value1 - t2.value1)
      else 0 END) kwhc, 
      (case
     when t1.value2>t2.value2 AND t2.value2>0
          then abs(t1.value2 - t2.value2)
     when t1.value2>t2.value2 AND t2.value2=0
          then t2.value2
     when t1.value2=t2.value2 AND t1.value2>0 
        then abs(t1.value2 - t2.value2)
      else 0 END)kwhi,
    (case
     when t1.value3>t2.value3 AND t2.value3>0
          then abs(t1.value3 - t2.value3)
     when t1.value3>t2.value3 AND t2.value3=0
          then t2.value3
     when t1.value3=t2.value3 AND t1.value3>0 
        then abs(t1.value3 - t2.value3)
      else 0 END) kwhfm
    FROM 
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t1
        JOIN
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t2
        on t1.dt = t2.dt + interval 1 hour) as q1
JOIN
    (select dataora, AVG(Test) as test from temperature where dataora between '$startTime' and '$endTime' GROUP BY date(dataora),hour(dataora))  as q2
    ON q1.time=q2.dataora");

// one year range loads daily data
} elseif ($range < 15 * 31 * 24 * 3600 * 1000) {
        if (!$link)     {
            die('Could not connect: ' . mysqli_error());};  
            echo "daily data2" ;
    $result = mysqli_query($link,"select 1000*unix_timestamp(q1.time) as time, q1.kwhg, q1.kwhc, q1.kwhi, q1.kwhfm, q2.test from (
SELECT
      (t1.dt) AS time,(case
     when t1.value>t2.value AND t2.value>0
          then abs(t1.value - t2.value)
     when t1.value>t2.value AND t2.value=0
          then t2.value
          else t1.value END)kwhg, 
     (case
     when t1.value1>t2.value1 AND t2.value1>0
          then abs(t1.value1 - t2.value1)
     when t1.value1>t2.value1 AND t2.value1=0
          then t2.value1
     when t1.value1=t2.value1 AND t1.value1>0 
        then abs(t1.value1 - t2.value1)
      else 0 END) kwhc, 
      (case
     when t1.value2>t2.value2 AND t2.value2>0
          then abs(t1.value2 - t2.value2)
     when t1.value2>t2.value2 AND t2.value2=0
          then t2.value2
     when t1.value2=t2.value2 AND t1.value2>0 
        then abs(t1.value2 - t2.value2)
      else 0 END)kwhi,
    (case
     when t1.value3>t2.value3 AND t2.value3>0
          then abs(t1.value3 - t2.value3)
     when t1.value3>t2.value3 AND t2.value3=0
          then t2.value3
     when t1.value3=t2.value3 AND t1.value3>0 
        then abs(t1.value3 - t2.value3)
      else 0 END) kwhfm
    FROM 
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt)) t1
        JOIN
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt)) t2
        on t1.dt = t2.dt + interval 1 day) as q1
JOIN
    (select dataora, AVG(Test) as test from temperature where dataora between '$startTime' and '$endTime' GROUP BY date(dataora))  as q2
    ON q1.time=q2.dataora");

// greater range loads monthly data
} else {
        if (!$link)     {
            die('Could not connect: ' . mysqli_error());};
                echo "monthly data2" ;
    $result = mysqli_query($link,"select 1000*unix_timestamp(q1.time) as time, q1.kwhg, q1.kwhc, q1.kwhi, q1.kwhfm, q2.test from (
SELECT
      (t1.dt) AS time,(case
     when t1.value>t2.value AND t2.value>0
          then abs(t1.value - t2.value)
     when t1.value>t2.value AND t2.value=0
          then t2.value
          else t1.value END)kwhg, 
     (case
     when t1.value1>t2.value1 AND t2.value1>0
          then abs(t1.value1 - t2.value1)
     when t1.value1>t2.value1 AND t2.value1=0
          then t2.value1
     when t1.value1=t2.value1 AND t1.value1>0 
        then abs(t1.value1 - t2.value1)
      else 0 END) kwhc, 
      (case
     when t1.value2>t2.value2 AND t2.value2>0
          then abs(t1.value2 - t2.value2)
     when t1.value2>t2.value2 AND t2.value2=0
          then t2.value2
     when t1.value2=t2.value2 AND t1.value2>0 
        then abs(t1.value2 - t2.value2)
      else 0 END)kwhi,
    (case
     when t1.value3>t2.value3 AND t2.value3>0
          then abs(t1.value3 - t2.value3)
     when t1.value3>t2.value3 AND t2.value3=0
          then t2.value3
     when t1.value3=t2.value3 AND t1.value3>0 
        then abs(t1.value3 - t2.value3)
      else 0 END) kwhfm
    FROM 
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t1
        JOIN
        (SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3  FROM misure where dataora between '$startTime' and '$endTime' GROUP BY date(dt),hour(dt)) t2
        on t1.dt = t2.dt + interval 1 month) as q1
JOIN
    (select dataora, AVG(Test) as test from temperature where dataora between '$startTime' and '$endTime' GROUP BY date(dataora),hour(dataora))  as q2
    ON q1.time=q2.dataora");
};

$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
        //echo $a= "hello";
        extract($row);

        $rows[] = "[$time,$test,$kwhg,$kwhc,$kwhi,$kwhfm]";
                                            }

// print it
header('Content-Type: text/javascript');
//echo $range;
//echo $startTime;
//echo $end;
echo "/* console.log(' range = $range, start = $start, end = $end, startTime = $startTime, endTime = $endTime '); */";
echo $callback ."([\n" . join(",\n", $rows) ."\n]);";

?>

The output data should be correct for the highcharts, as indicated by the demo, but i can't initialize the chart or display any series...

$(function() {

    // See source code from the JSONP handler at https://github.com/highslide-software/highcharts.com/blob/master/samples/data/from-sql.php
    $.getJSON('grafico_nuovo.php?callback=?', function(data) {

        // Add a null value for the end date 
        data = [].concat(data, [[Date.GMT(2013, 9, 14, 19, 59), null, null, null, null]]);

        // create the chart
        $('#container').highcharts('StockChart', {
            chart : {
                type: 'spline',
                zoomType: 'xy'
            },

            navigator : {
                adaptToUpdatedData: false,
                series : {
                    data : data
                }
            },

            scrollbar: {
                liveRedraw: false
            },

            title: {
                text: 'AAPL history by the minute from 1998 to 2011'
            },

            subtitle: {
                text: 'Displaying 1.7 million data points in Highcharts Stock by async server loading'
            },

            rangeSelector : {
                buttons: [{
                    type: 'hour',
                    count: 1,
                    text: '1h'
                }, {
                    type: 'day',
                    count: 1,
                    text: '1d'
                }, {
                    type: 'month',
                    count: 1,
                    text: '1m'
                }, {
                    type: 'year',
                    count: 1,
                    text: '1y'
                }, {
                    type: 'all',
                    text: 'All'
                }],
                inputEnabled: true, // it supports only days
                selected : 2 // day
            },

            xAxis : {
                events : {
                    afterSetExtremes : afterSetExtremes
                },
                minRange: 3600 * 1000 // one hour
            },

            series : [{
                data : data,
                dataGrouping: {
                    enabled: false
                }
            }]
        });
    });
});


/**
 * Load new data depending on the selected min and max
 */
function afterSetExtremes(e) {

    var currentExtremes = this.getExtremes(),
        range = e.max - e.min,
        chart = $('#container').highcharts();

    chart.showLoading('Loading data from server...');
    $.getJSON('grafico_nuovo.php?start='+ Math.round(e.min) +
            '&end='+ Math.round(e.max) +'&callback=?', function(data) {

        chart.series[0].setData(data[0]);
        chart.hideLoading();
    });

}

thanks for any help or suggestion Giorgio

解决方案

What echo $callback ."([\n" . join(",\n", $rows) ."\n]);"; returns? I advice to transform your array to json by json_encode() and then use in javascript.

EDIT:

You have no access to chart in you scirpt, morever You can use setData only on exsiting serie, not on series which not exist like chart.series[2]. You should to call addSeries

 val1 = [];
    val2 = [];
    val3 = [];
    val4 = [];
    val5 = [];
    $.each(data, function (key, value) {
        val1.push([value[0], value[1]]);
        val2.push([value[0], value[2]]);
        val3.push([value[0], value[3]]);
        val4.push([value[0], value[4]]);
        val5.push([value[0], value[5]]);
    });
chart.series[0].setData(val1);
    chart.addSeries({
        data: val2
    });

    chart.addSeries({
        data: val3
    });

    chart.addSeries({
        data: val4
    });

    chart.addSeries({
        data: val5
    });



    chart.hideLoading();

http://jsfiddle.net/4knAX/2/

这篇关于Highcharts异步服务器加载多个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆