<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ page import="com.google.gson.Gson"%> <%@ page import="com.google.gson.JsonObject"%> <% Gson gsonObj = new Gson(); Map<Object,Object> map = null; List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>(); map = new HashMap<Object,Object>(); map.put("label", "Traffic Tie-up"); map.put("y", 32); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "Woke-up Late"); map.put("y", 20); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "Family Problems"); map.put("y", 8); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "Sick"); map.put("y", 6); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "Had to take Public Transport"); map.put("y", 4); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "Bad Weather"); map.put("y", 3); list.add(map); String dataPoints = gsonObj.toJson(list); %> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> window.onload = function() { var chart = new CanvasJS.Chart("chartContainer", { title:{ text: "Causes for Late Arrival" }, axisY: { title: "Number of Occasions", lineColor: "#4F81BC", tickColor: "#4F81BC", labelFontColor: "#4F81BC" }, axisY2: { title: "Percent", suffix: "%", titleFontColor: "#C0504E", tickColor: "#C0504E", labelFontColor: "#C0504E" }, data: [{ type: "column", name: "Number of Occasions", dataPoints: <%out.print(dataPoints);%> }] }); chart.render(); createPareto(); function createPareto(){ var dps = []; var yValue, yTotal = 0, yPercent = 0; var color; 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", yValueFormatString: "0.##\"%\"", dataPoints: dps}); chart.data[1].set("axisYType", "secondary", false); chart.axisY[0].set("maximum", yTotal); chart.axisY2[0].set("maximum", 100); } } </script> </head> <body> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> </body> </html>