BarChartData GenerateBarData()
        {
            List <BarChartDataEntry> greenEntries  = new List <BarChartDataEntry>();
            List <BarChartDataEntry> blueEntries   = new List <BarChartDataEntry>();
            List <BarChartDataEntry> yellowEntries = new List <BarChartDataEntry>();
            List <BarChartDataEntry> orangeEntries = new List <BarChartDataEntry>();
            List <BarChartDataEntry> redEntries    = new List <BarChartDataEntry>();

            //Green
            greenEntries.Add(new BarChartDataEntry(0, 8f));
            greenEntries.Add(new BarChartDataEntry(1, 3));
            greenEntries.Add(new BarChartDataEntry(2, 1));
            greenEntries.Add(new BarChartDataEntry(3, -1));

            BarChartDataSet set1 = new BarChartDataSet(greenEntries.ToArray(), string.Empty);

            set1.DrawValuesEnabled = false;
            set1.SetColor(UIColor.Green);
            set1.ValueTextColor = UIColor.White;
            set1.ValueFont      = UIFont.SystemFontOfSize(15);
            //Blue
            blueEntries.Add(new BarChartDataEntry(3, -1f));
            blueEntries.Add(new BarChartDataEntry(4, -7f));
            blueEntries.Add(new BarChartDataEntry(5, -2f));
            blueEntries.Add(new BarChartDataEntry(6, -3f));
            blueEntries.Add(new BarChartDataEntry(7, -4f));

            BarChartDataSet set2 = new BarChartDataSet(blueEntries.ToArray(), string.Empty);

            set2.DrawValuesEnabled = false;
            set2.SetColor(UIColor.Red);
            set2.ValueTextColor = UIColor.White;
            set2.ValueFont      = UIFont.SystemFontOfSize(15);
            //Yellow

            List <IInterfaceChartDataSet> dataSets = new List <IInterfaceChartDataSet>();

            dataSets.Add(set1);
            dataSets.Add(set2);
            BarChartData barData = new BarChartData(dataSets.ToArray());

            barData.BarWidth = 0.7f;

            return(barData);
        }
        private void InitializeChart()
        {
            if (supportChart != null && supportChart.ChartData != null && chartOriginal != null)
            {
                SupportChart.OnInitializeChart(supportChart, chartOriginal);

                var data         = supportChart.ChartData;
                var dataSetItems = new List <BarChartDataSet>();

                foreach (var itemChild in data.IF_GetDataSet())
                {
                    var             entryOriginal = itemChild.IF_GetEntry().Select(item => new iOSCharts.BarChartDataEntry(item.GetXPosition(), item.GetYPosition()));
                    BarChartDataSet lineDataSet   = new BarChartDataSet(entryOriginal.ToArray(), itemChild.IF_GetTitle());
                    lineDataSet.SetColor(itemChild.IF_GetDataColor().ToUIColor());
                    lineDataSet.DrawValuesEnabled = (itemChild.IF_GetDrawValue());
                    dataSetItems.Add(lineDataSet);
                }

                BarChartData lineData = new BarChartData(dataSetItems.ToArray());
                chartOriginal.Data = lineData;
            }
        }
예제 #3
0
        public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var resultCell = collectionView.DequeueReusableCell(ResultViewController.resultCellId, indexPath) as ResultCollectionViewCell;

            resultCell.BackgroundColor = UIColor.White;

            var reportDataSet = this.reportsDatasets[indexPath.Row];

            if (ReportType.Overall.Equals(reports[indexPath.Row]))
            {
                resultCell.sectionLabel.Text = reports[indexPath.Row].ToString();

                if (reportDataSet.totalVotes > 0)
                {
                    var dataEntries = new List <PieChartDataEntry>();
                    for (int i = 0; i < reportDataSet.dataSets[0].Count; i++)
                    {
                        if (Convert.ToInt32(reportDataSet.dataSets[0][i]) > 0)
                        {
                            dataEntries.Add(new PieChartDataEntry(reportDataSet.dataSets[0][i], reportDataSet.labels[i]));
                        }
                    }

                    var dataSet = new PieChartDataSet(dataEntries.ToArray(), "");

                    dataSet.ValueFormatter = new ChartDefaultValueFormatter(new NSNumberFormatter()
                    {
                        MinimumFractionDigits = 0
                    });
                    dataSet.SliceSpace = 2;
                    //dataSet.Colors = ChartColorTemplates.Joyful;
                    dataSet.Colors          = this.chartColors.ToArray();
                    dataSet.ValueTextColor  = UIColor.FromRGB(90, 89, 89);
                    dataSet.ValueLineColor  = UIColor.FromRGB(90, 89, 89);
                    dataSet.EntryLabelColor = UIColor.FromRGB(90, 89, 89);
                    dataSet.XValuePosition  = PieChartValuePosition.OutsideSlice;
                    dataSet.YValuePosition  = PieChartValuePosition.OutsideSlice;

                    resultCell.pieChartView.Data = new PieChartData(new PieChartDataSet[] { dataSet });
                }

                resultCell.pieChartView.AnimateWithXAxisDuration(1.4, ChartEasingOption.EaseOutBack);
                resultCell.pieChartView.ChartDescription.Text = "";
                var colorAttributes = new UIStringAttributes
                {
                    ForegroundColor = UIColor.FromRGB(90, 89, 89)
                };
                resultCell.pieChartView.CenterAttributedText = new NSAttributedString(string.Format("Total {0} votes", reportDataSet.totalVotes, colorAttributes));
                resultCell.pieChartView.Legend.Enabled       = false;
                resultCell.pieChartView.NoDataText           = "No results to show";

                resultCell.AddSubview(resultCell.pieChartView);

                resultCell.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[v0]|", new NSLayoutFormatOptions(), "v0", resultCell.pieChartView));
                resultCell.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[v0(24)][v1]|", new NSLayoutFormatOptions(), "v0", resultCell.sectionLabel, "v1", resultCell.pieChartView));
            }
            else
            {
                resultCell.sectionLabel.Text = "By " + reports[indexPath.Row].ToString();

                resultCell.barChartView.Legend.Enabled             = true;
                resultCell.barChartView.Legend.HorizontalAlignment = ChartLegendHorizontalAlignment.Right;
                resultCell.barChartView.Legend.VerticalAlignment   = ChartLegendVerticalAlignment.Top;
                resultCell.barChartView.Legend.Orientation         = ChartLegendOrientation.Vertical;
                resultCell.barChartView.Legend.DrawInside          = true;
                resultCell.barChartView.Legend.YOffset             = 10.0f;
                resultCell.barChartView.Legend.XOffset             = 10.0f;
                resultCell.barChartView.Legend.YEntrySpace         = 0.0f;
                resultCell.barChartView.Legend.TextColor           = UIColor.FromRGB(90, 89, 89);

                resultCell.barChartView.XAxis.DrawGridLinesEnabled    = true;
                resultCell.barChartView.XAxis.LabelPosition           = XAxisLabelPosition.Bottom;
                resultCell.barChartView.XAxis.CenterAxisLabelsEnabled = true;
                resultCell.barChartView.XAxis.ValueFormatter          = new ChartIndexAxisValueFormatter(TranslateReportGroups(reportDataSet.groups.ToArray()));
                resultCell.barChartView.XAxis.Granularity             = 1;
                resultCell.barChartView.XAxis.LabelTextColor          = UIColor.FromRGB(90, 89, 89);
                resultCell.barChartView.XAxis.GridColor     = UIColor.FromRGB(90, 89, 89);
                resultCell.barChartView.XAxis.AxisLineColor = UIColor.FromRGB(90, 89, 89);

                resultCell.barChartView.LeftAxis.SpaceTop             = 0.35f;
                resultCell.barChartView.LeftAxis.AxisMinimum          = 0;
                resultCell.barChartView.LeftAxis.DrawGridLinesEnabled = false;
                resultCell.barChartView.LeftAxis.Granularity          = 1;
                resultCell.barChartView.LeftAxis.ValueFormatter       = new ChartDefaultAxisValueFormatter(new NSNumberFormatter()
                {
                    MinimumFractionDigits = 0
                });
                resultCell.barChartView.LeftAxis.LabelTextColor = UIColor.FromRGB(90, 89, 89);
                resultCell.barChartView.LeftAxis.GridColor      = UIColor.FromRGB(90, 89, 89);
                resultCell.barChartView.LeftAxis.AxisLineColor  = UIColor.FromRGB(90, 89, 89);
                resultCell.barChartView.LeftAxis.ZeroLineColor  = UIColor.FromRGB(90, 89, 89);

                resultCell.barChartView.RightAxis.Enabled = false;

                if (reportDataSet.totalVotes > 0)
                {
                    var groupCount   = reportDataSet.groups.Count;
                    var optionsCount = reportDataSet.labels.Count;

                    var dataEntriesList = new List <List <BarChartDataEntry> >();

                    for (int i = 0; i < optionsCount; i++)
                    {
                        var dataEntries = new List <BarChartDataEntry>();

                        for (int j = 0; j < groupCount; j++)
                        {
                            if (Convert.ToInt32(reportDataSet.dataSets[i][j]) > 0)
                            {
                                dataEntries.Add(new BarChartDataEntry(i, reportDataSet.dataSets[i][j]));
                            }
                        }

                        dataEntriesList.Add(dataEntries);
                    }

                    var chartDataSetList = new List <BarChartDataSet>();

                    var k = 0;

                    for (int i = 0; i < dataEntriesList.Count; i++)
                    {
                        var barChartDataSet = new BarChartDataSet(dataEntriesList[i].ToArray(), reportDataSet.labels[i]);

                        if (dataEntriesList[i].ToArray().Length > 0)
                        {
                            barChartDataSet.SetColor(this.chartColors[k++]);
                        }
                        else
                        {
                            barChartDataSet.SetColor(UIColor.Clear);
                        }

                        barChartDataSet.ValueFormatter = new ChartDefaultValueFormatter(new NSNumberFormatter()
                        {
                            MinimumFractionDigits = 0, ZeroSymbol = ""
                        });
                        barChartDataSet.ValueTextColor = UIColor.FromRGB(90, 89, 89);
                        barChartDataSet.BarBorderColor = UIColor.FromRGB(90, 89, 89);

                        chartDataSetList.Add(barChartDataSet);
                    }

                    var dataSets = chartDataSetList.ToArray();

                    var chartData = new BarChartData(dataSets);

                    var initialXValue = 0;

                    var groupSpace = 0.3;
                    var barSpace   = 0.05;
                    var barWidth   = (0.7 - (0.05 * optionsCount)) / optionsCount; // (barWidth + 0.05) * optionsCount + 0.3 = 1.00 -> interval per "group"

                    chartData.BarWidth = barWidth;
                    resultCell.barChartView.XAxis.AxisMinimum = initialXValue;
                    resultCell.barChartView.XAxis.AxisMaximum = initialXValue + chartData.GroupWidthWithGroupSpace(groupSpace, barSpace) * groupCount;

                    chartData.GroupBarsFromX(initialXValue, groupSpace, barSpace);

                    resultCell.barChartView.Data = chartData;
                }

                resultCell.barChartView.AnimateWithXAxisDuration(1.4, ChartEasingOption.Linear);
                resultCell.barChartView.ChartDescription.Text = "";
                resultCell.barChartView.NoDataText            = "No results to show";

                resultCell.AddSubview(resultCell.barChartView);

                resultCell.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[v0]|", new NSLayoutFormatOptions(), "v0", resultCell.barChartView));
                resultCell.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[v0(24)][v1]|", new NSLayoutFormatOptions(), "v0", resultCell.sectionLabel, "v1", resultCell.barChartView));
            }

            return(resultCell);
        }