Published
Edited
Aug 23, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
allValues = data.map((d) => d.value)
Insert cell
Insert cell
uniqueValues = _.uniq(allValues)
Insert cell
Insert cell
sortedUniqueValues = uniqueValues.sort((a, b) => a - b)
Insert cell
Insert cell
updatedData = data.map((d) => ({
...d,
calculatedPercentile:
(sortedUniqueValues.indexOf(d.value) + 1) / sortedUniqueValues.length
}))
Insert cell
Insert cell
Insert cell
dataSorted = [...updatedData].sort((a, b) => a.value - b.value)
Insert cell
Insert cell
dataRawPercentile = dataSorted.map((d, i) => ({
...d,
rawPercentile: (i + 1) / dataSorted.length
}))
Insert cell
Insert cell
dataRawPercentileGrouped = d3.groups(dataRawPercentile, (d) =>
d.value.toString()
)
Insert cell
finalData = dataRawPercentileGrouped.reduce((memo, [i, g]) => {
let meanPercentile = d3.mean(g, (d) => d.rawPercentile);
let calculated = g.map((d) => ({
...d,
meanPercentile
}));
return [...memo, ...calculated];
}, [])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3-array@^2.10")
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more