<!-- 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", { title: { text: "Temperature of Furnaces" }, axisY: { title: "Temperature ( in °C)", suffix: " °C", minimum: 700, stripLines:[{ value: 1100, labelAlign: "near", label: "Operating Temp.", showOnTop: true, color: "#335E28", labelFontColor: "#335E28" },{ value: 1500, label: "Max Safe Temp.", showOnTop: true, color: "#A90C0C", labelFontColor: "#A90C0C" }] }, data: [{ type: "column", yValueFormatString: "#,##0 °C", indexLabel: "{y}", dataPoints: dataPoints }] }); var yValue; var label; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> yValue = parseFloat("${dataPoint.y}"); label = parseInt("${dataPoint.label}"); dataPoints.push({ x : label, y : yValue, }); </c:forEach> </c:forEach> chart.render(); function updateChart() { var boilerColor, deltaY, yVal; var dps = chart.options.data[0].dataPoints; for (var i = 0; i < dps.length; i++) { deltaY = Math.round(20 + Math.random() *(-20-20)); yVal = Math.max(deltaY + dps[i].y, 700); boilerColor = yVal > 1500 ? "#DF7970" : yVal >= 1100 ? "#51CDA0" : "#4D91DF"; dps[i] = {label: "Furnace "+(i+1) , y: yVal, color: boilerColor}; } chart.options.data[0].dataPoints = dps; chart.render(); }; updateChart(); setInterval(function() {updateChart()}, 1000); } </script> </head> <body> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> </body> </html>
//CanvasjsChartController.java package com.canvasjs.chart.controllers; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; 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<Map<Object, Object>>> canvasjsDataList = canvasjsChartService.getCanvasjsChartData(); modelMap.addAttribute("dataPointsList", canvasjsDataList); return "chart"; } }
//CanvasjsChartService.java package com.canvasjs.chart.services; import java.util.List; import java.util.Map; public interface CanvasjsChartService { List<List<Map<Object, Object>>> getCanvasjsChartData(); } //CanvasjsChartServiceImpl.java package com.canvasjs.chart.services; import java.util.List; import java.util.Map; 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 List<List<Map<Object, Object>>> getCanvasjsChartData() { return canvasjsChartDao.getCanvasjsChartData(); } }
//CanvasjsChartDao.java package com.canvasjs.chart.daos; import java.util.List; import java.util.Map; public interface CanvasjsChartDao { List<List<Map<Object, Object>>> getCanvasjsChartData(); } //CanvasjsChartDaoImpl.java package com.canvasjs.chart.daos; import java.util.List; import java.util.Map; import com.canvasjs.chart.data.CanvasjsChartData; public class CanvasjsChartDaoImpl implements CanvasjsChartDao { @Override public List<List<Map<Object, Object>>> getCanvasjsChartData() { return CanvasjsChartData.getCanvasjsDataList(); } }
//CanvasjsChartData.java package com.canvasjs.chart.data; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CanvasjsChartData { static Map<Object,Object> map = null; static List<List<Map<Object,Object>>> list = new ArrayList<List<Map<Object,Object>>>(); static List<Map<Object,Object>> dataPoints1 = new ArrayList<Map<Object,Object>>(); static { map = new HashMap<Object,Object>(); map.put("label", "Furnace 1"); map.put("y", 1390);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Furnace 2"); map.put("y", 1300);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Furnace 3"); map.put("y", 1560);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Furnace 4"); map.put("y", 1206);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Furnace 5"); map.put("y", 1400);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Furnace 6"); map.put("y", 1050);dataPoints1.add(map); list.add(dataPoints1); } public static List<List<Map<Object, Object>>> getCanvasjsDataList() { return list; } }