selfprivacy.org.app/lib/ui/pages/server_details/network_charts.dart

144 lines
3.7 KiB
Dart
Raw Normal View History

2021-04-10 03:04:23 +00:00
import 'dart:math';
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 NetworkChart extends StatelessWidget {
NetworkChart(
this.listData,
this.period,
this.start,
);
final List<List<TimeSeriesData>> listData;
final Period period;
final DateTime start;
List<FlSpot> getSpots(data) {
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 SizedBox(
height: 150,
2021-10-13 21:49:24 +00:00
width: MediaQuery.of(context).size.width,
2021-04-10 03:04:23 +00:00
child: LineChart(
LineChartData(
lineTouchData: LineTouchData(enabled: false),
lineBarsData: [
LineChartBarData(
spots: getSpots(listData[0]),
isCurved: true,
barWidth: 1,
colors: [Colors.red],
dotData: FlDotData(
show: false,
),
),
LineChartBarData(
spots: getSpots(listData[1]),
isCurved: true,
barWidth: 1,
colors: [Colors.green],
dotData: FlDotData(
show: false,
),
),
],
minY: 0,
maxY: [
...listData[0].map((e) => e.value),
...listData[1].map((e) => e.value)
].reduce(max) *
1.2,
minX: listData[0].length - 200,
titlesData: FlTitlesData(
2021-10-13 21:49:24 +00:00
topTitles: SideTitles(showTitles: false),
2021-04-10 03:04:23 +00:00
bottomTitles: SideTitles(
interval: 20,
rotateAngle: 90.0,
showTitles: true,
2021-08-29 09:50:24 +00:00
getTextStyles: (_, __) => const TextStyle(
2021-04-10 03:04:23 +00:00
fontSize: 10,
color: Colors.purple,
fontWeight: FontWeight.bold,
),
getTitles: (value) {
return bottomTitle(value.toInt());
}),
leftTitles: SideTitles(
2021-10-13 21:49:24 +00:00
reservedSize: 50,
margin: 5,
2021-04-10 03:04:23 +00:00
interval: [
...listData[0].map((e) => e.value),
...listData[1].map((e) => e.value)
].reduce(max) *
2021-10-13 21:49:24 +00:00
2 /
2021-04-10 03:04:23 +00:00
10,
2021-08-29 09:50:24 +00:00
getTextStyles: (_, __) => progressTextStyleLight.copyWith(
2021-04-10 10:06:29 +00:00
color: Theme.of(context).brightness == Brightness.dark
? BrandColors.gray4
: null,
),
2021-04-10 03:04:23 +00:00
showTitles: true,
),
),
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 = listData[0][value].time;
switch (period) {
case Period.hour:
case Period.day:
res = hhmm.format(time);
break;
case Period.month:
res = day.format(time);
}
return res;
}
}