2013-05-21 49 views
1

我一直在使用php通过highcharts和json_encode摔跤几天。我相信我的数据格式正确,但图表没有正确更新。类别数据被更新,并且图表列和行数据的系列数据被格式化。使用php通过json_encode填充highcharts

任何人都可以帮助我得到其余的方式。这里是我使用的php脚本和highcharts javascript。

谢谢您的帮助:

<?php   
// data feed to power dynamic charts 
include_once('db_conn.php'); 

// get site data and store in array 
$query = mysql_query("SELECT site, SUM(impressions) AS impressions, SUM(clicks) AS  clicks FROM dfa_data WHERE campaign='$campaign' and time='$time' GROUP BY site ORDER BY impressions DESC"); 
$rows = array(); 
while($r = mysql_fetch_array($query)) { 
    $rows['categories'][] = $r['site']; 
} 

// get impression data and store in array 
$query = mysql_query("SELECT site, SUM(impressions) AS impressions, SUM(clicks) AS clicks FROM dfa_data WHERE campaign='$campaign' and time='$time' GROUP BY site ORDER BY impressions DESC"); 
$rows1 = array(); 
$rows1['name'] = 'Impressions'; 
$rows1['color'] = '#4572A7'; 
$rows1['type'] = 'column'; 
$rows1['yAxis'] = 1; 
while($rr = mysql_fetch_array($query)) { 
    $rows1['data'][] = $rr['impressions']; 
} 

// get ctr data and store in array 
$query = mysql_query("SELECT site, SUM(impressions) AS impressions, SUM(clicks) AS clicks FROM dfa_data WHERE campaign='$campaign' and time='$time' GROUP BY site ORDER BY impressions DESC"); 
$rows2 = array(); 
$rows2['name'] = 'CTR'; 
$rows2['color'] = '#89A54E'; 
$rows2['type'] = 'spline'; 
while($rrr = mysql_fetch_array($query)) { 
$ctr = number_format(($rrr['clicks']/$rrr['impressions'])*(100),2,'.',','); 
$impressions = number_format($rrr['impressions'],0,'.',','); 
$clicks = number_format($rrr['clicks'],0,'.',','); 
    $rows2['data'][] = $ctr; 
} 

$result = array(); 
$result1 = array(); 
array_push($result,$rows); 
array_push($result1,$rows1); 
array_push($result1,$rows2); 


?> 

<script> 
$(function() { 
    $('#container').highcharts({ 
     chart: { 
      zoomType: 'xy' 
     }, 
     title: { 
      text: 'Performance by Site' 
     }, 
     subtitle: { 
      text: '' 
     }, 
     xAxis: 
     <?php echo json_encode($result); ?> 
     , 
     yAxis: [{ // Primary yAxis 
      labels: { 
       format: '{value}%', 
       style: { 
        color: '#89A54E' 
       } 
      }, 
      title: { 
       text: 'CTR', 
       style: { 
        color: '#89A54E' 
       } 
      } 
     }, { // Secondary yAxis 
      title: { 
       text: 'Impressions', 
       style: { 
        color: '#4572A7' 
       } 
      }, 
      labels: { 
       format: '{value}', 
       style: { 
        color: '#4572A7' 
       } 
      }, 
      opposite: true 
     }], 
     tooltip: { 
      shared: true 
     }, 
     series: <?php echo json_encode($result1); ?> 
    }); 
    }); 
</script> 

的var_dump($结果)输出以下

array(1) { [0]=> array(1) { ["categories"]=> array(5) { [0]=> string(13) "Search Medica" [1]=> string(10) "Medscape 4" [2]=> string(11) "onclive.com" [3]=> string(22) "Oncology Nurse Advisor" [4]=> string(25) "chemotherapyadvisor.com 1" } } } 

的var_dump($ RESULT1)输出以下

string(7) "result1" 

这里是json_encode输出以及两者:

json_encode $结果

[{"categories":["Search Medica","Medscape 4","onclive.com","Oncology Nurse Advisor","chemotherapyadvisor.com 1"]}] 

json_encode $ RESULT1

[{"name":"Impressions","color":"#4572A7","type":"column","yAxis":1,"data":[140521,71905,69295,68456,49487]},{"name":"CTR","color":"#89A54E","type":"spline","data":[0.11,0.04,0.2,0.09,0.05]}] 
+0

'$ result'和'$ result1'是什么样的?你可以从PHP做一个var_dump()并发布结果吗? – Divey

+0

在上面添加了一些额外的细节。再次感谢您的回复。 – user1763208

回答

0

正如我看到的问题是只能用AJAX或类似的东西中获取数据,因为当我粘贴JSON(它看起来像细)那么一切正常http://jsfiddle.net/6fZcz/ 所以我建议去看看控制台,如果你收到任何错误。更多的PHP文件与图表脚本在同一台服务器上?

$('#container').highcharts({ 
    chart: { 
     type: 'line', 
     marginRight: 10 
    }, 
    xAxis: [{ 
     "categories": ["Search Medica", "Medscape 4", "onclive.com", "Oncology Nurse Advisor", "chemotherapyadvisor.com 1"] 
    }], 
    series: [{ 
     "name": "Impressions", 
      "color": "#4572A7", 
      "type": "column", 
     //"yAxis": 1, 
     "data": [140521, 71905, 69295, 68456, 49487] 
    }, { 
     "name": "CTR", 
      "color": "#89A54E", 
      "type": "spline", 
      "data": [0.11, 0.04, 0.2, 0.09, 0.05] 
    }] 
}); 
+0

感谢您的回复。我发现你的代码在jsfiddle中工作,但是图表不带yAxis,你已经注释出来让图表呈现。另外我想说明的是,我通过Chrome开发工具看到控制台没有错误。任何想知道在控制台中是否显示错误的情况下要检查什么?最后是php脚本和js在同一个服务器上的同一个php文件中,所以也不应该是这个问题。从我可以告诉我当前的代码正在输出系列数据“”,我认为这可能与它有关。 – user1763208

+0

解决了这个问题。我正在使用打印json_encode($ result1);而不是打印json_encode($ result1,JSON_NUMERIC_CHECK);输出系列数据。它将数据作为字符串处理,结果是在数字数据周围添加“”。谢谢你的帮助 – user1763208