-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathDataPointSelection.cs
242 lines (198 loc) · 8.59 KB
/
DataPointSelection.cs
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
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
using Com.Syncfusion.Charts;
using Com.Syncfusion.Charts.Enums;
using Android.Graphics;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System;
using Com.Syncfusion.Navigationdrawer;
namespace SampleBrowser
{
public class DataPointSelection : SamplePage
{
private TextView label;
private TextView productLabel1;
private TextView productLabel2;
double sumOfTotalSeries1 = 0;
double sumOfTotalSeries2 = 0;
IList<DataPoint> datapoint;
IList<DataPoint> datapoint1;
List<String> selectionMode;
ColumnSeries columnSeries;
ColumnSeries columnSeries1;
SfChart chart;
public override View GetSampleContent(Context context)
{
chart = new SfChart(context);
chart.Title.Text = "Product Sale 2016";
chart.SetBackgroundColor(Color.White);
var primaryAxis = new CategoryAxis { LabelPlacement = LabelPlacement.BetweenTicks };
primaryAxis.Title.Text = "Month";
chart.PrimaryAxis = primaryAxis;
var secondaryAxis = new NumericalAxis();
primaryAxis.ShowMajorGridLines = false;
secondaryAxis.Title.Text = "Sales";
secondaryAxis.LabelStyle.LabelFormat = "$##.##";
chart.SecondaryAxis = secondaryAxis;
columnSeries = new ColumnSeries
{
ItemsSource = MainPage.GetSelectionData(),
XBindingPath = "XValue",
YBindingPath = "YValue",
Color = Color.ParseColor("#00BDAE"),
SelectedDataPointColor = Color.ParseColor("#007168"),
DataPointSelectionEnabled = true,
EnableAnimation = true,
Label = "Product A"
};
chart.Series.Add(columnSeries);
columnSeries1 = new ColumnSeries
{
ItemsSource = MainPage.GetSelectionData1(),
XBindingPath = "XValue",
YBindingPath = "YValue",
Color = Color.ParseColor("#7F84E8"),
SelectedDataPointColor = Color.ParseColor("#4A4FB2"),
DataPointSelectionEnabled = true,
EnableAnimation = true,
Label = "Product B"
};
chart.Series.Add(columnSeries1);
datapoint = columnSeries.ItemsSource as IList<DataPoint>;
datapoint1 = columnSeries1.ItemsSource as IList<DataPoint>;
foreach (var data in datapoint)
{
sumOfTotalSeries1 += data.YValue;
}
foreach (var data in datapoint1)
{
sumOfTotalSeries2 += data.YValue;
}
chart.Behaviors.Add(new ChartSelectionBehavior());
chart.SelectionChanged += chart_SelectionChanged;
label = new TextView(context){ TextSize = 15 };
label.Text = "Tap on a bar segment to select a data point.";
label.SetPadding(5, 5, 5, 5);
productLabel1 = new TextView(context) { TextSize = 15, Visibility = ViewStates.Gone, };
productLabel1.SetTextColor(Color.ParseColor("#007168"));
productLabel1.SetPadding(5, 5, 5, 5);
productLabel2 = new TextView(context) { TextSize = 15, Visibility = ViewStates.Gone, };
productLabel2.SetTextColor(Color.ParseColor("#4A4FB2"));
productLabel2.SetPadding(5, 5, 5, 5);
var layout = new LinearLayout(context){ Orientation = Android.Widget.Orientation.Vertical };
var layoutLabel = new LinearLayout(context)
{
Orientation = Android.Widget.Orientation.Horizontal,
LayoutParameters =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
};
layoutLabel.SetHorizontalGravity(GravityFlags.CenterHorizontal);
layoutLabel.AddView(label);
layoutLabel.AddView(productLabel1);
layoutLabel.AddView(productLabel2);
layout.AddView(layoutLabel);
layout.AddView(chart);
return layout;
}
private void chart_SelectionChanged(object sender, SfChart.SelectionChangedEventArgs e)
{
var chart = sender as SfChart;
var series = e.P1.SelectedSeries;
if (series == null) return;
if (chart.EnableSeriesSelection)
{
productLabel1.Visibility = ViewStates.Gone;
productLabel2.Visibility = ViewStates.Gone;
label.Visibility = ViewStates.Visible;
if (columnSeries.IsSelected)
{
label.Text = series.Label + " | Total Sales : $" + sumOfTotalSeries1;
return;
}
else if (columnSeries1.IsSelected)
{
label.Text = series.Label + " | Total Sales : $" + sumOfTotalSeries2;
return;
}
label.Text = "Tap on a bar segment to select a series";
}
else
{
productLabel1.Visibility = ViewStates.Visible;
productLabel2.Visibility = ViewStates.Visible;
label.Visibility = ViewStates.Gone;
var selectedindex = e.P1.SelectedDataPointIndex;
var seriesIndex = chart.Series.IndexOf(e.P1.SelectedSeries);
if (selectedindex < 0)
{
if (seriesIndex == 0)
productLabel1.Text = "Tap on a bar segment";
else
productLabel2.Text = "Tap on a bar segment";
return;
}
else
{
if (seriesIndex == 0)
{
var x = datapoint[selectedindex].XValue;
var y = datapoint[selectedindex].YValue;
productLabel1.Text = "Month : " + x + ", Sales : $" + y;
}
else if (seriesIndex == 1)
{
var x = datapoint1[selectedindex].XValue;
var y = datapoint1[selectedindex].YValue;
productLabel2.Text = "Month : " + x + ", Sales : $" + y;
}
}
}
}
public override View GetPropertyWindowLayout(Context context)
{
int width = (context.Resources.DisplayMetrics.WidthPixels) / 2;
LinearLayout propertylayout = new LinearLayout(context);
propertylayout.Orientation = Android.Widget.Orientation.Vertical;
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
width * 2, 5);
layoutParams1.SetMargins(0, 20, 0, 0);
TextView selection = new TextView(context);
selection.TextSize = 20;
selection.Text = "Selection";
selection.SetPadding(5, 20, 0, 20);
Spinner selectLabelMode = new Spinner(context, SpinnerMode.Dialog);
selectionMode = new List<String>() { "Data Point Selection", "Series Selection" };
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(context, Android.Resource.Layout.SimpleSpinnerItem, selectionMode);
dataAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleDropDownItem1Line);
selectLabelMode.Adapter = dataAdapter;
selectLabelMode.ItemSelected += SelectLabelMode_ItemSelected;
SeparatorView separate = new SeparatorView(context, width * 2);
separate.LayoutParameters = new ViewGroup.LayoutParams(width * 2, 5);
propertylayout.AddView(selection);
propertylayout.AddView(selectLabelMode);
propertylayout.AddView(separate, layoutParams1);
return propertylayout;
}
private void SelectLabelMode_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
String selectedItem = selectionMode[e.Position];
if (selectedItem.Equals("Data Point Selection"))
{
chart.EnableSeriesSelection = false;
}
else if (selectedItem.Equals("Series Selection"))
{
chart.EnableSeriesSelection = true;
}
}
public override void OnApplyChanges()
{
base.OnApplyChanges();
}
}
}