heatmaps: part 23

This commit is contained in:
nym21
2026-06-01 18:03:41 +02:00
parent 2bbc535b58
commit be20633945
4 changed files with 20 additions and 97 deletions
+17
View File
@@ -0,0 +1,17 @@
/** @param {number} value */
export function formatCompact(value) {
if (value >= 1000) return `${formatNumber(value / 1000)}k`;
return formatNumber(value);
}
/** @param {number} value */
function formatNumber(value) {
if (value >= 100) return String(Math.round(value));
if (value >= 10) return trimNumber(value.toFixed(1));
return trimNumber(value.toFixed(2));
}
/** @param {string} value */
function trimNumber(value) {
return value.replace(/\.?0+$/, "");
}
-60
View File
@@ -31,18 +31,6 @@ const DIVERGING_POSITIVE_STOPS = [
export const DIVERGING_NEGATIVE_LUT = createColorLut(DIVERGING_NEGATIVE_STOPS);
export const DIVERGING_POSITIVE_LUT = createColorLut(DIVERGING_POSITIVE_STOPS);
/**
* @param {ArrayLike<number>} lut
* @returns {HeatmapColorFn}
*/
export function intensityColor(lut) {
return (value) => {
if (!Number.isFinite(value)) return 0x00000000;
const index = Math.min(255, Math.max(0, Math.round(value * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {ArrayLike<number>} lut
* @returns {HeatmapColorFn}
@@ -58,21 +46,6 @@ export function logIntensityColor(lut) {
};
}
/**
* @param {ArrayLike<number>} lut
* @returns {HeatmapColorFn}
*/
export function linearIntensityColor(lut) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const cap = context.grid.getMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const t = Math.min(1, value / cap);
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {ArrayLike<number>} lut
* @param {number} [exponent]
@@ -111,39 +84,6 @@ export function divergingPowerIntensityColor(
};
}
/**
* @param {ArrayLike<number>} lut
* @param {{ knee?: number, max?: number }} [options]
* @returns {HeatmapColorFn}
*/
export function softIntensityColor(lut, { knee = 0.15, max = 1 } = {}) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const cap = context.grid.getMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const ratio = Math.min(1, value / cap);
const t = (ratio / (ratio + knee)) * max;
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {ArrayLike<number>} lut
* @returns {HeatmapColorFn}
*/
export function smoothLogIntensityColor(lut) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const cap = context.grid.getMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const u = Math.log1p(value) / Math.log1p(cap);
const t = u * u * (3 - 2 * u);
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {number[][]} stops - Tuples of [position, red, green, blue].
*/
+1 -18
View File
@@ -1,4 +1,5 @@
import { brk } from "../../scripts/utils/client.js";
import { formatCompact } from "./format.js";
import { createAverageGrid } from "./grid.js";
import { INFERNO_LUT, logIntensityColor } from "./lut.js";
import { defaultTooltip } from "./tooltip/index.js";
@@ -137,21 +138,3 @@ function formatAmount(value) {
if (btc >= 1) return `${formatCompact(btc)} BTC`;
return `${formatCompact(btc * 100_000_000)} sats`;
}
/** @param {number} value */
function formatCompact(value) {
if (value >= 1000) return `${formatNumber(value / 1000)}k`;
return formatNumber(value);
}
/** @param {number} value */
function formatNumber(value) {
if (value >= 100) return String(Math.round(value));
if (value >= 10) return trimNumber(value.toFixed(1));
return trimNumber(value.toFixed(2));
}
/** @param {string} value */
function trimNumber(value) {
return value.replace(/\.?0+$/, "");
}
+2 -19
View File
@@ -1,5 +1,6 @@
import { brk } from "../../scripts/utils/client.js";
import { numberToShortUSFormat } from "../../scripts/utils/format.js";
import { formatCompact } from "./format.js";
import { createAverageGrid } from "./grid.js";
import {
DIVERGING_NEGATIVE_LUT,
@@ -175,6 +176,7 @@ function createUrpdHeatmapOption({
grid: createAverageGrid({
yMin: MIN_LOG,
yMax: MAX_LOG,
minCellSize: 2,
}),
color,
axis: {
@@ -207,7 +209,6 @@ async function fetchUrpdPoints(cohort, date, signal, getValue, onPoints) {
let points;
const urpd = await brk.getUrpdAt(cohort, date, AGGREGATION, {
signal,
cache: false,
onValue: onPoints
? (urpd) => {
points = toPoints(urpd, getValue);
@@ -277,21 +278,3 @@ function formatSignedDollar(value) {
if (value < 0) return `-${formatted}`;
return formatted;
}
/** @param {number} value */
function formatCompact(value) {
if (value >= 1000) return `${formatNumber(value / 1000)}k`;
return formatNumber(value);
}
/** @param {number} value */
function formatNumber(value) {
if (value >= 100) return String(Math.round(value));
if (value >= 10) return trimNumber(value.toFixed(1));
return trimNumber(value.toFixed(2));
}
/** @param {string} value */
function trimNumber(value) {
return value.replace(/\.?0+$/, "");
}