Skip to content

Instantly share code, notes, and snippets.

@otfrom
Created February 11, 2013 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otfrom/4754261 to your computer and use it in GitHub Desktop.
Save otfrom/4754261 to your computer and use it in GitHub Desktop.
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
"property" "annual_emissions" "normalised_cost" "monthly_cost" "Gas" "Electrical"
"foo" 9.48120598985868 3.32516229028219 23.4645618950913 0 9.48120598985868
"bar" 13.9329659660887 4.7563711817271 32.8744521510371 1.06302263189102 12.8699433341977
"baz" 14.8021361709446 5.19127050544977 30.8015383323353 0 14.8021361709446
<!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;
}
.line {
fill: none;
stroke: #0F0;
stroke-width: 2;
}
.dot {
stroke: #000;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="stacked-line.js"></script>
var margin = {top: 20, right: 40, bottom: 100, 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 yCost = d3.scale.linear()
.rangeRound([height, 0]);
var color = d3.scale.ordinal()
.range(["#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 yCostAxis = d3.svg.axis()
.scale(yCost)
.orient("right")
.tickFormat(d3.format(".0f"));
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.property); })
.y(function(d) { return yCost(d.normalised_cost); })
.interpolate("step-after");
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.tsv("data.csv", function(error, data) {
color.domain(["Gas", "Electrical"]);
data.forEach(function(d) {
var y0 = 0;
d.emissions = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
d.total = d.emissions[d.emissions.length - 1].y1;
d.normalised_cost = +d.normalised_cost;
});
data.sort(function(a, b) { return a.total - b.total; });
x.domain(data.map(function(d) { return d.property; }));
y.domain([0, 20 + d3.max(data, function(d) { return d.total; })]).nice();
yCost.domain([0, 5 + d3.max(data, function(d) { return d.normalised_cost; })]).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("y", 9)
.attr("x", 0)
.attr("dy", ".71em")
.attr("transform", "rotate(-45)")
.style("text-anchor", "end");
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("kgCO2");
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yCostAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Costs (£)");
var property = svg.selectAll(".property")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.property) + ",0)"; });
property.selectAll("rect")
.data(function(d) { return d.emissions; })
.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); });
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.property); })
.attr("cy", function(d) { return yCost(d.normalised_cost); });
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 - 28)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 34)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment