<%@ 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", "2010"); map.put("y", 1828); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2011"); map.put("y", 1723); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2012"); map.put("y", 1511); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2013"); map.put("y", 1571); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2014"); map.put("y", 1569); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2015"); map.put("y", 1350); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2016"); map.put("y", 1241); list.add(map); String dataPoints1 = gsonObj.toJson(list); list = new ArrayList<Map<Object,Object>>(); map = new HashMap<Object,Object>(); map.put("label", "2010"); map.put("y", 399); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2011"); map.put("y", 409); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2012"); map.put("y", 493); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2013"); map.put("y", 444); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2014"); map.put("y", 444); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2015"); map.put("y", 527); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2016"); map.put("y", 547); list.add(map); String dataPoints2 = gsonObj.toJson(list); list = new ArrayList<Map<Object,Object>>(); map = new HashMap<Object,Object>(); map.put("label", "2010"); map.put("y", 43); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2011"); map.put("y", 37); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2012"); map.put("y", 30); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2013"); map.put("y", 34); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2014"); map.put("y", 37); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2015"); map.put("y", 35); list.add(map); map = new HashMap<Object,Object>(); map.put("label", "2016"); map.put("y", 33); list.add(map); String dataPoints3 = 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: "Carbon Dioxide Emissions from Energy Consumption" }, axisY: { title: "Million Metric Tons of Carbon Dioxide" }, legend: { verticalAlign: "top" }, toolTip: { shared: true }, data: [{ type: "stackedBar", name: "Coal", showInLegend: true, dataPoints: <%out.print(dataPoints1);%> }, { type: "stackedBar", name: "Natural Gas", showInLegend: true, dataPoints: <%out.print(dataPoints2);%> }, { type: "stackedBar", name: "Others", showInLegend: true, dataPoints: <%out.print(dataPoints3);%> }] }); 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>