D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jfourmond
Full window
Github gist
Graphe & Date
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } .domain { display: none; } .YAxis line { stroke: #BDBDBD; stroke-opacity: 0.3; } </style> </head> <body> <svg width="960" height="500" ></svg> <script> const svg = d3.select("svg"); const margin = {top:20, right:20, bottom:30, left:50}; const width = svg.attr("width") - margin.left - margin.right; const height = svg.attr("height") - margin.top - margin.bottom; const g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")") const x = d3.scaleTime().rangeRound([0, width]); const y = d3.scaleLinear().rangeRound([height, 0]); const parseDate = d3.timeParse("%Y-%m-%d"); const displayDate = d3.timeFormat("%d-%m-%Y"); const displayValue = d3.format(",.0f"); let data = []; let years; d3.queue() .defer(d3.json, "data.json") .await(process); var line = d3.line() .x(function(d) { return x(d.year); }) .y(function(d) { return y(d.value); }); let yAxis; let xAxis; function process(error, json) { if(error) throw error; years = Object.keys(json); for(year of years) { data.push({year:parseDate(year), value:json[year]}); } // console.log("JSON : %O", json); // console.log("YEARS : %O", years); // console.log("DATA : %O", data); draw(); } function draw() { x.domain(d3.extent(data, function(d) { return d.year; })); y.domain([0, d3.max(data, function(d) { return d.value; })]); xAxis = g.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).tickFormat(d3.timeFormat("%B %d"))) .select(".domain") .remove(); yAxis = g.append("g") .attr("class", "YAxis") .call(d3.axisLeft(y).tickSize(-width)) .append("text") .attr("x", 10) .attr("y", y(y.ticks().pop()) + 0.5) .attr("dy", "0.32em") .attr("fill", "#000") .attr("font-weight", "bold") .attr("text-anchor", "start") .text("Nombre"); g.append("path") .datum(data) .attr("fill", "none") .attr("stroke", "steelblue") .attr("stroke-linejoin", "round") .attr("stroke-linecap", "round") .attr("stroke-width", 1.5) .attr("d", line); } </script> </body>
https://d3js.org/d3.v4.min.js