Last active
December 13, 2015 18:18
-
-
Save typeofgraphic/4953812 to your computer and use it in GitHub Desktop.
Multi-line chart based on Bostock's (http://bl.ocks.org/3884955). The data file is CSV with (discreet) integer values for the x axis, where the original was a tsv with dates for the x axis. There is an error (i think) between Line 66 (the Pages function) and Line 105 which attempts to parse the Pages values to the line path. However, it is not h…
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
Occurance | a | b | c | d | d | f | g | h | i | j | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 166292 | 30212 | 123040 | 27224 | 5992 | 276200 | 80 | 4408 | 246852 | 34932 | |
2 | 87256 | 22004 | 33608 | 16004 | 3316 | 125656 | 252 | 5120 | 147444 | 15652 | |
3 | 48932 | 20280 | 40816 | 11872 | 1948 | 72856 | 188 | 4588 | 99108 | 9016 |
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
display: none; | |
} | |
.line { | |
fill: none; | |
stroke: steelblue; | |
stroke-width: 1.5px; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script> | |
var margin = {top: 20, right: 80, bottom: 30, left: 50}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.linear() | |
.range([0, width]); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var color = d3.scale.category10(); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
var line = d3.svg.line() | |
.interpolate("basis") | |
.x(function(d) { return x(d.Occurance); }) | |
.y(function(d) { return y(d.count); }); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
d3.csv("data.csv", function(error, data) { | |
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Occurance"; })); | |
console.log(color.domain()); | |
var pages = color.domain().map(function(name) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
return {occurance: d.Occurance, count: +d[name]}; | |
}) | |
}; | |
}); | |
console.log(pages); | |
x.domain(d3.extent(data, function(d) { return d.Occurance; })); | |
y.domain([ | |
d3.min(pages, function(c) { return d3.min(c.values, function(v) { return v.count; }); }), | |
d3.max(pages, function(c) { return d3.max(c.values, function(v) { return v.count; }); }) | |
]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("count RTB"); | |
var page = svg.selectAll(".page") | |
.data(pages) | |
.enter().append("g") | |
.attr("class", "page"); | |
page.append("path") | |
.attr("class", "line") | |
.attr("d", function(d) { return line(d.values); }) | |
.style("stroke", function(d) { return color(d.name); }); | |
page.append("text") | |
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) | |
.attr("transform", function(d) { return "translate(" + x(d.value.occurance) + "," + y(d.value.count) + ")"; }) | |
.attr("x", 3) | |
.attr("dy", ".35em") | |
.text(function(d) { return d.name; }); | |
}); | |
</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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.bar { | |
fill: steelblue; | |
} | |
.x.axis path { | |
display: none; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1); | |
var y = d3.scale.linear() | |
.rangeRound([height, 0]); | |
var color = d3.scale.ordinal() | |
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.tickFormat(d3.format(".2s")); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
d3.csv("proportions_count_rtb.csv", function(error, data) { | |
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Occurance"; })); | |
data.forEach(function(d) { | |
var y0 = 0; | |
d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; }); | |
d.total = d.ages[d.ages.length - 1].y1; | |
d.Occurance = d3.format("d"); | |
console.log(d.Occurance); | |
}); | |
data.sort(function(a, b) { return b.total - a.total; }); | |
x.domain(data.map(function(d) { return d.Occurance; })); | |
y.domain([0, d3.max(data, function(d) { return d.total; })]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("RTB"); | |
var state = svg.selectAll(".state") | |
.data(data) | |
.enter().append("g") | |
.attr("class", "g") | |
.attr("transform", function(d) { return "translate(" + x(d.Occurance) + ",0)"; }); | |
state.selectAll("rect") | |
.data(function(d) { return d.ages; }) | |
.enter().append("rect") | |
.attr("width", x.rangeBand()) | |
.attr("y", function(d) { return y(d.y1); }) | |
.attr("height", function(d) { return y(d.y0) - y(d.y1); }) | |
.style("fill", function(d) { return color(d.name); }); | |
var legend = svg.selectAll(".legend") | |
.data(color.domain().slice().reverse()) | |
.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); | |
legend.append("rect") | |
.attr("x", width - 18) | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", width - 24) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "end") | |
.text(function(d) { return d; }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment