Rouzbeh Zarandi
Rouzbeh Zarandi

Reputation: 1082

Xamarin.Android MPAndroidChart Barchart : bars are not aligned correctly on x-axis labels

i am using MPAndroidChart barchart ... every thing is okay except the x-axis... my bars are not aligned properly on labels. when i zoome the chart then bars come on its labels but that zoom is not what i wanted and also when i zooming the labels repeat until next and so on. my data is like eg:

age(x)  weight(y)
2      90
5      100
7      200
10     300

the age is my labels and it is string type ... i need it to be string label.

this is what i got enter image description here

enter image description here

this is my code in c#

//-----chart-------------
            barChart = FindViewById<BarChart>(Resource.Id.barChart);


            List<BarEntry> entries = new List<BarEntry>();
            List<string> labelz = new List<string>();


            int c = 0;
            foreach(var rec in weighting_lst)
            {

                entries.Add(new BarEntry(c, rec.Weight));
                labelz.Add(rec.Age.ToString());
                c++;


            }


            BarDataSet dataset = new BarDataSet(entries, "وزن به گرم");
            BarData data = new BarData(dataset);


            var xdata = barChart.XAxis;
            xdata.ValueFormatter = new MyLabelFormatter(labelz);
            xdata.SetCenterAxisLabels(false);

            barChart.Data = data;
            barChart.SetScaleEnabled(true);
            barChart.SetFitBars(true);

public class MyLabelFormatter : Java.Lang.Object, IAxisValueFormatter
        {
            //private string[] customLabels = new string[] { "A", "B", "C", "D", "E", "F" };

            public List<string> label_lst = new List<string>();

            public MyLabelFormatter(List<string> _label_lst)
            {
                label_lst = _label_lst;
            }

            public string GetFormattedValue(float value, AxisBase axis)
            {
                return label_lst[(int)value % label_lst.Count];
            }
        }

for x position of bar entry i just use integer called c which increases by 1 on each loop so it gives me like 0f,1f,2f,......

Upvotes: 0

Views: 316

Answers (1)

Rouzbeh Zarandi
Rouzbeh Zarandi

Reputation: 1082

i added xdata.Granularity = 1f; and it works for me .... xdata is my Xaxis

Upvotes: 1

Related Questions