D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
riff4
Full window
Github gist
Lecture 3 HW - version 0.1
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; } </style> </head> <body> <script> var data=d3.csv('stocks.csv',function(error, data){ var dataset=data; data.forEach(function(d){ d.price = +d.price; }) var color = d3.scaleOrdinal(d3.schemeCategory20); var svg = d3.select("body").append("svg") .attr("width", 960) .attr("height", 500) .on("click",function(){ var opp_hor = (d3.select("rect.horizontal").style("opacity")); var opp_ver = (d3.select("rect.vertical").style("opacity")); svg.selectAll("rect.horizontal").style("opacity",Math.abs(100-opp_hor)); svg.selectAll("rect.vertical").style("opacity",Math.abs(100-opp_ver)); }); var x0 = d3.scaleBand() .range([0, d3.select("svg").attr("width")]) .domain(d3.map(data,function(d) { return d.symbol; }).keys()); var y0 = d3.scaleLinear() .domain([0, d3.max(data, function(d){return d.price})]) .range([0, +d3.select("svg").attr("height")/3]); var x1 = d3.scaleBand() .range([0, d3.select("svg").attr("height")]) .domain(d3.map(data,function(d) { return d.symbol; }).keys()); var y1 = d3.scaleLinear() .domain([0, d3.max(data, function(d){return d.price})]) .range([0, +d3.select("svg").attr("width")/3]); var s = svg.selectAll("rect").data(data); s.enter() .append("rect") .attr("class", "vertical") s.enter() .append("rect") .attr("class", "horizontal") svg.selectAll("rect.vertical") .attr("class", "vertical") .attr("height", function(d, i) {return x1.bandwidth(); }) .attr("width", function(d, i) { return y1(d.price); }) .attr("y", function(d, i) {return x1(d.symbol); }) .attr("x", function(d) { return 0 }) .style("fill", function(d){return color(d.symbol)}) .style('opacity',0) .on('click', function(d){ svg.selectAll("rect.vertical").style('opacity',0); svg.selectAll("rect.horizontal").style('opacity',100) }) svg.selectAll("rect.horizontal") .attr("class", "horizontal") .attr("width", function(d, i) {return x0.bandwidth(); }) .attr("height", function(d, i) { return y0(d.price); }) .attr("x", function(d, i) {return x0(d.symbol); }) .attr("y", function(d) { return 400 - y0(d.price); }) .style("fill", function(d){return color(d.symbol)}) .style('opacity',100) }) </script> </body>
https://d3js.org/d3.v4.min.js