简体   繁体   中英

How can I use my data from a JSON string to populate dimple d3.js line chart?

My objective is to get data from database and use it to populate a dimple js line chart. I am using JSON to carry the data from back-end. Below is the code from my servlet going to http request/respose.

String sql2 = "select a,date from table order by date";
JSONArray list = new JSONArray();
DataDAOImpl rdao = new DataDAOImpl();
list = rdao.getData(sql2);
req.setAttribute("line", list.toJSONString());
WebUtil.forward(req, resp, this, "/test/linechart.jsp");

And my code in jsp is as below. It is inside a javascript tag. The chart is not rendering although the data is available in JSP. Also this is running on local web server. Please advice what I am missing. I have to generate several reports and this is my proof of concept.

//script type="text/javascript">

var data = '${line}';
var chart = new dimple.chart(svg, data);
chart.setBounds(100, 200, 505, 305);
chart.addCategoryAxis("x", "date");
chart.addMeasureAxis("y", "a");
chart.addSeries(null, dimple.plot.line);
chart.draw();

///script>

have you tried JSON.parse('${line}'); ?

I'm not familiar with your exact case but I believe that will turn a string containing JSON into JSON. Is there any console error?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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