Example #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here

            SetContentView(Resource.Layout.Graph);

            webService     = new WebService();
            listStatsTable = new List <StatsTables>();
            listStatsTable = webService.GetAllDataStatsTable();

            float[] data    = new float[listStatsTable.Count];
            int     counter = 0;

            foreach (StatsTables item in listStatsTable)
            {
                data[counter] = item.diff_last_trans;
                counter++;
            }

            //BarModel test = new BarModel();

            //foreach (StatsTables item in listStatsTable)
            //{
            //    test.Legend = item.table_name;
            //}

            var viewChart = new BarChartView(this)
            {
                ItemsSource = Array.ConvertAll(data, v => new BarModel {
                    Value              = v,
                    Legend             = "Table name",
                    ValueCaptionHidden = false,
                })
            };

            viewChart.AutoLevelsEnabled = false;
            for (int i = 0; i <= 100; i += 10)
            {
                viewChart.AddLevelIndicator(i);
            }
            viewChart.LegendColor = Color.Red;
            viewChart.SetBackgroundColor(Color.White);
            viewChart.LegendFontSize       = 30;
            viewChart.BarCaptionFontSize   = 20;
            viewChart.BarCaptionOuterColor = Color.Black;
            viewChart.BarCaptionInnerColor = Color.White;
            viewChart.BarWidth             = 100;
            viewChart.BarOffset            = 100;

            AddContentView(viewChart, new ViewGroup.LayoutParams(
                               WindowManagerLayoutParams.FillParent, ViewGroup.LayoutParams.FillParent));
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Name            = Intent.Extras.GetString("name");
            Address         = Intent.Extras.GetString("address");
            DatasetName     = Intent.Extras.GetString("datasetName");
            DatasetAlias    = Intent.Extras.GetString("datasetAlias");
            TimestampOffset = Intent.Extras.GetLong("timestampOffset");
            var sourceUnit = Intent.Extras.GetString("datasetSourceUnit");
            var targetUnit = Intent.Extras.GetString("datasetTargetUnit");

            Title = $"{Name} ({Address})";

            try
            {
                Converter = UnitConverter.Create(sourceUnit, targetUnit);

                Api = new ApiClient(Address);

                Api.Call <Api.Responses.Dataset>(new Api.Requests.Dataset(DatasetName, null, null), Canceller.Token)
                .ContinueWith(t =>
                {
                    if (t.IsCanceled)
                    {
                        return;
                    }

                    if (t.IsFaulted)
                    {
                        Log.Error(TAG, $"Failed to fetch dataset {DatasetName} with error {t.Exception.Flatten().InnerException}.");
                        Toast.MakeText(this, "Failed to get performance information.", ToastLength.Short).Show();
                        return;
                    }

                    RunOnUiThread(() =>
                    {
                        // Change units.
                        var dataset = t
                                      .Result
                                      .dataset
                                      .Select(d => new DataPoint
                        {
                            Timestamp = d.Timestamp - TimestampOffset,
                            Value     = Converter.ChangeUnits(d.Value),
                        })
                                      .ToArray();

                        var minValue = dataset.Min(d => d.Value);
                        var min      = dataset.First(d => d.Value == minValue);
                        var maxValue = dataset.Max(d => d.Value);
                        var max      = dataset.First(d => d.Value == maxValue);
                        var width    = maxValue - minValue;

                        var datasetMap = dataset.ToDictionary(d => new BarModel
                        {
                            Value = d.Value,
                            ValueCaptionHidden = true,
                        });

                        var chart = new BarChartView(this)
                        {
                            ItemsSource  = datasetMap.Keys,
                            BarWidth     = 3,
                            BarOffset    = 1,
                            MinimumValue = min.Value - width / 2,
                            MaximumValue = max.Value + width / 2,
                        };

                        chart.AutoLevelsEnabled = false;
                        chart.AddLevelIndicator(min.Value, StringizeValue(min.Value));
                        chart.AddLevelIndicator(max.Value, StringizeValue(max.Value));

                        chart.BarClick += (sender, args) => {
                            DataPoint data = null;
                            if (!datasetMap.TryGetValue(args.Bar, out data))
                            {
                                return;
                            }

                            new AlertDialog.Builder(this)
                            .SetCancelable(false)
                            .SetTitle(UIConversion.FromMilliseconds(data.Timestamp).ToString())
                            .SetMessage(StringizeValue(data.Value))
                            .SetPositiveButton("OK", (s, e) => { })
                            .Create()
                            .Show();
                        };

                        SetContentView(chart, new ViewGroup.LayoutParams(
                                           ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
                                       );
                    });
                });
            }
            catch (Exception e)
            {
                Toast.MakeText(this, $"Failed to get performance information for {DatasetName} for {Title}", ToastLength.Short).Show();
                Log.Error(TAG, $"Failed to start activity for dataset {DatasetName} with error {e}.");
                Finish();
            }
        }