MPandroid Bar chart bars not aligned - android

The bars in my barchart are not aligned with the labels - see : https://imgur.com/gallery/QVtIvXq
My X Axis:
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setLabelCount(values.length + 1, true);
xAxis.setDrawLabels(true);
xAxis.setCenterAxisLabels(true);
xAxis.setValueFormatter(new MyXAxisValueFormatter(values));
xAxis.setGranularity(10f);
xAxis.setGranularityEnabled(true);
xAxis.setDrawGridLines(true);
xAxis.setDrawAxisLine(false);
//xAxis.setAxisMinimum(0);
xAxis.setAxisMaximum(values.length+1);
// custom X-axis labels
String[] values = new String[]{"Excited", "Happy", "Confident", "Proud", "Content", "Fine",
"Relaxed", "Calm", "Tired", "Guilty", "Sad", "Depressed", "Embarrassed", "Upset", "Stressed",
"Anxious", "Confused", "Disgusted"};
Any help would be greatly appreciated

What worked for me was to remove the line xAxis.setAxisMaximum(values.length+1); and instead insert a blank data set. This ensures all the labels are displayed without enforcing a maximum which was causing the bars not to align properly for some reason. Hope this helps someone in the future.

Related

MPAndroidChart show bar for 0 value

I want to show vertical bars for 0 values as well. I have tried setting chart.getAxisLeft().setAxisMinimum(-1);. The axis gets shifted but there is no bars shown for zero value.
BarData barData = new BarData(dataSet);
barData.setValueFormatter(formatter);
barData.setDrawValues(true);
barData.setBarWidth(0.5f);
chart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);
chart.setDrawBorders(false);
chart.animateY(1000);
chart.getAxisLeft().setGranularityEnabled(true);
chart.getAxisLeft().setAxisMaximum(5);
chart.getAxisLeft().setGranularity(1);
chart.getAxisRight().setEnabled(false);
chart.getAxisLeft().setAxisMinimum(-1);
chart.setDescription(null);
chart.getLegend().setEnabled(false);
chart.setDrawValueAboveBar(false);
chart.setRenderer(new RoundedBarChart(chart, chart.getAnimator(), chart.getViewPortHandler()));
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisLineWidth(1);
xAxis.setDrawGridLines(false);

MPAndroid LineChart - HOW to align XAxis labels with gridlines, display XAxis values

I am new to using the MPAndroid LineChart. I have a simple code and have produced a graph as shown below:
However, on this, I want to do the following:
1) To match the XAxis labels with the vertical grid lines, so that the grid lines also pass through the blue dots; AND
2) To show the XAxis value on the blue dots. By default, the YAxis values can be shown - I know how to do this; currently I have disabled this, and it is not shown in the picture below, but if I were to show enable them, then, they will be 0.0, 2.0, 4.0, 6.0 and 8.0 on the 5 blue dots. What I want is to show the XAxis values instead.
Could you please suggest a way? Many thanks.
I'm not sure. but its work for me.
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.RED);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
For dynamic XAxis label,
xAxis.setValueFormatter(new IndexAxisValueFormatter(getAreaCount));
public ArrayList<String> getAreaCount() {
ArrayList<String> label = new ArrayList<>();
for (int i = 0; i < yourList.size(); i++)
label.add(yourList.get(i).getTopicName());
return label;
}

mpAndroidChart : How to show the value on the last point of the LineChart?

