// 02 UTILITY ANALYTICS — synthetic 30-day data, actual selectable points. const facilities={a:{factor:1},b:{factor:.78},c:{factor:1.23}}; const metricInfo={flow:{base:14,unit:'MGD',precision:1,limit:1.21,title:'Flow'},energy:{base:4200,unit:'kWh/day',precision:0,limit:1.20,title:'Energy'},turbidity:{base:.52,unit:'NTU',precision:2,limit:1.80,title:'Turbidity'}}; function sampleData(facility,metric){ const m=metricInfo[metric],f=facilities[facility].factor; return Array.from({length:30},(_,i)=>{ const wobble=1+.07*Math.sin(i*.87+(['a','b','c'].indexOf(facility)+1))+.06*Math.cos(i*.32+metric.length); const incident=[11,25].includes(i)?(metric==='turbidity'?2.25:1.35):1; return {day:i+1,value:Math.max(0,m.base*f*wobble*incident)}; }); } const fmt=(num,p)=>num.toLocaleString('en-US',{minimumFractionDigits:p,maximumFractionDigits:p}); function initAnalytics(){ let period=7,chosen=null; function paint(){ const metric=$('#metric-select').value,facility=$('#facility-select').value,m=metricInfo[metric]; const data=sampleData(facility,metric).slice(-period); const mean=data.reduce((sum,p)=>sum+p.value,0)/data.length; const threshold=m.base*facilities[facility].factor*m.limit; const alerts=data.filter(p=>p.value>threshold); setText('#metric-average',fmt(mean,m.precision));setText('#metric-units',m.unit); setText('#metric-last',fmt(data.at(-1).value,m.precision));setText('#last-note','Day '+data.at(-1).day); setText('#metric-outliers',alerts.length); setText('#plot-title',`${m.title.toUpperCase()} / LAST ${period} DAYS`); setText('#chart-axis-start','Day '+data[0].day);setText('#chart-axis-end','Day '+data.at(-1).day); const low=Math.min(...data.map(x=>x.value))*0.85,high=Math.max(...data.map(x=>x.value))*1.13; const y=v=>270-215*(v-low)/(high-low||1),x=i=>40+i*820/(data.length-1); const coords=data.map((p,i)=>[x(i),y(p.value)]); $('#chart-line').setAttribute('d',coords.map(([xx,yy],i)=>(i?'L':'M')+xx.toFixed(2)+' '+yy.toFixed(2)).join(' ')); $('#chart-area').setAttribute('d',coords.map(([xx,yy],i)=>(i?'L':'M')+xx.toFixed(2)+' '+yy.toFixed(2)).join(' ')+'L860 270L40 270Z'); const grid=$('#chart-grid');grid.replaceChildren(); for(let i=0;i<4;i++){const line=document.createElementNS(namespaces,'line');const yy=56+i*69;line.setAttribute('x1','40');line.setAttribute('x2','860');line.setAttribute('y1',yy);line.setAttribute('y2',yy);line.setAttribute('stroke','#d9d1ed');line.setAttribute('stroke-dasharray','4 7');grid.append(line);} const dots=$('#chart-points');dots.replaceChildren(); for(const [i,p] of data.entries()){ const isAlert=p.value>threshold,showAlerts=$('#show-alerts').checked; const dot=document.createElementNS(namespaces,'circle');dot.setAttribute('cx',coords[i][0]);dot.setAttribute('cy',coords[i][1]);dot.setAttribute('r',chosen===p.day?9:isAlert&&showAlerts?8:5); dot.setAttribute('fill',isAlert&&showAlerts?'#e78572':'#7962c8');dot.setAttribute('class','plot-dot'+(chosen===p.day?' selected':'')); dot.setAttribute('tabindex','0');dot.setAttribute('role','button');dot.setAttribute('aria-label',`Day ${p.day}, ${fmt(p.value,m.precision)} ${m.unit}`); const select=()=>{chosen=p.day;showInsight(p,mean,threshold,m,isAlert);paintDots();}; dot.addEventListener('click',select);dot.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){e.preventDefault();select();}});dots.append(dot); } function paintDots(){for(const dot of $$('.plot-dot',dots)){const idx=[...dots.children].indexOf(dot);const on=data[idx].day===chosen;dot.classList.toggle('selected',on);dot.setAttribute('r',on?9:(data[idx].value>threshold&&$('#show-alerts').checked)?8:5);}} function showInsight(p,avg,lim,mi,alert){ setText('#insight-title',`Day ${p.day} · ${fmt(p.value,mi.precision)} ${mi.unit}`); const offset=(100*(p.value/avg-1)); setText('#insight-text',`${offset>=0?'+':''}${offset.toFixed(1)}% versus the selected period average. ${alert?'This exceeds the illustrative outlier threshold. Investigate before drawing conclusions.':'Within the illustrative sample threshold.'}`); } if(chosen&&data.some(p=>p.day===chosen)){showInsight(data.find(p=>p.day===chosen),mean,threshold,m,data.find(p=>p.day===chosen).value>threshold);}else{ chosen=null;setText('#insight-title',`${alerts.length} ${alerts.length===1?'outlier':'outliers'} in this view`); setText('#insight-text',alerts.length?`Days ${alerts.map(x=>x.day).join(', ')} cross the illustrative alert threshold. Select a dot to investigate.`:'No values cross the illustrative alert threshold. Select a dot for details.'); } } $('#facility-select').addEventListener('change',()=>{chosen=null;paint();}); $('#metric-select').addEventListener('change',()=>{chosen=null;paint();}); $('#show-alerts').addEventListener('change',paint); $$('[data-period]').forEach(btn=>btn.addEventListener('click',()=>{period=Number(btn.dataset.period);chosen=null;$$('[data-period]').forEach(x=>x.setAttribute('aria-pressed',String(x===btn)));paint();})); paint(); }