Created
January 15, 2018 20:35
-
-
Save johannes-weber/0aebf6dc879ba628d60d13827b03560a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/jafefedahe
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function createGraph(data, startingAge, endingAge, ageInterval) { | |
// calculate the number of buckets | |
const numBuckets = Math.ceil((endingAge - startingAge) / ageInterval) + 1; | |
const buckets = new Array(numBuckets).fill(0); | |
Object.keys(data).forEach((age) => { | |
let bucketIndex = Math.floor((age - startingAge) / ageInterval); | |
buckets[bucketIndex] += data[age]; | |
}); | |
let startInterval = startingAge; | |
let endInterval = startInterval + ageInterval; | |
const result = {}; | |
for(let i = 0; i < buckets.length; i++) { | |
result[startInterval + ' - ' + endInterval] = buckets[i]; | |
startInterval = endInterval; | |
endInterval += ageInterval; | |
} | |
console.log(buckets, result); | |
} | |
const data = { | |
25: 55, | |
26: 45, | |
27: 10, | |
28: 20, | |
30: 1, | |
31: 1, | |
32: 3, | |
33: 10, | |
60: 10, | |
64: 5, | |
65: 5 | |
}; | |
createGraph(data, 20, 65, 5); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function createGraph(data, startingAge, endingAge, ageInterval) { | |
// calculate the number of buckets | |
const numBuckets = Math.ceil((endingAge - startingAge) / ageInterval) + 1; | |
const buckets = new Array(numBuckets).fill(0); | |
Object.keys(data).forEach((age) => { | |
let bucketIndex = Math.floor((age - startingAge) / ageInterval); | |
buckets[bucketIndex] += data[age]; | |
}); | |
let startInterval = startingAge; | |
let endInterval = startInterval + ageInterval; | |
const result = {}; | |
for(let i = 0; i < buckets.length; i++) { | |
result[startInterval + ' - ' + endInterval] = buckets[i]; | |
startInterval = endInterval; | |
endInterval += ageInterval; | |
} | |
console.log(buckets, result); | |
} | |
const data = { | |
25: 55, | |
26: 45, | |
27: 10, | |
28: 20, | |
30: 1, | |
31: 1, | |
32: 3, | |
33: 10, | |
60: 10, | |
64: 5, | |
65: 5 | |
}; | |
createGraph(data, 20, 65, 5);</script></body> | |
</html> |
This file contains hidden or 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
function createGraph(data, startingAge, endingAge, ageInterval) { | |
// calculate the number of buckets | |
const numBuckets = Math.ceil((endingAge - startingAge) / ageInterval) + 1; | |
const buckets = new Array(numBuckets).fill(0); | |
Object.keys(data).forEach((age) => { | |
let bucketIndex = Math.floor((age - startingAge) / ageInterval); | |
buckets[bucketIndex] += data[age]; | |
}); | |
let startInterval = startingAge; | |
let endInterval = startInterval + ageInterval; | |
const result = {}; | |
for(let i = 0; i < buckets.length; i++) { | |
result[startInterval + ' - ' + endInterval] = buckets[i]; | |
startInterval = endInterval; | |
endInterval += ageInterval; | |
} | |
console.log(buckets, result); | |
} | |
const data = { | |
25: 55, | |
26: 45, | |
27: 10, | |
28: 20, | |
30: 1, | |
31: 1, | |
32: 3, | |
33: 10, | |
60: 10, | |
64: 5, | |
65: 5 | |
}; | |
createGraph(data, 20, 65, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment