private void DrawBarChart()
        {
            List <BarEntry> datalist = new List <BarEntry>();

            labelnames = new List <string>();
            for (int i = 0; i < lstbarChartData.Count; i++)
            {
                string month = lstbarChartData[i].Month;
                double total = lstbarChartData[i].Total;
                datalist.Add(new BarEntry(i, (float)total));
                labelnames.Add(month);
            }
            var SelectedMonthindex = labelnames.IndexOf(SelectedMonth.Substring(0, 3));

            int[] colors = new int[12];
            for (int i = 0; i < labelnames.Count; i++)
            {
                if (i == SelectedMonthindex)
                {
                    colors[i] = Android.Graphics.Color.ParseColor("#efbe5d");
                }
                else
                {
                    colors[i] = Android.Graphics.Color.ParseColor("#204060");
                }
            }


            prefs = PreferenceManager.GetDefaultSharedPreferences(this);
            MikePhil.Charting.Data.BarDataSet barDataSet = new MikePhil.Charting.Data.BarDataSet(datalist, "Months");
            barDataSet.SetColors(colors);
            barDataSet.ValueTextSize = 8.5f;
            BarData barData = new BarData(barDataSet);

            barData.HighlightEnabled = false;
            barchartStats.Data       = barData;

            // YAxis yAxis = barchartStats.AxisLeft;
            // yAxis.ValueFormatter = new IndexAxisValueFormatter(labelnames);
            XAxis xAxis = barchartStats.XAxis;

            xAxis.ValueFormatter = new IndexAxisValueFormatter(labelnames);

            xAxis.Position = XAxis.XAxisPosition.Top;
            xAxis.YOffset  = -4f;
            xAxis.SetDrawAxisLine(false);
            xAxis.SetDrawGridLines(false);
            xAxis.Granularity = 1f;
            xAxis.SetLabelCount(lstbarChartData.Count, false);
            xAxis.LabelRotationAngle = 270f;
            barchartStats.AnimateY(1000);

            barchartStats.Invalidate();

            progressBarStats.Visibility = ViewStates.Invisible;
            barchartStats.Visibility    = ViewStates.Visible;
            statsdata.Visibility        = ViewStates.Invisible;
            statstype.Text = "Total expense for the year" + "(" + SelectedYear + ")" + " = " + prefs.GetString("CurrencySymbolSelected", "") + lstbarChartData.Sum(x => Convert.ToDouble(x.Total));
        }
示例#2
0
        public MikePhil.Charting.Charts.BarChart getBarChart(List <CE_Pesquisa07> respostas, List <CE_Pesquisa06> ondas)
        {
            MikePhil.Charting.Charts.BarChart barChart = new MikePhil.Charting.Charts.BarChart(Android.App.Application.Context);

            try
            {
                List <Int32> colors = new List <Int32>();

                foreach (int c in MikePhil.Charting.Util.ColorTemplate.VordiplomColors)
                {
                    colors.Add(c);
                }

                foreach (int c in MikePhil.Charting.Util.ColorTemplate.JoyfulColors)
                {
                    colors.Add(c);
                }

                foreach (int c in MikePhil.Charting.Util.ColorTemplate.ColorfulColors)
                {
                    colors.Add(c);
                }

                foreach (int c in MikePhil.Charting.Util.ColorTemplate.LibertyColors)
                {
                    colors.Add(c);
                }

                foreach (int c in MikePhil.Charting.Util.ColorTemplate.PastelColors)
                {
                    colors.Add(c);
                }

                colors.Add(MikePhil.Charting.Util.ColorTemplate.HoloBlue);

                List <MikePhil.Charting.Interfaces.Datasets.IBarDataSet> datasets = new List <MikePhil.Charting.Interfaces.Datasets.IBarDataSet>();

                List <string> labels = new List <string>();

                Configuracao conf = ObterConfiguracao();

                float percentualMaximo = conf.PercentualMaximoGrafico;

                respostas = respostas.OrderBy(o => o.txresposta).ToList();

                int qt = respostas.Count > 3 ? 3 : respostas.Count;//respostas.Count > 5 ? respostas.IndexOf(respostas.FirstOrDefault(o => o.totalpercentual > (decimal)percentualMaximo)) + 1 : respostas.Count;

                for (int i = 0; i < qt; i++)
                {
                    labels.Add(respostas[i].txresposta != null ? respostas[i].txresposta : respostas[i].vlresposta.ToString());
                }

                /*if(respostas.Count > 5)
                 * {
                 *  if (respostas.Count == 6)
                 *      labels.Add(respostas[5].txresposta != null ? respostas[5].txresposta : respostas[5].vlresposta.ToString());
                 *  else
                 *      labels.Add("Outros");
                 * }*/

                for (int j = 1; j <= ondas.Count; j++)
                {
                    List <MikePhil.Charting.Data.BarEntry> barEntries = new List <MikePhil.Charting.Data.BarEntry>();

                    for (int i = 0; i < qt; i++)
                    {
                        float[] vals = new float[] { float.Parse(GetPropValue(respostas[i], "percentual" + j).ToString()) };
                        barEntries.Add(new MikePhil.Charting.Data.BarEntry(vals, i));
                    }

                    /*if (respostas.Count > 5)
                     * {
                     *  if (respostas.Count == 6)
                     *  {
                     *      barEntries.Add(new MikePhil.Charting.Data.BarEntry((float)GetPropValue(respostas[5], "percentual" + j), 5));
                     *  }
                     *  else
                     *  {
                     *      List<CE_Pesquisa07> outros = respostas.Where(o => o.totalpercentual > respostas[qt - 1].totalpercentual).ToList();
                     *
                     *      float valor = 0;
                     *
                     *      foreach (var item in outros)
                     *      {
                     *          valor += (float)GetPropValue(item, "percentual" + j);
                     *      }
                     *
                     *      barEntries.Add(new MikePhil.Charting.Data.BarEntry(valor, qt));
                     *  }
                     * }*/

                    MikePhil.Charting.Data.BarDataSet barDataSet = new MikePhil.Charting.Data.BarDataSet(barEntries, null);
                    barDataSet.SetColors(colors.ToArray());

                    datasets.Add(barDataSet);
                }

                MikePhil.Charting.Data.BarData barData = new MikePhil.Charting.Data.BarData(labels, datasets);
                barData.SetValueTextSize(15f);
                barChart.Data = barData;

                barChart.SetDescription("");
                barChart.Legend.Enabled = false;
                barChart.HighlightValues(null);
                barChart.Invalidate();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(barChart);
        }