https://www.safaribooksonline.com/blog/2014/02/17/building-responsible-visualizations-d3-js/
Open in separate window and resize the graph
xxxxxxxxxx
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=PT+Sans' rel='stylesheet' type='text/css'>
<link href='x_graph.css' rel='stylesheet' type='text/css'>
<script src="https://d3js.org/d3.v3.js"></script>
<body>
<svg id="graph"></svg>
<p>Resize me bellow 300x80</p>
<script>
var margin = 40,
width = 960 - margin*2,
height = 500 - margin*2;
var xScale = d3.time.scale()
.range([0, width])
.nice(d3.time.year);
var yScale = d3.scale.linear()
.range([height, 0])
.nice();
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return xScale(d.date); })
.y(function(d) { return yScale(d.close); });
var graph = d3.select("#graph")
.attr("width", width + margin*2)
.attr("height", height + margin*2)
.append("g")
.attr("transform", "translate(" + margin + "," + margin + ")");
var plotData;
// Read data and render
d3.csv("x_amzn.csv", function(error, data) {
plotData = data;
render(data);
resize();
if (error) {
console.error(error);
}
});
// resize on win resize, Open in new tab to test
d3.select(window).on('resize', resize);
function render (data) {
data.forEach(function(d) {
d.date = d3.time.format("%Y-%m-%d").parse(d.date);
d.close = +d.close;
});
// Update scale's domain for the new min, max
xScale.domain(d3.extent(data, function(d) { return d.date; }));
yScale.domain(d3.extent(data, function(d) { return d.close; }));
graph.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
graph.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("Price ($)");
graph.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
// Create first and last value point
// Hide it. Use on low resolution.
var firstRecord = data[data.length-1];
var lastRecord = data[0];
var first = graph.append("g")
.attr("class", "first")
.style("display", "none");
var last = graph.append("g")
.attr("class", "last")
.style("display", "none");
first.append("text")
.attr("x", -8)
.attr("y", 4)
.attr("text-anchor", "end")
.text("$" + firstRecord.close);
first.append("circle")
.attr("r", 4);
last.append("text")
.attr("x", -8)
.attr("y", 4)
.attr("text-anchor", "start")
.text("$" + lastRecord.close);
last.append("circle")
.attr("r", 4);
}
// Resize the plot
function resize() {
var width = parseInt(d3.select("#graph").style("width")) - margin*2,
height = parseInt(d3.select("#graph").style("height")) - margin*2;
xScale.range([0, width]).nice(d3.time.year);
yScale.range([height, 0]).nice();
// Control the number of ticks. One tick per 50px. Minimum 2 ticks.
yAxis.ticks(Math.max(height/50, 2));
xAxis.ticks(Math.max(width/50, 2));
// Move x axis on the bottom of the graph (set y for the graph height)
// Redraw the x axis after update
graph.select('.x.axis')
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Redraw the y axis after update
graph.select('.y.axis')
.call(yAxis);
// Redraw the line after axis update
graph.selectAll('.line')
.attr("d", line);
// If graph is smaller than 80x300 will drop axis
if (width < 300 && height < 80) {
graph.select('.x.axis').style("display", "none");
graph.select('.y.axis').style("display", "none");
graph.select(".first")
.attr("transform", "translate(" + xScale(xScale.domain()[0]) + "," + yScale(firstRecord.close) + ")")
.style("display", "initial");
graph.select(".last")
.attr("transform", "translate(" + xScale(xScale.domain()[1]) + "," + yScale(lastRecord.close) + ")")
.style("display", "initial");
} else {
graph.select('.x.axis').style("display", "initial");
graph.select('.y.axis').style("display", "initial");
}
}
</script>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js