I've created an ajax request that returns data like this
我创建了一个返回这样的数据的ajax请求
Object
negative:3
neutral:3
positive:94
This is directly from console.log();
这直接来自console.log();
Im trying to create a pie chart using this data but when I try it doesn't draw the chart and nothing shows up except for the credits and no errors in the console either. But if I manually input data like this:
我试图使用这些数据创建一个饼图,但是当我尝试它时不会绘制图表,除了学分和控制台中没有错误之外什么也没有显示。但如果我手动输入这样的数据:
series: [{
name: 'Brands',
data: [
{ name: 'Positive', y: 94 },
{ name: 'Negative', y: 3 },
{ name: 'Neutral', y: 3 },
]
}]
it works no problem.
它没有问题。
My code is:
我的代码是:
function pieChart(data) {
// Make monochrome colors and set them as default for all pies
Highcharts.getOptions().plotOptions.pie.colors = (function () {
var colors = [],
base = Highcharts.getOptions().colors[0],
i;
for (i = 0; i < 10; i += 1) {
// Start out with a darkened base color (negative brighten), and end
// up with a much brighter color
colors.push(Highcharts.Color(base).brighten((i - 3) / 7).get());
}
return colors;
}());
// Build the chart
Highcharts.chart('pieChart', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
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: [{
name: 'Brands',
data: data
}]
});
};
1 个解决方案
#1
1
The response you've received from server using ajax
request is an object.
您使用ajax请求从服务器收到的响应是一个对象。
All you need is to create a json
array from the response you've received.
您所需要的只是从您收到的响应中创建一个json数组。
var object={'positive':94,'neutral':2,'negative':2};
var data=[];
for(i in object){
data.push({"name":i,"y":object[i]});
}
And set it to highchart
series.
并将其设置为高图系列。
series: [{
name: 'Brands',
data: data
}]
#1
1
The response you've received from server using ajax
request is an object.
您使用ajax请求从服务器收到的响应是一个对象。
All you need is to create a json
array from the response you've received.
您所需要的只是从您收到的响应中创建一个json数组。
var object={'positive':94,'neutral':2,'negative':2};
var data=[];
for(i in object){
data.push({"name":i,"y":object[i]});
}
And set it to highchart
series.
并将其设置为高图系列。
series: [{
name: 'Brands',
data: data
}]