[ Launch: Lorenz Curve ] 9767676 by stevekinney
-
-
Save wonglok/4e2cdce8a24752fc2a05 to your computer and use it in GitHub Desktop.
Lorenz Curve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"description":"Lorenz Curve","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"draw.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"lorenx.js":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/rkrLAMx.png","inline-console":false} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var svg = d3.select('svg'); | |
var d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 100]; | |
d.sort(function (a,b) { | |
return a- b; | |
}); | |
function split(array, units) { | |
var length = array.length; | |
var output = []; | |
var i = 0; | |
while (i < length) { | |
var size = Math.ceil((length - i) / units--); | |
output.push(array.slice(i, i += size)); | |
} | |
return output; | |
} | |
function sum(array) { | |
return array.reduce(function (a, b) { | |
return a + b; | |
}, 0); | |
} | |
function splitAndSum(array, units) { | |
var splitArray = split(array, units); | |
return splitArray.map(function (a) { | |
return sum(a); | |
}); | |
} | |
function splitAndSumAndPercent(array, units) { | |
var s = sum(array); | |
return splitAndSum(array, units).map(function (a) { | |
return a/s * 100; | |
}); | |
} | |
var q = splitAndSumAndPercent(d, 5); | |
var x = d3.scale.ordinal() | |
.domain(d3.range(q.length)) | |
.rangeBands([10, 450]); | |
var y = d3.scale.linear() | |
.domain(d3.extent(q)) | |
.range([150, 10]); | |
var line = d3.svg.line() | |
.x(function (d, i) { return x(i); }) | |
.y(function (d, i) { return y(d); }); | |
svg.append("path") | |
.attr("d", line(q)) | |
.style({ | |
fill: "none", | |
stroke: "#000" | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment