eleusis-benchmark / app /src /content /embeds /excess-caution.html
dlouapre's picture
dlouapre HF Staff
Adding interactive charts + assesment
aee6411
<div class="d3-excess-caution"></div>
<style>
.d3-excess-caution {
width: 100%;
margin: 10px 0;
position: relative;
font-family: system-ui, -apple-system, sans-serif;
}
.d3-excess-caution svg {
display: block;
width: 100%;
height: auto;
}
.d3-excess-caution .axes path,
.d3-excess-caution .axes line {
stroke: var(--axis-color, var(--text-color));
}
.d3-excess-caution .axes text {
fill: var(--tick-color, var(--muted-color));
font-size: 11px;
}
.d3-excess-caution .grid line {
stroke: var(--grid-color, rgba(0,0,0,.08));
}
.d3-excess-caution .axes text.axis-label {
font-size: 14px;
font-weight: 500;
fill: var(--text-color);
}
.d3-excess-caution .strip-point {
opacity: 0.5;
}
.d3-excess-caution .mean-line {
stroke-width: 4;
cursor: pointer;
}
.d3-excess-caution .mean-line:hover {
stroke-width: 5;
}
.d3-excess-caution .legend {
font-size: 11px;
}
.d3-excess-caution .legend-text {
fill: var(--text-color);
}
.d3-excess-caution .d3-tooltip {
position: absolute;
top: 0;
left: 0;
transform: translate(-9999px, -9999px);
pointer-events: none;
padding: 10px 12px;
border-radius: 8px;
font-size: 12px;
line-height: 1.4;
border: 1px solid var(--border-color);
background: var(--surface-bg);
color: var(--text-color);
box-shadow: 0 4px 24px rgba(0,0,0,.18);
opacity: 0;
transition: opacity 0.12s ease;
z-index: 10;
}
.d3-excess-caution .d3-tooltip .model-name {
font-weight: 600;
margin-bottom: 4px;
}
.d3-excess-caution .d3-tooltip .metric {
display: flex;
justify-content: space-between;
gap: 16px;
}
.d3-excess-caution .d3-tooltip .metric-label {
color: var(--muted-color);
}
.d3-excess-caution .d3-tooltip .metric-value {
font-weight: 500;
}
</style>
<script>
(() => {
const ensureD3 = (cb) => {
if (window.d3 && typeof window.d3.select === 'function') return cb();
let s = document.getElementById('d3-cdn-script');
if (!s) {
s = document.createElement('script');
s.id = 'd3-cdn-script';
s.src = 'https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js';
document.head.appendChild(s);
}
const onReady = () => { if (window.d3 && typeof window.d3.select === 'function') cb(); };
s.addEventListener('load', onReady, { once: true });
if (window.d3) onReady();
};
const bootstrap = () => {
const scriptEl = document.currentScript;
let container = scriptEl ? scriptEl.previousElementSibling : null;
if (!(container && container.classList && container.classList.contains('d3-excess-caution'))) {
const candidates = Array.from(document.querySelectorAll('.d3-excess-caution'))
.filter((el) => !(el.dataset && el.dataset.mounted === 'true'));
container = candidates[candidates.length - 1] || null;
}
if (!container) return;
if (container.dataset) {
if (container.dataset.mounted === 'true') return;
container.dataset.mounted = 'true';
}
// Tooltip setup
container.style.position = container.style.position || 'relative';
const tip = document.createElement('div');
tip.className = 'd3-tooltip';
container.appendChild(tip);
// SVG setup
const svg = d3.select(container).append('svg');
const gRoot = svg.append('g');
// Chart groups
const gGrid = gRoot.append('g').attr('class', 'grid');
const gAxes = gRoot.append('g').attr('class', 'axes');
const gPoints = gRoot.append('g').attr('class', 'points');
const gMeans = gRoot.append('g').attr('class', 'means');
const gLegend = gRoot.append('g').attr('class', 'legend');
// State
let data = null;
let width = 800;
let height = 450;
const margin = { top: 20, right: 30, bottom: 50, left: 160 };
// Scales (swapped: X is now linear, Y is categorical)
const xScale = d3.scaleLinear();
const yScale = d3.scaleBand();
// Data loading
const DATA_URL = '/data/excess_caution.json';
// Seeded random for consistent jitter
function seededRandom(seed) {
const x = Math.sin(seed) * 10000;
return x - Math.floor(x);
}
// Compute quartiles from array
function computeQuartiles(values) {
const sorted = [...values].sort((a, b) => a - b);
const n = sorted.length;
const q1 = sorted[Math.floor(n * 0.25)];
const median = sorted[Math.floor(n * 0.5)];
const q3 = sorted[Math.floor(n * 0.75)];
return { q1, median, q3 };
}
function showTooltip(event, model) {
const rect = container.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const quartiles = computeQuartiles(model.values);
tip.innerHTML = `
<div class="model-name" style="color: ${model.color}">${model.name}</div>
<div class="metric">
<span class="metric-label">Mean:</span>
<span class="metric-value">${model.mean.toFixed(2)}</span>
</div>
<div class="metric">
<span class="metric-label">Median:</span>
<span class="metric-value">${quartiles.median}</span>
</div>
<div class="metric">
<span class="metric-label">Q1 / Q3:</span>
<span class="metric-value">${quartiles.q1} / ${quartiles.q3}</span>
</div>
<div class="metric">
<span class="metric-label">Samples:</span>
<span class="metric-value">${model.count}</span>
</div>
`;
const tipWidth = tip.offsetWidth || 150;
const tipHeight = tip.offsetHeight || 100;
let tipX = x + 12;
let tipY = y - tipHeight / 2;
if (tipX + tipWidth > width) tipX = x - tipWidth - 12;
if (tipY < 0) tipY = 8;
if (tipY + tipHeight > height) tipY = height - tipHeight - 8;
tip.style.transform = `translate(${tipX}px, ${tipY}px)`;
tip.style.opacity = '1';
}
function hideTooltip() {
tip.style.opacity = '0';
tip.style.transform = 'translate(-9999px, -9999px)';
}
function updateSize() {
width = container.clientWidth || 800;
// Taller chart for horizontal layout with 10 models
height = Math.max(400, Math.round(width * 0.6));
svg.attr('width', width).attr('height', height).attr('viewBox', `0 0 ${width} ${height}`);
gRoot.attr('transform', `translate(${margin.left},${margin.top})`);
return {
innerWidth: width - margin.left - margin.right,
innerHeight: height - margin.top - margin.bottom
};
}
function render() {
if (!data) return;
const { innerWidth, innerHeight } = updateSize();
// Sort models by mean (descending - most cautious at top)
const models = [...data.models].sort((a, b) => b.mean - a.mean);
// X scale: linear (early correct turns)
const maxValue = d3.max(models, m => d3.max(m.values)) || 10;
xScale
.domain([0, maxValue + 0.5])
.range([0, innerWidth]);
// Y scale: categorical (model names)
yScale
.domain(models.map(m => m.name))
.range([0, innerHeight])
.padding(0.3);
// Grid lines (vertical)
const xTicks = xScale.ticks(6);
gGrid.selectAll('.grid-x')
.data(xTicks)
.join('line')
.attr('class', 'grid-x')
.attr('x1', d => xScale(d))
.attr('x2', d => xScale(d))
.attr('y1', 0)
.attr('y2', innerHeight);
// Remove old horizontal grid lines
gGrid.selectAll('.grid-y').remove();
// Axes
const tickSize = 6;
gAxes.selectAll('.x-axis')
.data([0])
.join('g')
.attr('class', 'x-axis')
.attr('transform', `translate(0,${innerHeight})`)
.call(d3.axisBottom(xScale)
.ticks(6)
.tickFormat(d3.format('d'))
.tickSizeInner(-tickSize)
.tickSizeOuter(0));
gAxes.selectAll('.y-axis')
.data([0])
.join('g')
.attr('class', 'y-axis')
.call(d3.axisLeft(yScale)
.tickSizeInner(-tickSize)
.tickSizeOuter(0));
// X-axis label
gAxes.selectAll('.x-label')
.data([0])
.join('text')
.attr('class', 'x-label axis-label')
.attr('x', innerWidth / 2)
.attr('y', innerHeight + 40)
.attr('text-anchor', 'middle')
.text('Early Correct Turns');
// Remove old Y-axis label
gAxes.selectAll('.y-label').remove();
// Create flat array of all points with horizontal jitter
const bandHeight = yScale.bandwidth();
const jitterWidth = 8; // Fixed horizontal jitter in pixels
const pointRadius = Math.min(2.5, bandHeight / 20);
const allPoints = models.flatMap((model, modelIdx) =>
model.values.map((value, i) => ({
model,
value,
// Seeded random jitter for consistency (horizontal)
jitter: (seededRandom(modelIdx * 1000 + i) - 0.5) * jitterWidth
}))
);
// Draw all points as small circles
gPoints.selectAll('.strip-point')
.data(allPoints, (d, i) => `${d.model.name}-${i}`)
.join('circle')
.attr('class', 'strip-point')
.attr('cx', d => xScale(d.value) + d.jitter)
.attr('cy', d => yScale(d.model.name) + bandHeight / 2)
.attr('r', pointRadius)
.attr('fill', d => d.model.color);
// Mean lines with hover (now vertical)
const meanLineHeight = bandHeight * 0.78;
gMeans.selectAll('.mean-line')
.data(models, d => d.name)
.join('line')
.attr('class', 'mean-line')
.attr('x1', d => xScale(d.mean))
.attr('x2', d => xScale(d.mean))
.attr('y1', d => yScale(d.name) + bandHeight / 2 - meanLineHeight / 2)
.attr('y2', d => yScale(d.name) + bandHeight / 2 + meanLineHeight / 2)
.attr('stroke', d => d.color)
.on('mouseenter', (event, d) => showTooltip(event, d))
.on('mousemove', (event, d) => showTooltip(event, d))
.on('mouseleave', hideTooltip);
// Legend
gLegend.selectAll('.legend-note')
.data([0])
.join('text')
.attr('class', 'legend-note legend-text')
.attr('x', innerWidth / 2)
.attr('y', innerHeight + 40)
.attr('text-anchor', 'middle')
.attr('font-size', '11px')
.text('');
}
// Initialize
fetch(DATA_URL, { cache: 'no-cache' })
.then(r => r.json())
.then(json => {
data = json;
render();
})
.catch(err => {
const pre = document.createElement('pre');
pre.style.color = 'red';
pre.style.padding = '16px';
pre.textContent = `Error loading data: ${err.message}`;
container.appendChild(pre);
});
// Resize handling
if (window.ResizeObserver) {
new ResizeObserver(() => render()).observe(container);
} else {
window.addEventListener('resize', render);
}
// Theme change handling
const observer = new MutationObserver(() => render());
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['data-theme']
});
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => ensureD3(bootstrap), { once: true });
} else {
ensureD3(bootstrap);
}
})();
</script>