This example demonstrates D3's new d3.svg.axis
component, which makes it easy to add reference lines and labels to a custom visualization. The axis component even supports automatic transitions! Click on the area to change the scale and watch as new ticks smoothly replace the old ones, or zoom in and out via the mouse scroll wheel or touchpad scroll gesture. Built with D3.js.
xxxxxxxxxx
<html>
<head>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/d3@2.1.3/d3.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/d3@2.1.3/d3.csv.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/d3@2.1.3/d3.time.js"></script>
<style type="text/css">
body {
font: 10px sans-serif;
margin: 0;
}
path.line {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
path.area {
fill: #e7e7e7;
}
.axis {
shape-rendering: crispEdges;
}
.x.axis line {
stroke: #fff;
}
.x.axis .minor {
stroke-opacity: .5;
}
.x.axis path {
display: none;
}
.y.axis line, .y.axis path {
fill: none;
stroke: #000;
}
</style>
</head>
<body>
<script type="text/javascript">
var m = [80, 80, 80, 80], // t/r/b/l margins
w = 960 - m[1] - m[3],
h = 500 - m[0] - m[2],
svg, values, json, max_y,
parse = d3.time.format("%b %Y").parse;
// Scales and axes. Note the inverted domain for the y-scale: bigger is up!
var x = d3.time.scale().range([0, w]),
y = d3.scale.linear().range([h, 0]),
xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true),
yAxis = d3.svg.axis().scale(y).ticks(4).orient("right");
// An area generator, for the light fill.
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y0(h)
.y1(function(d) { return y(d.price); });
// A line generator, for the dark stroke.
var line = d3.svg.line()
.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
d3.csv("readme.csv", rerender);
function rerender(data) {
if (data) json = data; else data = json;
// Filter to one arbitrarily picked symbol
var symbol = data[~~(data.length * Math.random())].symbol;
values = data.filter(function(d) {
return d.symbol == symbol;
});
// Parse dates and numbers. We assume values are sorted by date.
values.forEach(function(d) {
if ('string' === typeof d.date)
d.date = parse(d.date);
d.price = +d.price;
});
// Compute the minimum and maximum date, and the maximum price.
x.domain([values[0].date, values[values.length - 1].date]);
y.domain([0, max_y = d3.max(values, function(d) { return d.price; })]).nice();
// Remove any prior incarnation of the graph (we're reentrant).
d3.select("body svg").remove();
// Add an SVG element with the desired dimensions and margin.
svg = d3.select("body").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
// Add the clip path.
svg.append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("width", w)
.attr("height", h);
// Add the area path.
svg.append("svg:path")
.attr("class", "area")
.attr("clip-path", "url(#clip)")
.attr("d", area(values));
// Add the x-axis.
svg.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
// Add the y-axis.
svg.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(" + w + ",0)")
.call(yAxis);
// Add the line path.
svg.append("svg:path")
.attr("class", "line")
.attr("clip-path", "url(#clip)")
.attr("d", line(values));
// Add a small label for the symbol name.
svg.append("svg:text")
.attr("x", w - 6)
.attr("y", h - 6)
.attr("text-anchor", "end")
.text(values[0].symbol)
.on("click", rerender);
// On click, update the x-axis.
svg.on("click", function() {
var n = values.length - 1,
i = Math.floor(Math.random() * n / 2),
j = i + Math.floor(Math.random() * n / 2) + 1;
x.domain([values[i].date, values[j].date]);
var t = svg.transition().duration(750);
t.select(".x.axis").call(xAxis);
t.select(".area").attr("d", area(values));
t.select(".line").attr("d", line(values));
});
// zoom x axis in and out via scroll wheel.
svg.call(d3.behavior.zoom().on("zoom", redraw));
}
function redraw() {
if (d3.event) d3.event.transform(x, y);
var min = values[0].date
, max = values[values.length - 1].date
, dom = x.domain()
, t = svg.transition().duration(750)
;
// make x domain stay within bounds.
x.domain([ Math.max(min, dom[0])
, Math.min(max, dom[1])
]);
// ...and y domain stay the same.
y.domain([ 0, max_y ]).nice();
t.select(".x.axis").call(xAxis);
t.select(".area").attr("d", area(values));
t.select(".line").attr("d", line(values));
}
</script>
</body>
</html>
Modified http://mbostock.github.com/d3/d3.js?2.0.0 to a secure url
Modified http://mbostock.github.com/d3/d3.csv.js?2.0.0 to a secure url
Modified http://mbostock.github.com/d3/d3.time.js?2.0.0 to a secure url
https://mbostock.github.com/d3/d3.js?2.0.0
https://mbostock.github.com/d3/d3.csv.js?2.0.0
https://mbostock.github.com/d3/d3.time.js?2.0.0