Created
April 28, 2013 12:49
-
-
Save takaheraw/5476797 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 fs = require('fs'); | |
var npm = require("npm"); | |
var npmCntData = []; | |
var yyyymmAry = []; | |
var fileName = __dirname + '/npm-pkg-ym.txt'; | |
npm.load(function(err, npm) { | |
if (err) throw err; | |
npm.commands.find([''], true, 600, function(err, datas) { | |
for (var i in datas) { | |
setCounts(datas[i]); | |
} | |
for (var j in yyyymmAry) { | |
var a = []; | |
a.push([j]); | |
a.push([yyyymmAry[j]]); | |
npmCntData.push(a); | |
} | |
fs.writeFile(fileName, JSON.stringify(npmCntData), function(er) { | |
if (err) throw err; | |
console.log('取得と保存完了'); | |
}); | |
}); | |
}); | |
function setCounts(data) { | |
var items = data.time.split('-'); | |
if (isNaN(items[0])) return; | |
var ym = '' + items[0] + items[1]; | |
if (yyyymmAry[ym] === undefined) { | |
yyyymmAry[ym] = 0; | |
} else { | |
yyyymmAry[ym]++; | |
} | |
} |
This file contains hidden or 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
<script src="http://ccchart.com/js/ccchart.js" charset="utf-8"></script> | |
<div class="flex charts"> | |
<dic class="chart"><canvas id="hoge"></canvas></div> | |
</div> | |
<script> | |
var chartdata = { | |
"config": { | |
"title" : "npm パッケージ 月次登録数推移", | |
"subTitle" : "各月毎のNode.js用 npm パッケージ登録数推移 --" + Date(), | |
"width" : (window.innerWidth - 60) / 1, | |
"height" : (window.innerHeigth - 60) / 1.5, | |
"type" : "bar", | |
"minY" : 0, | |
"xScaleSkip" : 2, | |
"maxWsColLen" : 25, | |
"useVal" : "yes", | |
"bg" : "#fff", | |
"textColors" : {"title" : "#555", | |
"subTitle" : "#555", | |
"hanrei" : "#777", | |
"unit": "#777"}, | |
"colorSet" : ["#DDA0DD", "#3CB000"], | |
"shadows" : {"hanrei" : ['#aaa', 5, 5, 5], | |
"bar" : ['#bbb', 3, 3, 8]} | |
}, | |
"data": [ | |
["年月"], | |
["登録数"] | |
] | |
} | |
ccchart | |
.init('hoge', chartdata) | |
.ws('ws://localhost:8020') | |
.on('message', ccchart.wscase.oneColAtATime); | |
</script> |
This file contains hidden or 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 fs = require('fs'); | |
var WsServer = require('ws').Server; | |
var ws = new WsServer({port: 8020}); | |
var fileName = __dirname + '/npm-pkg-ym.txt'; | |
ws.on('connection', function(socket) { | |
fs.readFile(fileName, function(err, data) { | |
if (err) throw err; | |
data = JSON.parse(data.toString()); | |
sendData(socket, data); | |
}); | |
}); | |
function sendData(socket, data) { | |
var tid = 0; | |
var i =0; | |
var dly = 100; | |
tid = setInterval(function() { | |
var d = data[i]; | |
i++; | |
if (i > data.length -1) { | |
clearInterval(tid); | |
} | |
socket.send(JSON.stringify(d)); | |
}, 100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment