forked from imaNNeo/fl_chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_chart_sample3.dart
259 lines (248 loc) · 9.26 KB
/
line_chart_sample3.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
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
class LineChartSample3 extends StatefulWidget {
final weekDays = ['Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
final List<double> yValues = [1.3, 1, 1.8, 1.5, 2.2, 1.8, 3];
@override
State createState() => _LineChartSample3State();
}
class _LineChartSample3State extends State<LineChartSample3> {
double touchedValue;
@override
void initState() {
touchedValue = -1;
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
Text(
'Average Line',
style: TextStyle(color: Colors.green, fontWeight: FontWeight.bold, fontSize: 16),
),
Text(
' and ',
style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 16),
),
Text(
'Indicators',
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 16),
),
],
),
const SizedBox(
height: 18,
),
SizedBox(
width: 300,
height: 140,
child: LineChart(
LineChartData(
lineTouchData: LineTouchData(
getTouchedSpotIndicator: (LineChartBarData barData, List<int> spotIndexes) {
return spotIndexes.map((spotIndex) {
final FlSpot spot = barData.spots[spotIndex];
if (spot.x == 0 || spot.x == 6) {
return null;
}
return TouchedSpotIndicatorData(
FlLine(color: Colors.blue, strokeWidth: 4),
FlDotData(
getDotPainter: (spot, percent, barData, index) {
if (index % 2 == 0) {
return FlDotCirclePainter(
radius: 8,
color: Colors.white,
strokeWidth: 5,
strokeColor: Colors.deepOrange);
} else {
return FlDotSquarePainter(
size: 16,
color: Colors.white,
strokeWidth: 5,
strokeColor: Colors.deepOrange,
);
}
},
),
);
}).toList();
},
touchTooltipData: LineTouchTooltipData(
tooltipBgColor: Colors.blueAccent,
getTooltipItems: (List<LineBarSpot> touchedBarSpots) {
return touchedBarSpots.map((barSpot) {
final flSpot = barSpot;
if (flSpot.x == 0 || flSpot.x == 6) {
return null;
}
return LineTooltipItem(
'${widget.weekDays[flSpot.x.toInt()]} \n${flSpot.y} k calories',
const TextStyle(color: Colors.white),
);
}).toList();
}),
touchCallback: (LineTouchResponse lineTouch) {
if (lineTouch.lineBarSpots.length == 1 &&
lineTouch.touchInput is! FlLongPressEnd &&
lineTouch.touchInput is! FlPanEnd) {
final value = lineTouch.lineBarSpots[0].x;
if (value == 0 || value == 6) {
setState(() {
touchedValue = -1;
});
return null;
}
setState(() {
touchedValue = value;
});
} else {
setState(() {
touchedValue = -1;
});
}
}),
extraLinesData: ExtraLinesData(horizontalLines: [
HorizontalLine(
y: 1.8,
color: Colors.green.withOpacity(0.8),
strokeWidth: 3,
dashArray: [20, 2],
),
]),
lineBarsData: [
LineChartBarData(
isStepLineChart: true,
spots: widget.yValues.asMap().entries.map((e) {
return FlSpot(e.key.toDouble(), e.value);
}).toList(),
isCurved: false,
barWidth: 4,
colors: [
Colors.orange,
],
belowBarData: BarAreaData(
show: true,
colors: [
Colors.orange.withOpacity(0.5),
Colors.orange.withOpacity(0.0),
],
gradientColorStops: [0.5, 1.0],
gradientFrom: const Offset(0, 0),
gradientTo: const Offset(0, 1),
spotsLine: BarAreaSpotsLine(
show: true,
flLineStyle: FlLine(
color: Colors.blue,
strokeWidth: 2,
),
checkToShowSpotLine: (spot) {
if (spot.x == 0 || spot.x == 6) {
return false;
}
return true;
},
),
),
dotData: FlDotData(
show: true,
getDotPainter: (spot, percent, barData, index) {
if (index % 2 == 0) {
return FlDotCirclePainter(
radius: 6,
color: Colors.white,
strokeWidth: 3,
strokeColor: Colors.deepOrange);
} else {
return FlDotSquarePainter(
size: 12,
color: Colors.white,
strokeWidth: 3,
strokeColor: Colors.deepOrange,
);
}
},
checkToShowDot: (spot, barData) {
return spot.x != 0 && spot.x != 6;
}),
),
],
minY: 0,
gridData: FlGridData(
show: true,
drawHorizontalLine: true,
drawVerticalLine: true,
getDrawingHorizontalLine: (value) {
if (value == 0) {
return FlLine(
color: Colors.deepOrange,
strokeWidth: 2,
);
} else {
return FlLine(
color: Colors.grey,
strokeWidth: 0.5,
);
}
},
getDrawingVerticalLine: (value) {
if (value == 0) {
return FlLine(
color: Colors.black,
strokeWidth: 2,
);
} else {
return FlLine(
color: Colors.grey,
strokeWidth: 0.5,
);
}
},
),
titlesData: FlTitlesData(
show: true,
leftTitles: SideTitles(
showTitles: true,
reservedSize: 30,
getTitles: (value) {
switch (value.toInt()) {
case 0:
return '';
case 1:
return '1k calories';
case 2:
return '2k calories';
case 3:
return '3k calories';
}
return '';
},
getTextStyles: (value) => const TextStyle(color: Colors.black, fontSize: 10),
),
bottomTitles: SideTitles(
showTitles: true,
getTitles: (value) {
return widget.weekDays[value.toInt()];
},
getTextStyles: (value) {
final isTouched = value == touchedValue;
return TextStyle(
color: isTouched ? Colors.deepOrange : Colors.deepOrange.withOpacity(0.5),
fontWeight: FontWeight.bold,
);
},
),
),
),
),
),
],
);
}
}