<!--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", { theme: "dark2", title: { text: "Website Traffic Source" }, legend: { horizontalAlign: "right", verticalAlign: "center" }, data: [{ type: "pie", showInLegend: true, legendText: "{label}", indexLabel: "{y}", indexLabelPlacement: "inside", yValueFormatString: "#,###\"%\"", dataPoints: dataPoints }] }); $.get("/CanvasJS_Spring_MVC_Charts/restfull-service/website-traffic-source.xml", function(data) { $(data).find("point").each(function() { var $dataPoint = jQuery(this); var label = $dataPoint.find("label").text(); var y = $dataPoint.find("y").text(); dataPoints.push({ label: label, y: parseFloat(y) }); }); chart.render(); }); } </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 java.io.PrintWriter; 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 = "/website-traffic-source.xml", method = RequestMethod.GET) public void getDataPoints(HttpServletRequest request, HttpServletResponse response) throws Exception { PrintWriter out = response.getWriter(); response.setContentType("text/xml"); response.setHeader("Content-type: text/xml;", " charset=utf-8"); out.println(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; public class CanvasjsChartData { static String xml = null; static { try { xml = readXMLFromUrl("https://canvasjs.com/data/gallery/spring-mvc/website-traffic-source.xml"); } 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 String readXMLFromUrl(String url) throws IOException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String xmlText = readAll(rd); return xmlText; } finally { is.close(); } } public static String getCanvasjsDataList() { return xml; } }