我有一个ajax电话
$(function () {
$.ajax({
url: '../../getdaily.php',
type:'POST',
dataType: '',
success: function(output_string){
console.log(output_string);
},
error: function (xhr, ajaxOptions, thrownError){
console.log(xhr.statusText);
console.log(thrownError);
}
});
});
并且console.log将输出...
[{"name":"Test-Cases","y":118},{"name":"White-Box","y":43},{"name":"Priority","y":44}]
我可以轻松地将其粘贴到我的data :
中以获取高价图表并获取我正在寻找的饼图
...
series: [{
type: 'pie',
name: 'Browser share',
data: [{"name":"Test-Cases","y":118},{"name":"White-Box","y":43},{"name":"Priority","y":44}]
}]
...
我的问题是如何将output_string
纳入Highcharts data : <here>
。我已经尝试了各种传入方式作为变量并开始在这上面进行分析,不知道为什么。
我用于highcharts并传入id的实际代码......
$('#container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Browser market shares at a specific website, 2014'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: [{"name":"Test-Cases","y":118},{"name":"White-Box","y":43},{"name":"Priority","y":44}]
}]
});
为了覆盖php文件,这里是部分结尾
...
$mysqli->real_query("SELECT priority FROM daily");
$rows2 = array();
$numb2 = 0;
$res2 = $mysqli->use_result();
$rows2['name'] = 'Priority';
while($r2 = $res2->fetch_assoc()) {
$numb2+= $r2['priority'];
$rows2['y'] = $numb2;
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
array_push($result,$rows2);
echo json_encode($result);
答案 0 :(得分:3)
只需将highcharts
包装在函数中,将ajax响应作为函数的参数传递,并将数据(在参数中)加载到high charts
,如下:
function my_chart(response) {
$('#container').highcharts({
...
...
series: [{
type: 'pie',
name: 'Browser share',
data: response
}]
});
}
并在ajax响应中调用您的函数,如:
$(function () {
$.ajax({
url: '../../getdaily.php',
type:'POST',
dataType: '',
success: function(output_string){
//call my_chart function
var parsed_response = jQuery.parseJSON(output_string);
my_chart(parsed_response);
},
error: function (xhr, ajaxOptions, thrownError){
console.log(xhr.statusText);
console.log(thrownError);
}
});
});