2021-04-10 03:04:23 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:fl_chart/fl_chart.dart';
|
2021-04-10 10:06:29 +00:00
|
|
|
import 'package:selfprivacy/config/brand_colors.dart';
|
|
|
|
import 'package:selfprivacy/config/text_themes.dart';
|
2021-04-10 03:04:23 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
class CpuChart extends StatelessWidget {
|
|
|
|
CpuChart(this.data, this.period, this.start);
|
|
|
|
|
|
|
|
final List<TimeSeriesData> data;
|
|
|
|
final Period period;
|
|
|
|
final DateTime start;
|
|
|
|
|
|
|
|
List<FlSpot> getSpots() {
|
|
|
|
var i = 0;
|
|
|
|
List<FlSpot> res = [];
|
|
|
|
|
|
|
|
for (var d in data) {
|
|
|
|
res.add(FlSpot(i.toDouble(), d.value));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return LineChart(
|
|
|
|
LineChartData(
|
|
|
|
lineTouchData: LineTouchData(enabled: false),
|
|
|
|
lineBarsData: [
|
|
|
|
LineChartBarData(
|
|
|
|
spots: getSpots(),
|
|
|
|
isCurved: true,
|
|
|
|
barWidth: 1,
|
2022-04-05 15:50:47 +00:00
|
|
|
color: Colors.red,
|
2021-04-10 03:04:23 +00:00
|
|
|
dotData: FlDotData(
|
|
|
|
show: false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
minY: 0,
|
|
|
|
maxY: 100,
|
|
|
|
minX: data.length - 200,
|
|
|
|
titlesData: FlTitlesData(
|
2022-04-05 15:50:47 +00:00
|
|
|
topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
|
|
|
bottomTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
2021-04-10 03:04:23 +00:00
|
|
|
interval: 20,
|
2022-04-05 15:50:47 +00:00
|
|
|
reservedSize: 50,
|
|
|
|
getTitlesWidget: (value, titleMeta) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: RotatedBox(
|
|
|
|
child: Text(bottomTitle(value.toInt()),
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 10,
|
|
|
|
color: Colors.purple,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
)),
|
|
|
|
quarterTurns: 1,
|
2021-04-10 03:04:23 +00:00
|
|
|
),
|
2022-04-05 15:50:47 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
showTitles: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
leftTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
|
|
|
getTitlesWidget: (value, titleMeta) {
|
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.only(right: 15),
|
|
|
|
child: Text(
|
|
|
|
value.toInt().toString(),
|
|
|
|
style: progressTextStyleLight.copyWith(
|
|
|
|
color: Theme.of(context).brightness == Brightness.dark
|
|
|
|
? BrandColors.gray4
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
interval: 25,
|
|
|
|
showTitles: false,
|
2021-04-10 10:06:29 +00:00
|
|
|
),
|
2021-04-10 03:04:23 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
gridData: FlGridData(show: true),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkToShowTitle(
|
|
|
|
double minValue,
|
|
|
|
double maxValue,
|
|
|
|
SideTitles sideTitles,
|
|
|
|
double appliedInterval,
|
|
|
|
double value,
|
|
|
|
) {
|
|
|
|
if (value < 0) {
|
|
|
|
return false;
|
|
|
|
} else if (value == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var _value = value - minValue;
|
|
|
|
var v = _value / 20;
|
|
|
|
return v - v.floor() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String bottomTitle(int value) {
|
|
|
|
final hhmm = DateFormat('HH:mm');
|
|
|
|
var day = DateFormat('MMMd');
|
|
|
|
String res;
|
|
|
|
|
|
|
|
if (value <= 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
var time = data[value].time;
|
|
|
|
switch (period) {
|
|
|
|
case Period.hour:
|
|
|
|
case Period.day:
|
|
|
|
res = hhmm.format(time);
|
|
|
|
break;
|
|
|
|
case Period.month:
|
|
|
|
res = day.format(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|