<!-- 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, title: { text: "Population of Island Countries" }, axisY: { title: "Population", includeZero: true, labelFormatter: addSymbols }, data: [{ type: "column", yValueFormatString: "#,##0", dataPoints: dataPoints }] }); $.get("/CanvasJS_Spring_MVC_Charts/restfull-service/population-of-island-countries.csv", getDataPointsFromCSV); function getDataPointsFromCSV(csv) { var points; var csvLines = csv.split(/[\r?\n|\r|\n]+/); for (var i = 1; i < csvLines.length; i++) { if (csvLines[i].length > 0) { points = csvLines[i].split(","); dataPoints.push({ label: points[0], y: parseFloat(points[1]) }); } } chart.render(); } function addSymbols(e){ var suffixes = ["", "K", "M", "B"]; var order = Math.max(Math.floor(Math.log(Math.abs(e.value)) / Math.log(1000)), 0); if(order > suffixes.length - 1) order = suffixes.length - 1; var suffix = suffixes[order]; return CanvasJS.formatNumber(e.value / Math.pow(1000, order)) + suffix; } } </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 = "/population-of-island-countries.csv", 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; public class CanvasjsChartData { static String csv = null; static { try { csv = readCSVFromUrl("https://canvasjs.com/data/gallery/spring-mvc/population-of-island-countries.csv"); } 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 readCSVFromUrl(String url) throws IOException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String csvText = readAll(rd); return csvText; } finally { is.close(); } } public static String getCanvasjsDataList() { System.out.println(csv); return csv; } }