<!-- 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"> <style> #backButton { border-radius: 4px; padding: 8px; border: none; font-size: 16px; background-color: #2eacd1; color: white; position: absolute; top: 10px; right: 10px; cursor: pointer; } .invisible { display: none; } </style> <script type="text/javascript"> window.onload = function() { var dps = [[],[],[]]; var totalVisitors = 883000; var yValue; var xValue; var name; var color; <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop"> <c:forEach items="${dataPoints}" var="dataPoint"> yValue = parseFloat("${dataPoint.y}"); xValue = parseFloat("${dataPoint.x}"); name = "${dataPoint.name}"; color = "${dataPoint.color}"; dps[parseInt("${loop.index}")].push({ x : xValue, y : yValue, name: name, color: color }); </c:forEach> </c:forEach> var visitorsData = { "New vs Returning Visitors": [{ click: visitorsChartDrilldownHandler, cursor: "pointer", explodeOnClick: false, innerRadius: "75%", legendMarkerType: "square", name: "New vs Returning Visitors", radius: "100%", showInLegend: true, startAngle: 90, type: "doughnut", dataPoints: dps[0] }], "New Visitors": [{ color: "#E7823A", name: "New Visitors", xValueType: "dateTime", type: "column", dataPoints: dps[1] }], "Returning Visitors": [{ color: "#546BC1", name: "Returning Visitors", xValueType: "dateTime", type: "column", dataPoints: dps[2] }] }; console.log(visitorsData["New vs Returning Visitors"]) var newVSReturningVisitorsOptions = { animationEnabled: true, theme: "light2", title: { text: "New VS Returning Visitors" }, subtitles: [{ text: "Click on Any Segment to Drilldown", backgroundColor: "#2eacd1", fontSize: 16, fontColor: "white", padding: 5 }], legend: { fontFamily: "calibri", fontSize: 14, itemTextFormatter: function (e) { return e.dataPoint.name + ": " + Math.round(e.dataPoint.y / totalVisitors * 100) + "%"; } }, data: [] }; var visitorsDrilldownedChartOptions = { animationEnabled: true, theme: "light2", axisX: { labelFontColor: "#717171", lineColor: "#a2a2a2", tickColor: "#a2a2a2" }, axisY: { gridThickness: 0, includeZero: false, labelFontColor: "#717171", lineColor: "#a2a2a2", tickColor: "#a2a2a2", lineThickness: 1 }, data: [] }; var chart = new CanvasJS.Chart("chartContainer", newVSReturningVisitorsOptions); chart.options.data = visitorsData["New vs Returning Visitors"]; chart.render(); function visitorsChartDrilldownHandler(e) { chart = new CanvasJS.Chart("chartContainer", visitorsDrilldownedChartOptions); chart.options.data = visitorsData[e.dataPoint.name]; chart.options.title = { text: e.dataPoint.name } chart.render(); $("#backButton").toggleClass("invisible"); } $("#backButton").click(function() { $(this).toggleClass("invisible"); chart = new CanvasJS.Chart("chartContainer", newVSReturningVisitorsOptions); chart.options.data = visitorsData["New vs Returning Visitors"]; chart.render(); }); } </script> </head> <body> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <button class="btn invisible" id="backButton">< Back</button> <script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script> <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("name", "New Visitors"); map.put("y", 519960); map.put("color", "#E7823A");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("name", "Returning Visitors"); map.put("y", 363040); map.put("color", "#546BC1");dataPoints1.add(map); map = new HashMap<Object,Object>(); map.put("x", 1420050600000L); map.put("y", 33000);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1422729000000L); map.put("y", 35960);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1425148200000L); map.put("y", 42160);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1427826600000L); map.put("y", 42240);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1430418600000L); map.put("y", 43200);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1433097000000L); map.put("y", 40600);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1435689000000L); map.put("y", 42560);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1438367400000L); map.put("y", 44280);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1441045800000L); map.put("y", 44800);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1443637800000L); map.put("y", 48720);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1446316200000L); map.put("y", 50840);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1448908200000L); map.put("y", 51600);dataPoints2.add(map); map = new HashMap<Object,Object>(); map.put("x", 1420050600000L); map.put("y", 22000);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1422729000000L); map.put("y", 26040);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1425148200000L); map.put("y", 25840);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1427826600000L); map.put("y", 23760);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1430418600000L); map.put("y", 28800);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1433097000000L); map.put("y", 29400);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1435689000000L); map.put("y", 33440);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1438367400000L); map.put("y", 37720);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1441045800000L); map.put("y", 35200);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1443637800000L); map.put("y", 35280);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1446316200000L); map.put("y", 31160);dataPoints3.add(map); map = new HashMap<Object,Object>(); map.put("x", 1448908200000L); map.put("y", 34400);dataPoints3.add(map); list.add(dataPoints1); list.add(dataPoints2); list.add(dataPoints3); } public static List<List<Map<Object, Object>>> getCanvasjsDataList() { return list; } }