<!-- 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", {
animationEnabled: true,
theme: "light2", // "light1", "dark1", "dark2"
title:{
text: "Website Traffic Source - 2017"
},
axisX: {
interval: 1,
intervalType: "month",
valueFormatString: "MMM"
},
axisY:{
suffix: "%"
},
toolTip: {
shared: true
},
legend:{
cursor: "pointer",
itemclick: toggleDataSeries
},
data: [{
type: "stackedArea100",
name: "Organic Search",
showInLegend: "true",
xValueType: "dateTime",
xValueFormatString: "MMM",
yValueFormatString: "#,##0\"%\"",
dataPoints: dps[0]
},
{
type: "stackedArea100",
name: "Social",
showInLegend: "true",
xValueType: "dateTime",
xValueFormatString: "MMM",
yValueFormatString: "#,##0\"%\"",
dataPoints: dps[1]
},{
type: "stackedArea100",
name: "Direct",
showInLegend: "true",
xValueType: "dateTime",
xValueFormatString: "MMM",
yValueFormatString: "#,##0\"%\"",
dataPoints: dps[2]
}]
});
var yValue;
var xValue;
<c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop">
<c:forEach items="${dataPoints}" var="dataPoint">
yValue = parseFloat("${dataPoint.y}");
xValue = parseFloat("${dataPoint.x}");
dps[parseInt("${loop.index}")].push({
x : xValue,
y : yValue,
});
</c:forEach>
</c:forEach>
chart.render();
function toggleDataSeries(e) {
if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
} else {
e.dataSeries.visible = true;
}
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>
//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("x", 1483209000000L); map.put("y", 40);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1485887400000L); map.put("y", 53);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1488306600000L); map.put("y", 51);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1490985000000L); map.put("y", 51);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1493577000000L); map.put("y", 51);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1496255400000L); map.put("y", 44);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1498847400000L); map.put("y", 52);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1501525800000L); map.put("y", 58);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1504204200000L); map.put("y", 35);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1506796200000L); map.put("y", 41);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1509474600000L); map.put("y", 50);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1512066600000L); map.put("y", 44);dataPoints1.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1483209000000L); map.put("y", 13);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1485887400000L); map.put("y", 10);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1488306600000L); map.put("y", 15);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1490985000000L); map.put("y", 12);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1493577000000L); map.put("y", 11);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1496255400000L); map.put("y", 15);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1498847400000L); map.put("y", 9);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1501525800000L); map.put("y", 7);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1504204200000L); map.put("y", 9);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1506796200000L); map.put("y", 13);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1509474600000L); map.put("y", 11);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1512066600000L); map.put("y", 14);dataPoints2.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1483209000000L); map.put("y", 47);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1485887400000L); map.put("y", 37);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1488306600000L); map.put("y", 34);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1490985000000L); map.put("y", 37);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1493577000000L); map.put("y", 38);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1496255400000L); map.put("y", 41);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1498847400000L); map.put("y", 39);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1501525800000L); map.put("y", 35);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1504204200000L); map.put("y", 56);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1506796200000L); map.put("y", 46);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1509474600000L); map.put("y", 39);dataPoints3.add(map);
map = new HashMap<Object,Object>(); map.put("x", 1512066600000L); map.put("y", 42);dataPoints3.add(map);
list.add(dataPoints1);
list.add(dataPoints2);
list.add(dataPoints3);
}
public static List<List<Map<Object, Object>>> getCanvasjsDataList() {
return list;
}
}