D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
anojoual
Full window
Github gist
etape5
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; margin: auto; position: relative; width: 960px; } text { font: 10px sans-serif; } .axis path { display: none; } .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .group-label { font-weight: bold; text-anchor: end; } form { position: absolute; right: 10px; top: 10px; } </style> <form> <label><input type="radio" name="mode" value="multiples" checked> Multiples</label> <label><input type="radio" name="mode" value="stacked"> Stacked</label> <label><input type="radio" name="mode" value="stream"> Stream</label> </form> <script src="//d3js.org/d3.v3.min.js"></script> <script> var area = d3.svg.area() .interpolate("basis") .x(function(d) { return x(d.date); }) .y1(function(d) { return y(d.price); }); var format = d3.time.format("%b %y").parse; var margin = {top: 10, right: 20, bottom: 20, left: 60}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var format = d3.time.format("%b %Y"); var y0 = d3.scale.ordinal() .rangeRoundBands([height, 0], .2); var y1 = d3.scale.linear(); var duration = 900, delay = 500; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1, 0); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .tickFormat(format); var nest = d3.nest() .key(function(d) { return d.symbol; }); var area = d3.svg.area() .interpolate("basis") .x(function(d) { return x(d.date); }) .y1(function(d) { return y(d.price); }); var stack = d3.layout.stack() .values(function(d) { return d.values; }) .x(function(d) { return d.date; }) .y(function(d) { return d.price; }) .out(function(d, y0) { d.valueOffset = y0; }); var color = d3.scale.category10(); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.csv("stocks.csv", function(error, data) { data.forEach(function(d) { d.date = format.parse(d.date); d.price = +d.price; }); var dataByGroup = nest.entries(data); stack(dataByGroup); x.domain(dataByGroup[0].values.map(function(d) { return d.date; })); y0.domain(dataByGroup.map(function(d) { return d.key; })); y1.domain([0, d3.max(data, function(d) { return d.price; })]).range([y0.rangeBand(), 0]); var symbol = svg.selectAll(".symbol") .data(dataByGroup) .enter().append("g") .attr("class", "symbol") .attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; }); symbol.append("text") .attr("class", "group-label") .attr("x", -6) .attr("y", function(d) { return y1(d.values[0].price / 2); }) .attr("dy", ".35em") .text(function(d) { return "Group " + d.key; }); symbol.selectAll("rect") .data(function(d) { return d.values; }) .enter().append("rect") .style("fill", function(d) { return color(d.symbol); }) .attr("x", function(d) { return x(d.date); }) .attr("y", function(d) { return y1(d.price); }) .attr("width", x.rangeBand()) .attr("height", function(d) { return y0.rangeBand() - y1(d.price); }); symbol.filter(function(d, i) { return !i; }).append("g") .attr("class", "x axis") .attr("transform", "translate(0," + y0.rangeBand() + ")") .call(xAxis); var timeout = setTimeout(function() { d3.select("input[value=\"multiples\"]").property("checked", true).each(change); }, 2000); function change() { clearTimeout(timeout); if (this.value === "multiples") transitionMultiples(); else if (this.value === "stacked") transitionStacked(); else chart(); } function transitionMultiples() { var t = svg.transition().duration(500), g = t.selectAll(".symbol").attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; }); g.selectAll("rect").attr("y", function(d) { return y1(d.price); }); g.select(".group-label").attr("y", function(d) { return y1(d.values[0].price / 2); }) setTimeout(transitionStacked , duration); } function transitionStacked() { var t = svg.transition().duration(500), g = t.selectAll(".symbol").attr("transform", "translate(0," + y0(y0.domain()[0]) + ")"); g.selectAll("rect").attr("y", function(d) { return y1(d.price + d.valueOffset); }); g.select(".group-label").attr("y", function(d) { return y1(d.values[0].price / 2 + d.values[0].valueOffset); }) setTimeout(stream, duration + delay); } function stream() { var t = svg.transition().duration(500), g = t.selectAll(".symbol").attr("transform", "translate(0," + y0(y0.domain()[1]) + ")"); g.selectAll("rect").attr("y", function(d) { return y1(d.price+d.valueOffset)/2; }); g.select(".group-label").attr("y", function(d) { return y1(d.values[0].price / 2 + d.values[0].valueOffset)/2; }) } function chart() { var format = d3.time.format("%b %Y"); var margin = {top: 20, right: 40, bottom: 30, left: 30}; var width = document.body.clientWidth - margin.left - margin.right; var height = 400 - margin.top - margin.bottom; var tooltip = d3.select("body") .append("div") .attr("class", "remove") .style("position", "absolute") .style("z-index", "20") .style("visibility", "hidden") .style("top", "30px") .style("left", "55px"); var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height-10, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .ticks(d3.time.years,1); var yAxis = d3.svg.axis() .scale(y); var yAxisr = d3.svg.axis() .scale(y); var stack = d3.layout.stack() .offset("silhouette") .values(function(d) { return d.values; }) .x(function(d) { return d.date; }) .y(function(d) { return d.price; }); var nest = d3.nest() .key(function(d) { return d.symbol; }); var area = d3.svg.area() .interpolate("cardinal") .x(function(d) { return x(d.date); }) .y0(function(d) { return y(d.y0); }) .y1(function(d) { return y(d.y0 + d.y); }); var svg = d3.select(".chart").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var graph = d3.csv("stocks.csv", function(data) { data.forEach(function(d) { d.date = format.parse(d.date); d.price = +d.price; }); var layers = stack(nest.entries(data)); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(data, function(d) { return d.y0 + d.y; })]); svg.selectAll(".layer") .data(layers) .enter().append("path") .attr("class", "layer") .attr("d", function(d) { return area(d.values); }) .style("fill", function(d) { return color(d.symbol); }) svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + width + ", 0)") .call(yAxis.orient("right")); svg.selectAll(".layer") .attr("opacity", 1) .on("mouseover", function(d, i) { svg.selectAll(".layer").transition() .duration(250) .attr("opacity", function(d, j) { return j != i ? 0.6 : 1; })}) .on("mousemove", function(d, i) { mousex = d3.mouse(this); mousex = mousex[0]; var invertedx = x.invert(mousex); invertedx = invertedx.getYear(); var selected = (d.values); for (var k = 0; k < selected.length; k++) { datearray[k] = selected[k].date datearray[k] = datearray[k].getYear(); } mousedate = datearray.indexOf(invertedx); pro = d.values[mousedate].price; pro2 = d.values[mousedate].symbol; d3.select(this) .classed("hover", true) .attr("stroke", strokecolor) .attr("stroke-width", "0.5px"), tooltip.html( "<p>" + pro2 + "<br>" + pro + "</p>" ).style("visibility", "visible"); }) .on("mouseout", function(d, i) { svg.selectAll(".layer") .transition() .duration(250) .attr("opacity", "1"); d3.select(this) .classed("hover", false) .attr("stroke-width", "0px"), tooltip.html( "<p>" + d.symbol + "<br>" + pro + "</p>" ).style("visibility", "hidden"); }) var vertical = d3.select(".chart") .append("div") .attr("class", "remove") .style("position", "absolute") .style("z-index", "19") .style("width", "1px") .style("height", "380px") .style("top", "10px") .style("bottom", "30px") .style("left", "0px") .style("background", "#fff"); d3.select(".chart") .on("mousemove", function(){ mousex = d3.mouse(this); mousex = mousex[0] + 5; vertical.style("left", mousex + "px" )}) .on("mouseover", function(){ mousex = d3.mouse(this); mousex = mousex[0] + 5; vertical.style("left", mousex + "px")}); }); } }); </script>
https://d3js.org/d3.v3.min.js