-
Notifications
You must be signed in to change notification settings - Fork 774
/
crosshair.dart
228 lines (212 loc) · 7.57 KB
/
crosshair.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
/// Dart import
import 'dart:math';
import 'package:flutter/material.dart';
/// Package imports
import 'package:intl/intl.dart';
/// Chart import
import 'package:syncfusion_flutter_charts/charts.dart';
/// Local imports
import '../../../../model/sample_view.dart';
import '../../../../widgets/custom_button.dart';
/// Renders the chart with crosshair sample.
class DefaultCrossHair extends SampleView {
/// creates the chart with crosshair.
const DefaultCrossHair(Key key) : super(key: key);
@override
_DefaultCrossHairState createState() => _DefaultCrossHairState();
}
/// State class of the chart with crosshair.
class _DefaultCrossHairState extends SampleViewState {
_DefaultCrossHairState();
late bool alwaysShow;
late double duration;
late List<String> _lineTypeList;
late String _selectedLineType;
late CrosshairLineType _lineType;
late List<ChartSampleData> randomData;
@override
void initState() {
_lineTypeList = <String>['both', 'vertical', 'horizontal'].toList();
randomData = getDatatTimeData();
_selectedLineType = 'both';
_lineType = CrosshairLineType.both;
duration = 2;
alwaysShow = true;
super.initState();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(
bottom: model.isWebFullView || !isCardView ? 0 : 60),
child: _buildDefaultCrossHairChart());
}
@override
void dispose() {
_lineTypeList.clear();
randomData.clear();
super.dispose();
}
@override
Widget buildSettings(BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter stateSetter) {
return ListView(
shrinkWrap: true,
children: <Widget>[
Row(
children: <Widget>[
const SizedBox(width: 16.0),
Text('Line type',
softWrap: false,
style: TextStyle(fontSize: 16, color: model.textColor)),
Container(
height: 50.0,
padding: !model.isWebFullView
? const EdgeInsets.fromLTRB(65, 0, 0, 0)
: const EdgeInsets.fromLTRB(42, 0, 0, 0),
child: DropdownButton<String>(
dropdownColor: model.drawerBackgroundColor,
focusColor: Colors.transparent,
underline:
Container(color: const Color(0xFFBDBDBD), height: 1),
value: _selectedLineType,
items: _lineTypeList.map((String value) {
return DropdownMenuItem<String>(
value: (value != null) ? value : 'both',
child: Text(value,
style: TextStyle(color: model.textColor)));
}).toList(),
onChanged: (dynamic value) {
onLineTypeChange(value);
stateSetter(() {});
})),
],
),
Row(
children: <Widget>[
const SizedBox(width: 16.0),
Text(model.isWebFullView ? 'Show \nalways' : 'Show always',
softWrap: false,
style: TextStyle(
fontSize: 16,
color: model.textColor,
)),
Container(
height: 50.0,
padding: !model.isWebFullView
? const EdgeInsets.fromLTRB(25, 0, 0, 0)
: const EdgeInsets.fromLTRB(55, 0, 0, 0),
child: Checkbox(
activeColor: model.primaryColor,
value: alwaysShow,
onChanged: (bool? value) {
setState(() {
alwaysShow = value!;
stateSetter(() {});
});
})),
],
),
Row(
children: <Widget>[
const SizedBox(width: 16.0),
Text('Hide delay ',
softWrap: false,
style: TextStyle(fontSize: 16, color: model.textColor)),
Container(
height: 50.0,
padding: !model.isWebFullView
? const EdgeInsets.fromLTRB(32, 0, 0, 0)
: const EdgeInsets.fromLTRB(10, 0, 0, 0),
child: CustomDirectionalButtons(
maxValue: 10,
initialValue: duration,
onChanged: (double val) => setState(() {
duration = val;
}),
step: 2,
iconColor: model.textColor,
style: TextStyle(fontSize: 20.0, color: model.textColor),
),
),
],
),
],
);
});
}
/// Returns the cartesian chart with crosshair.
SfCartesianChart _buildDefaultCrossHairChart() {
_lineType = _lineType;
return SfCartesianChart(
plotAreaBorderWidth: 0,
primaryXAxis: DateTimeAxis(
dateFormat: DateFormat.y(),
majorGridLines: const MajorGridLines(width: 0),
edgeLabelPlacement: EdgeLabelPlacement.shift,
interactiveTooltip: InteractiveTooltip(
enable: (_lineType == CrosshairLineType.both ||
_lineType == CrosshairLineType.vertical)
? true
: false)),
/// To enable the cross hair for cartesian chart.
crosshairBehavior: CrosshairBehavior(
enable: true,
hideDelay: duration * 1000,
activationMode: ActivationMode.singleTap,
shouldAlwaysShow: alwaysShow,
lineType: _lineType),
primaryYAxis: NumericAxis(
axisLine: const AxisLine(width: 0),
interactiveTooltip: InteractiveTooltip(
enable: (_lineType == CrosshairLineType.both ||
_lineType == CrosshairLineType.horizontal)
? true
: false),
majorTickLines: const MajorTickLines(width: 0)),
series: getDefaultCrossHairSeries(),
);
}
/// Returns the list of chart series which need to
/// render on the Cartesian chart.
List<LineSeries<ChartSampleData, DateTime>> getDefaultCrossHairSeries() {
return <LineSeries<ChartSampleData, DateTime>>[
LineSeries<ChartSampleData, DateTime>(
dataSource: randomData,
xValueMapper: (ChartSampleData sales, _) => sales.x as DateTime,
yValueMapper: (ChartSampleData sales, _) => sales.y)
];
}
/// Method the update the crosshair line type in the chart on change.
void onLineTypeChange(String item) {
_selectedLineType = item;
if (_selectedLineType == 'both') {
_lineType = CrosshairLineType.both;
}
if (_selectedLineType == 'horizontal') {
_lineType = CrosshairLineType.horizontal;
}
if (_selectedLineType == 'vertical') {
_lineType = CrosshairLineType.vertical;
}
setState(() {
/// update the crosshair line type changes
});
}
}
/// Method to get random data points for the chart with crosshair sample.
List<ChartSampleData> getDatatTimeData() {
final List<ChartSampleData> randomData = <ChartSampleData>[];
final Random rand = Random();
double value = 100;
for (int i = 1; i < 2000; i++) {
if (rand.nextDouble() > 0.5) {
value += rand.nextDouble();
} else {
value -= rand.nextDouble();
}
randomData.add(ChartSampleData(x: DateTime(1900, i), y: value));
}
return randomData;
}