D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
cgvarela
Full window
Github gist
Responsive Visualizations: Basic Graph
<!DOCTYPE html> <meta charset="utf-8"> <link href='https://fonts.googleapis.com/css?family=PT+Sans' rel='stylesheet' type='text/css'> <link href='graph.css' rel='stylesheet' type='text/css'> <script src="https://d3js.org/d3.v3.js"></script> <body> <svg id="graph"></svg> <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 + ")"); d3.csv("amzn.csv", function(error, data) { data.forEach(function(d) { d.date = d3.time.format("%Y-%m-%d").parse(d.date); d.close = +d.close; }); 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); }); </script>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js