我刚开始学习JQuery和Highcharts。我用静态数据创建了一个多Y轴高分布图。我想能够将数据从java传递到系列数据。我该怎么做?我如何让JQuery代码从我的java类中获取数据并将其加载到高图中。以下是我的代码:将Java中的系列数据传递给Y轴Highcharts
// MultiY.js
$(document).ready(function() {
chart1 = new Highcharts.Chart({
chart: {
renderTo: 'chart_1',
height: 350,
},
title: {
text: 'Sample Highcharts'
},
xAxis: {
categories: ['4/28/2013', '4/29/2013', '4/30/2013', '5/1/2013', '5/2/2013', '5/3/2013', '5/4/2013']
},
yAxis: [{
opposite: true,
title: {
text: 'Cost',
style: {
color: '#dbf400'
}
},
labels: {
style: {
color: '#dbf400'
}
},plotOptions: {
series: {
pointWidth: 20
}
}
},
{
lineWidth: 2,
title: {
text: 'Silver',
style: {
color: '#89A54E'
}
},
labels: {
style: {
color: '#89A54E'
}
}
}, {
lineWidth: 2,
opposite: true,
title: {
text: 'Gold',
style: {
color: '#4572A7'
}
},
labels: {
style: {
color: '#4572A7'
}
}
}, {
lineWidth: 2,
opposite: true,
title: {
text: 'Score',
style: {
color: '#AA4643'
}
},
labels: {
style: {
color: '#AA4643'
}
}
}],
series: [ {
name: 'Cost',
type: 'column',
color: '#dbf400',
data: [65078.70, 70816.51, 71211.22, 56130.71, 67839.10, 59170.91, 52826.47] ,
yAxis: 3
}, {
name: 'Silver',
type: 'spline',
color: '#89A54E',
dashStyle: 'shortdot',
data: [6357434, 7190915, 6737487, 6001323, 8628154, 7446175, 5953040]
}, {
name: 'Gold',
type: 'spline',
color: '#4572A7',
data: [2652304, 2862748, 2645867, 2506507, 2531869, 2352410, 2127584] ,
yAxis: 1
}, {
name: 'Score',
type: 'spline',
color: '#AA4643',
data: [57994, 68114, 64582, 26526, 52712, 55464, 46802] ,
yAxis: 2
}]
});
});
我的Java函数返回:
trendingData.add(new TrendingDataObjects(silver, gold, score, cost, day));
我想你可能会这样做:1)使用AJAX,从jQuery可能是get()或getJSON()。我建议使用第二。 2)在您的Java类添加库导出到JSON,并编码为该格式。 3)将AJAX请求连接到将返回该JSON的URL。 4)要添加新系列,请使用[chart.addSeries](http://api.highcharts.com/highcharts#Chart.addSeries())或为现有系列设置新数据[chart.series.setData](http: //api.highcharts.com/highcharts#Series.setData()) –
我跟随你的建议。我写了一个ajax函数并获得了json数据。我尝试使用以下方法创建系列数据: 'success:function(data){ \t chart.xAxis [0] .setCategories(data.timestamp); ) \t chart.addSeries({“Cost”, data:data.cost \t});' 但现在图表不加载:( – Praveen
data.timestamp应该是一个字符串数组(例如:[['a','b','c']'),data.cost应该是一个点数组(例如:'[123,145,156]')。你确定这是你的JSON格式的数据吗?你可以显示你的JSON数据样本吗? –