-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathline_chart_sample10.dart
146 lines (134 loc) · 4.1 KB
/
line_chart_sample10.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 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.withValues(alpha: 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.withValues(alpha: 0), widget.cosColor],
stops: const [0.1, 1.0],
),
barWidth: 4,
isCurved: false,
);
}
@override
void dispose() {
timer.cancel();
super.dispose();
}
}