Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 33x 2x 106x 31x 31x 31x 31x 106x 75x 2x 44x 44x 56x 54x 2x 28x 28x 28x 28x 62x 1x 98x 61x 61x 28x 98x 98x 34x 98x 28x 28x 2x 8x 5x 6x 3x 3x 6x 6x 6x 6x 3x 42x 50x 50x 10x 2x 10x 10x 10x 10x 2x 23x 23x 23x 2x 27x 6x 21x 40x 21x 19x 33x 23x 10x 20x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 33x 33x 23x 23x 23x 10x | import type {
BuildLayoutParams,
ColorWeight,
LayoutMark,
PositionedMark,
TimelineCluster,
TimelineLayout,
TimelineNode,
} from './types';
export const DOT_SIZE_PX = 12;
export const CLUSTER_DOT_SIZE_PX = 18;
export const EXPANDED_SPACING_PX = 20;
export const MIN_SQUEEZE_PITCH_PX = DOT_SIZE_PX;
export const BAND_PADDING_PX = 8;
export const SQUEEZE_GAP_PX = 12;
export const MAX_BAND_RATIO = 0.8;
export const DEFAULT_CLUSTER_THRESHOLD_PX = 16;
export const DIMMED_OPACITY = 0.35;
export const normalizePositions = (marks: LayoutMark[], containerWidth: number): PositionedMark[] => {
if (marks.length === 0) {
return [];
}
const values = marks.map((mark) => mark.value);
const minValue = Math.min(...values);
const maxValue = Math.max(...values);
const range = maxValue - minValue;
// Mirrors TimelineSlider's behavior: when every mark shares one value, they all land at the right edge.
return marks
.map((mark) => ({
color: mark.color,
id: mark.id,
positionPx: (range === 0 ? 1 : (mark.value - minValue) / range) * containerWidth,
}))
.sort((a, b) => a.positionPx - b.positionPx);
};
export const toColorWeights = (colors: string[]): ColorWeight[] => {
const weights = new Map<string, number>();
colors.forEach((color) => {
weights.set(color, (weights.get(color) ?? 0) + 1);
});
return [...weights.entries()].map(([color, weight]) => ({ color, weight }));
};
export const buildClusters = (marks: LayoutMark[], containerWidth: number, thresholdPx: number): TimelineCluster[] => {
const positioned = normalizePositions(marks, containerWidth);
const clusters: TimelineCluster[] = [];
let current: PositionedMark[] = [];
const flush = () => {
if (current.length === 0) {
return;
}
const total = current.reduce((sum, member) => sum + member.positionPx, 0);
clusters.push({
anchorPx: total / current.length,
id: `cluster-${current[0].id}`,
members: current,
});
current = [];
};
positioned.forEach((mark) => {
const previous = current[current.length - 1];
if (previous !== undefined && mark.positionPx - previous.positionPx >= thresholdPx) {
flush();
}
current.push(mark);
});
flush();
return clusters;
};
export const toConicGradient = (colorWeights: ColorWeight[]): string => {
if (colorWeights.length <= 1) {
return colorWeights[0]?.color ?? 'transparent';
}
const total = colorWeights.reduce((sum, { weight }) => sum + weight, 0);
let consumed = 0;
const stops = colorWeights.map(({ color, weight }) => {
const startDeg = (consumed / total) * 360;
consumed += weight;
const endDeg = (consumed / total) * 360;
return `${color} ${startDeg}deg ${endDeg}deg`;
});
return `conic-gradient(${stops.join(', ')})`;
};
const toCollapsedNode = (cluster: TimelineCluster, dimmed: boolean): TimelineNode => ({
colorWeights: toColorWeights(cluster.members.map((member) => member.color)),
dimmed,
id: cluster.id,
leftPx: cluster.anchorPx,
markIds: cluster.members.map((member) => member.id),
sizePx: cluster.members.length > 1 ? CLUSTER_DOT_SIZE_PX : DOT_SIZE_PX,
});
const clamp = (value: number, min: number, max: number): number => Math.min(Math.max(value, min), max);
const expandedSpacing = (memberCount: number, containerWidth: number, maxBandWidth: number): number => {
Iif (memberCount <= 1) {
return EXPANDED_SPACING_PX;
}
const cap = Math.min(containerWidth * MAX_BAND_RATIO, maxBandWidth);
const fitted = (cap - DOT_SIZE_PX - 2 * BAND_PADDING_PX) / (memberCount - 1);
return Math.max(0, Math.min(EXPANDED_SPACING_PX, fitted));
};
const rescale = (value: number, fromStart: number, fromEnd: number, toStart: number, toEnd: number): number => {
const fromSpan = fromEnd - fromStart;
Iif (fromSpan <= 0) {
return toStart;
}
return toStart + ((value - fromStart) / fromSpan) * (toEnd - toStart);
};
export const buildLayout = ({
containerWidth,
expandedClusterId,
marks,
thresholdPx = DEFAULT_CLUSTER_THRESHOLD_PX,
}: BuildLayoutParams): TimelineLayout => {
if (containerWidth <= 0 || marks.length === 0) {
return { nodes: [] };
}
const clusters = buildClusters(marks, containerWidth, thresholdPx);
const expanded = clusters.find((cluster) => cluster.id === expandedClusterId && cluster.members.length > 1);
if (expanded === undefined) {
return { nodes: clusters.map((cluster) => toCollapsedNode(cluster, false)) };
}
const sideClusters = clusters.filter((cluster) => cluster.id !== expanded.id);
const leftCount = sideClusters.filter((cluster) => cluster.anchorPx < expanded.anchorPx).length;
const rightCount = sideClusters.length - leftCount;
const reserve = (count: number) => (count === 0 ? 0 : SQUEEZE_GAP_PX + (count - 1) * MIN_SQUEEZE_PITCH_PX);
const leftReserve = reserve(leftCount);
const rightReserve = reserve(rightCount);
const minBandWidth = DOT_SIZE_PX + 2 * BAND_PADDING_PX;
const maxBandWidth = Math.max(minBandWidth, containerWidth - leftReserve - rightReserve);
const spacing = expandedSpacing(expanded.members.length, containerWidth, maxBandWidth);
const widthPx = Math.min(maxBandWidth, (expanded.members.length - 1) * spacing + minBandWidth);
const leftPx = clamp(
expanded.anchorPx - widthPx / 2,
leftReserve,
Math.max(leftReserve, containerWidth - widthPx - rightReserve)
);
const band = { leftPx, memberCount: expanded.members.length, widthPx };
const leftLimit = Math.max(0, leftPx - SQUEEZE_GAP_PX);
const rightLimit = Math.min(containerWidth, leftPx + widthPx + SQUEEZE_GAP_PX);
const nodes = clusters.flatMap((cluster): TimelineNode[] => {
if (cluster.id === expanded.id) {
return cluster.members.map((member, index) => ({
colorWeights: [{ color: member.color, weight: 1 }],
dimmed: false,
id: member.id,
leftPx: leftPx + BAND_PADDING_PX + DOT_SIZE_PX / 2 + index * spacing,
markIds: [member.id],
sizePx: DOT_SIZE_PX,
}));
}
const collapsed = toCollapsedNode(cluster, true);
const squeezed =
cluster.anchorPx < expanded.anchorPx
? rescale(cluster.anchorPx, 0, expanded.anchorPx, 0, leftLimit)
: rescale(cluster.anchorPx, expanded.anchorPx, containerWidth, rightLimit, containerWidth);
return [{ ...collapsed, leftPx: squeezed }];
});
return { band, nodes };
};
|