D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
matt-mcdaniel
Full window
Github gist
d3.layout.stack() error with differing array lengths
hello markdown
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <style> body { margin:0; box-sizing: border-box; width: 100vw; height: 100vh; position: relative; } svg { width: 100%; height: 100%; } .original-example, .revised-example { position: absolute; right: 40px; width: 250px; } .original-example { top: 0; } .revised-example { top: 270px; } pre { background-color: #d0d0d0; padding: 5px; } .warning { visibility: hidden; color: red; position: absolute; top: 40%; left: 13%; font-weight: bold; } input[type="radio"]:last-of-type { margin-left: 4em; } .axis path, .axis line { fill: none; stroke: #000; stroke-width: 2px; shape-rendering: crispEdges; } .minus { position: absolute; right: 265px; top: 295px; font-size: 55px; color: red; } </style> </head> <body> <div class="original-example"> <input type="radio" class="original">Original Data <pre></pre> </div> <div class="revised-example"> <input type="radio" class="revised">Remove Object from Array <pre></pre> </div> <p class="minus">-</p> <p class="warning">Uncaught TypeError: Cannot read property '1' of undefined</p> <figure class="svg-container"></figure> <script> var data = [ { key: 'Thing1', values: [ { year: 1990, amount: 5 }, { year: 1991, amount: 6 }, { year: 1992, type: 'fries', amount: 3 } ] }, { key: 'Thing2', values: [ { year: 1990, amount: 2 }, { year: 1991, amount: 1 }, { year: 1992, amount: 1 } ] }, { key: 'Thing3', values: [ { year: 1990, amount: 4 }, { year: 1991, amount: 2 }, { year: 1992, amount: 6 } ] } ]; var layers; // display data examples in DOM d3.select('.original-example pre').html(function() { return JSON.stringify(data[2]['values'], null, 4); }); d3.select('.revised-example pre').html(function() { var copy = data[2]['values'].slice(0); copy.splice(1, 1); return JSON.stringify(copy, null, 4); }); //initialize original data d3.select('.original-example input').node().checked = true; var colors = ["#045A8D", "#2B8CBE", "#74A9CF"]; var width = 500; var height = 400; var margin = {top: 10, right: 10, bottom: 0, left: 50}; var svg = d3.select('.svg-container').append('svg') .attr('width', width + margin.left + margin.right) .attr('height', height + margin.top + margin.bottom); var g = svg.append('g') .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); var x = d3.scale.linear().domain([1990, 1992]).range([0, width]); var y = d3.scale.linear().domain([0, 20]).range([height, 0]); var z = d3.scale.ordinal().domain([0,2]).range(colors); var xAxis = d3.svg.axis().orient('bottom').scale(x) .tickValues([1990, 1991, 1992]) .tickFormat(d3.format(".0f")); var yAxis = d3.svg.axis().orient('left').scale(y); var stack = d3.layout.stack() .offset("silhouette") .values(function(d) { return d.values; }) .x(function(d) { return +d.year; }) .y(function(d) { return +d.amount; }); var area = d3.svg.area() .interpolate("cardinal") .x(function(d) { return x(d.year); }) .y0(function(d) { return y(d.y0); }) .y1(function(d) { return y(d.y0 + d.y); }); layers = stack(data); g.selectAll('.layer') .data(layers) .enter().append('path') .classed('layer', true) .attr('d', function(d) { return area(d.values); }) .style('fill', function(d, i) { return z(i); }); g.append('text') .attr('transform', 'translate(350, 250) rotate(-10)') .text('Example Data Set ➜') .attr('fill', 'white') // radio button clicks d3.select('.original').on('click', function() { d3.select('.revised').node().checked = false; d3.select('.svg-container').style('visibility', 'visible'); d3.select('.warning').style('visibility', 'hidden'); }); d3.select('.revised').on('click', function() { d3.select('.original').node().checked = false; d3.select('.svg-container').style('visibility', 'hidden'); d3.select('.warning').style('visibility', 'visible'); }); // x axis g.append('g') .attr('class', 'x axis') .attr('transform', 'translate(0,'+ height + ')') .call(xAxis); // y axis g.append('g') .attr('class', 'y axis') .call(yAxis); </script> </body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js