<!-- File Name: chart.jsp Author: Team CanvasJS --> <%@ 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 chart = new CanvasJS.Chart("chartContainer", { theme: "light2", // "light1", "dark1", "dark2" animationEnabled: true, title: { text: "Letter Frequency of Vowels in English" }, axisX: { labelFontSize: 18 }, axisY: { title: "Frequency", suffix: "%" }, data: [{ type: "column", indexLabel: "{y}", indexLabelFontSize: 18, yValueFormatString: "#,##0.0#\"%\"" }] }); function addData(data) { chart.options.data[0].dataPoints = data; chart.render(); } $.getJSON("/CanvasJS_Spring_MVC_Charts/restfull-service/letter-frequency-of-vowels-in-english.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 = "/letter-frequency-of-vowels-in-english.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/letter-frequency-of-vowels-in-english.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(); } }