-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathline_chart_sample6.dart
286 lines (264 loc) · 8.41 KB
/
line_chart_sample6.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart_app/util/extensions/color_extensions.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
// ignore: must_be_immutable
class LineChartSample6 extends StatelessWidget {
LineChartSample6({
super.key,
Color? line1Color1,
Color? line1Color2,
Color? line2Color1,
Color? line2Color2,
}) : line1Color1 = line1Color1 ?? AppColors.contentColorOrange,
line1Color2 = line1Color2 ?? AppColors.contentColorOrange.darken(60),
line2Color1 = line2Color1 ?? AppColors.contentColorBlue.darken(60),
line2Color2 = line2Color2 ?? AppColors.contentColorBlue {
minSpotX = spots.first.x;
maxSpotX = spots.first.x;
minSpotY = spots.first.y;
maxSpotY = spots.first.y;
for (final spot in spots) {
if (spot.x > maxSpotX) {
maxSpotX = spot.x;
}
if (spot.x < minSpotX) {
minSpotX = spot.x;
}
if (spot.y > maxSpotY) {
maxSpotY = spot.y;
}
if (spot.y < minSpotY) {
minSpotY = spot.y;
}
}
}
final Color line1Color1;
final Color line1Color2;
final Color line2Color1;
final Color line2Color2;
final spots = const [
FlSpot(0, 1),
FlSpot(2, 5),
FlSpot(4, 3),
FlSpot(6, 5),
];
final spots2 = const [
FlSpot(0, 3),
FlSpot(2, 1),
FlSpot(4, 2),
FlSpot(6, 1),
];
late double minSpotX;
late double maxSpotX;
late double minSpotY;
late double maxSpotY;
Widget leftTitleWidgets(double value, TitleMeta meta) {
final style = TextStyle(
color: line1Color1,
fontWeight: FontWeight.bold,
fontSize: 18,
);
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
if (intValue == (maxSpotY + minSpotY)) {
return Text('', style: style);
}
return Padding(
padding: const EdgeInsets.only(right: 6),
child: Text(
intValue.toString(),
style: style,
textAlign: TextAlign.center,
),
);
}
Widget rightTitleWidgets(double value, TitleMeta meta) {
final style = TextStyle(
color: line2Color2,
fontWeight: FontWeight.bold,
fontSize: 18,
);
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
if (intValue == (maxSpotY + minSpotY)) {
return Text('', style: style);
}
return Text(intValue.toString(), style: style, textAlign: TextAlign.right);
}
Widget topTitleWidgets(double value, TitleMeta meta) {
if (value % 1 != 0) {
return Container();
}
const style = TextStyle(
fontWeight: FontWeight.bold,
color: AppColors.mainTextColor2,
);
return SideTitleWidget(
meta: meta,
child: Text(value.toInt().toString(), style: style),
);
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(right: 22, bottom: 20),
child: AspectRatio(
aspectRatio: 2,
child: LineChart(
LineChartData(
lineTouchData: LineTouchData(
touchTooltipData: LineTouchTooltipData(
tooltipRoundedRadius: 0,
getTooltipColor: (spot) => Colors.white,
getTooltipItems: (List<LineBarSpot> touchedSpots) {
return touchedSpots.map((LineBarSpot touchedSpot) {
return LineTooltipItem(
touchedSpot.y.toString(),
TextStyle(
color: touchedSpot.bar.gradient!.colors.first,
fontWeight: FontWeight.bold,
fontSize: 20,
),
);
}).toList();
},
),
getTouchedSpotIndicator: (
_,
indicators,
) {
return indicators
.map((int index) => const TouchedSpotIndicatorData(
FlLine(color: Colors.transparent),
FlDotData(show: false),
))
.toList();
},
touchSpotThreshold: 12,
distanceCalculator:
(Offset touchPoint, Offset spotPixelCoordinates) =>
(touchPoint - spotPixelCoordinates).distance,
),
lineBarsData: [
LineChartBarData(
gradient: LinearGradient(
colors: [
line1Color1,
line1Color2,
],
),
spots: reverseSpots(spots, minSpotY, maxSpotY),
isCurved: true,
isStrokeCapRound: true,
barWidth: 10,
belowBarData: BarAreaData(
show: false,
),
dotData: FlDotData(
show: true,
getDotPainter: (spot, percent, barData, index) {
return FlDotCirclePainter(
radius: 12,
color: Color.lerp(
line1Color1,
line1Color2,
percent / 100,
)!,
strokeColor: Colors.white,
strokeWidth: 1,
);
},
),
),
LineChartBarData(
gradient: LinearGradient(
colors: [
line2Color1,
line2Color2,
],
),
spots: reverseSpots(spots2, minSpotY, maxSpotY),
isCurved: true,
isStrokeCapRound: true,
barWidth: 10,
belowBarData: BarAreaData(
show: false,
),
dotData: FlDotData(
show: true,
getDotPainter: (spot, percent, barData, index) {
return FlDotCirclePainter(
radius: 12,
color: Color.lerp(
line2Color1,
line2Color2,
percent / 100,
)!,
strokeColor: Colors.white,
strokeWidth: 1,
);
},
),
),
],
minY: 0,
maxY: maxSpotY + minSpotY,
titlesData: FlTitlesData(
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
getTitlesWidget: leftTitleWidgets,
reservedSize: 38,
),
),
rightTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
getTitlesWidget: rightTitleWidgets,
reservedSize: 30,
),
),
bottomTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
topTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 32,
getTitlesWidget: topTitleWidgets,
),
),
),
gridData: FlGridData(
show: true,
drawVerticalLine: true,
checkToShowHorizontalLine: (value) {
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
if (intValue == (maxSpotY + minSpotY).toInt()) {
return false;
}
return true;
},
),
borderData: FlBorderData(
show: true,
border: const Border(
left: BorderSide(color: AppColors.borderColor),
top: BorderSide(color: AppColors.borderColor),
bottom: BorderSide(color: Colors.transparent),
right: BorderSide(color: Colors.transparent),
),
),
),
),
),
);
}
double reverseY(double y, double minX, double maxX) {
return (maxX + minX) - y;
}
List<FlSpot> reverseSpots(List<FlSpot> inputSpots, double minY, double maxY) {
return inputSpots.map((spot) {
return spot.copyWith(y: (maxY + minY) - spot.y);
}).toList();
}
}