diff --git a/pyramid/README.md b/pyramid/README.md new file mode 100644 index 0000000..fdeffee --- /dev/null +++ b/pyramid/README.md @@ -0,0 +1,16 @@ +# Pyramid Chart Layout built in d3.js + +Demo: + +```js +var pyramid = d3.pyramid() + .size([width,height]) + .value(function(d) { return d.value; }); +``` + +``` +var g = d3.selectAll(".pyramid-group") + .data(pyramid(data)) + .enter().append("g") + .attr("class", "pyramid-group"); +``` diff --git a/pyramid/pyramid.js b/pyramid/pyramid.js new file mode 100644 index 0000000..ed77f72 --- /dev/null +++ b/pyramid/pyramid.js @@ -0,0 +1,80 @@ +d3.pyramid = function() { + var size = [1,1], + value = function(d) { return d.value}, + coordinates; + + var percentageValues = function (data) { + var values = data.map(value); + var sum = d3.sum(values, function (d){ + return +d; + }); + var percentValues = data.map(function (d,i){ + d.value = +values[i]; + return values[i]/sum*100; + }); + percentValues.sort(function(a,b){ + return b-a; + }); + return percentValues; + } + var coordinatesCalculation = function(data){ + var w = size[0], + h = size[1], + ratio = (w/2)/h, + percentValues = percentageValues(data), + coordinates = [], + area_of_triangle = (w * h) / 2; + function d3Sum (i) { + return d3.sum(percentValues,function (d, j){ + if (j>=i) { + return d; + } + }); + } + for (var i=0,len=data.length;i