我使用json和mysql查询从Highcharts创建一个双轴,线和列图表。通过firebug调试,我确保数据通过json生成,但图形不显示在浏览器中。 我需要在样条图表上显示“产品成本”,并在柱形图上显示来自数据库的“产品规格率”。 请求参考[link] http://jsfiddle.net/mjena/vjdLu/4/ 我也在尝试在stackoverflow上找到但没有成功工作的所有建议。
我的图表product.php的代码
$(function () {
$('#container').highcharts({
chart: {
zoomType: 'xy'
},
title: {
text: 'Product Specification Rating'
},
subtitle: {
text: 'product raja'
},
xAxis: [{
categories: []
}],
yAxis: [{ // Primary yAxis
labels: {
// format: '{value} Rs.',
style: {
color: Highcharts.getOptions().colors[1]
}
},
title: {
text: 'Product Cost',
style: {
color: Highcharts.getOptions().colors[1]
}
}
}, { // Secondary yAxis
title: {
text: 'Product Specification rating',
style: {
color: Highcharts.getOptions().colors[0]
}
},
labels: {
//format: '{value} out of 100',
style: {
color: Highcharts.getOptions().colors[0]
}
},
opposite: true
}],
tooltip: {
shared: true
},
legend: {
layout: 'vertical',
align: 'left',
x: 120,
verticalAlign: 'top',
y: 100,
floating: true,
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
},
series: [{
name: 'Product Rate',
type: 'column',
yAxis: 1,
data: [],
tooltip: {
valueSuffix: ' out of 100'
}
}, {
name: 'Product Cost',
type: 'spline',
data: [],
tooltip: {
valueSuffix: 'Rs.'
}
}]
});
$.getJSON("data.php", function(json) {
var theChart = $('#container').highcharts();
theChart.xAxis[0].setCategories(json[0]['data']);
theChart.series[0].setData(json[1]);
theChart.series[1].setData(json[2]);
// chart = new Highcharts.Chart(options);
});
});
data.php
$sql1 = mysql_query("SELECT products.product_name,product_rate.pro_rating
FROM products LEFT OUTER JOIN product_rate
ON products.product_id= product_rate.product_id ");
$category = array();
$category['name'] = 'Product Name';
while($r1 = mysql_fetch_array($sql1)) {
$category['data'][] = $r1['product_name'];
}
$sql2 = mysql_query("SELECT products.product_name,product_rate.pro_rating,prod_cost.product_cost
FROM products LEFT OUTER JOIN product_rate
ON products.product_id= product_rate.product_id
LEFT OUTER JOIN prod_cost ON product_rate.product_id=prod_cost.product_id ");
$series1 = array();
$series1['name'] = 'Product Rate';
$series2 = array();
$series2['name'] = 'Product Cost';
while($r2 = mysql_fetch_array($sql2)) {
$series1['data'][] = $r2['pro_rating'];
$series2['data'][] = $r2['product_cost'];
}
$result = array();
array_push($result,$category);
array_push($result,$series1);
array_push($result,$series2);
print json_encode($result, JSON_NUMERIC_CHECK);
请你帮帮我。
答案 0 :(得分:0)
1。)你JSON被错误引用,通过JSONLint运行它:
[{"name":"Product Name","data":["HP", "DELL", "APPLE", "HCL", "LENOVE", "SONY","TOSHIBA", "SAMSUNG", "ACER", "VIDEOCON', "IBM', "MAC"]},{"name":"Product Rate","data":[49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]},{"name":"Product Cost","data":[7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]}]
Parse error on line 14:
... "VIDEOCON', "IBM', "M
-----------------------^
Expecting '}', ':', ',', ']'
JSONLint不喜欢"VIDEOCON'
和"IBM'
2。)setData方法只获取数据而不是整个系列配置选项。你想要:
theChart.series[0].setData(json[1]["data"]);
3。)你应该把你的json调用放在图表的load
事件中。通过这种方式,您可以在绘制图表/ json调用时没有时间问题。