-
-
Save ToeJamson/8286430 to your computer and use it in GitHub Desktop.
Bitcoin Graph Real-Time
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 pubnub = PUBNUB.init({ | |
subscribe_key: 'sub-c-50d56e1e-2fd9-11e3-a041-02ee2ddab7fe' | |
}); |
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
// Subscribe for new data updates | |
pubnub.subscribe({ | |
channel: 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f', | |
callback: function (message) { | |
addData(message, graphData); | |
graphData = updateData(graphData); | |
updatePrice(message); | |
// Flip the coin! | |
flipCoin(); | |
}, | |
presence: function (presence) { | |
if (updateUsers != null) { | |
updateUsers(presence); | |
} | |
} | |
}); |
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
x.domain(d3.extent(data.map(function(d) { return d.ticker.now; }))); | |
var maximum = d3.max(data.map(function(d) { return d.ticker.avg.value; })), | |
minimum = d3.min(data.map(function (d) { return d.ticker.avg.value; })); | |
y.domain([minimum - 10, maximum + 10]); | |
x2.domain(x.domain()); | |
y2.domain(y.domain()); | |
path1.datum(data) | |
.attr("d", area); | |
gy1.call(yAxis); | |
path2.datum(data) | |
.attr("d", area2); | |
gx2.call(xAxis2); | |
brushed(); |
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
pubnub.subscribe({ | |
"channel" : "d5f06780-30a8-4a48-a2f8-7ed181b4a13f",, | |
"message" : function (message) { | |
console.log( | |
"The current price is: ", | |
message.ticker.avg.value, | |
" recorded at: ", | |
message.ticker.now | |
); | |
} | |
}); |
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
{ | |
"channel":"d5f06780-30a8-4a48-a2f8-7ed181b4a13f", | |
"channel_name":"ticker.BTCUSD", | |
"op":"private", | |
"origin":"broadcast", | |
"private":"ticker", | |
"ticker": { | |
"avg": { | |
"value":"780.00000", | |
"value_int":"78000000", | |
"display":"$780.00000", | |
"display_short":"$780.00", | |
"currency":"USD" | |
}, | |
… rest of tickers … | |
now: “1387324772657959” | |
} | |
} |
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
function HistoryLoader(pubnub, channel) { | |
this.pubnub = pubnub; | |
this.channel = channel; | |
}; | |
// Loads one piece of data for every <increment> and gives it to <callback> | |
HistoryLoader.prototype.loadHistory = function(startDate, increment, callback) { | |
var data = [], that = this; | |
function getHistory(history) { | |
if (history.length === 1) { | |
// Just push one value since we are only requesting one | |
data.push(history[0]); | |
// Increment our date | |
startDate += increment | |
// Request the next value | |
that.getHistory(startDate, 1, getHistory); | |
} else { | |
// We have reached the end | |
callback(data); | |
} | |
}; | |
// Start the recursion | |
this.getHistory(startDate, 1, getHistory); | |
}; | |
// Gets history for the given <date> and <count> | |
HistoryLoader.prototype.getHistory = function(date, count, callback) { | |
// PubNub wants dates with greater precision | |
date *= 10000 | |
pubnub.history({ | |
channel: this.channel, | |
count: count, | |
start: date, | |
reverse: true, | |
callback: function (history) { | |
callback(history[0]); | |
} | |
}); | |
}; |
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
// Load the past 24 hours of history | |
var historyLoader = new HistoryLoader(pubnub, 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f'), | |
oneHour = 1000 * 60 * 60; | |
historyLoader.loadHistory((Date.now() - oneHour * 24), oneHour, function (data) { | |
… Do something with data, load our graph, and listen for updates … | |
}); |
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
// Parse into a Date object and extract float value | |
function convertData(d) { | |
d.ticker.now = new Date(parseFloat(d.ticker.now) / 10000); | |
d.ticker.avg.value = parseFloat(d.ticker.avg.value); | |
}; | |
… | |
data.forEach(convertData); |
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 area = d3.svg.area() | |
.interpolate("monotone") | |
.x(function(d) { return x(d.ticker.now); }) | |
.y0(height) | |
.y1(function(d) { return y(d.ticker.avg.value); }); |
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 brush = d3.svg.brush() | |
.x(x2) | |
.on("brush", brushed); |
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
// Callback for brushing on the minimap graph | |
function brushed() { | |
x.domain(brush.empty() ? x2.domain() : brush.extent()); | |
focus.select("path").attr("d", area); | |
focus.select(".x.axis").call(xAxis); | |
svg.selectAll("circle").attr("cx", function (d) { | |
var cx = x(d.ticker.now) + margin.left; | |
if (cx < margin.left) { | |
cx = -50; | |
} | |
return cx; | |
}).attr("cy", function (d) { return y(d.ticker.avg.value) + margin.top; }); | |
}; |
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
svg.selectAll("circle") | |
.data(data) | |
.enter().append("circle") | |
.attr("r", 5) | |
.attr("cx", function (d) { | |
var cx = x(d.ticker.now) + margin.left; | |
if (cx < margin.left) { | |
cx = -50; | |
} | |
return cx; | |
}) | |
.attr("cy", function (d) { return y(d.ticker.avg.value) + margin.top; }) | |
.style("fill", "black") | |
.style("stroke", "none") | |
.style("pointer-events", "all") | |
.on("mouseover", function (d) { | |
tooltip.transition() | |
.duration(200) | |
.style("opacity", 0.9); | |
tooltip.html(d.ticker.avg.display) | |
.style("left", (d3.event.pageX) + "px") | |
.style("top", (d3.event.pageY - 28) + "px"); | |
}) | |
.on("mouseout", function (d) { | |
tooltip.transition() | |
.duration(500) | |
.style("opacity", 0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment