<!--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 dps = [[]]; <c:if test="${error == null}"> var chart = new CanvasJS.Chart("chartContainer", { theme: "light2", // "light1", "dark1", "dark2" animationEnabled: true, title: { text: "Column Chart from Database" }, data: [{ type: "column", dataPoints: dps[0] }] }); </c:if> var xValue; var yValue; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> xValue = parseInt("${dataPoint.x}"); yValue = parseFloat("${dataPoint.y}"); dps[parseInt("${loop.index}")].push({ x : xValue, y : yValue, }); </c:forEach> </c:forEach> <c:if test="${error == null}"> chart.render(); </c:if> } </script> </head> <body> <c:if test="${error != null}"> <div style='width: 50%; margin-left: auto; margin-right: auto; margin-top: 200px; text-align: center;'>${error}</div> </c:if> <c:if test="${error == null}"> <div id="chartContainer" style="height: 370px; width: 100%;"></div> </c:if> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> </body> </html>
//CanvasjsChartController.java package com.canvasjs.chart.controllers; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.canvasjs.chart.data.CanvasjsChartData.DataPointModel; import com.canvasjs.chart.data.CanvasjsChartData.DatabaseConnectionException; import com.canvasjs.chart.services.CanvasjsChartService; @Controller @RequestMapping("/canvasjschart") public class CanvasjsChartController { @Autowired private CanvasjsChartService canvasjsChartService; @RequestMapping(method = RequestMethod.GET) public String springMVC(ModelMap modelMap) { List<List<DataPointModel>> canvasjsDataList = canvasjsChartService.getCanvasjsChartData(); modelMap.addAttribute("dataPointsList", canvasjsDataList); return "chart"; } @ExceptionHandler({DatabaseConnectionException.class}) public ModelAndView getSuperheroesUnavailable(DatabaseConnectionException ex) { return new ModelAndView("chart", "error", ex.getMessage()); } }
//CanvasjsChartService.java package com.canvasjs.chart.services; import java.util.List; import com.canvasjs.chart.data.CanvasjsChartData.DataPointModel; public interface CanvasjsChartService { List<List<DataPointModel>> getCanvasjsChartData(); } //CanvasjsChartServiceImpl.java package com.canvasjs.chart.services; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.canvasjs.chart.daos.CanvasjsChartDao; import com.canvasjs.chart.data.CanvasjsChartData.DataPointModel; public class CanvasjsChartServiceImpl implements CanvasjsChartService { @Autowired private CanvasjsChartDao canvasjsChartDao; public void setCanvasjsChartDao(CanvasjsChartDao canvasjsChartDao) { this.canvasjsChartDao = canvasjsChartDao; } @Override public List<List<DataPointModel>> getCanvasjsChartData() { return canvasjsChartDao.getCanvasjsChartData(); } }
//CanvasjsChartDao.java package com.canvasjs.chart.daos; import java.util.List; import com.canvasjs.chart.data.CanvasjsChartData.DataPointModel; public interface CanvasjsChartDao { List<List<DataPointModel>> getCanvasjsChartData(); } //CanvasjsChartDaoImpl.java package com.canvasjs.chart.daos; import java.util.List; import com.canvasjs.chart.data.CanvasjsChartData; import com.canvasjs.chart.data.CanvasjsChartData.DataPointModel; public class CanvasjsChartDaoImpl implements CanvasjsChartDao { @Override public List<List<DataPointModel>> getCanvasjsChartData() { return CanvasjsChartData.getCanvasjsDataList(); } }
//CanvasjsChartData.java package com.canvasjs.chart.data; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class CanvasjsChartData { private static JdbcTemplate jdbcTemplate; static Map<Object,Object> map = null; static List<List<DataPointModel>> list = new ArrayList<List<DataPointModel>>(); static List<DataPointModel> dataPoints1 = new ArrayList<DataPointModel>(); static { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/canvasjs_db"); dataSource.setUsername("root"); dataSource.setPassword(""); jdbcTemplate = new JdbcTemplate(dataSource); } public static class DataPointModel { int x; int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } public static class DatabaseConnectionException extends RuntimeException{ private static final long serialVersionUID = 1L; public DatabaseConnectionException(String message) { super(message); } } private static void getDataPoints(){ String sql = "select * from datapoints"; try { dataPoints1 = jdbcTemplate.query(sql, new RowMapper<DataPointModel>() { @Override public DataPointModel mapRow(ResultSet rs, int rowNum) throws SQLException { DataPointModel dataPoint = new DataPointModel(); dataPoint.setX(rs.getInt("x")); dataPoint.setY(rs.getInt("y")); return dataPoint; }}); } catch(Exception e){ dataPoints1 = null; throw new DatabaseConnectionException("Error while getting dataPoints"); } list.add(dataPoints1); } public static List<List<DataPointModel>> getCanvasjsDataList() { getDataPoints(); return list; } }