var fs = require('fs') var path = require('path') var pull = require('pull-stream') var File = require('pull-file') var Split = require('pull-split') var PNG = require('pngjs').PNG var common = require('./common') var start = common.startTime var startDay = Math.floor(start / 86400000) var startWeek = Math.floor(start / 86400000 / 7) var startMonth = Math.floor(start / 86400000 / 30.43666666666666666666) var startT var now = Date.now() var nowDay = Math.floor(now / 86400000) var nowWeek = Math.floor(now / 86400000 / 7) var nowMonth = Math.floor(now / 86400000 / 30.43666666666666666666) var series = process.argv[2] if (!series) throw new TypeError('Missing series argument') if (series === 'day') { width = nowDay - startDay startT = startDay } else if (series === 'week') { width = nowWeek - startWeek startT = startWeek } else if (series === 'month') { width = nowMonth - startMonth startT = startMonth } else { throw new TypeError('Invalid series') } var info = JSON.parse(fs.readFileSync(path.join(__dirname, series + '.json'))) var info1 = JSON.parse(fs.readFileSync(path.join(__dirname, 'ids-colors.json'))) var ids = info1.ids var userColors = info1.colors var width var height = info.max var png = new PNG({ width: width, height: height, colorType: 6 }) var userNumbers = {} for (var i = 0; i < ids.length; i++) { var id = ids[i] userNumbers[id] = i } function setPixel(data, x, y, color) { var i = (y * width + x) << 2 data[i] = color[0] data[i+1] = color[1] data[i+2] = color[2] data[i+3] = color[3] } function compareIds(a, b) { return userNumbers[a] - userNumbers[b] } pull( File(path.join(__dirname, series + '.jsonp')), Split(), pull.filter(Boolean), pull.map(JSON.parse), pull.drain(function (interval) { var x = interval.time - startT var ids = interval.ids.sort(compareIds) for (var i = 0; i < ids.length; i++) { var id = ids[i] var y = height - i var color = userColors[id] || [0, 0, 0, 255] setPixel(png.data, x, y, color) } }, function (err) { if (err) throw err     png.pack().pipe(fs.createWriteStream(series + '.png')) }) )