D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Ljfernando
Full window
Github gist
HW1 Chart4
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis--x path { display: none; } .line { fill: none; stroke: lightsteelblue; stroke-width: 1.5px; } .grid line { stroke: lightgrey; stroke-opacity: 0.7; shape-rendering: crispEdges; } .grid path { 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: 40, right: 30, bottom: 30, 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.Day); }) .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) } d3.tsv("data.tsv", function(d){ console.log(); d.Count = +d.Count; console.log(d.Count) d.Day = +d.Day; return d; }, function(error, data) { if (error) throw error; x.domain(d3.extent(data, function(d) { return d.Day; })); y.domain([0, d3.max(data, function(d) {return d.Count; })]); 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", "none") .attr("stroke", "steelblue") .attr("stroke-width", 3.0) .attr("d", line); 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("PD Calls Per Day of December in Southern District"); }); </script>
https://d3js.org/d3.v4.min.js