172 lines
5.2 KiB
Dart
172 lines
5.2 KiB
Dart
|
import 'dart:async';
|
||
|
import 'dart:math' as math;
|
||
|
|
||
|
import 'package:fl_chart/fl_chart.dart';
|
||
|
//import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
class AppColors {
|
||
|
static const Color primary = contentColorCyan;
|
||
|
static const Color menuBackground = Color(0xFF090912);
|
||
|
static const Color itemsBackground = Color(0xFF1B2339);
|
||
|
static const Color pageBackground = Color(0xFF282E45);
|
||
|
static const Color mainTextColor1 = Colors.white;
|
||
|
static const Color mainTextColor2 = Colors.white70;
|
||
|
static const Color mainTextColor3 = Colors.white38;
|
||
|
static const Color mainGridLineColor = Colors.white10;
|
||
|
static const Color borderColor = Colors.white54;
|
||
|
static const Color gridLinesColor = Color(0x11FFFFFF);
|
||
|
|
||
|
static const Color contentColorBlack = Colors.black;
|
||
|
static const Color contentColorWhite = Colors.white;
|
||
|
static const Color contentColorBlue = Color(0xFF2196F3);
|
||
|
static const Color contentColorYellow = Color(0xFFFFC300);
|
||
|
static const Color contentColorOrange = Color(0xFFFF683B);
|
||
|
static const Color contentColorGreen = Color(0xFF3BFF49);
|
||
|
static const Color contentColorPurple = Color(0xFF6E1BFF);
|
||
|
static const Color contentColorPink = Color(0xFFFF3AF2);
|
||
|
static const Color contentColorRed = Color(0xFFE80054);
|
||
|
static const Color contentColorCyan = Color(0xFF50E4FF);
|
||
|
}
|
||
|
|
||
|
class LineChartSample10 extends StatefulWidget {
|
||
|
const LineChartSample10({super.key});
|
||
|
|
||
|
final Color sinColor = AppColors.contentColorBlue;
|
||
|
final Color cosColor = AppColors.contentColorPink;
|
||
|
|
||
|
@override
|
||
|
State<LineChartSample10> createState() => _LineChartSample10State();
|
||
|
}
|
||
|
|
||
|
class _LineChartSample10State extends State<LineChartSample10> {
|
||
|
final limitCount = 100;
|
||
|
final sinPoints = <FlSpot>[];
|
||
|
final cosPoints = <FlSpot>[];
|
||
|
|
||
|
double xValue = 0;
|
||
|
double step = 0.05;
|
||
|
|
||
|
late Timer timer;
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
timer = Timer.periodic(const Duration(milliseconds: 40), (timer) {
|
||
|
while (sinPoints.length > limitCount) {
|
||
|
sinPoints.removeAt(0);
|
||
|
cosPoints.removeAt(0);
|
||
|
}
|
||
|
setState(() {
|
||
|
sinPoints.add(FlSpot(xValue, math.sin(xValue)));
|
||
|
cosPoints.add(FlSpot(xValue, math.cos(xValue)));
|
||
|
});
|
||
|
xValue += step;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return cosPoints.isNotEmpty
|
||
|
? Column(
|
||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||
|
children: [
|
||
|
const SizedBox(height: 12),
|
||
|
Text(
|
||
|
'x: ${xValue.toStringAsFixed(1)}',
|
||
|
style: const TextStyle(
|
||
|
color: AppColors.mainTextColor2,
|
||
|
fontSize: 18,
|
||
|
fontWeight: FontWeight.bold,
|
||
|
),
|
||
|
),
|
||
|
Text(
|
||
|
'sin: ${sinPoints.last.y.toStringAsFixed(1)}',
|
||
|
style: TextStyle(
|
||
|
color: widget.sinColor,
|
||
|
fontSize: 18,
|
||
|
fontWeight: FontWeight.bold,
|
||
|
),
|
||
|
),
|
||
|
Text(
|
||
|
'cos: ${cosPoints.last.y.toStringAsFixed(1)}',
|
||
|
style: TextStyle(
|
||
|
color: widget.cosColor,
|
||
|
fontSize: 18,
|
||
|
fontWeight: FontWeight.bold,
|
||
|
),
|
||
|
),
|
||
|
const SizedBox(
|
||
|
height: 12,
|
||
|
),
|
||
|
AspectRatio(
|
||
|
aspectRatio: 1.5,
|
||
|
child: Padding(
|
||
|
padding: const EdgeInsets.only(bottom: 24.0),
|
||
|
child: LineChart(
|
||
|
LineChartData(
|
||
|
minY: -1,
|
||
|
maxY: 1,
|
||
|
minX: sinPoints.first.x,
|
||
|
maxX: sinPoints.last.x,
|
||
|
lineTouchData: const LineTouchData(enabled: false),
|
||
|
clipData: const FlClipData.all(),
|
||
|
gridData: const FlGridData(
|
||
|
show: true,
|
||
|
drawVerticalLine: false,
|
||
|
),
|
||
|
borderData: FlBorderData(show: false),
|
||
|
lineBarsData: [
|
||
|
sinLine(sinPoints),
|
||
|
cosLine(cosPoints),
|
||
|
],
|
||
|
titlesData: const FlTitlesData(
|
||
|
show: false,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
)
|
||
|
],
|
||
|
)
|
||
|
: Container();
|
||
|
}
|
||
|
|
||
|
LineChartBarData sinLine(List<FlSpot> points) {
|
||
|
return LineChartBarData(
|
||
|
spots: points,
|
||
|
dotData: const FlDotData(
|
||
|
show: false,
|
||
|
),
|
||
|
gradient: LinearGradient(
|
||
|
colors: [widget.sinColor.withOpacity(0), widget.sinColor],
|
||
|
stops: const [0.1, 1.0],
|
||
|
),
|
||
|
barWidth: 4,
|
||
|
isCurved: false,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
LineChartBarData cosLine(List<FlSpot> points) {
|
||
|
return LineChartBarData(
|
||
|
spots: points,
|
||
|
dotData: const FlDotData(
|
||
|
show: false,
|
||
|
),
|
||
|
gradient: LinearGradient(
|
||
|
colors: [widget.cosColor.withOpacity(0), widget.cosColor],
|
||
|
stops: const [0.1, 1.0],
|
||
|
),
|
||
|
barWidth: 4,
|
||
|
isCurved: false,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
|
timer.cancel();
|
||
|
super.dispose();
|
||
|
}
|
||
|
}
|
||
|
|