Created
February 11, 2015 17:14
-
-
Save lnmunhoz/203b43cb62ac8d57e6a2 to your computer and use it in GitHub Desktop.
RealTime chart with ChartJS, Socket.io and KnockoutJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta name="author" content="lnmunhoz"> | |
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.1.0/knockout-min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1/Chart.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.3/socket.io.min.js"></script> | |
<title>Chart.js with Socket.io</title> | |
</head> | |
<body> | |
<div> | |
<canvas id="canvas" height="450" width="600"></canvas> | |
</div> | |
<script type="text/javascript"> | |
function MainViewModel(data) { | |
var self = this; | |
var socket = io.connect('localhost:8080'); | |
self.lineChartData = ko.observable({ | |
labels : ["January","February","March","April","May","June","July"], | |
datasets : [ | |
{ | |
fillColor : "rgba(151,187,205,0.5)", | |
strokeColor : "rgba(151,187,205,1)", | |
pointColor : "rgba(151,187,205,1)", | |
pointStrokeColor : "#fff", | |
data : [65,59,90,81,56,55,40] | |
} | |
] | |
}); | |
socket.on('pushdata', function (data) { | |
self.lineChartData().datasets[0].data.shift(); | |
self.lineChartData().datasets[0].data.push(data); | |
self.initLine(); | |
}); | |
self.initLine = function() { | |
var options = { | |
animation : false, | |
scaleOverride : true, | |
scaleSteps : 10, // The number of steps in a hard coded scale | |
scaleStepWidth : 10, // The value jump in the hard coded scale | |
scaleStartValue : 10 // The scale starting value | |
}; | |
var ctx = $("#canvas").get(0).getContext("2d"); | |
var myLine = new Chart(ctx).Line( vm.lineChartData(), options ); | |
} | |
} | |
var vm = new MainViewModel(); | |
ko.applyBindings(vm); | |
vm.initLine(); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "realTimeChart", | |
"version": "1.0.0", | |
"description": "Real Time Chart", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "lnmunhoz", | |
"license": "MIT", | |
"dependencies": { | |
"express": "^4.11.2", | |
"socket.io": "^1.3.3" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('express')(); | |
var server = require('http').Server(app); | |
var io = require('socket.io')(server); | |
server.listen(8080); | |
console.log('Server listening on port :8080'); | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
io.on('connection', function (socket) { | |
setInterval(function(){ | |
var data = getRandomInt(0,100); | |
io.sockets.emit('pushdata', data); | |
},2000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment