MPAndroidChart show bar for 0 value - android

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);

Related

MPAndroidChart - how to reduce the amount of entries to display

I am using MPAndroidChart to display Linecharts in my Android Application. Some of my Linecharts have a hugh amount of data which I add to a LineDataSet. When I display
the data on the screen, it becomes quite confusing for the user because the dots are so close to each other. So I am looking for a possiblity to display e.g. only every
fith data dot on the chart.
LineDataSet dataSet = new LineDataSet(entries, mViewModel.getMeterUnit().getValue());
dataSet.setColor(Color.RED);
dataSet.setDrawValues(false);
dataSet.setCircleColor(Color.BLACK);
dataSet.setCircleHoleColor(Color.BLACK);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new LineBarMeasurementFragment.LineChartXAxisValueFormatter());
xAxis.setLabelCount(0);
for (LimitLine l1 : limitLines) {
xAxis.addLimitLine(l1);
}
MinMax mx = getXMinMax(measurementList) ;
xAxis.setAxisMaximum(mx.getxMax());
xAxis.setAxisMinimum(mx.getxMin());
chart.getAxisLeft().setAxisMinimum(getYAxisMin(measurementList));
chart.getAxisRight().setEnabled(false);
chart.getDescription().setText(mViewModel.getMeterIdentifier().getValue());
LineData lineData = new LineData(dataSet);
chart.setData(lineData);
// something like chart.displayOnly(5)
chart.invalidate();

MPandroid Bar chart bars not aligned

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.

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 how to set x-axis to the bottom of the chart?

I have downloaded the MPAndroidChart library to draw a LineChart, i have noticed that this LineChart always draw the xAxis on top of yAxis, me i need to draw xAxis on bottom of yAxis
this is how i initialize the chart
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setDescription("");
mChart.setNoDataTextDescription("You need to provide data for the chart.");
// enable value highlighting
mChart.setHighlightEnabled(true);
// enable touch gestures
mChart.setTouchEnabled(true);
mChart.setDragDecelerationFrictionCoef(0.9f);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
mChart.setHighlightPerDragEnabled(true);
mChart.setBackgroundColor(Color.WHITE);
XAxis xAxis = mChart.getXAxis();
xAxis.setDrawGridLines(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setAxisMaxValue(200f);
leftAxis.setDrawGridLines(false);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawAxisLine(false);
rightAxis.setTextColor(Color.WHITE);
rightAxis.setDrawGridLines(false);
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
// set the marker to the chart
mChart.setMarkerView(mv);
Try this: xAxis.setPosition(XAxisPosition.BOTTOM)
More that in the documentation.

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