This line chart is constructed from a TSV file storing the daily average temperatures of New York, San Francisco and Austin over the last year. The chart employs conventional margins and a number of D3 features:
forked from mbostock's block: Multi-Series Line Chart
xxxxxxxxxx
<meta charset="utf-8">
<style>
/* .axis--x path {
display: none;
}
*/
/* .axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.y.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
} */
.line {
fill: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 110, left: 40},
margin2 = {top: 430, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
height2 = +svg.attr("height") - margin2.top - margin2.bottom;
// svg.attr("width", width + margin.left + margin.right)
// .attr("height", height + margin.top + margin.bottom);
var parseTime = d3.timeParse("%Y%m%d");
var x = d3.scaleTime().range([0, width]),
x2 = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
y2 = d3.scaleLinear().range([height2, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);
var xAxis = d3.axisBottom(x),
xAxis2 = d3.axisBottom(x2),
yAxis = d3.axisLeft(y);
function brushed() {
x.domain(d3.event.selection === null ? x2.domain() : brush.extent());
focus.selectAll("path.line").attr("d", function(d) {return line(d.values)});
focus.select(".x.axis").call(xAxis);
focus.select(".y.axis").call(yAxis);
}
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush", brushed);
// var zoom = d3.zoom()
// .scaleExtent([1, Infinity])
// .translateExtent([[0, 0], [width, height]])
// .extent([[0, 0], [width, height]])
// .on("zoom", zoomed);
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temperature); });
var line2 = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temperature); });
// var clip = svg.append("defs").append("svg:clipPath")
// .attr("id", "clip")
// .append("svg:rect")
// .attr("width", width)
// .attr("height", height)
// .attr("x", 0)
// .attr("y", 0);
// var chart = svg.append("g")
// .attr("class", "focus")
// .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
// .attr("clip-path", "url(#clip)");
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
d3.tsv("data.tsv", type, function(error, data) {
if (error) throw error;
var cities = data.columns.slice(1).map(function(id) {
return {
id: id,
values: data.map(function(d) {
return {date: d.date, temperature: d[id]};
})
};
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(cities, function(c) { return console.log('c', c); d3.min(c.values, function(d) { console.log('temp', d.temperature); return d.temperature; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(d) { return d.temperature; }); })
]);
x2.domain(x.domain());
y2.domain(y.domain());
z.domain(cities.map(function(c) { return c.id; }));
var focuslineGroups = focus.selectAll("g")
.data(cities)
.enter().append("g");
var focuslines = focuslineGroups.append("path")
.attr("class","line")
.attr("d", function(d) { console.log(d.values); return line(d.values); })
.style("stroke", function(d) {return z(d.id);})
.attr("clip-path", "url(#clip)")
.call(transition);
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
focus.append("g")
.attr("class", "axis axis--y")
.attr("transform", "translate(10,0)")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Temperature, ºF");
var contextlineGroups = context.selectAll("g")
.data(cities)
.enter().append("g");
var contextLines = contextlineGroups.append("path")
.attr("class", "line")
.attr("d", function(d) { return line2(d.values); })
.style("stroke", function(d) {return z(d.id);})
.attr("clip-path", "url(#clip)");
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "x brush")
.call(brush)
.call(brush.move, x.range());
// chart.append("path")
// .datum(data)
// .attr("class", "line")
// .attr("d", line);
// city.append("path")
// .attr("class", "line")
// .attr("d", function(d) { return line(d.values); })
// .style("stroke", function(d) { return z(d.id); })
// .call(transition);
// city.append("text")
// .datum(function(d) { return {id: d.id, 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", "0.35em")
// .style("font", "10px sans-serif")
// .text(function(d) { return d.id; });
function transition(path) {
path.transition()
.duration(4000).attrTween("stroke-dasharray", tweenDash);
}
function tweenDash() {
var l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function (t) { return i(t); };
}
});
function type(d, _, columns) {
d.date = parseTime(d.date);
for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c];
return d;
}
</script>
https://d3js.org/d3.v4.min.js