MPAndroidchart two Axes overlapping - android

I have a chart with two datasets displayed on both yAxes. However these datasets overlap each other. Is there a way to display them like a grouped chart side by side over one label
If i use chart.groupBars(0f, 0.8f, 0.1f) the chart gets cut off because the bar width does not scale correctly.

OK After a bit more digging around and reading the docs more carefully i found the answer.
The sum of your barWidth, barSpace and groupSpace have to be zero, eg in my case with two bars per group:
val barWidth = 0.3
val groupSpace = 0.3
val barSpace = 0.05
val offsetForFirstX = -0.5 // for three bars 0.33 etc
chartView.groupBars(-0.5, groupSpace, barSpace)

For any problem on this charts lib
Download & run
https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample
Make changes to the sample chart you wish to implement with view as required.
Sounds long cut, but we get to understand the working of library with help of documentation and don't get stuck in issues

Related

MPAndroidChart centre view on middle of chart

I am using the MPAndroidChart library to graph live sensor data in a line graph. I'm hoping to give the data a sort of 'buffer' on the right-hand end so that new data is in the centre of the screen. I've tried to illustrate what I mean below:
Basically, I have got it so the ViewPort scrolls and shows the most recent item in the right-most end of the screen, but I would like it in the centre. Thanks!
you need to set Xaxis maximum, set visibleXrange and last move the viewport
double range = 5; // how many data you want to show in view port
double maxX = 100; // your highest X value
chartView.getXAxis().setAxisMaximum(maxX + range/2);
chartView.setVisibleXRange(range, range);
chartView.moveViewToX(maxX);
chartView.notifyDataSetChanged();

Certain values are not visible in MPAndroidChart?

I am using MPAndroidChart to display my charts, in the barchart it displays some values perfectly but for some other values, the bar is visible and can be highlighted but its value is not visible.
Any clue please on how to fix this ?
This is a picture to clarify what I am saying. The red areas is where the bar is visible but not its value.
Before setting the data (sorry I couldn't find the data in your question):
graph.setVisibleYRangeMaximum(barDataSet.getYMax() + 20, YAxis.AxisDependency.LEFT);
replacing 20 with the height of your custom layout.
I've had the same problem and this is how I've solved it.
float maxValue = barSet.getYMax();
int extraSpace = (int) (maxValue / (yVals.size() + 3));
chart.getAxisLeft().mAxisMaximum = (int) maxValue + (extraSpace < 1 ? 1 : extraSpace);
yVals is the list of Y axis values, while you can play with +3 so the whole chart view seems OK according to your layout.

How to create space between x-axis labels in MPAndroidChart?

I am using MPAndroidChart library.
I'm trying to make spaces between labels in the XAxis of my line chart.
I have tried this but nothing changes:
xAxis.setSpaceBetweenLabels(someInt);
A bit late, but you can increase the zoom level using zoom(scaleX, scaleY, x, y) method. Increase the scaleX value and you will start seeing spaces between labels on the x axis. A good estimation is dividing the number of labels by 10. So if you have 500 labels, use zoom(50f, 0.5f, 1f, 1f).
xAxis.labelCount = 3
//replace the integer with whatever you need
You can use xAxis.setLabelCount(some int);
or
chart.setVisibleXRangeMaximum(some float);
which sets the size of the area (range on the x-axis) that should be minimum visible at once.
You can read it also in the documentation
https://github.com/PhilJay/MPAndroidChart/wiki/The-Axis
and for the View Port https://github.com/PhilJay/MPAndroidChart/wiki/Modifying-the-Viewport

MPAndroidChart Scatter points are cropped in Combined chart

I use combined chart which combines line (RED) and scatter charts. However for some reason scatter chart points are cropped at the edge of chart here is example:
Is there any way to prevent it?
here is my chart code:
chart.getAxisLeft().setAxisMaxValue(1.1f);
chart.getLegend().setEnabled(false);
chart.setTouchEnabled(false);
chart.setDragEnabled(false);
chart.setScaleEnabled(false);
chart.setPinchZoom(false);
chart.setDoubleTapToZoomEnabled(false);
chart.setHighlightPerDragEnabled(false);
chart.setHighlightPerTapEnabled(false);
chart.getAxisRight().setEnabled(false);
chart.getAxisLeft().setEnabled(false);
chart.getXAxis().setEnabled(false);
chart.setDescription("");
chart.setNoDataText("");
and my scatter data set:
foodDataSet = new ScatterDataSet(foodEntries, "Food");
foodDataSet.setColor(ContextCompat.getColor(getContext(), R.color.green_main));
foodDataSet.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
foodDataSet.setScatterShapeSize(10f);
foodDataSet.setDrawValues(false);
foodDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
Update
here is chart with axis
After some tries I was able to find hack to solve issue. However it would be good if proper solution could be advised. Current solution
chart.setData(data);
chart.getXAxis().setAxisMinValue(-0.1f);
chart.setVisibleXRangeMinimum(xAxisLabels.size() - 0.81f);
chart.invalidate();
Min value should be increased if bigger data points are used, but it will require update min visible x range too. Reduce subtraction value to increase offset.
Disadvantage of such solution is it doesn't scale well
If anyone else is having this problem, here's what I did to make mine scale with the number of entries. This assumes that the number of entries is the same for each data set which may or may not be the case for your specific data.
float xAxisSideOffset = (float)(xMax - xMin) / (data.getEntryCount()/data.getDataSetCount());
combinedChart.getXAxis().setAxisMinimum(xMin - timeInterval - xAxisSideOffset/2);
combinedChart.getXAxis().setAxisMaximum(xMax - timeInterval + xAxisSideOffset/2);

How to modify the bar width in MPAndroidChart BarChart?

I am using MPAndroidChart library.
I would like modify the size of bar present in bar chart. Could you let us know where to check for modify size of bar in bar chart in mp android chart
You can reduce the spacing between the bars that will automatically increase the width of bars.
BarDataSet set = new BarDataSet(vals, "Set A");
set.setBarSpacePercent(50f);
UPDATE v3.0.0:
As of this library version, you can control the bar width with the following method:
BarData barData = new BarData(...);
barData.setBarWidth(1f);
The number you provide for this method represents a certain interval on the x-axis. If your total axis has a range of e.g. 10k, and you set the bar width to 1000, the bar will cover 10% of the total x-axis range.
As for the latest version of the library BarDataSet does not have this function any longer, its in BarData class.
BarData data = new BarData(dataset);
data.setBarWidth(0.5f);
Kotlin:
barChartView.data = BarData(dataSetList).apply {
barWidth = 0.5f
}
To fit data in graph (No scroll behavior)
barChartView.xAxis.axisMaximum = dataList.size
barChartView.setVisibleXRange(1f,xAxisLabels.size.toFloat())
This applies to MPAndroidChart API. To reduce the Bar width you have to increase space between bars (as mentioned Rakesh). The value 50F should be good for mobile display but you can increase/decrease it to suit your device screen.
Sample code as requested by one user :
BarDataSet barDataSet = new BarDataSet(group1, "X");
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
barDataSet.setBarSpacePercent(50f);
The API imports are-
import com.github.mikephil.charting.data.BarDataSet;
data.barWidth = 0.5f
it means barWidth/(barWidth + blankWith) = 0.5f

Categories

Resources