<!-- 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 = [[]]; var chart = new CanvasJS.Chart("chartContainer", { theme: "light2", // "light1", "dark1", "dark2" title: { text: "Average & High/Low Temperature - London 2017" }, axisY: { title: "Temperature", suffix: " °C" }, legend:{ cursor: "pointer", itemclick: toggleDataSeries }, toolTip: { shared: true }, data: [{ type: "rangeSplineArea", name: "High/Low Temperature", showInLegend: true, xValueType: "dateTime", xValueFormatString: "MMM YYYY", yValueFormatString: "#,##0 °C", markerSize: 0, toolTipContent: "<b>{x}<br></b><span style=\"color:#6D78AD\">{name}</span>: {y[0]} to {y[1]}", dataPoints: dps[0] }] }); var yValue; var xValue; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> yValue = [parseFloat("${dataPoint.y[0]}"), parseFloat("${dataPoint.y[1]}")]; xValue = parseFloat("${dataPoint.x}"); dps[parseInt("${loop.index}")].push({ x : xValue, y : yValue, }); </c:forEach> </c:forEach> chart.render(); findAverage(chart); function findAverage(chart){ var dataPoints = []; var sum, avg; for(var i = 0; i < chart.data[0].dataPoints.length; i++){ sum = 0; for(var j = 0; j < chart.data[0].dataPoints[i].y.length; j++){ sum += chart.data[0].dataPoints[i].y[j]; } avg = sum / chart.data[0].dataPoints[i].y.length; dataPoints.push({ x: chart.data[0].dataPoints[i].x, y: avg }); } var dataSeries = { type: "spline", name: "Average Temperature", showInLegend: true, xValueType: "dateTime", yValueFormatString: "#,##0.0# °C", markerSize: 0, toolTipContent: "<span style=\"color:#51CDA0\">{name}</span>: {y}", dataPoints: dataPoints } chart.addTo("data", dataSeries); } function toggleDataSeries(e){ if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) { e.dataSeries.visible = false; } else{ e.dataSeries.visible = true; } chart.render(); } } </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("x", 1483209000000L); map.put("y", new Integer[] {3,6});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1485887400000L); map.put("y", new Integer[] {3,7});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1488306600000L); map.put("y", new Integer[] {4,10});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1490985000000L); map.put("y", new Integer[] {6,13});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1493577000000L); map.put("y", new Integer[] {9,17});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1496255400000L); map.put("y", new Integer[] {12,20});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1498847400000L); map.put("y", new Integer[] {14,22});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1501525800000L); map.put("y", new Integer[] {14,21});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1504204200000L); map.put("y", new Integer[] {12,19});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1506796200000L); map.put("y", new Integer[] {9,14});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1509474600000L); map.put("y", new Integer[] {6,10});dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1512066600000L); map.put("y", new Integer[] {3,7});dataPoints1.add(map); list.add(dataPoints1); } public static List<List<Map<Object, Object>>> getCanvasjsDataList() { return list; } }