Exemplo n.º 1
0
        void UpdatePowerMonitorChart()
        {
            var chart = FindViewById <LineChart>(Resource.Id.lineChart);


            Entry[] values = new Entry[powerHistory.values.Length];

            for (int i = 0; i < values.Count(); i++)
            {
                values[i] = new Entry(i, powerHistory.values[i]);
            }

            LineDataSet set1;

            if (chart.Data != null &&
                chart.Data.DataSetCount > 0)
            {
                set1        = (LineDataSet)chart.Data.GetDataSetByIndex(0);
                set1.Values = values;
                chart.Data.NotifyDataChanged();
                chart.NotifyDataSetChanged();
            }
            else
            {
                // create a dataset and give it a type
                set1 = new LineDataSet(values, "Power Usage");

                set1.SetMode(LineDataSet.Mode.CubicBezier);
                set1.CubicIntensity = 0.2f;
                set1.SetDrawFilled(true);
                set1.SetDrawCircles(false);
                set1.LineWidth = 1.6f;
                //set1.CircleRadius = 4f;
                //set1.SetCircleColor(Color.White);
                set1.HighLightColor = Color.Rgb(244, 117, 117);
                set1.Color          = Color.ParseColor("#68B9C0");
                set1.FillColor      = Color.ParseColor("#68B9C0");
                set1.FillAlpha      = 100;
                set1.SetDrawHorizontalHighlightIndicator(false);
                set1.SetDrawVerticalHighlightIndicator(false);
                //    set1.FillFormatter = new IFillFormatter() {
                //    public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider)
                //    {
                //        return chart.getAxisLeft().getAxisMinimum();
                //    }
                //};

                // create a data object with the data sets
                LineData data = new LineData(set1);
                //data.setValueTypeface(tfLight);
                data.SetValueTextSize(12f);
                data.SetDrawValues(false);

                // set data
                chart.Data = data;
            }
            chart.SetViewPortOffsets(0, 0, 0, 0);
            chart.Invalidate();
        }
        public LineChart SetupChartData(DateTime startDate, DateTime endDate)
        {
            //when first entered into this activity the default is the past weeks data, so we need to get that
            Globals dbHelp = null;

            try
            {
                if (_lineChart != null)
                {
                    Log.Info(TAG, "SetupChartData: Found _progressChart");
                    DateTime loopDate = startDate;
                    //this next array will contain the string labels for the X axis, which is the date of the point
                    List <string> pointsDates = new List <string>();

                    if (GlobalData.MoodListItems != null)
                    {
                        dbHelp = new Globals();
                        dbHelp.OpenDatabase();
                        LineDataSet         moodDataSet      = null;
                        List <ILineDataSet> moodLineDataSets = new List <ILineDataSet>();
                        var daysDifference = endDate.Subtract(startDate).Days;
                        Log.Info(TAG, "SetupChartData: Attempting to get " + daysDifference.ToString() + " days data from " + startDate.ToShortDateString() + " to " + endDate.ToShortDateString());
                        for (var a = 0; a <= daysDifference; a++)
                        {
                            pointsDates.Add(loopDate.ToShortDateString());
                            Log.Info(TAG, "SetupChartData: Added '" + loopDate.ToShortDateString() + " to date list");
                            loopDate = loopDate.AddDays(1);
                        }

                        Log.Info(TAG, "SetupChartData: MoodListItems global list contains " + GlobalData.MoodListItems.Count.ToString() + " items");
                        foreach (var item in GlobalData.MoodListItems)
                        {
                            Log.Info(TAG, "SetupChartData: Attempting to retrieve data points for " + item.MoodName.Trim());
                            moodDataSet = GetDataPoints(item.MoodId, startDate, endDate, item.MoodName);
                            if (moodDataSet != null)
                            {
                                int[] colorList = new int[1] {
                                    GlobalData.ColourList[item.MoodId - 1]
                                };
                                moodDataSet.SetColors(colorList);
                                moodLineDataSets.Add(moodDataSet);
                                Log.Info(TAG, "SetupChartData: Added " + item.MoodName + " to data sets list");
                            }
                            else
                            {
                                Log.Info(TAG, "SetupChartData: No data set returned for " + item.MoodName + "!");
                            }
                        }
                        LineData moodLineData = new LineData(pointsDates, moodLineDataSets);
                        Log.Info(TAG, "SetupChartData: Created LineData containing date list and mood data sets");
                        _lineChart.Data = moodLineData;
                        _lineChart.Legend.WordWrapEnabled = true;
                        _lineChart.SetDescriptionColor(Color.White);
                        _lineChart.SetDescription(_activity.GetString(Resource.String.MyProgressGraphDescription) + " " + startDate.ToShortDateString() + " - " + endDate.ToShortDateString());
                        _lineChart.XAxis.TextColor         = Color.White;
                        _lineChart.XAxis.AxisLineColor     = Color.White;
                        _lineChart.XAxis.GridColor         = Color.White;
                        _lineChart.AxisLeft.AxisLineColor  = Color.White;
                        _lineChart.AxisRight.AxisLineColor = Color.White;
                        _lineChart.AxisLeft.GridColor      = Color.White;
                        _lineChart.AxisRight.GridColor     = Color.White;
                        _lineChart.AxisLeft.TextColor      = Color.White;

                        _lineChart.AxisRight.TextColor = Color.White;
                        _lineChart.AxisLeft.TextSize   = 10f;
                        _lineChart.AxisRight.TextSize  = 10f;
                        var yaxis1 = _lineChart.GetAxis(YAxis.AxisDependency.Left);
                        yaxis1.AxisLineColor = Color.White;
                        yaxis1.GridColor     = Color.White;
                        yaxis1.TextColor     = Color.White;
                        yaxis1.TextSize      = 10f;
                        var yaxis2 = _lineChart.GetAxis(YAxis.AxisDependency.Right);
                        yaxis2.AxisLineColor = Color.White;
                        yaxis2.GridColor     = Color.White;
                        yaxis2.TextColor     = Color.White;
                        yaxis1.TextSize      = 10f;
                        _lineChart.SetBorderColor(Color.White);
                        moodLineData.SetValueTextColor(Color.White);
                        moodLineData.SetValueTextSize(10f);

                        _lineChart.Invalidate();
                        dbHelp.CloseDatabase();
                        Log.Info(TAG, "SetupChartData: Finished retrieving chart data");
                    }
                }
            }
            catch (Exception e)
            {
                if (dbHelp != null)
                {
                    if (dbHelp.GetSQLiteDatabase().IsOpen)
                    {
                        dbHelp.CloseDatabase();
                    }
                }
                Log.Error(TAG, "SetupChartData: Exception - " + e.Message);
                if (GlobalData.ShowErrorDialog)
                {
                    ErrorDisplay.ShowErrorAlert(_activity, e, "Setting data for chart", "ProgressChartHelper.SetupChartData");
                }
            }

            return(_lineChart);
        }