rawknee-69 commited on
Commit
745bf08
·
1 Parent(s): 8f04120

Fix real-time chart rendering during live battle runs.

Browse files

Lazy-init Chart.js instances when graphs tab first opens (avoids 0x0
canvas from hidden container), preserve array references with splice/
length=0 so chart datasets stay linked, and resize charts on tab switch.

Made-with: Cursor

Files changed (1) hide show
  1. public/index.html +62 -37
public/index.html CHANGED
@@ -364,22 +364,24 @@
364
  loss: [], violation: [], novelty: [], hallucination: [], holdoutAsr: [], holdoutHelpfulness: [],
365
  };
366
 
367
- // Chart.js Instances
368
  let charts = {};
 
 
369
 
370
  Chart.defaults.color = '#94a3b8';
371
  Chart.defaults.font.family = "'Inter', sans-serif";
372
-
373
- function initCharts() {
374
- const commonOptions = {
375
  responsive: true,
376
  maintainAspectRatio: false,
377
- animation: { duration: 400, easing: 'easeOutQuart' },
378
  scales: {
379
  x: { grid: { color: 'rgba(255,255,255,0.05)' }, title: { display: true, text: 'Turns' } },
380
  y: { grid: { color: 'rgba(255,255,255,0.05)' }, beginAtZero: true }
381
  },
382
- plugins: {
383
  legend: { position: 'top', labels: { usePointStyle: true, boxWidth: 8, font: { family: "'Inter', sans-serif", size: 11 } } },
384
  tooltip: {
385
  backgroundColor: 'rgba(15, 23, 42, 0.9)',
@@ -397,61 +399,80 @@
397
  },
398
  interaction: { mode: 'index', intersect: false }
399
  };
 
 
 
 
 
400
 
401
  charts.core = new Chart($('chart-core'), {
402
  type: 'line',
403
  data: { labels: series.turns, datasets: [
404
- { label: 'Violation', data: series.violation, borderColor: '#f97316', backgroundColor: 'rgba(249,115,22,0.1)', fill: true, tension: 0.4 },
405
- { label: 'Novelty', data: series.novelty, borderColor: '#fbbf24', backgroundColor: 'transparent', tension: 0.4 },
406
- { label: 'Hallucination', data: series.hallucination, borderColor: '#a855f7', backgroundColor: 'rgba(168,85,247,0.1)', fill: true, tension: 0.4 },
407
- { label: 'Red Reward', data: series.redReward, borderColor: '#ef4444', backgroundColor: 'transparent', tension: 0.4 },
408
- { label: 'Blue Reward', data: series.blueReward, borderColor: '#0ea5e9', backgroundColor: 'transparent', tension: 0.4 }
409
  ]},
410
- options: { ...commonOptions, scales: { y: { min: 0, max: 1.0, grid: { color: 'rgba(255,255,255,0.05)' } }, x: commonOptions.scales.x } }
411
  });
412
 
413
  charts.weights = new Chart($('chart-weights'), {
414
  type: 'line',
415
- data: { labels: series.turns, datasets: [{ label: 'Weight Delta Magnitude', data: series.weightDelta, borderColor: '#10b981', backgroundColor: 'rgba(16,185,129,0.1)', fill: true, tension: 0.4 }] },
416
- options: commonOptions
417
  });
418
 
419
  charts.rewards = new Chart($('chart-rewards'), {
420
  type: 'line',
421
  data: { labels: series.turns, datasets: [
422
- { label: 'Red Reward', data: series.redReward, borderColor: '#ef4444', backgroundColor: 'transparent', tension: 0.4 },
423
- { label: 'Blue Reward', data: series.blueReward, borderColor: '#0ea5e9', backgroundColor: 'transparent', tension: 0.4 },
424
- { label: 'Loss', data: series.loss, borderColor: '#f59e0b', backgroundColor: 'transparent', tension: 0.4, borderDash: [5, 5] }
425
  ]},
426
- options: commonOptions
427
  });
428
 
429
  charts.grader = new Chart($('chart-grader'), {
430
  type: 'line',
431
  data: { labels: series.turns, datasets: [
432
- { label: 'Violation Score', data: series.violation, borderColor: '#f97316', backgroundColor: 'rgba(249,115,22,0.1)', fill: true, tension: 0.4 },
433
- { label: 'Hallucination Score', data: series.hallucination, borderColor: '#a855f7', backgroundColor: 'rgba(168,85,247,0.1)', fill: true, tension: 0.4 }
434
  ]},
435
- options: { ...commonOptions, scales: { y: { min: 0, max: 1.0, grid: { color: 'rgba(255,255,255,0.05)' } }, x: commonOptions.scales.x } }
436
  });
437
 
438
  charts.holdout = new Chart($('chart-holdout'), {
439
  type: 'line',
440
  data: { labels: series.turns, datasets: [
441
- { label: 'Holdout ASR', data: series.holdoutAsr, borderColor: '#22c55e', tension: 0.4 },
442
- { label: 'Holdout Helpfulness', data: series.holdoutHelpfulness, borderColor: '#38bdf8', tension: 0.4 }
443
  ]},
444
- options: { ...commonOptions, scales: { y: { min: 0, max: 1.0, grid: { color: 'rgba(255,255,255,0.05)' } }, x: commonOptions.scales.x } }
445
  });
 
 
 
446
  }
447
 
448
  function updateCharts() {
449
- if(!charts.weights) return;
450
- charts.core.update();
451
- charts.weights.update();
452
- charts.rewards.update();
453
- charts.grader.update();
454
- charts.holdout.update();
 
 
 
 
 
 
 
 
 
 
 
455
  }
456
 
457
  async function api(path, options = {}) {
@@ -771,7 +792,7 @@
771
  series.novelty.push(Number(turn.novelty_score || 0));
772
  series.hallucination.push(Number(turn.hallucination_score || 0));
773
  });
774
- if (series.turns.length > 60) Object.keys(series).forEach(k => series[k] = series[k].slice(-60));
775
 
776
  updateCharts();
777
  }
@@ -793,7 +814,9 @@
793
  $('page-main').classList.toggle('hidden', target !== 'main');
794
  $('page-graphs').classList.toggle('hidden', target !== 'graphs');
795
  $('page-logs').classList.toggle('hidden', target !== 'logs');
796
- if (target === 'graphs') updateCharts();
 
 
797
  if (target === 'logs') loadLogsTree();
798
  }
799
 
@@ -804,8 +827,8 @@
804
  series.holdoutAsr.push(Number(kpi.red_asr || 0));
805
  series.holdoutHelpfulness.push(Number(kpi.blue_helpfulness || 0));
806
  if (series.holdoutAsr.length > 60) {
807
- series.holdoutAsr = series.holdoutAsr.slice(-60);
808
- series.holdoutHelpfulness = series.holdoutHelpfulness.slice(-60);
809
  }
810
  } catch (_) {}
811
  }
@@ -822,8 +845,11 @@
822
  gameEntities.red.text = ''; gameEntities.blue.text = '';
823
  gameEntities.red.x = 200; gameEntities.blue.x = 1000;
824
  gameEntities.projectiles = []; gameEntities.explosions = [];
825
- Object.keys(series).forEach(k => series[k] = []);
826
- updateCharts();
 
 
 
827
  }
828
 
829
  function startPolling(sessionId) {
@@ -1009,7 +1035,6 @@
1009
  }
1010
 
1011
  window.onload = () => {
1012
- initCharts();
1013
  loadTasks();
1014
  };
1015
  </script>
 
364
  loss: [], violation: [], novelty: [], hallucination: [], holdoutAsr: [], holdoutHelpfulness: [],
365
  };
366
 
