简体   繁体   English

如何使用通过Spring MVC传递的D3和Dimple Java JSONArray? 我收到404错误

[英]How do I use a Java JSONArray with D3 and Dimple, passed using Spring MVC? I'm getting a 404 error

Using Spring MVC, I have a JSONArray in my controller, which I pass to my view using view.addObject like this: 使用Spring MVC,我在控制器中有一个JSONArray,我可以使用view.addObject将其传递到视图,如下所示:

@RequestMapping("/graphWeek")
public ModelAndView getGraphWeek(){
    JSONArray tweetsForWeek = getGraphWeekData();
    ModelAndView mv = new ModelAndView("graphWeek");
    mv.addObject("tweetsForWeek", tweetsForWeek);
    return mv;
}

In my jsp view, I have this: 在我的jsp视图中,我有这个:

var svg = dimple.newSvg("#bar", 800, 500);
var src= ${tweetsForWeek};
console.log(src);

d3.json(src, function (error, data) {
  var barChart = new dimple.chart(svg, data);
  barChart.addCategoryAxis("x", ["Day", "Topic"]);
  barChart.addMeasureAxis("y", "Tweets");
  barChart.addSeries("Topic", dimple.plot.bar);
  barChart.addLegend(100, 10, 510, 20, "right");
  barChart.width = 600;
  barChart.height = 400;
  barChart.draw();
});

I added a console.log(src) call to make sure the array was being received by the view and it is. 我添加了一个console.log(src)调用,以确保该视图正被数组接收。 But my graph doesn't display and it gives me an error. 但是我的图形没有显示,这给了我一个错误。 This is the result of loading my view, showing the console log and error in firebug: 这是加载视图的结果,显示了控制台日志和firebug中的错误:

在此处输入图片说明

So you can see in the console that the array is being received by the view but it is not being handled correctly by d3, does anyone know what could be causing this error? 因此,您可以在控制台中看到视图正在接收该数组,但是d3没有正确处理该数组,有人知道是什么原因导致此错误吗? Any help would be greatly appreciated, thanks. 任何帮助将不胜感激,谢谢。

You don't need d3.json here. 您在这里不需要d3.json You have your data already. 您已经有数据了。

var svg = dimple.newSvg("#bar", 800, 500);
var src= ${tweetsForWeek};
console.log(src);

var barChart = new dimple.chart(svg, src); //<-- you have your data already
barChart.addCategoryAxis("x", ["Day", "Topic"]);
barChart.addMeasureAxis("y", "Tweets");
barChart.addSeries("Topic", dimple.plot.bar);
barChart.addLegend(100, 10, 510, 20, "right");
barChart.width = 600;
barChart.height = 400;
barChart.draw();

The alternative here is to, of course, forget using the JSP templating to load your array and do it via JSON: 当然,这里的替代方法是忘记使用JSP模板来加载数组并通过JSON完成操作:

d3.json("/graphWeek", function (error, data) {
    var svg = dimple.newSvg("#bar", 800, 500);
    var barChart = new dimple.chart(svg, data);
    barChart.addCategoryAxis("x", ["Day", "Topic"]);
    barChart.addMeasureAxis("y", "Tweets");
    barChart.addSeries("Topic", dimple.plot.bar);
    barChart.addLegend(100, 10, 510, 20, "right");
    barChart.width = 600;
    barChart.height = 400;
    barChart.draw();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM