|
@@ -21,11 +21,10 @@ class Chart extends CustomPainter {
|
|
|
..color = const Color(0xffDCDCDC)
|
|
|
..strokeWidth = 0.5
|
|
|
..isAntiAlias = true;
|
|
|
- final Paint _linePaint = Paint()
|
|
|
+ final Paint _paintCurrent = Paint()
|
|
|
..color = const Color(0xffFFC400)
|
|
|
- ..strokeWidth = 2
|
|
|
- ..isAntiAlias = true
|
|
|
- ..style = PaintingStyle.stroke;
|
|
|
+ ..strokeWidth = 0.5
|
|
|
+ ..isAntiAlias = true;
|
|
|
|
|
|
static const Color _maxColor = Color(0xffFF5B1D);
|
|
|
final Paint _maxPaint = Paint()
|
|
@@ -41,6 +40,8 @@ class Chart extends CustomPainter {
|
|
|
textAlign: TextAlign.right,
|
|
|
fontSize: 8,
|
|
|
);
|
|
|
+ final Paint columnPaint = Paint()..color = const Color(0xffFFC400)
|
|
|
+ ..isAntiAlias = true;
|
|
|
|
|
|
double _zero = 0;
|
|
|
double _paddingLeft = 35;
|
|
@@ -57,6 +58,7 @@ class Chart extends CustomPainter {
|
|
|
bool drawMax = false;
|
|
|
String unit = "kal";
|
|
|
double _max = 750;
|
|
|
+ int _currentIndex = 0;
|
|
|
|
|
|
Chart({this.type, this.records, this.dateTime, this.drawMax, this.unit});
|
|
|
|
|
@@ -66,14 +68,17 @@ class Chart extends CustomPainter {
|
|
|
values = {};
|
|
|
var records = this.records ?? [];
|
|
|
|
|
|
+ var now = DateTime.now();
|
|
|
+
|
|
|
if (this.type == 0) {
|
|
|
records.forEach((element) {
|
|
|
var t = DateTime.parse(element.createdAt);
|
|
|
values.update("${max(6, t.hour) - 5}", (value) => value + element.value, ifAbsent: () => Converter.toDouble(element.value));
|
|
|
});
|
|
|
- for (int i = 1; i <= 24; i++) {
|
|
|
+ for (int i = 6; i <= 24; i++) {
|
|
|
values.putIfAbsent("$i", () => 0.0);
|
|
|
}
|
|
|
+ _currentIndex = -1;
|
|
|
} else if (this.type == 1) {
|
|
|
records.forEach((element) {
|
|
|
var t = DateTime.parse(element.createdAt);
|
|
@@ -82,6 +87,7 @@ class Chart extends CustomPainter {
|
|
|
for (int i = 1; i <= 7; i++) {
|
|
|
values.putIfAbsent("$i", () => 0.0);
|
|
|
}
|
|
|
+ _currentIndex = (now.weekday == 0 ? 7 : now.weekday) - 1;
|
|
|
} else if (this.type == 2) {
|
|
|
records.forEach((element) {
|
|
|
var t = DateTime.parse(element.createdAt);
|
|
@@ -90,6 +96,7 @@ class Chart extends CustomPainter {
|
|
|
for (int i = 1; i <= 31; i++) {
|
|
|
values.putIfAbsent("$i", () => 0.0);
|
|
|
}
|
|
|
+ _currentIndex = now.day - 1;
|
|
|
} else if (this.type == 3) {
|
|
|
records.forEach((element) {
|
|
|
values.update("${element.createdAt}", (value) => value + element.value, ifAbsent: () => Converter.toDouble(element.value));
|
|
@@ -97,6 +104,7 @@ class Chart extends CustomPainter {
|
|
|
for (int i = 1; i <= 12; i++) {
|
|
|
values.putIfAbsent("$i", () => 0.0);
|
|
|
}
|
|
|
+ _currentIndex = now.month - 1;
|
|
|
}
|
|
|
values.values.forEach((element) {
|
|
|
_max = max(maxValue, Converter.toDouble(element));
|
|
@@ -144,7 +152,7 @@ class Chart extends CustomPainter {
|
|
|
// draw 数据列
|
|
|
double left = _paddingLeft + 10;
|
|
|
double width = size.width - _paddingRight - left; // 柱子的有效空间
|
|
|
- double valueStroke = 20; // 柱子宽度
|
|
|
+ double valueStroke = 8; // 柱子宽度
|
|
|
List<String> valueLabel = []; // 柱子数量
|
|
|
int scaleCount = 0; // 刻度数量
|
|
|
int scaleWeight = 3;
|
|
@@ -152,14 +160,14 @@ class Chart extends CustomPainter {
|
|
|
// 日,按时间
|
|
|
scaleWeight = 2;
|
|
|
valueLabel = List.generate(10, (index) => "${6 + index * scaleWeight}:00");
|
|
|
- valueStroke = width / valueLabel.length / scaleWeight - 5;
|
|
|
+ // valueStroke = width / valueLabel.length / scaleWeight - 5;
|
|
|
scaleCount = 18;
|
|
|
} else if (this.type == 1) {
|
|
|
left += 20;
|
|
|
width -= 40;
|
|
|
scaleWeight = 1;
|
|
|
valueLabel = List.generate(7, (index) => WEEK[index]);
|
|
|
- valueStroke = 12;
|
|
|
+ // valueStroke = 12;
|
|
|
scaleCount = 6;
|
|
|
} else if (this.type == 2) {
|
|
|
DateTime now = this.dateTime;
|
|
@@ -169,13 +177,14 @@ class Chart extends CustomPainter {
|
|
|
int diffDay = endTime.difference(startTime).inDays;
|
|
|
// List<int> days = [1, 8, 15, 22, diffDay == 28 ? 28:29];
|
|
|
scaleCount = diffDay - 1;
|
|
|
- scaleWeight = 2;
|
|
|
+ scaleWeight = 1;
|
|
|
// valueLabel = days.map((e) => '$e/${now.month}').toList();
|
|
|
- valueLabel = List.generate(diffDay ~/ scaleWeight + (scaleCount % scaleWeight == 0 ? 1 : 0), (index) => "${index * 2 + 1}");
|
|
|
+ // valueLabel = List.generate(diffDay ~/ scaleWeight + (scaleCount % scaleWeight == 0 ? 1 : 0), (index) => "${index * 2 + 1}");
|
|
|
+ valueLabel = List.generate(diffDay, (index) => '${index + 1}');
|
|
|
valueStroke = 5;
|
|
|
} else if (this.type == 3) {
|
|
|
valueLabel = List.generate(12, (index) => '${index + 1}');
|
|
|
- valueStroke = 12;
|
|
|
+ // valueStroke = 12;
|
|
|
scaleCount = 12 - 1;
|
|
|
scaleWeight = 1;
|
|
|
}
|
|
@@ -192,15 +201,32 @@ class Chart extends CustomPainter {
|
|
|
// }
|
|
|
|
|
|
for (var i = 0; i < labelSize; i++) {
|
|
|
+ int index = i;
|
|
|
String label = valueLabel[i];
|
|
|
+ if(type == 2 && i % 2 == 1) {
|
|
|
+ if(i != _currentIndex)
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // print("1111111111111111111 $i $_currentIndex $label");
|
|
|
+ bool curr = i == _currentIndex;
|
|
|
ParagraphBuilder pb = ParagraphBuilder(_labelStyle)
|
|
|
- ..pushStyle(ui.TextStyle(color: Color(0xff999999)))
|
|
|
+ ..pushStyle(ui.TextStyle(color: curr ? const Color(0xffFFC400) : const Color(0xff999999)))
|
|
|
..addText(label);
|
|
|
ParagraphConstraints constraints = ParagraphConstraints(width: labelSpace);
|
|
|
Paragraph paragraph = pb.build()..layout(constraints);
|
|
|
paragraph.computeLineMetrics().forEach((element) {
|
|
|
- canvas.drawParagraph(paragraph, Offset(left + scaleWidth * scaleWeight * i - element.width / 2, zero + 5));
|
|
|
+ var x = left + scaleWidth * scaleWeight * index - element.width / 2;
|
|
|
+ var y = zero + 5;
|
|
|
+ canvas.drawParagraph(paragraph, Offset(x, y));
|
|
|
+ if(curr){
|
|
|
+ x = x + element.width / 2 ;
|
|
|
+ canvas.drawPath(Path()..moveTo(x, y + 11)..lineTo(x - 3, y + 14)..lineTo(x + 3, y + 14)..close(), _paintCurrent);
|
|
|
+ }
|
|
|
});
|
|
|
+
|
|
|
+ if(type == 2){
|
|
|
+
|
|
|
+ }
|
|
|
//
|
|
|
// double l = left + scaleWidth * scaleWeight * i - valueStroke / 2;
|
|
|
// Rect rect = Rect.fromLTRB(l, zero, l + valueStroke, 0);
|
|
@@ -224,13 +250,7 @@ class Chart extends CustomPainter {
|
|
|
num value = values[key];
|
|
|
double l = left + scaleWidth * i - valueStroke / 2;
|
|
|
Rect rect = Rect.fromLTRB(l, zero, l + valueStroke, calValue(value));
|
|
|
- Paint valuePaint = Paint()
|
|
|
- ..shader = LinearGradient(
|
|
|
- begin: Alignment.bottomCenter,
|
|
|
- end: Alignment.topCenter,
|
|
|
- colors: <Color>[Color(0xffFF9100), Color(0xffFFE600)],
|
|
|
- ).createShader(rect);
|
|
|
- canvas.drawRRect(RRect.fromRectAndRadius(rect, Radius.circular(100)), valuePaint);
|
|
|
+ canvas.drawRRect(RRect.fromRectAndRadius(rect, Radius.circular(100)), columnPaint);
|
|
|
|
|
|
if (this.drawMax) {
|
|
|
if (maxVal <= value) {
|