通过json_encode用php填充highcharts
Populating highcharts with php via json_encode
我已经通过php与highcharts和json_encode搏斗了几天了。我相信我的数据格式正确,但图表没有正确更新。类别数据被更新,用于绘制列和行数据图表的系列数据被格式化。
谁能帮我走完剩下的路?以下是我正在使用的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($result)输出如下
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]}]
正如我所看到的问题只是通过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]
}]
});
相关文章:
- PHP下拉列表由要使用javascript的数据库填充
- PHP:Can't填充TinyMCE文本区域
- 用PHP/MMySQL和JavaScript填充几个HTML分区
- 填充文本框取决于php中的下拉选择
- 使用 PHP 填充 Javascript 饼图
- 如何在javascript中添加一个具有checkbox属性的变量,该变量将填充view.php中的一个表
- Ajax-为php填充多个文本框
- 使用 AJAX 和 ORACLE 以及不使用 php 填充下拉列表(选择 )
- 使用 PHP 填充引导输入 - 字符串变量中出现空格错误
- 使用 Javascript 添加一个选择框;到由 PHP 填充的 html 表单
- PHP 填充下拉列表与 ajax onchange 问题 IE 仅限
- PHP填充的按钮点击问题
- 用PHP填充JS代码段
- 使用php填充javascript数组
- 通过PHP填充HTML表
- 从PHP填充Selectize下拉列表
- 通过json_encode用php填充highcharts
- 在表单中动态添加带有PHP填充的选项的选择框
- 用下拉菜单和PHP填充jQuery数据表
- 使用jquery,php填充文本区域