D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
35degrees
Full window
Github gist
aborted lbs (tried to zoom)
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .area { fill: none; clip-path: url(#clip); } .line { fill: MidnightBlue; stroke: 2px; } .grid line { stroke: lightgrey; stroke-opacity: 0.6; shape-rendering: crispEdges } </style> <svg width="960" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 40}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom; var parseDate = d3.timeParse("%d-%b-%y"); var formatTime = d3.timeFormat("%e %b %y"); var x = d3.scaleTime().range([0, width]), y = d3.scaleLinear().range([height, 0]); var xAxis = d3.axisBottom(x), yAxis = d3.axisLeft(y); var zoom = d3.zoom() .scaleExtent([1, 32]) .translateExtent([[0, 0], [width, height]]) .extent([[0, 0], [width, height]]) .on("zoom", zoomed); var valueline = d3.line() .curve(d3.curveMonotoneX) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.lbs); }); var area = d3.area() .curve(d3.curveMonotoneX) .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return y(d.lbs); }); svg.append("defs").append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); function make_x_gridlines() { return d3.axisBottom(x) .ticks(20) } function make_y_gridlines() { return d3.axisLeft(y) .ticks(12) } d3.tsv("myLbs.tsv", type, function(error, data) { if (error) throw error; x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([200, d3.max(data, function(d) { return d.lbs; })]); svg.append("g") .attr("class","grid") .attr("transform","translate(0," + height + ")") .style("stroke-dasharray",("3,3")) .call(make_x_gridlines() .tickSize(-height) .tickFormat("") ) svg.append("g") .attr("class","grid") .style("stroke-dasharray",("3,3")) .call(make_y_gridlines() .tickSize(-width) .tickFormat("") ) svg.append('path') .data([data]) .attr('class','line') .attr('d',valueline) g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(xAxis); g.append("g") .attr("class", "axis axis--y") .call(yAxis); }); function zoomed() { var t = d3.event.transform, xt = t.rescaleX(x); g.select(".line").attr("d", line.x(function(d) { return xt(d.date); })); g.select(".axis--x").call(xAxis.scale(xt)); } function type(d) { d.date = parseDate(d.date); d.price = +d.lbs; return d; } </script>
https://d3js.org/d3.v4.min.js