<!-- chart.jsp --> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript"> window.onload = function() { var dataPoints = []; var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, zoomEnabled: true, title: { text: "eBay Inc. Stock Price - 2017" }, axisX: { crosshair: { enabled: true, snapToDataPoint: true, valueFormatString: "DD MMM" } }, axisY: { title: "Closing Price (in USD)", crosshair: { enabled: true, snapToDataPoint: true, valueFormatString: "$#,##0.00" } }, data: [{ type: "line", xValueFormatString: "DD MMM", yValueFormatString: "$#,##0.00", xValueType: "dateTime", dataPoints: dataPoints }] }); function addData(data) { for (var i = 0; i < data.length; i++) { dataPoints.push({ x: new Date(data[i].x), y: data[i].y }); } chart.render(); } $.getJSON("/CanvasJS_Spring_MVC_Charts/restfull-service/eBay-inc-stock-price.json", addData); } </script> </head> <body> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> </body> </html>
//CanvasjsChartController.java package com.canvasjs.chart.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/canvasjschart") public class CanvasjsChartController { @RequestMapping(method = RequestMethod.GET) public String springMVC(ModelMap modelMap) { return "chart"; } } //RestController.java package com.canvasjs.chart.controllers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.canvasjs.chart.services.CanvasjsChartService; @Controller @RequestMapping(value = "/restfull-service") public class RestController extends HttpServlet { private static final long serialVersionUID = 1L; @Autowired private CanvasjsChartService canvasjsChartService; @RequestMapping(value = "/eBay-inc-stock-price.json", method = RequestMethod.GET) public @ResponseBody String getDataPoints(HttpServletRequest request, HttpServletResponse response) throws Exception { return canvasjsChartService.getCanvasjsChartData(); } }
//CanvasjsChartService.java package com.canvasjs.chart.services; public interface CanvasjsChartService { String getCanvasjsChartData(); } //CanvasjsChartServiceImpl.java package com.canvasjs.chart.services; import org.springframework.beans.factory.annotation.Autowired; import com.canvasjs.chart.daos.CanvasjsChartDao; public class CanvasjsChartServiceImpl implements CanvasjsChartService { @Autowired private CanvasjsChartDao canvasjsChartDao; public void setCanvasjsChartDao(CanvasjsChartDao canvasjsChartDao) { this.canvasjsChartDao = canvasjsChartDao; } @Override public String getCanvasjsChartData() { return canvasjsChartDao.getCanvasjsChartData(); } }
//CanvasjsChartDao.java package com.canvasjs.chart.daos; public interface CanvasjsChartDao { String getCanvasjsChartData(); } //CanvasjsChartDaoImpl.java package com.canvasjs.chart.daos; import com.canvasjs.chart.data.CanvasjsChartData; public class CanvasjsChartDaoImpl implements CanvasjsChartDao { @Override public String getCanvasjsChartData() { return CanvasjsChartData.getCanvasjsDataList(); } }
//CanvasjsChartData.java package com.canvasjs.chart.data; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; import org.json.JSONException; import org.json.JSONArray; public class CanvasjsChartData { static JSONArray json = null; static { try { json = readJsonFromUrl("https://canvasjs.com/data/gallery/spring-mvc/eBay-inc-stock-price.json"); } catch (Exception e) { e.printStackTrace(); } } private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); } public static JSONArray readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONArray json = new JSONArray(jsonText); return json; } finally { is.close(); } } public static String getCanvasjsDataList() { return json.toString(); } }