var hashDelimiter = ',';
var maxDataPoints = 1500; // How many datapoints before we start erasing them
var chartType = 'area';
var snmpProcessor = 'https://api.myjson.com/bins/8se3o';
var invertInOut = false;
// Global variables
var charts = null;
var chartOptions = null;
var chartData = null;
$("#title").hide();
$("#content").html('').addClass('fullChart');
fixBodyHeight();
$( window ).resize(fixBodyHeight);
getChartData();
// Load the Chart
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(function () {
chartData = new google.visualization.DataTable();
chartData.addColumn('datetime', 'Time');
chartData.addColumn('number', 'Traffic In', 'style');
chartData.addColumn('number', 'Traffic Out');
chartOptions = {
title: name,
curveType: 'function',
legend: {
position: 'top',
alignment: 'end',
},
vAxis: {
format: '# Mbps',
minValue: 0,
},
hAxis: {
},
colors: ['#0f0', '#00f'],
series: {
0: {
'line-width': 3,
},
1: {
'line-width': 1,
},
},
pointSize: 4,
chartArea: {
right: 5,
left: '7%',
bottom: 20,
top: 60,
},
};
if (chartType == 'area') {
chartOptions.pointSize = 0;
charts = new google.visualization.AreaChart(document.getElementById('content'));
} else {
charts = new google.visualization.LineChart(document.getElementById('content'));
}
charts.draw(chartData, chartOptions);
});
function getChartData() {
$.get(snmpProcessor, function( d ) {
$.each(d, function(index, element) {
if (element.inDiff > 0 && element.outDiff > 0) {
if (invertInOut) {
addChartPoint(new Date(element.time * 1000), element.timeDiff, element.outDiff, element.inDiff);
} else {
addChartPoint(new Date(element.time * 1000), element.timeDiff, element.inDiff, element.outDiff);
}
}
});
});
}
function addChartPoint(time, timeDiff, dataIn, dataOut) {
dataIn = dataIn / 100000 / timeDiff;
dataOut = dataOut / 100000 / timeDiff;
//dataIn = (dataIn*8) / (1024*1024) / timeDiff;
//dataOut = (dataOut*8) / (1024*1024) / timeDiff;
if (chartData) {
chartData.addRow([time, dataIn, dataOut]);
if (chartData.getNumberOfRows() > maxDataPoints) {
chartData.removeRows(0, chartData.getNumberOfRows() - maxDataPoints + 1);
}
charts.draw(chartData, chartOptions);
}
}
function fixBodyHeight() {
$("#content").css('height', $( window ).innerHeight()+'px');
if (chartData) {
charts.draw(chartData, chartOptions);
}
}
function parseQueryString() {
var queryString = location.search.substr(1);
var params = {}, queries, temp, i, l;
queries = queryString.split("&");
for ( i = 0, l = queries.length; i < l; i++ ) {
temp = queries[i].split('=');
params[temp[0]] = temp[1];
}
return params;
}
html, body {
margin: 0;
padding: 0;
}
.click {
cursor: pointer;
}
.fullChart {
width: 100%;
height: 100%;
overflow: hidden;
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<h1 id="title"></h1>
<div id="content"></div>