Skip to content

Commit 2b37edb

Browse files
committed
api: fix line chart rendering with null gaps (use labels+data arrays, not {x,y} objects)
Chart.js 4's LineController.parseObjectData throws 'Cannot read properties of null (reading x)' when a dataset mixes {x,y} point objects with null entries. This aborted loadMainCharts() after the PV chart, leaving the overview/battery/SOC charts blank. Switch to the standard labels (timestamps) + data (values) array form, which Chart.js handles natively for gaps via spanGaps:false.
1 parent 18ba765 commit 2b37edb

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

‎api/static/index.html‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,13 @@ <h3 id="rule-editor-title">Edit Rule</h3>
666666
renderDetailHTML();
667667
}
668668

669-
function createLineChart(id, datasets, opts={}) {
669+
function createLineChart(id, labels, datasets, opts={}) {
670670
const ctx = document.getElementById(id);
671671
if (!ctx) return;
672672
if (charts[id]) charts[id].destroy();
673673
charts[id] = new Chart(ctx.getContext('2d'), {
674674
type:'line',
675-
data:{datasets},
675+
data:{labels, datasets},
676676
options:{
677677
responsive:true,
678678
maintainAspectRatio:false,
@@ -708,14 +708,15 @@ <h3 id="rule-editor-title">Edit Rule</h3>
708708
try {
709709
const res = await fetchJson(API + '/api/history?minutes=1440&max_points=300&fields=pv1_power,pv2_power,charge_power,discharge_power,grid_import_power,grid_export_power,load_power,battery_voltage,soc');
710710
const rows = res.snapshots || [];
711+
const labels = rows.map(r => r.timestamp);
711712
const mk = (label, field, color, fill=true) => ({
712713
label,
713-
data: rows.map(r => r.registers[field] != null ? {x:r.timestamp, y:r.registers[field]} : null),
714+
data: rows.map(r => r.registers[field] != null ? r.registers[field] : null),
714715
spanGaps: false,
715716
borderColor: color, backgroundColor: color + (fill?'33':''), fill, tension:0.2, pointRadius:0
716717
});
717-
createLineChart('chart-pv-today', [mk('PV1','pv1_power','#eab308'), mk('PV2','pv2_power','#facc15')]);
718-
createLineChart('chart-overview-24h', [
718+
createLineChart('chart-pv-today', labels, [mk('PV1','pv1_power','#eab308'), mk('PV2','pv2_power','#facc15')]);
719+
createLineChart('chart-overview-24h', labels, [
719720
mk('Solar','pv1_power','#eab308'),
720721
mk('Battery charge','charge_power','#a855f7'),
721722
mk('Battery discharge','discharge_power','#22c55e'),
@@ -731,13 +732,13 @@ <h3 id="rule-editor-title">Edit Rule</h3>
731732
const d = r.registers['discharge_power'];
732733
const c = r.registers['charge_power'];
733734
if (d == null && c == null) return null;
734-
return { x: r.timestamp, y: (d || 0) - (c || 0) };
735+
return (d || 0) - (c || 0);
735736
}),
736737
spanGaps: false,
737738
borderColor: '#22c55e', backgroundColor: '#22c55e', fill: false, tension: 0.2, pointRadius: 0
738739
};
739-
createLineChart('chart-battery-power', [batPower]);
740-
createLineChart('chart-battery-soc', [mk('SOC','soc','#a855f7', false)]);
740+
createLineChart('chart-battery-power', labels, [batPower]);
741+
createLineChart('chart-battery-soc', labels, [mk('SOC','soc','#a855f7', false)]);
741742
} catch (e) { console.error('loadMainCharts error', e); }
742743
}
743744

0 commit comments

Comments
 (0)