Spaces:
Running
Running
| <div class="d3-score-stack"></div> | |
| <style> | |
| .d3-score-stack { | |
| width: 100%; | |
| margin: 10px 0; | |
| position: relative; | |
| font-family: system-ui, -apple-system, sans-serif; | |
| } | |
| .d3-score-stack svg { | |
| display: block; | |
| width: 100%; | |
| height: auto; | |
| } | |
| .d3-score-stack .axes path, | |
| .d3-score-stack .axes line { | |
| stroke: var(--axis-color, var(--text-color)); | |
| } | |
| .d3-score-stack .axes text { | |
| fill: var(--tick-color, var(--muted-color)); | |
| font-size: 11px; | |
| } | |
| .d3-score-stack .grid line { | |
| stroke: var(--grid-color, rgba(0,0,0,.08)); | |
| } | |
| .d3-score-stack .axes text.axis-label { | |
| font-size: 15px; | |
| font-weight: 500; | |
| fill: var(--text-color); | |
| } | |
| .d3-score-stack .bar-segment { | |
| cursor: pointer; | |
| transition: opacity 0.15s ease; | |
| } | |
| .d3-score-stack .bar-segment:hover { | |
| opacity: 0.8; | |
| } | |
| .d3-score-stack .model-label { | |
| font-size: 12px; | |
| fill: var(--text-color); | |
| } | |
| .d3-score-stack .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-score-stack .d3-tooltip .model-name { | |
| font-weight: 600; | |
| margin-bottom: 4px; | |
| } | |
| .d3-score-stack .d3-tooltip .metric { | |
| display: flex; | |
| justify-content: space-between; | |
| gap: 16px; | |
| } | |
| .d3-score-stack .d3-tooltip .metric-label { | |
| color: var(--muted-color); | |
| } | |
| .d3-score-stack .d3-tooltip .metric-value { | |
| font-weight: 500; | |
| } | |
| .d3-score-stack .legend { | |
| display: flex; | |
| flex-wrap: wrap; | |
| justify-content: center; | |
| gap: 16px; | |
| margin-top: 12px; | |
| font-size: 12px; | |
| } | |
| .d3-score-stack .legend-item { | |
| display: flex; | |
| align-items: center; | |
| gap: 6px; | |
| } | |
| .d3-score-stack .legend-swatch { | |
| width: 14px; | |
| height: 14px; | |
| border-radius: 2px; | |
| } | |
| .d3-score-stack .legend-label { | |
| color: var(--text-color); | |
| } | |
| </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-score-stack'))) { | |
| const candidates = Array.from(document.querySelectorAll('.d3-score-stack')) | |
| .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 gBars = gRoot.append('g').attr('class', 'bars'); | |
| // Legend container | |
| const legendDiv = document.createElement('div'); | |
| legendDiv.className = 'legend'; | |
| container.appendChild(legendDiv); | |
| // State | |
| let data = null; | |
| let width = 800; | |
| let height = 500; | |
| const margin = { top: 20, right: 30, bottom: 56, left: 160 }; | |
| // Colors for segments | |
| const segmentColors = { | |
| floored: '#afb2c5', // Gray - floored score | |
| noStakes: '#34b31e' // Green - no-stakes gain | |
| }; | |
| // Scales | |
| const xScale = d3.scaleLinear(); | |
| const yScale = d3.scaleBand(); | |
| // Data loading | |
| const DATA_URL = '/data/score_stack.json'; | |
| function updateSize() { | |
| width = container.clientWidth || 800; | |
| const barCount = data ? data.models.length : 10; | |
| height = Math.max(400, barCount * 44 + margin.top + margin.bottom); | |
| 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 showTooltip(event, d, segment) { | |
| const rect = container.getBoundingClientRect(); | |
| const x = event.clientX - rect.left; | |
| const y = event.clientY - rect.top; | |
| let segmentName, segmentValue, description; | |
| if (segment === 'floored') { | |
| segmentName = 'Score'; | |
| segmentValue = d.avg_floored_score.toFixed(2); | |
| description = 'Floored score (negative scores count as 0)'; | |
| } else { | |
| segmentName = 'No-Stakes Gain'; | |
| segmentValue = '+' + d.no_stakes_delta.toFixed(2); | |
| description = 'Additional gain without guess penalties'; | |
| } | |
| tip.innerHTML = ` | |
| <div class="model-name" style="color: ${d.color}">${d.name}</div> | |
| <div class="metric"> | |
| <span class="metric-label">${segmentName}:</span> | |
| <span class="metric-value">${segmentValue}</span> | |
| </div> | |
| <div style="font-size: 11px; color: var(--muted-color); margin-top: 4px;">${description}</div> | |
| <hr style="border: none; border-top: 1px solid var(--border-color); margin: 8px 0;"> | |
| <div class="metric"> | |
| <span class="metric-label">Score:</span> | |
| <span class="metric-value">${d.avg_floored_score.toFixed(2)}</span> | |
| </div> | |
| <div class="metric"> | |
| <span class="metric-label">No-Stakes Score:</span> | |
| <span class="metric-value">${d.avg_no_stakes_score.toFixed(2)}</span> | |
| </div> | |
| `; | |
| const tipWidth = tip.offsetWidth || 200; | |
| const tipHeight = tip.offsetHeight || 150; | |
| 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 render() { | |
| if (!data) return; | |
| const { innerWidth, innerHeight } = updateSize(); | |
| // Sort models by floored score (descending) | |
| const models = [...data.models].sort((a, b) => b.avg_floored_score - a.avg_floored_score); | |
| // Update scales | |
| const maxScore = d3.max(models, d => d.avg_no_stakes_score); | |
| xScale | |
| .domain([0, maxScore + 1]) | |
| .range([0, innerWidth]) | |
| .nice(); | |
| yScale | |
| .domain(models.map(d => d.name)) | |
| .range([0, innerHeight]) | |
| .padding(0.25); | |
| // Grid lines | |
| const xTicks = xScale.ticks(8); | |
| 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); | |
| // 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(8).tickSizeInner(-tickSize).tickSizeOuter(0)); | |
| gAxes.selectAll('.y-axis') | |
| .data([0]) | |
| .join('g') | |
| .attr('class', 'y-axis') | |
| .call(d3.axisLeft(yScale).tickSize(0)) | |
| .selectAll('text') | |
| .attr('class', 'model-label'); | |
| // Axis label | |
| gAxes.selectAll('.x-label') | |
| .data([0]) | |
| .join('text') | |
| .attr('class', 'x-label axis-label') | |
| .attr('x', innerWidth / 2) | |
| .attr('y', innerHeight + 44) | |
| .attr('text-anchor', 'middle') | |
| .text('Score'); | |
| const barHeight = yScale.bandwidth(); | |
| // Helper to sanitize names for CSS selectors (remove periods, spaces, etc.) | |
| const toClassName = (name) => name.replace(/[^a-zA-Z0-9]/g, '-'); | |
| // Draw stacked bars for each model | |
| models.forEach(d => { | |
| const y = yScale(d.name); | |
| const safeId = toClassName(d.name); | |
| // Calculate segment positions | |
| // Floored score starts from 0 | |
| const flooredStart = 0; | |
| const flooredEnd = d.avg_floored_score; | |
| // No-stakes delta starts where floored ends | |
| const noStakesStart = flooredEnd; | |
| const noStakesEnd = noStakesStart + d.no_stakes_delta; | |
| // Floored score segment (base) | |
| gBars.selectAll(`.bar-floored-${safeId}`) | |
| .data([d]) | |
| .join('rect') | |
| .attr('class', `bar-segment bar-floored-${safeId}`) | |
| .attr('x', xScale(flooredStart)) | |
| .attr('y', y) | |
| .attr('width', Math.max(0, xScale(flooredEnd) - xScale(flooredStart))) | |
| .attr('height', barHeight) | |
| .attr('fill', segmentColors.floored) | |
| .on('mouseenter', (e) => showTooltip(e, d, 'floored')) | |
| .on('mousemove', (e) => showTooltip(e, d, 'floored')) | |
| .on('mouseleave', hideTooltip); | |
| // No-stakes delta segment (only if positive) | |
| if (d.no_stakes_delta > 0.01) { | |
| gBars.selectAll(`.bar-nostakes-${safeId}`) | |
| .data([d]) | |
| .join('rect') | |
| .attr('class', `bar-segment bar-nostakes-${safeId}`) | |
| .attr('x', xScale(noStakesStart)) | |
| .attr('y', y) | |
| .attr('width', Math.max(0, xScale(noStakesEnd) - xScale(noStakesStart))) | |
| .attr('height', barHeight) | |
| .attr('fill', segmentColors.noStakes) | |
| .attr('opacity', 0.5) | |
| .on('mouseenter', (e) => showTooltip(e, d, 'noStakes')) | |
| .on('mousemove', (e) => showTooltip(e, d, 'noStakes')) | |
| .on('mouseleave', hideTooltip); | |
| } | |
| }); | |
| // Update legend | |
| legendDiv.innerHTML = ` | |
| <div class="legend-item"> | |
| <div class="legend-swatch" style="background: ${segmentColors.floored}"></div> | |
| <span class="legend-label">Score</span> | |
| </div> | |
| <div class="legend-item"> | |
| <div class="legend-swatch" style="background: ${segmentColors.noStakes}"></div> | |
| <span class="legend-label">No-Stakes Gain</span> | |
| </div> | |
| `; | |
| } | |
| // 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> | |