<!-- 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" animationEnabled: true, title: { text: "Diamond Production: 2006 - 2016" }, axisX: { valueFormatString: "YYYY" }, axisY: { title: "Volume (in million carats)" }, legend: { cursor: "pointer", itemclick: toggleDataSeries, verticalAlign: "top" }, data: [{ type: "area", name: "Russia", showInLegend: true, xValueType: "dateTime", xValueFormatString: "YYYY", yValueFormatString: "#,##0.0mn carats", dataPoints: dps[0] },{ type: "area", name: "Congo", showInLegend: true, xValueType: "dateTime", xValueFormatString: "YYYY", yValueFormatString: "#,##0.0mn carats", dataPoints: dps[1] },{ type: "area", name: "Australia", showInLegend: true, xValueType: "dateTime", xValueFormatString: "YYYY", yValueFormatString: "#,##0.0mn carats", dataPoints: dps[2] }] }); var yValue; var xValue; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> yValue = parseFloat("${dataPoint.y}"); xValue = parseFloat("${dataPoint.x}"); dps[parseInt("${loop.index}")].push({ x : xValue, y : yValue, }); </c:forEach> </c:forEach> chart.render(); function toggleDataSeries(e) { if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) { e.dataSeries.visible = false; } else { e.dataSeries.visible = true; } e.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 List<Map<Object,Object>> dataPoints2 = new ArrayList<Map<Object,Object>>(); static List<Map<Object,Object>> dataPoints3 = new ArrayList<Map<Object,Object>>(); static { map = new HashMap<Object,Object>(); map.put("x", 1136053800000L); map.put("y", 38.4);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1167589800000L); map.put("y", 38.3);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1199125800000L); map.put("y", 36.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1230748200000L); map.put("y", 34.8);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1262284200000L); map.put("y", 34.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1293820200000L); map.put("y", 35.1);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1325356200000L); map.put("y", 34.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1356978600000L); map.put("y", 37.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1388514600000L); map.put("y", 38.3);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1420050600000L); map.put("y", 41.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1451586600000L); map.put("y", 40.3);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1136053800000L); map.put("y", 29);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1167589800000L); map.put("y", 28.5);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1199125800000L); map.put("y", 33.4);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1230748200000L); map.put("y", 21.3);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1262284200000L); map.put("y", 20.2);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1293820200000L); map.put("y", 19.2);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1325356200000L); map.put("y", 21.5);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1356978600000L); map.put("y", 15.7);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1388514600000L); map.put("y", 15.7);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1420050600000L); map.put("y", 16);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1451586600000L); map.put("y", 23.2);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1136053800000L); map.put("y", 29.9);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1167589800000L); map.put("y", 18.5);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1199125800000L); map.put("y", 14.9);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1230748200000L); map.put("y", 15.6);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1262284200000L); map.put("y", 10);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1293820200000L); map.put("y", 7.8);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1325356200000L); map.put("y", 9.2);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1356978600000L); map.put("y", 11.7);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1388514600000L); map.put("y", 9.3);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1420050600000L); map.put("y", 13.6);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1451586600000L); map.put("y", 14);dataPoints3.add(map); list.add(dataPoints1); list.add(dataPoints2); list.add(dataPoints3); } public static List<List<Map<Object, Object>>> getCanvasjsDataList() { return list; } }