我用莫里斯图表中我的应用程序的项目,以显示有关销售数量的一些细节。 执行AJAX请求后,该图表表示无序way.It不显示销售每个city.I要这样例如具有静态数据显示它们的数据http://jsfiddle.net/marsi/LaJXP/1/
var json = (function () {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': 'sales.php',
'dataType': "json",
'success': function (data) {
json = data;
}
});
return json;
})
();
Morris.Area({
element: 'graph-area',
padding: 10,
behaveLikeLine: true,
gridEnabled: false,
gridLineColor: '#dddddd',
axes: true,
fillOpacity:.7,
data:json,
lineColors:['#ED5D5D','#D6D23A','#32D2C9'],
xkey: 'data',
ykeys:['cityname','total'],
labels: ['city','total'],
pointSize: 0,
lineWidth: 0,
hideHover: 'auto'
});
<div id="graph-area"></div>
从JSON文件(sales.php)的结果是这样的:
[{"cityname":"Modena","total":"810.82","data":"2014-02-05 16:55:52"},
{"cityname":"Bologna","total":"396.22","data":"2014-02-09 23:58:20"},
{"cityname":"Rimini","total":"380.00","data":"2014-02-10 10:36:12"},
{"cityname":"Bologna","total":"736.30","data":"2014-02-10 23:30:58"},
{"cityname":"Bologna","total":"0.00","data":"2014-02-12 23:41:52"},
{"cityname":"Modena","total":"0.00","data":"2014-02-13 15:21:17"}]
你必须使用Morris.Area内JSON对象
采用
var result = JSON.parse(json);
Morris.Area({
element: 'graph-area',
padding: 10,
behaveLikeLine: true,
gridEnabled: false,
gridLineColor: '#dddddd',
axes: true,
fillOpacity:.7,
data:result ,
lineColors:['#ED5D5D','#D6D23A','#32D2C9'],
xkey: 'data',
ykeys:['cityname','total'],
labels: ['city','total'],
pointSize: 0,
lineWidth: 0,
hideHover: 'auto'
});
这是我做到了。 使用Java,Spring和莫里斯图表
This is the controler sudocode :
@RequestMapping(value = "/shellMarketingControls/getDatForChart", method = RequestMethod.POST)
public @ResponseBody List<MorrisSingleLine> getDatForChart(@RequestBody String get_value_type, Principal principal)
throws Exception {
List<MorrisSingleLine> temp = new ArrayList<MorrisSingleLine>();
.......
return temp;
这里是对象:
@JsonAutoDetect
@EnableWebMvc
public class MorrisSingleLine implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4992047206653043217L;
private Number xaxis;
private Number yaxis;
@JsonView(Views.Public.class)
public Number getXaxis() {
return xaxis;
}
public void setXaxis(Number xaxis) {
this.xaxis = xaxis;
}
@JsonView(Views.Public.class)
public Number getYaxis() {
return yaxis;
}
public void setYaxis(Number yaxis) {
this.yaxis = yaxis;
}
}
}
最后是JavaScript的
function getDatForChart(get_value_type) {
$
.ajax({
contentType : "application/json",
url : "${pageContext.request.contextPath}/pathToYourController/yourControllermethod",
dataType : 'JSON',
type : 'POST',
data : JSON.stringify(get_value_type),
//timeout : 10000,
success : function(response){
var result = {
feed: {
entries: []
}
};
var count=0;
for(count; count<(response.length);count++){
var tl="";
var tv=0;
tl=response[count].xaxis;
tv=response[count].yaxis;
result.feed.entries.push({
year: tl,
value: tv
});
}
console.log(result);
drawMorrisCharts(result);
},
error : function() {
alert('Error');
}
});
};
function drawMorrisCharts(response) {
$('#morris-one-line-chart').empty();
Morris.Line({
element : 'morris-one-line-chart',
data : response.feed.entries,
xkey : 'year',
ykeys : [ 'value' ],
resize : true,
lineWidth : 4,
labels : [ 'Value' ],
lineColors : [ '#85CE36'],
pointSize : 5,
});
}