<!-- 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: "Refund in Software Sales" }, axisY: { title: "Count", lineColor: "#4F81BC", tickColor: "#4F81BC", labelFontColor: "#4F81BC", gridThickness: 0 }, axisY2: { title: "Cumulative Percent", suffix: "%", gridThickness: 0, lineColor: "#C0504E", tickColor: "#C0504E", labelFontColor: "#C0504E" }, data: [{ type: "column", dataPoints: dps[0] }] }); var yValue; var label; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> yValue = parseFloat("${dataPoint.y}"); label = "${dataPoint.label}"; dps[parseInt("${loop.index}")].push({ label : label, y : yValue, }); </c:forEach> </c:forEach> chart.render(); createPareto(); function createPareto(){ var dps = []; var yValue, yTotal = 0, yPercent = 0; for(var i = 0; i < chart.data[0].dataPoints.length; i++) yTotal += chart.data[0].dataPoints[i].y; for(var i = 0; i < chart.data[0].dataPoints.length; i++) { yValue = chart.data[0].dataPoints[i].y; yPercent += (yValue / yTotal * 100); dps.push({label: chart.data[0].dataPoints[i].label, y: yPercent }); } chart.addTo("data", {type:"line", axisYType: "secondary", yValueFormatString: "0.##\"%\"", indexLabel: "{y}", indexLabelFontColor: "#C24642", dataPoints: dps}); chart.axisY[0].set("maximum", yTotal, false); chart.axisY2[0].set("maximum", 105, false ); chart.axisY2[0].set("interval", 25 ); } } </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", "Not Compatible"); map.put("y", 23);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Not as expected"); map.put("y", 15);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Available at Low Price"); map.put("y", 11);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Changed Mind"); map.put("y", 10);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Too Complicated"); map.put("y", 7);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Poor Manual"); map.put("y", 5);dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("label", "Missing Disk"); map.put("y", 4);dataPoints1.add(map); list.add(dataPoints1); } public static List<List<Map<Object, Object>>> getCanvasjsDataList() { return list; } }