forked from imaNNeo/fl_chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_chart_sample8.dart
205 lines (195 loc) · 6.57 KB
/
line_chart_sample8.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
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_svg/flutter_svg.dart';
class LineChartSample8 extends StatefulWidget {
@override
_LineChartSample8State createState() => _LineChartSample8State();
}
class _LineChartSample8State extends State<LineChartSample8> {
List<Color> gradientColors = const [
Color(0xffEEF3FE),
Color(0xffEEF3FE),
];
bool showAvg = false;
Future<ui.Image> loadImage(String asset) async {
final ByteData data = await rootBundle.load(asset);
final ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
final ui.FrameInfo fi = await codec.getNextFrame();
return fi.image;
}
Future<SizedPicture> loadSvg() async {
const String rawSvg =
'<svg height="14" width="14" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" transform="translate(-.000014)"><circle cx="7" cy="7" fill="#495DFF" r="7"/><path d="m7 10.9999976c1.6562389 0 2.99998569-1.34374678 2.99998569-2.99999283s-1.34374679-4.99998808-2.99998569-4.99998808c-1.6562532 0-3 3.34374203-3 4.99998808s1.3437468 2.99999283 3 2.99999283z" fill="#fff" fill-rule="nonzero"/></g></svg>';
final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);
final ui.Picture picture = svgRoot.toPicture();
final sizedPicture = SizedPicture(picture, 14, 14);
return sizedPicture;
}
@override
Widget build(BuildContext context) {
return FutureBuilder<SizedPicture>(
future: loadSvg(),
builder: (BuildContext context, imageSnapshot) {
if (imageSnapshot.connectionState == ConnectionState.done) {
return Stack(
children: <Widget>[
AspectRatio(
aspectRatio: 1.70,
child: Container(
child: Padding(
padding: const EdgeInsets.only(right: 18.0, left: 12.0, top: 24, bottom: 12),
child: LineChart(
mainData(imageSnapshot.data),
),
),
),
),
],
);
} else {
return const CircularProgressIndicator();
}
});
}
LineChartData mainData(SizedPicture sizedPicture) {
return LineChartData(
rangeAnnotations: RangeAnnotations(
verticalRangeAnnotations: [
VerticalRangeAnnotation(
x1: 2,
x2: 5,
color: const Color(0xffD5DAE5),
),
VerticalRangeAnnotation(
x1: 8,
x2: 9,
color: const Color(0xffD5DAE5),
),
],
horizontalRangeAnnotations: [
HorizontalRangeAnnotation(
y1: 2,
y2: 3,
color: const Color(0xffEEF3FE),
),
],
),
// uncomment to see ExtraLines with RangeAnnotations
extraLinesData: ExtraLinesData(
// extraLinesOnTop: true,
horizontalLines: [
HorizontalLine(
y: 5,
color: const Color.fromRGBO(197, 210, 214, 1),
strokeWidth: 2,
dashArray: [5, 10],
label: HorizontalLineLabel(
show: true,
alignment: Alignment.topRight,
padding: const EdgeInsets.only(right: 5, bottom: 5),
style: const TextStyle(color: Colors.black, fontSize: 9),
labelResolver: (line) => 'H: ${line.y}',
),
),
],
verticalLines: [
VerticalLine(
x: 5.7,
color: const Color.fromRGBO(197, 210, 214, 1),
strokeWidth: 2,
dashArray: [5, 10],
label: VerticalLineLabel(
show: true,
alignment: Alignment.topRight,
padding: const EdgeInsets.only(left: 10, top: 5),
style: const TextStyle(color: Colors.black, fontSize: 9),
labelResolver: (line) => 'V: ${line.x}',
),
),
VerticalLine(
x: 8.5,
color: Colors.transparent,
sizedPicture: sizedPicture,
)
],
),
gridData: FlGridData(
show: true, drawVerticalLine: false, drawHorizontalLine: false, verticalInterval: 1),
titlesData: FlTitlesData(
show: true,
bottomTitles: SideTitles(
showTitles: true,
reservedSize: 22,
textStyle: TextStyle(color: Colors.black87, fontSize: 10),
interval: 4,
margin: 8,
checkToShowTitle: (minValue, maxValue, sideTitles, appliedInterval, value) => true,
),
leftTitles: SideTitles(
interval: 2,
showTitles: true,
textStyle: TextStyle(
color: Colors.black87,
fontSize: 10,
),
reservedSize: 28,
margin: 12,
),
),
lineTouchData: LineTouchData(
fullHeightTouchLine: true,
getTouchedSpotIndicator: (LineChartBarData barData, List<int> spotIndexes) {
return spotIndexes.map((spotIndex) {
return TouchedSpotIndicatorData(
FlLine(color: Colors.orange, strokeWidth: 3),
FlDotData(
getDotPainter: (spot, percent, barData, index) =>
FlDotCirclePainter(radius: 8, color: Colors.deepOrange)),
);
}).toList();
},
touchTooltipData: LineTouchTooltipData(
tooltipBgColor: Colors.blueAccent,
),
),
borderData:
FlBorderData(show: true, border: Border.all(color: const Color(0xffecf1fe), width: 1)),
minX: 0,
maxX: 11,
minY: 0,
maxY: 6,
lineBarsData: [
LineChartBarData(
spots: [
FlSpot(0, 1),
FlSpot(2, 1),
FlSpot(4.9, 5),
FlSpot(6.8, 5),
FlSpot(7.5, 4),
FlSpot(null, null),
FlSpot(7.5, 2),
FlSpot(8, 1),
FlSpot(10, 2),
FlSpot(11, 2.5),
],
dashArray: [2, 4],
isCurved: true,
colors: const [Color(0xff0F2BF6), Color(0xff0F2BF6)],
barWidth: 2,
isStrokeCapRound: true,
dotData: FlDotData(
show: false,
),
belowBarData: BarAreaData(
show: false,
colors: gradientColors.map((color) => color.withOpacity(0.5)).toList(),
),
),
],
);
}
}