265 lines
10 KiB
Dart
265 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:fl_chart/fl_chart.dart';
|
|
|
|
class TemperatureChart extends StatelessWidget {
|
|
final List<FlSpot> dataPoints;
|
|
final String title;
|
|
final Color lineColor;
|
|
final Color gradientStartColor;
|
|
final Color gradientEndColor;
|
|
final double minY;
|
|
final double maxY;
|
|
final String unit;
|
|
|
|
const TemperatureChart({
|
|
super.key,
|
|
required this.dataPoints,
|
|
this.title = 'Suhu Real-time',
|
|
this.lineColor = Colors.orange,
|
|
this.gradientStartColor = Colors.orange,
|
|
this.gradientEndColor = Colors.red,
|
|
this.minY = 20,
|
|
this.maxY = 60,
|
|
this.unit = '°C',
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(16),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.05),
|
|
blurRadius: 10,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
color: lineColor.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Icon(Icons.thermostat, color: lineColor, size: 20),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
title,
|
|
style: const TextStyle(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.black87,
|
|
),
|
|
),
|
|
Text(
|
|
dataPoints.isNotEmpty
|
|
? '${dataPoints.last.y.toStringAsFixed(1)} $unit'
|
|
: '-- $unit',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.grey.shade600,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: lineColor.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
Icons.fiber_manual_record,
|
|
color: lineColor,
|
|
size: 8,
|
|
),
|
|
const SizedBox(width: 4),
|
|
const Text(
|
|
'LIVE',
|
|
style: TextStyle(
|
|
fontSize: 10,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.black54,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
SizedBox(
|
|
height: 200, // Naik dari 180 jadi 200
|
|
child: dataPoints.isEmpty
|
|
? Center(
|
|
child: Text(
|
|
'Menunggu data...',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.grey.shade400,
|
|
),
|
|
),
|
|
)
|
|
: Padding(
|
|
padding: const EdgeInsets.only(left: 8, right: 8),
|
|
child: LineChart(
|
|
LineChartData(
|
|
gridData: FlGridData(
|
|
show: true,
|
|
drawVerticalLine: false,
|
|
horizontalInterval: 10,
|
|
getDrawingHorizontalLine: (value) {
|
|
return FlLine(
|
|
color: Colors.grey.shade200,
|
|
strokeWidth: 1,
|
|
);
|
|
},
|
|
),
|
|
titlesData: FlTitlesData(
|
|
show: true,
|
|
rightTitles: const AxisTitles(
|
|
sideTitles: SideTitles(showTitles: false),
|
|
),
|
|
topTitles: const AxisTitles(
|
|
sideTitles: SideTitles(showTitles: false),
|
|
),
|
|
bottomTitles: AxisTitles(
|
|
sideTitles: SideTitles(
|
|
showTitles: true,
|
|
reservedSize: 30,
|
|
interval: 5,
|
|
getTitlesWidget: (value, meta) {
|
|
return Padding(
|
|
padding: const EdgeInsets.only(top: 8.0),
|
|
child: Text(
|
|
'${value.toInt()}s',
|
|
style: const TextStyle(
|
|
fontSize: 10,
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
leftTitles: AxisTitles(
|
|
sideTitles: SideTitles(
|
|
showTitles: true,
|
|
interval: 10,
|
|
reservedSize: 45,
|
|
getTitlesWidget: (value, meta) {
|
|
return Text(
|
|
'${value.toInt()}$unit',
|
|
style: const TextStyle(
|
|
fontSize: 10,
|
|
color: Colors.grey,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
borderData: FlBorderData(
|
|
show: true,
|
|
border: Border(
|
|
bottom: BorderSide(color: Colors.grey.shade300),
|
|
left: BorderSide(color: Colors.grey.shade300),
|
|
),
|
|
),
|
|
minX: dataPoints.isNotEmpty
|
|
? dataPoints.first.x
|
|
: 0,
|
|
maxX: dataPoints.isNotEmpty
|
|
? dataPoints.last.x
|
|
: 10,
|
|
minY: minY,
|
|
maxY: maxY,
|
|
lineBarsData: [
|
|
LineChartBarData(
|
|
spots: dataPoints,
|
|
isCurved: true,
|
|
curveSmoothness: 0.3,
|
|
color: lineColor,
|
|
barWidth: 3,
|
|
isStrokeCapRound: true,
|
|
dotData: FlDotData(
|
|
show: true,
|
|
getDotPainter: (spot, percent, barData, index) {
|
|
// Hanya tampilkan dot di titik terakhir
|
|
if (index == dataPoints.length - 1) {
|
|
return FlDotCirclePainter(
|
|
radius: 5,
|
|
color: Colors.white,
|
|
strokeWidth: 3,
|
|
strokeColor: lineColor,
|
|
);
|
|
}
|
|
return FlDotCirclePainter(
|
|
radius: 0,
|
|
color: Colors.transparent,
|
|
);
|
|
},
|
|
),
|
|
belowBarData: BarAreaData(
|
|
show: true,
|
|
gradient: LinearGradient(
|
|
colors: [
|
|
gradientStartColor.withOpacity(0.3),
|
|
gradientEndColor.withOpacity(0.1),
|
|
Colors.transparent,
|
|
],
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
lineTouchData: LineTouchData(
|
|
touchTooltipData: LineTouchTooltipData(
|
|
getTooltipColor: (touchedSpot) => Colors.black87,
|
|
tooltipRoundedRadius: 8,
|
|
fitInsideHorizontally: true,
|
|
fitInsideVertically: true,
|
|
tooltipPadding: const EdgeInsets.all(8),
|
|
tooltipMargin: 8,
|
|
getTooltipItems: (List<LineBarSpot> touchedSpots) {
|
|
return touchedSpots.map((LineBarSpot touchedSpot) {
|
|
return LineTooltipItem(
|
|
'${touchedSpot.y.toStringAsFixed(1)} $unit',
|
|
const TextStyle(
|
|
color: Colors.white,
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 12,
|
|
),
|
|
);
|
|
}).toList();
|
|
},
|
|
),
|
|
handleBuiltInTouches: true,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|