using OxyPlot; using OxyPlot.Axes; using OxyPlot.Series; using OxyPlot.Xamarin.Forms; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; namespace TestGrpah { public partial class MainPage : ContentPage { ObservableCollection RouterCollection; private ListView RouterChartListView; public MainPage() { InitializeComponent(); RouterChartListView = this.FindByName("ControlRouterlistview"); RouterCollection = new ObservableCollection() ; RouterCTSADetails routername; routername = new RouterCTSADetails(); routername.Uri = "Helpdesk"; RouterCollection.Add(routername); routername = new RouterCTSADetails(); routername.Uri = "Support"; RouterCollection.Add(routername); routername = new RouterCTSADetails(); routername.Uri = "Email"; RouterCollection.Add(routername); routername = new RouterCTSADetails(); routername.Uri = "Chat"; RouterCollection.Add(routername); RouterChartListView.ItemsSource = RouterCollection; } private void ViewCell_Tapped(object sender, EventArgs e) { try { if (((ViewCell)sender).BindingContext == null) return; var Details = ((ViewCell)sender).BindingContext as RouterCTSADetails; int index = RouterCollection.IndexOf(Details); if (Details.isvisible) { Details.isvisible = false; Details.Uri = Details.Uri + "Unclicked"; if (index != -1) { RouterCollection.Remove(Details); RouterCollection.Insert(index, Details); } } else { if (!string.IsNullOrEmpty(Details.Uri)) { PlotModel m; PlotView v1; List getChartDataFinal; List getChartData = new List(new string[] { "08:00", "09:00", "10:00", "11:00", "3", "6", "1", "8", "33", "62", "15", "86" }); List getChartData2 = new List(new string[] { "12:30", "13:30", "14:30", "15:30", "3", "6", "1", "8", "33", "62", "15", "86" }); List getChartData3 = new List(new string[] { "16:40", "17:40", "18:40", "19:40", "3", "6", "1", "8", "33", "62", "15", "86" }); List getChartData4 = new List(new string[] { "21:50", "22:50", "23:50", "24:00", "3", "6", "1", "8", "33", "62", "15", "86" }); GenerateLineChart g = new GenerateLineChart(); if (index == 0) getChartDataFinal = getChartData; else if(index==1) getChartDataFinal = getChartData2; else if (index == 2) getChartDataFinal = getChartData3; else if (index == 3) getChartDataFinal = getChartData4; else getChartDataFinal = getChartData; m = g.DrawLineChart(getChartDataFinal); // Details.MyPlotViewModel = pr.GetRouterHistoryForLineChart(new string[] { Details.Uri }); // Details.MyPiePlotViewModel = pr.GetRouterResultForPieChart(new string[] { Details.Uri }); Details.isvisible = !Details.isvisible; Details.Uri = Details.Uri + "clicked"; if (index != -1) { v1 = ((ViewCell)sender).FindByName("LinePlotModel"); v1.IsVisible = true; v1.Model = m; RouterCollection.Remove(Details); RouterCollection.Insert(index, Details); ((ViewCell)sender).BindingContext = Details; } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } } public class RouterCTSADetails { public string Uri { get; set; } public bool isvisible { get; set; } } public class GenerateLineChart { LineSeries callLineSeries, serviceLineSeries; TimeSpan tspan; public PlotModel DrawLineChart(List getChartData) { int i = 0; int l = 0; string[] time = null; string[] call = null; string[] service = null; string[] chartData; chartData = new List(getChartData).ToArray(); try { if (chartData != null && chartData.Length > 0) { time = new string[chartData.Length / 3]; for (i = 0; i < chartData.Length / 3; i++) { time[i] = chartData[i]; } } if (i == (chartData.Length / 3)) { int j = chartData.Length / 3; l = j + j; int k = 0; call = new string[chartData.Length / 3]; for (i = j; i <= l - 1; i++) { call[k] = chartData[i]; k++; } } if (i > chartData.Length / 2) { int j = chartData.Length / 2; service = new string[chartData.Length / 3]; int k = 0; for (i = l; i < chartData.Length; i++) { service[k] = chartData[i]; k++; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Could not load coordinates for line chart: " + ex.Message); } return drawChart(time, call, service, getChartData); } public PlotModel drawChart(string[] time, string[] call, string[] service, List lineChartData) { double[] dCalls = null; PlotModel model = new PlotModel { Title = "", TitleColor = OxyColors.Gray }; model.Background = OxyColors.White; model.ResetAllAxes(); model.LegendPosition = LegendPosition.TopCenter; model.LegendPlacement = LegendPlacement.Outside; model.LegendOrientation = LegendOrientation.Horizontal; model.PlotType = PlotType.XY; model.InvalidatePlot(true); try { callLineSeries = new LineSeries() { Title = "Calls", StrokeThickness = 3, MarkerType = MarkerType.Circle, MarkerSize = 3, Color = OxyColors.SkyBlue }; serviceLineSeries = new LineSeries() { Title = "Service level", StrokeThickness = 3, Color = OxyColors.Orange, MarkerType = MarkerType.Square, MarkerSize = 3 }; TimeSpan ts; dCalls = new double[time.Length]; List timeValues = new List(); for (int i = 0; i < time.Length; i++) { if (TimeSpan.TryParse(time[i], out ts)) timeValues.Add(ts); callLineSeries.Points.Add(new DataPoint(TimeSpanAxis.ToDouble(ts), Double.Parse(call[i]))); dCalls[i] = double.Parse(call[i]); serviceLineSeries.Points.Add(new DataPoint(TimeSpanAxis.ToDouble(ts), Double.Parse(service[i]))); } double highestCalls = (dCalls.Max()) + 1; int Length() { if (timeValues != null && timeValues.Count > 0) { if (timeValues.Count > 10 && timeValues.Count < 13) return 20; else if (timeValues.Count >= 13) return 15; else if (timeValues.Count < 10) return 30; else return 30; } else return 30; } model.PlotAreaBorderColor = OxyColors.Transparent; model.Axes.Add(new LinearAxis { Position = AxisPosition.Right, Minimum = 0, Maximum = 110, Title ="Service level", TitleColor = OxyColors.Orange, AxislineStyle = LineStyle.Solid, MinorTickSize = 0, MajorTickSize = 5, IsPanEnabled = false, IsZoomEnabled = false }); model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = 0, Maximum = highestCalls, Title = "Calls", TitleColor = OxyColors.SkyBlue, AxislineStyle = LineStyle.Solid, Key = "CallsAxis", MinorTickSize = 0, MajorTickSize = 5, IsPanEnabled = false, IsZoomEnabled = false }); model.Axes.Add(new TimeSpanAxis { Position = AxisPosition.Bottom, LabelFormatter = d => { return string.Format("{0:00}:{1:00}", TimeSpanAxis.ToTimeSpan(d).Hours, TimeSpanAxis.ToTimeSpan(d).Minutes); }, Angle = -90, AxislineStyle = LineStyle.Solid, MajorTickSize = 5, MinorTickSize = 0, Minimum = TimeSpanAxis.ToDouble(timeValues[0]), Maximum = TimeSpanAxis.ToDouble(timeValues[timeValues.Count - 1]), IntervalLength = Length(), IsZoomEnabled = false, IsPanEnabled = false }); callLineSeries.YAxisKey = "CallsAxis"; model.Series.Add(callLineSeries); model.Series.Add(serviceLineSeries); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Could not create line chart: " + ex.Message); } return model; } } }