D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
adurivault
Full window
Github gist
Stocks transition_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 dataset var w = 960; var h = 500; d3.csv("stocks.csv", function(error, data){ if (error){ console.log(error) } else{ data.forEach(function(d){ d.price = +d.price; d.year = d3.timeParse("%b %Y")(d.date).getFullYear(); }) dataset = data; create_vizualisation(dataset); } }) function create_vizualisation(dataset){ var z = d3.scaleOrdinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var n_year = d3.nest() .key(function(d){return d.year}) .entries(dataset) .length; var n_symbol = d3.nest() .key(function(d){return d.symbol}) .entries(dataset) .length; var x0 = d3.scaleBand() .domain(d3.range(n_year)) .range([0, w], .2); var x1 = d3.scaleBand() .domain(d3.range(n_symbol)) .range([0, x0.bandwidth() - 10]); var y = d3.scaleLinear() .domain([0, d3.max(dataset, function(d){return d.price})]) .range([h, 0]); var color = d3.scaleOrdinal() .range((["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"])); data_year_symb = d3.nest() .key(function(d){return d.year}) .key(function(d){return d.symbol}) .rollup(function(v) {return d3.mean(v, function(d){return d.price})}) .entries(dataset) data_symb_year = d3.nest() .key(function(d){return d.symbol}) .key(function(d){return d.year}) .rollup(function(v) {return d3.mean(v, function(d){return d.price})}) .entries(dataset) var svg = d3.select("body").append("svg") .attr("width", w) .attr("height", h); console.log(data_year_symb) console.log(d3.stack() .keys() .value(function(d, i){console.log(d.values); return d.values}) (dataset)); // svg.append("g").selectAll("g") // .data(d3.stack().keys(d3.range(n_))) /* svg.append("g").selectAll("g") .data(nested_dataset) .enter().append("g") .style("fill", function(d, i){return z(i)}) .attr("transform", function(d, i) { return "translate(" + x1(i) + ",0)"; }) .attr("fill", function(d, i){return color(i)}) .selectAll("rect") .data(function(d) {return d.values}) .enter().append("rect") .attr("width", x1.bandwidth()) .attr("height", function(d){return y(d.value)}) .attr("x", function(d, i) { return x0(i); }) .attr("y", function(d) { return h - y(d.value); }) */ } </script> </body>
https://d3js.org/d3.v4.min.js