这是我的js代码。
function paint(){
var chart = new Highcharts.Chart({
chart:{
renderTo:'container',
type:'scatter',//显示类型 散点图
zoomType: 'xy'
},
title:{
text:'散点图' //图表的标题
},
xAxis:{
title: {
enabled: true,
text: 'sepal length'
},
startOnTick: true,
endOnTick: true,
showLastLabel: true
},
yAxis:{
title:{
text:'sepal width' //Y轴的名称
}
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
x: 100,
y: 70,
floating: true,
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF',
borderWidth: 1
},
credits:{
enabled:false
},
plotOptions: {
scatter: {
marker: {
radius: 5,
states: {
hover: {
enabled: true,
lineColor: 'rgb(100,100,100)'
}
}
},
states: {
hover: {
marker: {
enabled: false
}
}
},
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: '{point.x}, {point.y}'
}
}
},
series:[]
});
chart.series[0].setData[2.0,3.6];
$.ajax({
type:'post',
url:'ScatterServlet',//请求数据的地址
data:{"name":"scatter"},
dataType:'json',
async:'true',
success:function(data,status){
alert("data:"+data);
alert("status:"+status);
}
},
error:function(XMLHttpRequest,textStatus,errorThrown){
alert("异常! "+"XMLHttpRequest.status:"+XMLHttpRequest.status+" XMLHttpRequest.readyState:"+XMLHttpRequest.readyState+" textStatus:"+textStatus);
}
});
}
$(document).ready(function(){
var showId= $("#scatter");
showId.click(function(){
paint();
});
});
我的ajax获取的json数据里有5个属性,我现在想取其中两个属性值,然后将这两个属性值动态加载到series里?求助各位了,弄了两天没弄出来。。在线等啊!!
{"iris":[{"sepal length":"5.1","sepal width":"3.5","petal length":"1.4","petal width":"0.2","species":"Iris-setosa"},{"sepal length":"4.9","sepal width":"3.0","petal length":"1.4","petal width":"0.2","species":"Iris-setosa"}]}。这是我的json数据,我想将sepal length和sepal width这两个属性的值加载到series里。
你的 series:[]要配置值,哪怕是空的,不然你调用chart.series[0].setData会出错,有多个series要配置多个空值。
series:[{}]//配置空的值,
而且你语法都错了。。chart.series[0].setData[2.0,3.6];==>chart.series[0].setData([2.0,3.6]);
你是要串接一起还是干什么。。下面是串一起的,只有一条线
success:function(data,status){
var arr=[];
for(var i=0;i<data.iris.length;i++){
arr.push(data.iris[i]["sepal length"]);
arr.push(data.iris[i]["sepal width"]);
}
chart.series[0].setData(arr);
}