D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Ljfernando
Full window
Github gist
HW1 Chart3
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis--x path { display: none; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } .grid line { stroke: lightgrey; stroke-opacity: 0.7; shape-rendering: crispEdges; } .grid path { stroke-width: 0; } .area { fill: lightsteelblue; stroke-width: 0; } </style> <svg width="960" height="500"></svg> <script src="//d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 50, right: 80, bottom: 40, left: 50}, width = svg.attr("width") - margin.left - margin.right, height = svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var x = d3.scaleLinear().range([0, width]), y = d3.scaleLinear().range([height, 0]), z = d3.scaleOrdinal(d3.schemeCategory10); var line = d3.line() .curve(d3.curveLinear) .x(function(d) { return x(d.Hour); }) .y(function(d) { return y(d.Count); }); function make_x_gridlines() { return d3.axisBottom(x) .ticks(25) } function make_y_gridlines() { return d3.axisLeft(y) .ticks(7) } var area = d3.area() .x(function(d) { return x(d.Hour); }) .y1(function(d) { return y(d.Count); }); d3.tsv("data.tsv", function(d){ console.log(); d.Count = +d.Count; console.log(d.Count) d.Hour = +d.Hour; return d; }, function(error, data) { if (error) throw error; x.domain(d3.extent(data, function(d) { return d.Hour; })); y.domain([0, d3.max(data, function(d) {return d.Count; })]); area.y0(y(0)); g.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(25)); g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y)) g.append("text") .attr("y", 0- margin.left) .attr("x", 0 - (height/2)) .attr("transform", "rotate(-90)") .attr("dy", "0.71em") .attr("fill", "#000") .text("Number of Calls"); g.append("g") .attr("class", "grid") .call(make_x_gridlines() .tickSize(height) .tickFormat("")) g.append("g") .attr("class", "grid") .call(make_y_gridlines() .tickSize(-width) .tickFormat("")) g.append("path") .datum(data) .attr("fill", "steelblue") .attr("d", area); g.append("text") .attr("x", (width / 2)) .attr("y", 0 - (margin.top / 2)) .attr("text-anchor", "middle") .style("font-size", "20px") .style("font-family", "Times New Roman") .text("SF PD Calls Per Hour in December"); g.append("text") // text label for the x axis .attr("x", (width / 2) ) .attr("y", height + margin.bottom ) .style("text-anchor", "middle") .text("Hour"); g.append("path") .datum(data) .attr("class", "area") .attr("d", area); }); </script>
https://d3js.org/d3.v4.min.js