IEX API
Last active
March 15, 2017 03:29
-
-
Save iexviz/d807c0ce4325459b3db49663aa6a6cd0 to your computer and use it in GitHub Desktop.
IEX Historical - 60 Day HBar Chart
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
height: 930 | |
license: gpl-3.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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.bar rect { | |
fill: orange; | |
} | |
.bar text.value { | |
fill: black; | |
} | |
.axis { | |
shape-rendering: crispEdges; | |
} | |
.axis path { | |
fill: none; | |
} | |
.x.axis line { | |
stroke: #fff; | |
stroke-opacity: .8; | |
} | |
.y.axis path { | |
stroke: black; | |
} | |
</style> | |
<body align=center> | |
<span class=dropdown></span><span class=dropdown2></span><br> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script src="jsonp.js"></script> | |
<script> | |
d3.jsonp("https://api.iextrading.com/1.0/stats/historical/daily?callback=visualise&last=60"); | |
function visualise(data){ | |
var margin = {top: 30, right: 300, bottom: 0, left: 120}, | |
w = 960 - margin.left - margin.right, | |
h = 3000 - margin.top - margin.bottom; | |
m = [30,300,0,120] | |
var x = d3.scale.linear().range([0, w]), | |
y = d3.scale.ordinal().rangeRoundBands([0, h/4], .1); | |
var xAxis = d3.svg.axis().scale(x).orient("top").tickSize(-h), | |
yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0); | |
var format = d3.format(",.0f"); | |
var format2 = d3.format(",.2f"); | |
data.forEach(function(d) { | |
d.name = d.date; | |
d.marketShare = (d.marketShare*100).toFixed(3); | |
}); | |
var data2=data | |
var option = ["volume", "litVolume", "routedVolume", "marketShare"] | |
var key = option[0] | |
var option2 = ["sortvalue", "sortkey"] | |
var key2 = option2[0] | |
var select = d3.select('.dropdown') | |
.append('select') | |
.style('font-size', '20px') | |
.attr('class','select') | |
.on('change',onchange); | |
var options = select | |
.selectAll('option') | |
.data(option).enter() | |
.append('option') | |
.text(function (d) { return d; }); | |
function onchange() { | |
updateviz(d3.select('.select').property('value'),d3.select('.select2').property('value')) | |
}; | |
var select2 = d3.select('.dropdown2') | |
.append('select') | |
.style('font-size', '20px') | |
.attr('class','select2') | |
.on('change',onchange2); | |
var options = select2 | |
.selectAll('option2') | |
.data(option2).enter() | |
.append('option') | |
.text(function (d) { return d; }); | |
function onchange2() { | |
updateviz(d3.select('.select').property('value'),d3.select('.select2').property('value')) }; | |
function updateviz(key,key2) { | |
data=[] | |
data2.forEach(function(d){data.push(d)}); | |
d3.select("svg").remove(); | |
data.forEach(function(d) { | |
d.value = d[key]; | |
}); | |
var svg = d3.select("body").append("svg") | |
.attr("width", w + m[1] + m[3]) | |
.attr("height", h + m[0] + m[2]) | |
.append("g") | |
.attr("transform", "translate(" + m[3] + "," + m[0] + ")"); | |
// Parse numbers, and sort by value. | |
data.forEach(function(d) { d.value = +d.value; }); | |
if(key2 == "sortvalue") { | |
data.sort(function(a, b) { return b.value - a.value; }); | |
} | |
// Set the scale domain. | |
x.domain([0, d3.max(data, function(d) { return d.value; })]); | |
y.domain(data.map(function(d) { return d.name; })); | |
var bar = svg.selectAll("g.bar") | |
.data(data) | |
.enter().append("g") | |
.attr("class", "bar") | |
.attr("transform", function(d) { return "translate(0," + y(d.name) + ")"; }); | |
bar.append("rect") | |
.attr("width", function(d) { return x(d.value); }) | |
.attr("height", y.rangeBand()); | |
bar.append("text") | |
.attr("class", "value") | |
.attr("x", function(d) { return x(d.value); }) | |
.attr("y", y.rangeBand() / 2) | |
.attr("dx", -3) | |
.attr("dy", ".35em") | |
.attr("text-anchor", "end") | |
.text(function(d) { | |
if(key == "marketShare") { return format2(d.value)+"%"} | |
return format(d.value); }); | |
svg.append("g") | |
.attr("class", "x axis") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
} | |
updateviz(key,key2) | |
} | |
</script> |
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
d3.jsonp = function (url, callback) { | |
function rand() { | |
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | |
c = '', i = -1; | |
while (++i < 15) c += chars.charAt(Math.floor(Math.random() * 52)); | |
return c; | |
} | |
function create(url) { | |
var e = url.match(/callback=d3.jsonp.(\w+)/), | |
c = e ? e[1] : rand(); | |
d3.jsonp[c] = function(data) { | |
callback(data); | |
delete d3.jsonp[c]; | |
script.remove(); | |
}; | |
return 'd3.jsonp.' + c; | |
} | |
var cb = create(url), | |
script = d3.select('head') | |
.append('script') | |
.attr('type', 'text/javascript') | |
.attr('src', url.replace(/(\{|%7B)callback(\{|%7D)/, cb)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment