Add live voting pie chart (Chart.js doughnut) for player and host views
- Loads Chart.js 4.4.4 from jsDelivr CDN - Canvas elements in #pVoteCard (player) and #hVoteCard (host sidebar) - computeTally() counts only locked votes — no ghost nobody entries - updateVotingChart() creates on first call with 600ms entry animation, then patches data in-place (update 'none') on each poll tick - destroyChart() tears down the Chart.js instance and hides the canvas - Player chart appears once the player has locked their vote (or after resolution); host chart appears whenever the vote tally sidebar is shown - Both charts destroyed and hidden on newRound() - Theme-matched colors (navy, accent, green, red, gold) on dark bg Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f8f6f5afeb
commit
841109d995
1 changed files with 125 additions and 0 deletions
125
index.html
125
index.html
|
|
@ -9,6 +9,7 @@
|
|||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.4/dist/chart.umd.min.js"></script>
|
||||
<style>
|
||||
:root{
|
||||
--bg:#0e1117;--bg2:#141820;--panel:#1a2030;--panel2:#1f2840;--line:#2a3550;
|
||||
|
|
@ -320,6 +321,10 @@
|
|||
.nav-links a{padding:4px 8px;font-size:11px}
|
||||
}
|
||||
|
||||
/* ── Voting Pie Chart ───────────────────────────────────── */
|
||||
.voting-chart{display:block;width:100%!important;max-width:320px;
|
||||
height:auto!important;aspect-ratio:1;margin:14px auto 4px}
|
||||
|
||||
/* ── Chat ───────────────────────────────────────────────── */
|
||||
.chat-card{display:flex;flex-direction:column;max-height:280px}
|
||||
.chat-msgs{flex:1;overflow-y:auto;min-height:80px;max-height:200px;
|
||||
|
|
@ -445,6 +450,7 @@
|
|||
<div class="card hidden" id="pVoteCard">
|
||||
<h2><span class="dot"></span>Wer ist der Imposter?</h2>
|
||||
<div id="pVoteGrid"></div>
|
||||
<canvas id="pVotingChart" class="voting-chart hidden"></canvas>
|
||||
<div id="pOutcomeBanner"></div>
|
||||
</div>
|
||||
|
||||
|
|
@ -504,6 +510,7 @@
|
|||
<div class="sidebar-card hidden" id="hVoteCard">
|
||||
<div class="sidebar-title">Voting</div>
|
||||
<div id="hVoteTally"></div>
|
||||
<canvas id="hVotingChart" class="voting-chart hidden"></canvas>
|
||||
</div>
|
||||
|
||||
<!-- Punktestand -->
|
||||
|
|
@ -721,6 +728,112 @@ const App = (() => {
|
|||
try{ const d=await api('state',{code:room},'GET'); render(d.state); }catch(e){}
|
||||
}
|
||||
|
||||
// ── Voting Pie Chart ──────────────────────────────────────
|
||||
const CHART_COLORS=['#4A5BE8','#3ECC5C','#e85d5c','#ffcf6e','#2bb585',
|
||||
'#6B7BFF','#65DC7A','#f09b3a','#85B7EB','#D4537E'];
|
||||
const chartInstances={};
|
||||
|
||||
function computeTally(players){
|
||||
// Only locked votes; unlocked players don't contribute
|
||||
const counts={__nobody__:0};
|
||||
players.forEach(p=>{ counts[p.pkey]=0; });
|
||||
players.filter(p=>p.voteLocked).forEach(p=>{
|
||||
const vs=p.votes||[];
|
||||
if(!vs.length||vs.includes('__nobody__')){ counts.__nobody__++; }
|
||||
else{ vs.forEach(v=>{ if(counts[v]!==undefined) counts[v]++; }); }
|
||||
});
|
||||
return counts;
|
||||
}
|
||||
|
||||
function updateVotingChart(canvasId, players, state){
|
||||
const canvas=$(canvasId);
|
||||
if(!canvas) return;
|
||||
|
||||
const counts=computeTally(players);
|
||||
// Build label/data arrays — skip entries with 0 votes
|
||||
const labels=[];
|
||||
const data=[];
|
||||
const colors=[];
|
||||
let colorIdx=0;
|
||||
players.forEach(p=>{
|
||||
if(counts[p.pkey]>0){
|
||||
labels.push(p.name);
|
||||
data.push(counts[p.pkey]);
|
||||
colors.push(CHART_COLORS[colorIdx%CHART_COLORS.length]);
|
||||
colorIdx++;
|
||||
}
|
||||
});
|
||||
if(counts.__nobody__>0){
|
||||
labels.push('Niemand');
|
||||
data.push(counts.__nobody__);
|
||||
colors.push('#7a8aaa');
|
||||
}
|
||||
|
||||
if(!data.length){ show(canvasId,false); return; }
|
||||
show(canvasId,true);
|
||||
|
||||
if(chartInstances[canvasId]){
|
||||
// Update existing chart in place (no flicker)
|
||||
const ch=chartInstances[canvasId];
|
||||
ch.data.labels=labels;
|
||||
ch.data.datasets[0].data=data;
|
||||
ch.data.datasets[0].backgroundColor=colors;
|
||||
ch.update('none'); // skip animation on live updates
|
||||
return;
|
||||
}
|
||||
|
||||
// First render — create with entry animation
|
||||
chartInstances[canvasId]=new Chart(canvas,{
|
||||
type:'doughnut',
|
||||
data:{
|
||||
labels,
|
||||
datasets:[{
|
||||
data,
|
||||
backgroundColor:colors,
|
||||
borderColor:'#141820',
|
||||
borderWidth:3,
|
||||
hoverOffset:8,
|
||||
}]
|
||||
},
|
||||
options:{
|
||||
responsive:true,
|
||||
maintainAspectRatio:true,
|
||||
cutout:'55%',
|
||||
animation:{duration:600,easing:'easeInOutQuart'},
|
||||
plugins:{
|
||||
legend:{
|
||||
position:'bottom',
|
||||
labels:{color:'#a8b8d8',font:{family:'Inter',size:12},
|
||||
padding:12,boxWidth:12,boxHeight:12}
|
||||
},
|
||||
tooltip:{
|
||||
callbacks:{
|
||||
label:(ctx)=>{
|
||||
const total=ctx.dataset.data.reduce((a,b)=>a+b,0);
|
||||
const pct=total?Math.round(ctx.parsed/total*100):0;
|
||||
return ` ${ctx.parsed} Stimme${ctx.parsed!==1?'n':''} (${pct}%)`;
|
||||
}
|
||||
},
|
||||
backgroundColor:'#1a2030',
|
||||
borderColor:'#2a3550',
|
||||
borderWidth:1,
|
||||
titleColor:'#e8edf8',
|
||||
bodyColor:'#a8b8d8',
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function destroyChart(canvasId){
|
||||
if(chartInstances[canvasId]){
|
||||
chartInstances[canvasId].destroy();
|
||||
delete chartInstances[canvasId];
|
||||
}
|
||||
const canvas=$(canvasId);
|
||||
if(canvas) show(canvasId,false);
|
||||
}
|
||||
|
||||
// ── Chat ──────────────────────────────────────────────────
|
||||
let lastChatLen=0;
|
||||
function renderChat(msgs, elId){
|
||||
|
|
@ -954,7 +1067,15 @@ const App = (() => {
|
|||
}
|
||||
|
||||
$('pOutcomeBanner').innerHTML=outcomeHTML(state,pkey)+(resolved?voteResultsHTML(state):'');
|
||||
|
||||
// Pie chart: show when player has voted or round is resolved
|
||||
if(me.voteLocked||resolved){
|
||||
updateVotingChart('pVotingChart',players,state);
|
||||
} else {
|
||||
destroyChart('pVotingChart');
|
||||
}
|
||||
} else {
|
||||
destroyChart('pVotingChart');
|
||||
show('pVoteCard',false);
|
||||
const waiting=players.filter(p=>!p.answered).length;
|
||||
box.innerHTML=`<div class="status live"><span class="live-dot"></span>
|
||||
|
|
@ -1166,6 +1287,9 @@ const App = (() => {
|
|||
<span class="sb-name" style="color:var(--muted)">Niemand</span>
|
||||
<span class="sb-score" style="font-size:13px">${tally['__nobody__']||0}🗳</span>
|
||||
</div>`;
|
||||
updateVotingChart('hVotingChart', players, state);
|
||||
} else {
|
||||
destroyChart('hVotingChart');
|
||||
}
|
||||
|
||||
if(state.roundNum !== lastRoundNum){
|
||||
|
|
@ -1336,6 +1460,7 @@ const App = (() => {
|
|||
lastRoundNum=-1; localVotes=new Set(); lastChatLen=0;
|
||||
$('hNormalQ').value=''; $('hImposterQ').value='';
|
||||
if($('pAnswer')) $('pAnswer').value='';
|
||||
destroyChart('pVotingChart'); destroyChart('hVotingChart');
|
||||
refresh();
|
||||
}catch(e){ err(e.message); }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue