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;
}
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();
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.
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());
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.
Is their any possibility to hide all rounded items from this picture.
I have used the following code,
public void setDataList(List<HorizontalBarChartData> dataList, Resources resources) {
ArrayList<String> categories = new ArrayList<String>();
ArrayList<BarEntry> values = new ArrayList<BarEntry>();
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
BarDataSet set1;
for (int i = 0; i < dataList.size(); i++) {
categories.add(dataList.get(i).getName());
values.add(new BarEntry(dataList.get(i).getValue(), i));
}
/*set1 = new BarDataSet(values, "Income, Expense, Disposable Income");*/
set1 = new BarDataSet(values, "Category 1, Category 2, Category 3");
set1.setBarSpacePercent(35f);
set1.setColors(new int[]{resources.getColor(R.color.cyan_blue), resources.getColor(R.color.vermilion_tint), resources.getColor(R.color.sea_green)});
dataSets.add(set1);
BarData data = new BarData(categories, dataSets);
data.setValueTextSize(10f);
horizontalBarChart.setData(data);
}
Update
How to hide rounded part from this image?
Yes, is possible, just using following code:
mChart.setDescription(""); // Hide the description
mChart.getAxisLeft().setDrawLabels(false);
mChart.getAxisRight().setDrawLabels(false);
mChart.getXAxis().setDrawLabels(false);
mChart.getLegend().setEnabled(false); // Hide the legend
As per this answer
mChart.getXAxis().setDrawLabels(false); will hide the entire X-Axis(as required for this question).
For positioning the X-Axis, following code works.
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
Position can be set to
BOTTOM
BOTH_SIDED
BOTTOM_INSIDE
TOP
TOP_INSIDE
This helps if you are trying to hide only the particular side axis instead of hiding the entire axis.
It appears mChart.setDescription() no longer accepts a String.
The method now accepts an instance of the Description Class like this:
mChart.setDescription(Description description)
So to modify or delete the Chart Description you may do it like below
Description description = new Description();
description.setText("");
mChart.setDescription(description);
Following code work for all chart
Legend l = mchart.getLegend();
l.setEnabled(false);.
To hide description, use this
mChart.getDescription().setEnabled(false)
Below code works for PieChart. Try to get same method for your Chart.
Legend l = mChart.getLegend();
l.setPosition(LegendPosition.NONE);
chart=(LineChart) findViewById(R.id.Chart);
chart.getLegend().setEnabled(false); // for hiding square on below graph
Kotlin solution for MPAndroidChart:v3.1.0
chart.description.isEnabled = false // hide the description
chart.legend.isEnabled = false // hide the legend
chart.xAxis.setDrawLabels(false) // hide bottom label
chart.axisLeft.setDrawLabels(false) // hide left label
chart.axisRight.setDrawLabels(false) // hide right label
I had the problem, that the bottom xAxis Labels are cutted off when I used mChart.getLegend().setEnabled(false)
Now I use chart.getLegend().setForm(Legend.LegendForm.NONE); instead and the labels are not cutted of anymore