d3-array#quantile JavaScript Examples
The following examples show how to use
d3-array#quantile.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: sequentialQuantile.js From cs-wiki with GNU General Public License v3.0 | 6 votes |
export default function sequentialQuantile() {
var domain = [],
interpolator = identity;
function scale(x) {
if (x != null && !isNaN(x = +x)) return interpolator((bisect(domain, x, 1) - 1) / (domain.length - 1));
}
scale.domain = function(_) {
if (!arguments.length) return domain.slice();
domain = [];
for (let d of _) if (d != null && !isNaN(d = +d)) domain.push(d);
domain.sort(ascending);
return scale;
};
scale.interpolator = function(_) {
return arguments.length ? (interpolator = _, scale) : interpolator;
};
scale.range = function() {
return domain.map((d, i) => interpolator(i / (domain.length - 1)));
};
scale.quantiles = function(n) {
return Array.from({length: n + 1}, (_, i) => quantile(domain, i / n));
};
scale.copy = function() {
return sequentialQuantile(interpolator).domain(domain);
};
return initInterpolator.apply(scale, arguments);
}
Example #2
Source File: patterns.js From Turnip-Calculator with MIT License | 6 votes |
calculateQuantiles = (patterns, quantileRange = 75) => {
const samples = patternReducer(patterns, samplesReducer);
return [
(100 - quantileRange) * 0.005,
0.5,
1 - (100 - quantileRange) * 0.005,
].map((q) => {
return samples.map((daySamples, i) => Math.floor(quantile(daySamples, q)));
});
}
Example #3
Source File: MapLegend.js From covid19india-react with MIT License | 4 votes |
function legend({
svg,
color,
title,
tickSize = 6,
width = 320,
height = 44 + tickSize,
marginTop = 18,
marginRight = 0,
marginBottom = 16 + tickSize,
marginLeft = 0,
ticks = width / 64,
tickFormat,
tickValues,
ordinalWeights,
} = {}) {
const t = svg.transition().duration(D3_TRANSITION_DURATION);
let tickAdjust = (g) => {
const ticks = g.selectAll('.tick line');
ticks.attr('y1', marginTop + marginBottom - height);
// select(ticks.nodes()[ticks.size() - 1]).remove();
};
let x;
// Continuous
if (color.interpolate) {
const n = Math.min(color.domain().length, color.range().length);
x = color
.copy()
.rangeRound(quantize(interpolate(marginLeft, width - marginRight), n));
svg
.select('.ramp')
.attr('x', marginLeft)
.attr('y', marginTop)
.attr('width', width - marginLeft - marginRight)
.attr('height', height - marginTop - marginBottom)
.attr(
'xlink:href',
ramp(color.copy().domain(quantize(interpolate(0, 1), n))).toDataURL()
);
}
// Sequential
else if (color.interpolator) {
svg
.select('.bars')
.selectAll('rect')
.transition(t)
.attr('opacity', 0)
.remove();
x = Object.assign(
color
.copy()
.interpolator(interpolateRound(marginLeft, width - marginRight)),
{
range() {
return [marginLeft, width - marginRight];
},
}
);
svg
.select('.ramp')
.attr('x', marginLeft)
.attr('y', marginTop)
.attr('width', width - marginLeft - marginRight)
.attr('height', height - marginTop - marginBottom)
.attr('xlink:href', ramp(color.interpolator()).toDataURL())
.attr('display', 'visible')
.transition(t)
.attr('opacity', 1);
// scaleSequentialQuantile doesn’t implement ticks or tickFormat.
if (!x.ticks) {
if (tickValues === undefined) {
const n = Math.round(ticks + 1);
tickValues = range(n).map((i) => quantile(color.domain(), i / (n - 1)));
}
if (typeof tickFormat !== 'function') {
tickFormat = format(tickFormat === undefined ? ',f' : tickFormat);
}
}
}
// Threshold
else if (color.invertExtent) {
const thresholds = color.thresholds
? color.thresholds() // scaleQuantize
: color.quantiles
? color.quantiles() // scaleQuantile
: color.domain(); // scaleThreshold
const thresholdFormat =
tickFormat === undefined
? (d) => d
: typeof tickFormat === 'string'
? format(tickFormat)
: tickFormat;
x = scaleLinear()
.domain([-1, color.range().length - 1])
.rangeRound([marginLeft, width - marginRight]);
svg
.append('g')
.selectAll('rect')
.data(color.range())
.join('rect')
.attr('x', (d, i) => x(i - 1))
.attr('y', marginTop)
.attr('width', (d, i) => x(i) - x(i - 1))
.attr('height', height - marginTop - marginBottom)
.attr('fill', (d) => d);
tickValues = range(-1, thresholds.length);
tickFormat = (i) => {
if (i === -1) return thresholdFormat(1);
else if (i === thresholds.length - 1) return;
else if (i === thresholds.length - 2)
return thresholdFormat(thresholds[i] + '+', i);
return thresholdFormat(thresholds[i], i);
};
}
// Ordinal
else {
svg
.select('.ramp')
.transition(t)
.attr('opacity', 0)
.attr('xlink:href', null);
if (!ordinalWeights) {
x = scaleBand()
.domain(color.domain().filter((d) => d))
.rangeRound([marginLeft, width - marginRight]);
svg
.select('.bars')
.selectAll('rect')
.data(color.domain().filter((d) => d))
.join('rect')
.attr('x', x)
.attr('y', marginTop)
.attr('width', Math.max(0, x.bandwidth() - 1))
.attr('height', height - marginTop - marginBottom)
.attr('fill', color);
} else {
const widthScale = scaleLinear()
.domain([0, ordinalWeights.reduce((a, b) => a + b)])
.rangeRound([0, width - marginLeft - marginRight]);
const xPos = ordinalWeights.map((w, i) =>
ordinalWeights
.slice(0, i)
.reduce((acc, w) => acc + widthScale(w), marginLeft)
);
x = scaleOrdinal().domain(color.domain()).range(xPos);
svg
.select('.bars')
.selectAll('rect')
.data(color.domain())
.join((enter) =>
enter
.append('rect')
.attr('x', x)
.attr('width', (d, i) => widthScale(ordinalWeights[i]))
)
.attr('y', marginTop)
.attr('height', height - marginTop - marginBottom)
.attr('fill', color)
.transition(t)
.attr('x', x)
.attr('width', (d, i) => widthScale(ordinalWeights[i]))
.attr('opacity', 1);
}
tickAdjust = () => {};
}
svg
.select('.axis')
.attr('transform', `translate(0,${height - marginBottom})`)
.transition(t)
.attr('class', 'axis')
.call(
axisBottom(x)
.ticks(ticks, typeof tickFormat === 'string' ? tickFormat : undefined)
.tickFormat(typeof tickFormat === 'function' ? tickFormat : undefined)
.tickSize(tickSize)
.tickValues(tickValues)
)
.on('start', () => {
svg.call(tickAdjust).call((svg) => svg.select('.domain').remove());
})
.call((g) =>
g
.select('.axistext')
.attr('class', 'axistext')
.attr('x', marginLeft)
.attr('y', marginTop + marginBottom - height - 6)
.attr('fill', 'currentColor')
.attr('text-anchor', 'start')
.attr('font-weight', 'bold')
.text(title)
);
return svg.node();
}