In response to mbostock's block: Local Variables and the Tweet I implemented a little variation having a .local(name[, value)
method you can use on selections.
On set, the value is stored on the given element:
element.local("foo", value);
On get, the value is retrieved from given element, or the nearest ancestor that defines it:
var value = element.local("foo");
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
margin: 0;
}
.line {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
.area {
fill: #e7e7e7;
}
text {
text-anchor: end;
}
</style>
<body>
<script src="//d3js.org/d3.v4.0.0-alpha.44.js"></script>
<script src="d3-local.js"></script>
<script>
var margin = {top: 8, right: 10, bottom: 2, left: 10},
width = 960 - margin.left - margin.right,
height = 69 - margin.top - margin.bottom;
var parseDate = d3.timeParse("%b %Y");
var x = d3.scaleTime()
.range([0, width]);
d3.tsv("stocks.tsv", type, function(error, data) {
if (error) throw error;
var symbols = d3.nest()
.key(function(d) { return d.symbol; })
.entries(data);
x.domain([
d3.min(symbols, function(symbol) { return symbol.values[0].date; }),
d3.max(symbols, function(symbol) { return symbol.values[symbol.values.length - 1].date; })
]);
var svg = d3.select("body").selectAll("svg")
.data(symbols)
.enter().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 + ")")
.local("ty", function(d){
return d3.scaleLinear()
.domain([0, d3.max(d.values, function(d) { return d.price; })])
.range([height, 0]);
})
.local("area", function(d){
var ty = d3.select(this).local("ty");
return d3.area()
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return ty(d.price); });
})
.local("line", function(d){
var ty = d3.select(this).property("ty");
return d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return ty(d.price); });
})
svg.append("path")
.attr("class", "area")
.attr("d", function(d) {
return d3.select(this).local("area")(d.values)
});
svg.append("path")
.attr("class", "line")
.attr("d", function(d) {
return d3.select(this).local("line")(d.values);
});
svg.append("text")
.attr("x", width - 6)
.attr("y", height - 6)
.text(function(d) { return d.key; });
});
function type(d) {
d.price = +d.price;
d.date = parseDate(d.date);
return d;
}
</script>
https://d3js.org/d3.v4.0.0-alpha.44.js