<!-- 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", { title: { text: "Fuel Economy vs Horsepower of Roadsters/Sport Cars" }, axisX: { title: "Fuel Economy in Miles Per Gallon (MPG)" }, axisY: { title: "Horsepower", includeZero: true, }, legend: { horizontalAlign: "right" }, data: [ { type: "bubble", showInLegend: true, legendText: "Size of bubble shows Weight of Car", toolTipContent: "<b>{name}</b><br> {x}: {y}", dataPoints: dps[0] } ] }); var name; var xValue; var yValue; var zValue; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> name = "${dataPoint.name}"; xValue = parseFloat("${dataPoint.x}"); yValue = parseFloat("${dataPoint.y}"); zValue = parseFloat("${dataPoint.z}"); dps[parseInt("${loop.index}")].push({ name : name, x : xValue, y : yValue, z : zValue, }); </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 { //map = new HashMap<Object,Object>(); map.put("name", "Mazda MX-5 Miata Club"); map.put("x", 34); map.put("y", 155); map.put("z", "2332");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Mini Cooper S"); map.put("x", 30); map.put("y", 162); map.put("z", "2605");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Toyota 86"); map.put("x", 30); map.put("y", 200); map.put("z", "2811");dataPoints1.add(map); //map = new HashMap<Object,Object>(); map.put("name", "Subaru BR-Z Premium"); map.put("x", 30); map.put("y", 205); map.put("z", "2800");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Volkswagen GTI Autobahn"); map.put("x", 29); map.put("y", 220); map.put("z", "3116");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Ford Fiesta ST"); map.put("x", 29); map.put("y", 120); map.put("z", "2611");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "BMW Z4 sDrive28i"); map.put("x", 28); map.put("y", 240); map.put("z", "3250");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Volkswagen GLI Autobahn"); map.put("x", 27); map.put("y", 184); map.put("z", "3071");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Porsche 718 Boxster"); map.put("x", 26); map.put("y", 350); map.put("z", "3059");dataPoints1.add(map); //map = new HashMap<Object,Object>(); map.put("name", "Subaru Impreza WRX Premium"); map.put("x", 26); map.put("y", 305); map.put("z", "3451");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Audi TT 2.0T"); map.put("x", 26); map.put("y", 292); map.put("z", "3220");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Ford Mustang Premium"); map.put("x", 25); map.put("y", 460); map.put("z", "3720");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Ford Focus ST"); map.put("x", 25); map.put("y", 160); map.put("z", "3100");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "BMW M235i"); map.put("x", 25); map.put("y", 248); map.put("z", "3571");dataPoints1.add(map); list.add(dataPoints1); } public static List<List<Map<Object, Object>>> getCanvasjsDataList() { return list; } }