367
+ // Chart.js Instances — lazy-initialized when graphs tab is first shown
368
  let charts = {};
369
+ let chartsInitialized = false;
370
+ let chartsDirty = false;
371
 
372
  Chart.defaults.color = '#94a3b8';
373
  Chart.defaults.font.family = "'Inter', sans-serif";
374
+
375
+ function getCommonChartOptions() {
376
+ return {
377
  responsive: true,
378
  maintainAspectRatio: false,
379
+ animation: { duration: 300, easing: 'easeOutQuart' },
380
  scales: {
381
  x: { grid: { color: 'rgba(255,255,255,0.05)' }, title: { display: true, text: 'Turns' } },
382
  y: { grid: { color: 'rgba(255,255,255,0.05)' }, beginAtZero: true }
383
  },
384
+ plugins: {
385
  legend: { position: 'top', labels: { usePointStyle: true, boxWidth: 8, font: { family: "'Inter', sans-serif", size: 11 } } },
386
  tooltip: {
387
  backgroundColor: 'rgba(15, 23, 42, 0.9)',
 
399
  },
400
  interaction: { mode: 'index', intersect: false }
401
  };
402
+ }
403
+
404
+ function initCharts() {
405
+ if (chartsInitialized) return;
406
+ const co = getCommonChartOptions();
407
 
408
  charts.core = new Chart($('chart-core'), {
409
  type: 'line',
410
  data: { labels: series.turns, datasets: [
411
+ { label: 'Violation', data: series.violation, borderColor: '#f97316', backgroundColor: 'rgba(249,115,22,0.1)', fill: true, tension: 0.4, pointRadius: 3 },
412
+ { label: 'Novelty', data: series.novelty, borderColor: '#fbbf24', backgroundColor: 'transparent', tension: 0.4, pointRadius: 3 },
413
+ { label: 'Hallucination', data: series.hallucination, borderColor: '#a855f7', backgroundColor: 'rgba(168,85,247,0.1)', fill: true, tension: 0.4, pointRadius: 3 },
414
+ { label: 'Red Reward', data: series.redReward, borderColor: '#ef4444', backgroundColor: 'transparent', tension: 0.4, pointRadius: 3 },
415
+ { label: 'Blue Reward', data: series.blueReward, borderColor: '#0ea5e9', backgroundColor: 'transparent', tension: 0.4, pointRadius: 3 }
416
  ]},
417
+ options: { ...co, scales: { y: { min: 0, max: 1.0, grid: { color: 'rgba(255,255,255,0.05)' } }, x: co.scales.x } }
418
  });
419
 
420
  charts.weights = new Chart($('chart-weights'), {
421
  type: 'line',
422
+ data: { labels: series.turns, datasets: [{ label: 'Weight Delta Magnitude', data: series.weightDelta, borderColor: '#10b981', backgroundColor: 'rgba(16,185,129,0.1)', fill: true, tension: 0.4, pointRadius: 3 }] },
423
+ options: co
424
  });
425
 
426
  charts.rewards = new Chart($('chart-rewards'), {
427
  type: 'line',
428
  data: { labels: series.turns, datasets: [
429
+ { label: 'Red Reward', data: series.redReward, borderColor: '#ef4444', backgroundColor: 'transparent', tension: 0.4, pointRadius: 3 },
430
+ { label: 'Blue Reward', data: series.blueReward, borderColor: '#0ea5e9', backgroundColor: 'transparent', tension: 0.4, pointRadius: 3 },
431
+ { label: 'Loss', data: series.loss, borderColor: '#f59e0b', backgroundColor: 'transparent', tension: 0.4, borderDash: [5, 5], pointRadius: 3 }
432
  ]},
433
+ options: co
434
  });
435
 
436
  charts.grader = new Chart($('chart-grader'), {
437
  type: 'line',
438
  data: { labels: series.turns, datasets: [
439
+ { label: 'Violation Score', data: series.violation, borderColor: '#f97316', backgroundColor: 'rgba(249,115,22,0.1)', fill: true, tension: 0.4, pointRadius: 3 },
440
+ { label: 'Hallucination Score', data: series.hallucination, borderColor: '#a855f7', backgroundColor: 'rgba(168,85,247,0.1)', fill: true, tension: 0.4, pointRadius: 3 }
441
  ]},
442
+ options: { ...co, scales: { y: { min: 0, max: 1.0, grid: { color: 'rgba(255,255,255,0.05)' } }, x: co.scales.x } }
443
  });
444
 
445
  charts.holdout = new Chart($('chart-holdout'), {
446
  type: 'line',
447
  data: { labels: series.turns, datasets: [
448
+ { label: 'Holdout ASR', data: series.holdoutAsr, borderColor: '#22c55e', tension: 0.4, pointRadius: 3 },
449
+ { label: 'Holdout Helpfulness', data: series.holdoutHelpfulness, borderColor: '#38bdf8', tension: 0.4, pointRadius: 3 }
450
  ]},
451
+ options: { ...co, scales: { y: { min: 0, max: 1.0, grid: { color: 'rgba(255,255,255,0.05)' } }, x: co.scales.x } }
452
  });
453
+
454
+ chartsInitialized = true;
455
+ chartsDirty = false;
456
  }
457
 
458
  function updateCharts() {
459
+ if (!chartsInitialized) {
460
+ chartsDirty = true;
461
+ return;
462
+ }
463
+ Object.values(charts).forEach(c => { c.resize(); c.update('none'); });
464
+ }
465
+
466
+ function ensureChartsVisible() {
467
+ if (!chartsInitialized) {
468
+ initCharts();
469
+ return;
470
+ }
471
+ Object.values(charts).forEach(c => c.resize());
472
+ if (chartsDirty) {
473
+ Object.values(charts).forEach(c => c.update('none'));
474
+ chartsDirty = false;
475
+ }
476
  }
477
 
478
  async function api(path, options = {}) {
 
792
  series.novelty.push(Number(turn.novelty_score || 0));
793
  series.hallucination.push(Number(turn.hallucination_score || 0));
794
  });
