D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sarubenfeld
Full window
Github gist
2013 San Francisco Weather
<!DOCTYPE html> <meta charset="utf-8"> <link href="https://fonts.googleapis.com/css?family=Bungee+Hairline" rel="stylesheet"> <style> body { font-family: 'Bungee Hairline', cursive; text-anchor: middle } .axis { font-family: Futura; font-size: 8px; text-anchor: middle } .axis-title { text-anchor: end; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .axis--x path { display: none; } .axis--y .tick:not(.tick--one) line { stroke-opacity: .15; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; stroke-linejoin: round; stroke-linecap: round; } </style> <body> <div class="g-chart-container"> <h2> 2013 San Francisco Weather </h2> </div> </body> <script src="//d3js.org/d3.v3.min.js"></script> <script> // credit to mbostock for his "Percentage Change" example: https://bl.ocks.org/mbostock/c69f5960c6b1a95b6f78 var margin = {top: 30, right: 30, bottom: 40, left: 50}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var formatPercent = d3.format("+.0%"), formatChange = function(x) { return formatPercent(x - 1); }, parseDate = d3.time.format("%Y-%m-%d").parse; var x = d3.time.scale() .range([0, width]); var y = d3.scale.log() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickSize(-width, 0) .tickFormat(formatChange); var line = d3.svg.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.ratio); }); var svg = d3.select("body").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 + ")"); var gX = svg.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")"); var gY = svg.append("g") .attr("class", "axis axis--y"); gY.append("text") .attr("class", "axis-title") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .text("Change in Temp"); d3.csv("maxf.csv", function(error, data) { if (error) throw error; var baseValue = +data[0].temp; data.forEach(function(d) { d.date = parseDate(d.date); d.ratio = d.temp / baseValue; }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain(d3.extent(data, function(d) { return d.ratio; })); yAxis.tickValues(d3.scale.linear() .domain(y.domain()) .ticks(20)); gX.call(xAxis); gY.call(yAxis) .selectAll(".tick") .classed("tick--one", function(d) { return Math.abs(d - 1) < 1e-6; }); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line); }); </script>
https://d3js.org/d3.v3.min.js