The value on the last point of the LineChart doesn't show even
in the mpAndroidChart example.
always value of the last point can't be seen I tried adding padding , layout_margin and setExtraOffsets ..
Screenshot
I'm using v3.0.0, and this is the LineChart Code:
lineChart.setDragEnabled(true);
lineChart.getDescription().setEnabled(false);
lineChart.animateY(2000);
lineChart.setDrawBorders(false);
lineChart.setVisibleXRange(3,7);
YAxis leftAxis=lineChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setEnabled(false);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);
IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(lineChart);
XAxis xAxis = lineChart.getXAxis();
xAxis.setTextColor(Color.WHITE);
xAxis.setTextSize(13);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(false);
xAxis.setValueFormatter(xAxisFormatter);
I finally got it to work by a workaround.
Add an extra Entry for each dataSet after the last Entry and then set the setAxisMaximum for the xAxis to the last value, like this
dataset1.addEntry(new Entry(endDay+1,yValues1[i]));
dataSet2.addEntry(new Entry(endDay+1,yValues2[i]));
dataSet3.addEntry(new Entry(endDay+1,yValues3[i]));
xAxis.setAxisMaximum(endDay+0.1f);`

MPAndroidChart - First and last bars not rendering correctly

In the bar chart I am creating the first and last rows are consistently being cut in half (even if I add additional bars). This also causes the values above the bars to be out of place. I am inflating this within a fragment.
The axis are also only increasing by 0.9 instead of 1. To fix this do I need to implement the AxisValueFormatter interface?
Image:
Code:
.java
chart = (BarChart) view.findViewById(R.id.chart1);
// Chart settings
chart.setDrawGridBackground(false);
chart.setHighlightFullBarEnabled(true);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.setDescription("");
// Settings for X-Axis
XAxis xAxis = chart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setEnabled(true);
xAxis.setDrawLabels(true);
xAxis.setPosition(XAxisPosition.BOTTOM);
// Settings for Y-Axis
YAxis leftAxis = chart.getAxisLeft();
YAxis rightAxis = chart.getAxisRight();
leftAxis.setAxisMinValue(0f);
rightAxis.setAxisMinValue(0f);
BARENTRY = new ArrayList<>();
BarEntryLabels = new ArrayList<String>();
BARENTRY.add(new BarEntry(2f, 1));
BARENTRY.add(new BarEntry(3f, 2));
BARENTRY.add(new BarEntry(4f, 3));
BARENTRY.add(new BarEntry(5f, 4));
BARENTRY.add(new BarEntry(6f, 5));
Bardataset = new BarDataSet(BARENTRY, "Projects");
Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
BARDATA = new BarData(Bardataset);
chart.setData(BARDATA);
chart.invalidate(); // refresh
chart.animateY(1500);
return view;
To answer your questions:
In order to properly show your bars you need to fit the bars, like this (don't ask me why it's not enabled by default):
chart.setFitBars(true)
You can find more information in the BarChart javadocs:
setFitBars(boolean enabled): Adds half of the bar width to each side of the x-axis range in order to allow the bars of the barchart to be fully displayed.
If you have a CombinedChart you could use setSpaceMin() and setSpaceMax() to add additional spacing on both ends of the axis:
XAxis xAxis = chart.getXAxis();
xAxis.setSpaceMin(barData.getBarWidth() / 2f);
xAxis.setSpaceMax(barData.getBarWidth() / 2f);
It is currently impossible to infuence the value positions rendered on the axis. Creating a ValueFormatter only changes the displayed text, not the actual position of the label.
Please use setAxisMinimum and setAxisMaximum, it will work perfectly!
After your BarData is instantiated, you just need
chart.getXAxis().setAxisMinimum(-data.getBarWidth() / 2);
chart.getXAxis().setAxisMaximum(count-data.getBarWidth() / 2);`
There is no need for setFitBars, and the count is BARENTRY size.
If the problem is with the first bar only, you can use negative values for the axis minimum:
xAxis.setAxisMinimum(-0.5f);
Like in the answer to this question here:
i just fixed it,settings the x range min, using
barChart.setFitBars(true);
barChart.setVisibleXRangeMinimum(barEntries.size());

MPAndroidChart - BarChart on xaxis geting postion instead of values

I was trying MPAndroidChart for a basic bar chart. I am facing issue with Xaxis.
Here is my code to set Xaxis :
protected List weeks =new ArrayList() ;
weeks.add("jan");
weeks.add("feb");
weeks.add("mar");
weeks.add("apr");
weeks.add("may");
weeks.add("jun");
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
xAxis.setTextColor(Color.WHITE);
xAxis.setValues(weeks);
But in xaxis I am geting postion i.e. 0 , 1, 2, 3, 4, 5 instead of months name.
Thanks
You should provide the x-axis labels for the data object you are using and not set them directly to the axis.
E.g.
LineData data = new LineData(xValues, dataSets);
For more details, read the documentation.

Categories

Resources