WIP #120 Random color for states

This commit is contained in:
Yegor Vialov 2018-10-28 21:02:38 +02:00
parent fcd4ac7292
commit c20200b609
6 changed files with 78 additions and 50 deletions

View File

@ -1,41 +1,5 @@
part of '../main.dart';
class EntityColors {
static const _stateColors = {
"on": Colors.amber,
"auto": Colors.amber,
"idle": Colors.amber,
"playing": Colors.amber,
"above_horizon": Colors.amber,
"home": Colors.amber,
"open": Colors.amber,
"off": Color.fromRGBO(68, 115, 158, 1.0),
"closed": Color.fromRGBO(68, 115, 158, 1.0),
"default": Color.fromRGBO(68, 115, 158, 1.0),
"heat": Colors.redAccent,
"cool": Colors.lightBlue,
"closing": Colors.cyan,
"opening": Colors.purple,
"unavailable": Colors.black26,
"unknown": Colors.black26,
};
static Color stateColor(String state) {
return _stateColors[state] ?? _stateColors["default"];
}
static charts.Color historyStateColor(String state) {
Color c = stateColor(state);
return charts.Color(
r: c.red,
g: c.green,
b: c.blue,
a: c.alpha
);
}
}
class Entity {
static const badgeColors = {

View File

@ -0,0 +1,56 @@
part of '../main.dart';
class EntityColors {
static const _stateColors = {
"on": Colors.amber,
"auto": Colors.amber,
"idle": Colors.amber,
"playing": Colors.amber,
"above_horizon": Colors.amber,
"home": Colors.amber,
"open": Colors.amber,
"off": Color.fromRGBO(68, 115, 158, 1.0),
"closed": Color.fromRGBO(68, 115, 158, 1.0),
"below_horizon": Color.fromRGBO(68, 115, 158, 1.0),
"default": Color.fromRGBO(68, 115, 158, 1.0),
"heat": Colors.redAccent,
"cool": Colors.lightBlue,
"closing": Colors.cyan,
"opening": Colors.purple,
"unavailable": Colors.black26,
"unknown": Colors.black26,
};
static Color stateColor(String state) {
return _stateColors[state] ?? _stateColors["default"];
}
static charts.Color chartHistoryStateColor(String state, int id) {
Color c = _stateColors[state];
if (c != null) {
return charts.Color(
r: c.red,
g: c.green,
b: c.blue,
a: c.alpha
);
} else {
return charts.MaterialPalette.getOrderedPalettes(id+1)[id].shadeDefault;
}
}
static Color historyStateColor(String state, int id) {
Color c = _stateColors[state];
if (c != null) {
return c;
} else {
if (id > -1) {
charts.Color c1 = charts.MaterialPalette.getOrderedPalettes(id + 1)[id].shadeDefault;
return Color.fromARGB(c1.a, c1.r, c1.g, c1.b);
} else {
return _stateColors["default"];
}
}
}
}

View File

@ -22,7 +22,6 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> {
List _history;
bool _needToUpdateHistory;
int _selectedId = -1;
@override
void initState() {
@ -66,7 +65,7 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> {
);
} else if (_history.isEmpty) {
children.add(
Text("No history for last 24h")
Text("No history")
);
} else {
children.add(

View File

@ -36,6 +36,7 @@ class _NumericStateHistoryChartWidgetState extends State<NumericStateHistoryChar
selectedState: "$selectedState",
onPrevTap: () => _selectPrev(),
onNextTap: () => _selectNext(),
colorIndex: -1,
),
SizedBox(
height: 150.0,
@ -79,18 +80,21 @@ class _NumericStateHistoryChartWidgetState extends State<NumericStateHistoryChar
data.add(NumericEntityStateHistoryMoment(double.tryParse(stateData["state"]), time, i));
}
data.add(NumericEntityStateHistoryMoment(data.last.value, now, widget.rawHistory.length));
if (_selectedId == -1) {
_selectedId = 0;
}
return [
new charts.Series<NumericEntityStateHistoryMoment, DateTime>(
id: 'State',
colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => EntityColors.historyStateColor("unavailable"),
colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("unavailable", historyMoment.id),
domainFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.time,
measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value,
data: data,
),
new charts.Series<NumericEntityStateHistoryMoment, DateTime>(
id: 'State',
radiusPxFn: (NumericEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 4.0 : 2.0,
colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? EntityColors.historyStateColor("on") : EntityColors.historyStateColor("off"),
radiusPxFn: (NumericEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 2.0,
colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("off", historyMoment.id),
domainFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.time,
measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value,
data: data,

View File

@ -39,6 +39,7 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW
selectedState: selectedState,
onPrevTap: () => _selectPrev(),
onNextTap: () => _selectNext(),
colorIndex: _selectedId,
),
SizedBox(
height: 70.0,
@ -79,11 +80,14 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW
data.add(SimpleEntityStateHistoryMoment(stateData["state"], startTime, endTime, i));
}
data.add(SimpleEntityStateHistoryMoment(data.last.state, now, null, widget.rawHistory.length));
if (_selectedId == -1) {
_selectedId = 0;
}
return [
new charts.Series<SimpleEntityStateHistoryMoment, DateTime>(
id: 'State',
strokeWidthPxFn: (SimpleEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 70.0 : 40.0,
colorFn: (SimpleEntityStateHistoryMoment historyMoment, __) => EntityColors.historyStateColor(historyMoment.state),
colorFn: (SimpleEntityStateHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor(historyMoment.state, historyMoment.id),
domainFn: (SimpleEntityStateHistoryMoment historyMoment, _) => historyMoment.startTime,
measureFn: (SimpleEntityStateHistoryMoment historyMoment, _) => 0,
data: data,
@ -131,8 +135,9 @@ class HistoryControlWidget extends StatelessWidget {
final DateTime selectedTimeStart;
final DateTime selectedTimeEnd;
final String selectedState;
final int colorIndex;
const HistoryControlWidget({Key key, this.onPrevTap, this.onNextTap, this.selectedTimeStart, this.selectedTimeEnd, this.selectedState}) : super(key: key);
const HistoryControlWidget({Key key, this.onPrevTap, this.onNextTap, this.selectedTimeStart, this.selectedTimeEnd, this.selectedState, @ required this.colorIndex}) : super(key: key);
@override
Widget build(BuildContext context) {
@ -156,7 +161,7 @@ class HistoryControlWidget extends StatelessWidget {
textAlign: TextAlign.right,
style: TextStyle(
fontWeight: FontWeight.bold,
color: EntityColors.stateColor(selectedState),
color: EntityColors.historyStateColor(selectedState, colorIndex),
fontSize: 22.0
),
),
@ -173,7 +178,7 @@ class HistoryControlWidget extends StatelessWidget {
);
} else {
return Container(height: 32.0);
return Container(height: 48.0);
}
}
@ -187,11 +192,9 @@ class HistoryControlWidget extends StatelessWidget {
Text("${formatDate(selectedTimeEnd, [M, ' ', d, ', ', HH, ':', nn, ':', ss])}", textAlign: TextAlign.left,)
);
}
return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
);
}

View File

@ -14,6 +14,7 @@ import 'package:date_format/date_format.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_colorpicker/material_picker.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'dart:math' as math;
part 'entity_class/entity.class.dart';
part 'entity_class/switch_entity.class.dart';
@ -35,6 +36,7 @@ part 'entity_widgets/entity_name.dart';
part 'entity_widgets/last_updated.dart';
part 'entity_widgets/mode_swicth.dart';
part 'entity_widgets/mode_selector.dart';
part 'entity_widgets/entity_colors.class.dart';
part 'entity_widgets/entity_page_container.dart';
part 'entity_widgets/history_chart/entity_history.dart';
part 'entity_widgets/history_chart/simple_state_history_chart.dart';