<!-- 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", { animationEnabled: true, exportEnabled: true, theme: "light2", // "light1", "dark1", "dark2" title: { text: "Heart Weight vs Body Weight of Domestic Cats" }, axisX: { title: "Body Weight (in kg)", suffix: " kg" }, axisY: { title: "Heart Weight (in g)", suffix: " g" }, legend: { cursor: "pointer", itemclick: toggleDataSeries }, data: [{ type: "scatter", showInLegend: true, name: "Female", markerType: "cross", xValueFormatString: "#,##0.0 kg", yValueFormatString: "#,##0.0 g", toolTipContent: "<span style=\"color:#6D78AD\">{name} Cat</span><br>Body Weight: {x}<br>Heart Weight: {y}", dataPoints: dps[0] }, { type: "scatter", showInLegend: true, name: "Male", markerType: "triangle", xValueFormatString: "#,##0.0 kg", yValueFormatString: "#,##0.0 g", toolTipContent: "<span style=\"color:#51CDA0\">{name} Cat</span><br>Body Weight: {x}<br>Heart Weight: {y}", dataPoints: dps[1] }] }); function toggleDataSeries(e) { if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) { e.dataSeries.visible = false; } else { e.dataSeries.visible = true; } chart.render(); } var xValue; var yValue; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> xValue = parseFloat("${dataPoint.x}"); yValue = parseFloat("${dataPoint.y}"); dps[parseInt("${loop.index}")].push({ x : xValue, y : yValue }); </c:forEach> </c:forEach> 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 { map = new HashMap<Object,Object>(); map.put("x", 2); map.put("y", 7);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2); map.put("y", 7.4);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.1); map.put("y", 8.1);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.1); map.put("y", 8.7);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.2); map.put("y", 10.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.3); map.put("y", 11.2);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.4); map.put("y", 10.2);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.5); map.put("y", 10.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.6); map.put("y", 10.1);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.7); map.put("y", 10.8);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.9); map.put("y", 9.9);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.9); map.put("y", 10.1);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.9); map.put("y", 10.1);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 3); map.put("y", 10.6);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 3); map.put("y", 13);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 2); map.put("y", 6.5);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.1); map.put("y", 10.1);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.2); map.put("y", 8.5);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.3); map.put("y", 9.6);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.4); map.put("y", 9.3);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.5); map.put("y", 8.8);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.6); map.put("y", 9.4);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.6); map.put("y", 11.5);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.7); map.put("y", 12);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.7); map.put("y", 12.5);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.8); map.put("y", 12);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 2.9); map.put("y", 11.8);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 3); map.put("y", 13.8);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 3.1); map.put("y", 14.3);dataPoints2.add(map); list.add(dataPoints1); list.add(dataPoints2); } public static List<List<Map<Object, Object>>> getCanvasjsDataList() { return list; } }