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.
Related
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();
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);
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.
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;
}
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);`