Flash3001 / iOSCharts.Xamarin

A Xamarin port by Lucas Teixeira for Charts (ios-charts) by Daniel Cohen Gindi, inspired by Philipp Jahoda
Other
68 stars 20 forks source link

Last bar not shown in bar graph #38

Closed Ruchita-Bora closed 5 years ago

Ruchita-Bora commented 6 years ago

Here is the code i used to plot bar graph. But last orange bar is not visible. Tried to scroll but its not scrolling. I think gap between first blue bar and yaxis might be the issue. But not able to remove that gap. Can you help me with this? unadjustednonraw_thumb_1

       ChartXAxis xAxis = barChartView.XAxis;
        xAxis.DrawGridLinesEnabled = false;
        xAxis.LabelPosition = XAxisLabelPosition.Bottom;
        xAxis.CenterAxisLabelsEnabled = true;
        xAxis.SpaceMin = 0.0;
        xAxis.Granularity = 1;
        xAxis.AvoidFirstLastClippingEnabled = false;

        ChartYAxis rightAxis = barChartView.RightAxis;
        rightAxis.DrawGridLinesEnabled = false;
        rightAxis.DrawLabelsEnabled = false;
        rightAxis.DrawAxisLineEnabled = false;

        ChartLegend legend = barChartView.Legend;
        legend.Position = ChartLegendPosition.AboveChartCenter;

        var unitsSold1 = new double[] { 20.0, 4.0, 6.0, 3.0, 12.0 };
        var unitsSold2 = new double[] { 10.0, 8.0, 12.0, 6.0, 6.0 };

        List<BarChartDataEntry> dataEntries1 = new List<BarChartDataEntry>();
        List<BarChartDataEntry> dataEntries2 = new List<BarChartDataEntry>();

        for (int i = 0; i < unitsSold1.Length; i++)
        {
            BarChartDataEntry dataEntry1 = new BarChartDataEntry(i, unitsSold1[i]);
            dataEntries1.Add(dataEntry1);

            BarChartDataEntry dataEntry2 = new BarChartDataEntry(i, unitsSold2[i]);
            dataEntries2.Add(dataEntry2);
        }

        BarChartDataSet dataSet1 = new BarChartDataSet(dataEntries1.ToArray(), "Units Sold");
        dataSet1.Colors = new UIColor[] { ColorUtils.ColorWithHex(GlobalIOSColorConstants.DarkBlueColor) };
        dataSet1.AxisDependency = AxisDependency.Left;

        BarChartDataSet dataSet2 = new BarChartDataSet(dataEntries2.ToArray(), "Units Sold Last Year");
        dataSet2.Colors = new UIColor[] { ColorUtils.ColorWithHex(GlobalIOSColorConstants.BarOrangeColor) };
        dataSet2.AxisDependency = AxisDependency.Left;

        BarChartData barChartData = new BarChartData(new List<BarChartDataSet> { dataSet1, dataSet2 }.ToArray());
        barChartData.BarWidth = 0.25;
        barChartData.GroupBarsFromX(0, 0.42, 0.04);//group space, bar space
        barChartView.Data = barChartData;
        barChartView.HighlightPerTapEnabled = false;
        barChartView.HighlightPerDragEnabled = false;
        barChartView.DoubleTapToZoomEnabled = false;
        barChartView.PinchZoomEnabled = false;
        barChartView.SetScaleEnabled(false);
        barChartView.FitBars = true;
Flash3001 commented 6 years ago

Hey @Ruchita-Bora,

I found two different solutions to this problem.

1) Set the SpaceMax to 1 xAxis.SpaceMax = 1;

2) Disable the property to center the x-axis labels and use use the SpaceMin as the starting point in GroupBarsFromX instead of setting it to zero. xAxis.CenterAxisLabelsEnabled = false; ... barChartView.GroupBarsFromX(-xAxis.SpaceMin, 0.42, 0.04);

With any of the two all bars will fit into the screen, but I'm not aware of the side effects for each one.

Flash3001 commented 6 years ago

Btw, when picking the values for BarWidth, groupSpace and barSpace you need to keep the following math being true:

1f = (BarWidth + barSpace) * DATASET_COUNT + groupSpace