Java Morris图表与外部JSON断开
我在使用莫里斯图表时遇到一些问题 我想使用Morris图表和来自外部源的JSON显示事务的详细信息。这是我的密码:Java Morris图表与外部JSON断开,java,jquery,json,servlets,morris.js,Java,Jquery,Json,Servlets,Morris.js,我在使用莫里斯图表时遇到一些问题 我想使用Morris图表和来自外部源的JSON显示事务的详细信息。这是我的密码: $(document).ready(function() { //getting JSON data from external sources var json_data = (function() { var json; $.ajax({ type:'GET', url: 'http://localhost:8080/mas
$(document).ready(function() {
//getting JSON data from external sources
var json_data = (function() {
var json;
$.ajax({
type:'GET',
url: 'http://localhost:8080/masterpiece/chartGetTransaction',
async: false,
global: false,
success: function(data) {
json = data;
},
error:function(){
alert("Error loading chart");
}
});
return json;
})();
//testing purposes to see whether json is shown up or not.
alert(json_data);
new Morris.Bar({
// ID of the element in which to draw the chart.
element: 'pesanan-chart-area',
data: json_data,
xkey: 'tahun',
ykeys: ['jml'],
labels: ['Jumlah'],
smooth: false
});
});
但它总是坏的
JSON本身:
[{"tahun":"2013-10-12","jml":3},{"tahun":"2013-11-16","jml":2},{"tahun":"2013-12-23","jml":4},]
当我手动将JSON设置为“data:”属性时,就会显示图表。但当它从外部源json_数据var引用时,它被破坏了
有什么想法吗
谢谢
更新
更新!JSON是使用名为JSON simple的第三方库从JAVA函数生成的
然后通过servlet调用:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* Execution of jSON Chart */
Analytics show = new Analytics();
List<JSONObject> json = show.getStatisticPesanan();
out.print('[');
for(JSONObject js : json){
out.print(js.toJSONString()+",");
}
out.print(']');
} finally {
out.close();
}
}
有什么解决办法吗?谢谢。alrite在搜索和创建新函数后,我意识到morris.js无法解析JSON对象 首先将servlet更改为:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* Execution of jSON Chart */
Analytics show = new Analytics();
List<JSONObject> json = show.getStatisticPesanan();
out.print(js.toJSONString());
} finally {
out.close();
}
}
我们需要使用函数$.parseJSON使图形正确显示,否则图形将显示未定义
我希望这会对某人有所帮助。我被morris donut chart打动了,我尝试加载json数据文件,但它未能加载图表。函数drawChart{var jsonData=$.getJSONdata.json,functionjson{console.logjson;//在console}中显示信息;Morris.Donut{element:'Donut example',data:jsonData};}知道它在哪里失败了吗???@GovindKailas你能创建一个新的帖子吗?我会尽力帮你的。谢谢。我自己创建了一个yday,请看看这个
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* Execution of jSON Chart */
Analytics show = new Analytics();
List<JSONObject> json = show.getStatisticPesanan();
out.print(js.toJSONString());
} finally {
out.close();
}
}
new Morris.Bar({
// ID of the element in which to draw the chart.
element: 'pesanan-chart-area',
data: $.parseJSON(json_data),
xkey: 'tahun',
ykeys: ['jml'],
labels: ['Jumlah'],
smooth: false
});