Created
January 24, 2016 02:59
-
-
Save a-x-/07ded7324bdbc475f5e6 to your computer and use it in GitHub Desktop.
minimal d3 example for console running
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
// init | |
var cl = console.log.bind(console); | |
var loadJs = src => new Promise((res, rej) => { | |
var script = document.createElement('script'); | |
script.src = src; | |
script.onload=()=>{cl('loaded', src); res(src)}; | |
document.head.appendChild(script); | |
}); | |
var createDiv = className => { | |
var div = document.createElement("div"); | |
document.body.appendChild(div); | |
div.className = className; | |
}; | |
var insertStyles = styles => { | |
var style = document.createElement("style"); | |
document.body.appendChild(style); | |
style.innerText = styles; | |
}; | |
// load libs | |
Promise.all([ | |
loadJs('https://cdnjs.cloudflare.com/ajax/libs/jstat/1.5.2/jstat.min.js'), | |
loadJs('https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.13/d3.min.js'), | |
loadJs('https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.0.0/lodash.min.js') | |
]) | |
.then(()=>{ | |
createDiv('chart'); | |
var data = [4, 8, 15, 16, 23, 42]; | |
var x = d3.scale.linear() | |
.domain([0, d3.max(data)]) | |
.range([0, 420]); | |
d3.select(".chart") | |
.selectAll("div") | |
.data(data) | |
.enter().append("div") | |
.style("width", function(d) { return x(d) + "px"; }) | |
.text(function(d) { return d; }); | |
}); | |
insertStyles | |
(` | |
.chart div { | |
font: 10px sans-serif; | |
background-color: steelblue; | |
text-align: right; | |
padding: 3px; | |
margin: 1px; | |
color: white; | |
} | |
`) |
Author
a-x-
commented
Jan 24, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment