<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
window.onload = function() { 
var dataPoints1 = [];
var dataPoints2 = [];
var chart = new CanvasJS.Chart("chartContainer", {
	zoomEnabled: true,
	title: {
		text: "Rotational Speed of Two Car Engines"
	},
	axisX: {
		title: "chart updates every 2 secs"
	},
	axisY:{
		suffix: " rpm"
	}, 
	toolTip: {
		shared: true
	},
	legend: {
		cursor:"pointer",
		verticalAlign: "top",
		fontSize: 18,
		fontColor: "dimGrey",
		itemclick : toggleDataSeries
	},
	data: [{ 
		type: "stepLine",
		xValueFormatString: "#,##0 seconds",
		yValueFormatString: "#,##0 rpm",
		showInLegend: true,
		name: "Engine A",
		dataPoints: dataPoints1
	},
	{				
		type: "stepLine",
		xValueFormatString: "#,##0 seconds",
		yValueFormatString: "#,##0 rpm",
		showInLegend: true,
		name: "Engine B" ,
		dataPoints: dataPoints2
	}]
});
function toggleDataSeries(e) {
	if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
		e.dataSeries.visible = false;
	}
	else {
		e.dataSeries.visible = true;
	}
	chart.render();
}
var updateInterval = 2000;
// initial value
var yValue1 = 2500; 
var yValue2 = 2530;
var xValue = 0;
function updateChart(count) {
	count = count || 1;
	var deltaY1, deltaY2;
	for (var i = 0; i < count; i++) {
	xValue += 4;
	deltaY1 = 5 + Math.random() *(-5-5);
	deltaY2 = 5 + Math.random() *(-5-5);
	// adding random value and rounding it to two digits. 
	yValue1 = Math.round(yValue1 + deltaY1);
	yValue2 = Math.round(yValue2 + deltaY2);
	// pushing the new values
	dataPoints1.push({
		x: xValue,
		y: yValue1
	});
	dataPoints2.push({
		x: xValue,
		y: yValue2
	});
	}
	// updating legend text with  updated with y Value 
	chart.options.data[0].legendText = " Engine A - " + yValue1 + " rpm";
	chart.options.data[1].legendText = " Engine B - " + yValue2 + " rpm"; 
	chart.render();
}
// generates first set of dataPoints 
updateChart(20);	
setInterval(function(){updateChart()}, updateInterval);
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
</body>
</html>