Skip to content

Instantly share code, notes, and snippets.

@cgroll
Last active August 29, 2015 14:16
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 cgroll/b9542e84486a59e2a149 to your computer and use it in GitHub Desktop.
Save cgroll/b9542e84486a59e2a149 to your computer and use it in GitHub Desktop.
Multi-series line chart with breaks

The plot shows multiple time series with missing values, where missing values are visualized by line breaks.

You can view a rendered version of this gist at bl.ocks.org.

// define margins
var margin = {top: 20, right: 80, bottom: 30, left: 150};
// graphics size without axis
var width = 960 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;
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 + ")");
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.log()
.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")
.ticks(5);
var parseDate = d3.time.format("%Y-%m-%d").parse;
var line = d3.svg.line()
.defined(function(d) { return !isNaN(d.gdp); })
.interpolate("basis")
.x(function(d) { return x(d.idx); })
.y(function(d) { return y(d.gdp); });
var tsdata = d3.csv("/cgroll/raw/491344b798af8dcc8dc4/gdp_us_br_af.csv", function (data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "idx"; }));
data.forEach(function(d) {
d.idx = parseDate(d.idx);
});
var tseries = color.domain().map(function(name) {
countryData = data.map(function(d) {
return {idx: d.idx, gdp: +d[name]/1000000000};
})
return {name: name,
values: countryData
};
});
x.domain(d3.extent(data, function(d) { return d.idx; }));
y.domain([
d3.min(tseries, function(c) { return d3.min(c.values, function(v) { return v.gdp; }); }),
d3.max(tseries, function(c) { return d3.max(c.values, function(v) { return v.gdp; }); })
]);
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("GDP in bn $");
var gdp = svg.selectAll(".gdp")
.data(tseries)
.enter().append("g")
.attr("class", "gdp");
gdp.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); });
// gdp.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.date) + "," + y(d.value.temperature) + ")"; })
// .attr("x", 3)
// .attr("dy", ".35em")
// .text(function(d) { return d.name; });
});
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 16px 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;
}
.grid .tick {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid path {
stroke-width: 0;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8">
</script>
<script src="assembly_code.js">
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment