-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathDataUsage.cs
175 lines (139 loc) · 6.66 KB
/
DataUsage.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Com.Syncfusion.Rangenavigator;
using Android.Graphics;
using Com.Syncfusion.Charts;
using Com.Syncfusion.Charts.Enums;
using Java.Util;
using Android.Util;
using Java.Text;
using System.Collections.ObjectModel;
namespace SampleBrowser
{
public class DataUsage : SamplePage
{
List<DataPoint> dataPoints;
TextView dataRangeText;
TextView dataUsageText;
public override View GetSampleContent(Context context)
{
LayoutInflater layoutInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
View view = layoutInflater.Inflate(Resource.Layout.range_navigator_data_usage, null);
CustomRangeNavigator sfRangeNavigator = view.FindViewById<CustomRangeNavigator>(Resource.Id.range_navigator_datausage);
dataRangeText = view.FindViewById<TextView>(Resource.Id.date_time_range_text);
dataUsageText = view.FindViewById<TextView>(Resource.Id.date_time_usage_mb);
sfRangeNavigator.SetMinimumHeight(100);
SfChart rangeNavigatorContent = new SfChart(context);
sfRangeNavigator.Intervals = Com.Syncfusion.Rangenavigator.DateTimeIntervalType.Month | Com.Syncfusion.Rangenavigator.DateTimeIntervalType.Week;
Calendar min = new GregorianCalendar(2015, 0, 1);
Calendar max = new GregorianCalendar(2015, 4, 1);
Calendar rangeStart = new GregorianCalendar(2015, 1, 13);
Calendar rangeEnd = new GregorianCalendar(2015, 2, 13);
sfRangeNavigator.Minimum = min.Time;
sfRangeNavigator.Maximum = max.Time;
sfRangeNavigator.ViewRangeStart = rangeStart.Time;
sfRangeNavigator.ViewRangeEnd = rangeEnd.Time;
sfRangeNavigator.TooltipEnabled = false;
UpdateChart(rangeNavigatorContent, Visibility.Gone);
sfRangeNavigator.Content = rangeNavigatorContent;
sfRangeNavigator.SetClipChildren(false);
dataRangeText.SetPadding(5, 20, 5, 5);
dataUsageText.SetPadding(5, 5, 5, 5);
sfRangeNavigator.RangeChanged += SfRangeNavigator_RangeChanged;
sfRangeNavigator.MinorScaleLabelsCreate += SfRangeNavigator_LabelsGenerated;
sfRangeNavigator.MinorScaleStyle.ShowGridLines = false;
sfRangeNavigator.MajorScaleStyle.SelectedLabelTextColor = Color.ParseColor("#1cb2d5");
sfRangeNavigator.MajorScaleStyle.LabelTextColor = Color.ParseColor("#5f6872");
return view;
}
void SfRangeNavigator_LabelsGenerated(object sender, SfDateTimeRangeNavigator.MinorScaleLabelsCreateEventArgs e)
{
e.MinorScaleLabels.Clear();
}
void SfRangeNavigator_RangeChanged(object sender, SfDateTimeRangeNavigator.RangeChangedEventArgs e)
{
dataRangeText.Text = "Data usage cycle " + new SimpleDateFormat("MMM dd").Format(e.ViewRangeStart) + " - " + new SimpleDateFormat("MMM dd").Format(e.ViewRangeEnd);
DateTime minimum = ConvertToDateTime(e.ViewRangeStart);
DateTime maximum = ConvertToDateTime(e.ViewRangeEnd);
double dataUsage = 0;
for (int i = 0; i < dataPoints.Count; i++)
{
DataPoint chartDataPoint = (DataPoint)dataPoints[i];
DateTime ChartDate = (DateTime)chartDataPoint.Date;
if (ChartDate > minimum && ChartDate < maximum)
{
dataUsage = dataUsage + (Double)chartDataPoint.YValue;
}
}
dataUsageText.Text = "Date Usage " + (int)dataUsage + " MB";
}
SfChart UpdateChart(SfChart chart, Visibility axisVisibility)
{
DateTimeAxis dateTimeAxis = new DateTimeAxis();
dateTimeAxis.Visibility = axisVisibility;
dateTimeAxis.LabelStyle.LabelFormat = "dd/MMM";
dateTimeAxis.ShowMajorGridLines = axisVisibility == Visibility.Visible ? true : false;
chart.PrimaryAxis = dateTimeAxis;
NumericalAxis numericalAxis = new NumericalAxis();
numericalAxis.Visibility = axisVisibility;
numericalAxis.ShowMajorGridLines = axisVisibility == Visibility.Visible ? true : false;
chart.SecondaryAxis = numericalAxis;
chart.Series.Add(new SplineAreaSeries()
{
ItemsSource = GetDateTimeValue(),
XBindingPath = "Date",
YBindingPath = "YValue",
Alpha = 0.5f,
Color = Color.ParseColor("#7ce6c7"),
StrokeColor = Color.ParseColor("#1cb2d5")
});
return chart;
}
public List<DataPoint> GetDateTimeValue()
{
var dateTime = new DateTime(2015, 1, 1);
dataPoints = new List<DataPoint>();
dataPoints.Add(new DataPoint(dateTime, 10d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 31d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 28d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 45d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 10d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 23d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 35d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 56d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 10d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 39d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 26d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 21d));
dateTime = dateTime.AddMonths(1);
dataPoints.Add(new DataPoint(dateTime, 31d));
return dataPoints;
}
public static DateTime ConvertToDateTime(object date)
{
if (date == null) return DateTime.Now;
var nativeDate = (Java.Util.Date)date;
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return dateTime.AddMilliseconds(((Java.Util.Date)date).Time).ToLocalTime();
}
}
}