forked from imaNNeo/fl_chart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline_chart_sample6.dart
More file actions
199 lines (182 loc) · 6.35 KB
/
line_chart_sample6.dart
File metadata and controls
199 lines (182 loc) · 6.35 KB
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
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
// ignore: must_be_immutable
class LineChartSample6 extends StatelessWidget {
final spots = [
FlSpot(0, 1),
FlSpot(2, 5),
FlSpot(4, 3),
FlSpot(6, 5),
];
final spots2 = [
FlSpot(0, 3),
FlSpot(2, 1),
FlSpot(4, 2),
FlSpot(6, 1),
];
double minSpotX, maxSpotX;
double minSpotY, maxSpotY;
LineChartSample6() {
minSpotX = spots.first.x;
maxSpotX = spots.first.x;
minSpotY = spots.first.y;
maxSpotY = spots.first.y;
for (FlSpot 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;
}
}
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: const [
BoxShadow(
color: Colors.black,
blurRadius: 10,
spreadRadius: 0,
)
]),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(colors: [
Colors.yellowAccent,
Colors.yellowAccent.withOpacity(0.1),
], begin: Alignment.topCenter, end: Alignment.bottomCenter),
),
child: Padding(
padding: const EdgeInsets.only(right: 22.0, bottom: 20),
child: SizedBox(
width: 300,
height: 200,
child: LineChart(
LineChartData(
lineTouchData: LineTouchData(enabled: false),
lineBarsData: [
LineChartBarData(
colors: [
Colors.deepOrangeAccent,
Colors.orangeAccent,
],
spots: reverseSpots(spots, minSpotY, maxSpotY),
isCurved: true,
isStrokeCapRound: true,
barWidth: 10,
belowBarData: BarAreaData(
show: false,
),
dotData: FlDotData(
show: true,
getDotPainter: (spot, percent, barData, index) =>
FlDotCirclePainter(radius: 12, color: Colors.deepOrange.withOpacity(0.5)),
),
),
LineChartBarData(
colors: [
Colors.lightBlueAccent,
Colors.blue,
],
spots: reverseSpots(spots2, minSpotY, maxSpotY),
isCurved: true,
isStrokeCapRound: true,
barWidth: 10,
belowBarData: BarAreaData(
show: false,
),
dotData: FlDotData(
show: true,
getDotPainter: (spot, percent, barData, index) =>
FlDotCirclePainter(radius: 12, color: Colors.blue.withOpacity(0.5)),
),
),
],
minY: 0,
maxY: maxSpotY + minSpotY,
titlesData: FlTitlesData(
leftTitles: SideTitles(
showTitles: true,
getTitles: (double value) {
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
if (intValue == (maxSpotY + minSpotY)) {
return '';
}
return intValue.toString();
},
getTextStyles: (value) => const TextStyle(
color: Colors.deepOrange, fontWeight: FontWeight.bold, fontSize: 18),
margin: 16,
),
rightTitles: SideTitles(
showTitles: true,
reservedSize: 0,
getTitles: (double value) {
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
if (intValue == (maxSpotY + minSpotY)) {
return '';
}
return intValue.toString();
},
getTextStyles: (value) => const TextStyle(
color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 18),
margin: 16,
),
bottomTitles: SideTitles(showTitles: false),
topTitles: SideTitles(
showTitles: true,
reservedSize: 28,
margin: 8,
getTextStyles: (value) =>
const TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
getTitles: (double value) {
return value.toInt().toString();
},
),
),
gridData: FlGridData(
show: true,
drawVerticalLine: true,
checkToShowHorizontalLine: (value) {
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
if (intValue.toInt() == (maxSpotY + minSpotY).toInt()) {
return false;
}
return true;
}),
borderData: FlBorderData(
show: true,
border: const Border(
left: BorderSide(color: Colors.black),
top: BorderSide(color: Colors.black),
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();
}
}