795
+ if (series.turns.length > 60) Object.keys(series).forEach(k => series[k].splice(0, series[k].length - 60));
796
 
797
  updateCharts();
798
  }
 
814
  $('page-main').classList.toggle('hidden', target !== 'main');
815
  $('page-graphs').classList.toggle('hidden', target !== 'graphs');
816
  $('page-logs').classList.toggle('hidden', target !== 'logs');
817
+ if (target === 'graphs') {
818
+ requestAnimationFrame(() => ensureChartsVisible());
819
+ }
820
  if (target === 'logs') loadLogsTree();
821
  }
822
 
 
827
  series.holdoutAsr.push(Number(kpi.red_asr || 0));
828
  series.holdoutHelpfulness.push(Number(kpi.blue_helpfulness || 0));
829
  if (series.holdoutAsr.length > 60) {
830
+ series.holdoutAsr.splice(0, series.holdoutAsr.length - 60);
831
+ series.holdoutHelpfulness.splice(0, series.holdoutHelpfulness.length - 60);
832
  }
833
  } catch (_) {}
834
  }
 
845
  gameEntities.red.text = ''; gameEntities.blue.text = '';
846
  gameEntities.red.x = 200; gameEntities.blue.x = 1000;
847
  gameEntities.projectiles = []; gameEntities.explosions = [];
848
+ Object.keys(series).forEach(k => series[k].length = 0);
849
+ if (chartsInitialized) {
850
+ Object.values(charts).forEach(c => c.update('none'));
851
+ }
852
+ chartsDirty = false;
853
  }
854
 
855
  function startPolling(sessionId) {
 
1035
  }
1036
 
1037
  window.onload = () => {
 
1038
  loadTasks();
1039
  };
1040
  </script>