D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
JulesSauvinet
Full window
Github gist
Simple line chart
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v3.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } .line { fill: none; stroke: black; stroke-width: 1.5px; } </style> </head> <body> <script> // Feel free to change or delete any of the code you see in this editor! var svg = d3.select("body").append("svg") .attr("width", 960) .attr("height", 500) var width = 800; var height = 400; // https://bl.ocks.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/ var parseDate = d3.time.format("%Y-%m"); var displayDate = d3.time.format("%b %y"); var displayValue = d3.format(",.0f"); // Ordinal scale var x = d3.scale.ordinal() .rangeRoundBands([0, width], .5); var y = d3.scale.linear() .range([height, height - 200]); var line = d3.svg.line() .x(function(d) { return x(d.name); }) .y(function(d) { return y(d.value); }); var g = svg.append("g") .attr("transform", "translate(50, 0)") d3.json("dataset.json", function(data) { // Pre-processing data.forEach(function(d) { d.value;// = +d.value; d["date"] = parseDate.parse(d["date"]); }); x.domain(data.map(function(d) { return d.name; })); y.domain([0, d3.max(data, function(d) { return d.value; })]); svg.selectAll("text").data(data).enter() .append("text") .text(function(d, i) { return displayDate(d.date); }) .attr("y", 420) .attr("x", function(d) { return x(d.name); }) .style("font-size", 10) .style("font-family", "monospace"); g.selectAll(".value").data([data[data.length -1]]).enter() .append("text") .text(function(d, i) { return displayValue(d.value); }) .attr("class", "value") .attr("y", function(d) { return y(d.value); }) .attr("x", width - 20) .style("font-size", 20) .style("font-family", "monospace"); g.selectAll("path").data([data]).enter().append("path") .attr("class", "line") .attr("d", line); var circles = g.selectAll('circle').data(data); var nodes = g.selectAll("g") .data(data).enter() .append("g"); nodes.append('circle').attr('class', 'circle') .attr("cx", function(d, i) { return x(d.name); }) .attr("cy", function(d, i) { return y(d.value); }) .attr("r", 10) .style("fill", "grey") .style( "opacity", 0.25) nodes.append('text').attr('style', 'display:none;') .text(function(d) { return d.value; }) .attr('x', function(d) { return x(d.name); }) .attr('y', function(d) { return y(d.value) - 20; }); nodes.append("path") .attr("id", function(d,i) { return "path"+ d.name; }) .attr("class", "line") .attr('style', 'display:none;') nodes.selectAll('.circle').on('mouseover', function( d, i) { d3.select(this).style("fill", "red").style( "opacity", 0.9); nodes.selectAll("text").filter(function(e) { return d.name === e.name; }) .attr('style', 'display:inline;') .style("font-size", function(d,i){return 14;}) .style("font-family", "monospace"); var tmpLine = [ { "x": x(d.name), "y": 400}, { "x": x(d.name), "y": y(d.value)} ]; var lineFunction = d3.svg.line() .x(function(d) { return d.x; }) .y(function(d) { return d.y; }) .interpolate("linear"); nodes.selectAll("path").attr("d", lineFunction(tmpLine)) .filter(function(e) { return d.name === e.name; }) .attr('style', 'display:inline;') .style("stroke-dasharray", ("3, 3")) }); nodes.selectAll('.circle').on("mouseout", function( data, index) { d3.select(this).transition().duration(100).style("fill", "grey").style( "opacity", 0.25); nodes.selectAll("text").transition().duration(100).attr('style', 'display:none;'); nodes.selectAll("path").transition().duration(100).attr('style', 'display:none;'); }) }); </script> </body>
https://d3js.org/d3.v3.min.js