diff --git a/lib/entity_widgets/history_chart/combined_history_chart.dart b/lib/entity_widgets/history_chart/combined_history_chart.dart index d1c3610..42d3b78 100644 --- a/lib/entity_widgets/history_chart/combined_history_chart.dart +++ b/lib/entity_widgets/history_chart/combined_history_chart.dart @@ -19,6 +19,12 @@ class _CombinedHistoryChartWidgetState extends State int _selectedId = -1; List> _parsedHistory; + @override + void initState() { + // TODO: implement initState + super.initState(); + } + @override Widget build(BuildContext context) { _parsedHistory = _parseHistory(); @@ -28,11 +34,11 @@ class _CombinedHistoryChartWidgetState extends State if ((_selectedId > -1) && (_parsedHistory != null) && (_parsedHistory.first.data.length >= (_selectedId + 1))) { selectedTime = _parsedHistory.first.data[_selectedId].startTime; _parsedHistory.where((item) { return item.id == "state"; }).forEach((item) { - selectedStates.add("${item.data[_selectedId].state}"); + selectedStates.add(item.data[_selectedId].state); colorIndexes.add(item.data[_selectedId].colorId); }); _parsedHistory.where((item) { return item.id == "value"; }).forEach((item) { - selectedStates.add("${item.data[_selectedId].value}"); + selectedStates.add("${item.data[_selectedId].value ?? '-'}"); colorIndexes.add(item.data[_selectedId].colorId); }); } @@ -101,10 +107,11 @@ class _CombinedHistoryChartWidgetState extends State DateTime endTime; bool hiddenLine; double value; + double previousValue = 0.0; value = _parseToDouble(stateData["attributes"]["$attrName"]); bool hiddenDot = (value == null); if (hiddenDot && i > 0) { - value = data[i-1].value; + previousValue = data[i-1].value ?? data[i-1].previousValue; } if (i < (widget.rawHistory.length - 1)) { endTime = DateTime.tryParse(widget.rawHistory[i+1]["last_updated"])?.toLocal(); @@ -114,9 +121,9 @@ class _CombinedHistoryChartWidgetState extends State hiddenLine = hiddenDot; endTime = now; } - data.add(CombinedEntityStateHistoryMoment(value, hiddenDot, hiddenLine, stateData["state"], startTime, endTime, i, colorIdCounter)); + data.add(CombinedEntityStateHistoryMoment(value, previousValue, hiddenDot, hiddenLine, stateData["state"], startTime, endTime, i, colorIdCounter)); } - data.add(CombinedEntityStateHistoryMoment(data.last.value, data.last.hiddenDot, data.last.hiddenLine, data.last.state, now, null, widget.rawHistory.length, colorIdCounter)); + data.add(CombinedEntityStateHistoryMoment(data.last.value, data.last.previousValue, data.last.hiddenDot, data.last.hiddenLine, data.last.state, now, null, widget.rawHistory.length, colorIdCounter)); numericDataLists.addAll({attrName: data}); colorIdCounter += 1; }); @@ -142,7 +149,7 @@ class _CombinedHistoryChartWidgetState extends State }, strokeWidthPxFn: (CombinedEntityStateHistoryMoment historyMoment, __) => historyMoment.hiddenLine ? 0.0 : 2.0, domainFn: (CombinedEntityStateHistoryMoment historyMoment, _) => historyMoment.startTime, - measureFn: (CombinedEntityStateHistoryMoment historyMoment, _) => historyMoment.value ?? 0, + measureFn: (CombinedEntityStateHistoryMoment historyMoment, _) => historyMoment.value ?? historyMoment.previousValue, data: dataList, /*domainLowerBoundFn: (CombinedEntityStateHistoryMoment historyMoment, _) => historyMoment.time.subtract(Duration(hours: 1)), domainUpperBoundFn: (CombinedEntityStateHistoryMoment historyMoment, _) => historyMoment.time.add(Duration(hours: 1)),*/ @@ -254,7 +261,7 @@ class CombinedHistoryControlWidget extends StatelessWidget { for (int i = 0; i < selectedStates.length; i++) { children.add( Text( - "${selectedStates[i]}", + "${selectedStates[i] ?? '-'}", textAlign: TextAlign.right, style: TextStyle( fontWeight: FontWeight.bold, @@ -292,11 +299,12 @@ class CombinedEntityStateHistoryMoment { final DateTime startTime; final DateTime endTime; final double value; + final double previousValue; final int id; final int colorId; final String state; final bool hiddenDot; final bool hiddenLine; - CombinedEntityStateHistoryMoment(this.value, this.hiddenDot, this.hiddenLine, this.state, this.startTime, this.endTime, this.id, this.colorId); + CombinedEntityStateHistoryMoment(this.value, this.previousValue, this.hiddenDot, this.hiddenLine, this.state, this.startTime, this.endTime, this.id, this.colorId); } diff --git a/lib/entity_widgets/history_chart/numeric_state_history_chart.dart b/lib/entity_widgets/history_chart/numeric_state_history_chart.dart index ef40742..25f096f 100644 --- a/lib/entity_widgets/history_chart/numeric_state_history_chart.dart +++ b/lib/entity_widgets/history_chart/numeric_state_history_chart.dart @@ -34,7 +34,7 @@ class _NumericStateHistoryChartWidgetState extends State[ HistoryControlWidget( selectedTimeStart: selectedTime, - selectedState: "$selectedState", + selectedState: "${selectedState ?? '-'}", onPrevTap: () => _selectPrev(), onNextTap: () => _selectNext(), colorIndex: -1, @@ -72,9 +72,22 @@ class _NumericStateHistoryChartWidgetState extends State 0) { + previousValue = data[i-1].value ?? data[i-1].previousValue; + } + if (i < (widget.rawHistory.length - 1)) { + double nextValue = double.tryParse(widget.rawHistory[i+1]["state"]); + hiddenLine = (nextValue == null || hiddenDot); + } else { + hiddenLine = hiddenDot; + } + data.add(NumericEntityStateHistoryMoment(value, previousValue, hiddenDot, hiddenLine, time, i)); } - data.add(NumericEntityStateHistoryMoment(data.last.value, now, widget.rawHistory.length)); + data.add(NumericEntityStateHistoryMoment(data.last.value, data.last.previousValue, data.last.hiddenDot, data.last.hiddenLine, now, widget.rawHistory.length)); if (_selectedId == -1) { _selectedId = 0; } @@ -83,9 +96,18 @@ class _NumericStateHistoryChartWidgetState extends State EntityColors.chartHistoryStateColor("on", -1), domainFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.time, - measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value, + measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value ?? historyMoment.previousValue, data: data, - radiusPxFn: (NumericEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 1.0, + strokeWidthPxFn: (NumericEntityStateHistoryMoment historyMoment, __) => historyMoment.hiddenLine ? 0.0 : 2.0, + radiusPxFn: (NumericEntityStateHistoryMoment historyMoment, __) { + if (historyMoment.hiddenDot) { + return 0.0; + } else if (historyMoment.id == _selectedId) { + return 5.0; + } else { + return 1.0; + } + }, ) ]; } @@ -126,7 +148,10 @@ class _NumericStateHistoryChartWidgetState extends State