I have download MPAndroidChart Example in order to make some changes so I decide to change
ListViewMultiChartActivity and in LineChartItem I add upper/lower limit and run applications and it is working but when I scroll down, in linechart is added other upper/lower limits that I do not create and it is strange. I added some image and a code
I am using Android Studio and Android 8.0 API 26 and mpandroidchart 3.1.0-alpha
That only happen when I scroll down faster but If I scroll slower it is not happen. The problem that I see is that upper/lower limit are belong to other line graph and in one graph is overlaping.
https://user-images.githubusercontent.com/34872326/50542025-3ede8280-0b80-11e9-9463-1153a26caa0d.jpg
https://user-images.githubusercontent.com/34872326/50542022-3e45ec00-0b80-11e9-9c75-d9f15634bac4.jpg
holder.chart.getDescription().setEnabled(false);
holder.chart.setDrawGridBackground(false);
XAxis xAxis = holder.chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTypeface(mTf);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(true);
YAxis leftAxis = holder.chart.getAxisLeft();
leftAxis.setTypeface(mTf);
leftAxis.setLabelCount(5, false);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis rightAxis = holder.chart.getAxisRight();
rightAxis.setTypeface(mTf);
rightAxis.setLabelCount(5, false);
rightAxis.setDrawGridLines(false);
rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
LimitLine upper_limit = new LimitLine(mChartData.getYMax(), "Max Value");
upper_limit.setLineWidth(2f);
upper_limit.enableDashedLine(10f, 10f, 0f);
upper_limit.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
upper_limit.setTextSize(10f);
LimitLine lower_limit = new LimitLine(mChartData.getYMin(), "Min Value");
lower_limit.setLineWidth(2f);
lower_limit.enableDashedLine(10f, 10f, 0f);
lower_limit.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
lower_limit.setTextSize(10f);
leftAxis.addLimitLine(upper_limit);
leftAxis.addLimitLine(lower_limit);
leftAxis.setDrawLimitLinesBehindData(true);
// set data
holder.chart.setData((LineData) mChartData);
// do not forget to refresh the chart
// holder.chart.invalidate();
holder.chart.animateX(750);
return convertView;
I found the anwser. I need to put leftAxis.removeAllLimitLines(); before to add a limit because that line code prevent to have limit overlaping –
Related
Is it possible to replace LimitLine with custom layout? So it looks something like this:enter image description here
LimitLine ll1 = new LimitLine(150f, "Upper Limit");
ll1.setLineWidth(4f);
ll1.enableDashedLine(10f, 10f, 0f);
ll1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
ll1.setTextSize(10f);
ll1.setTypeface(tf);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
leftAxis.addLimitLine(ll1);
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 using MPAndroidChart.
How to change the BarEntryLabels in to left in HorizontalBarChart?vHow to remove that projects from its bottom?
How to disable zoom ?
While clicking on to particular bar colour is changing how to disable it?
IN BarChart there i have added 6 months but EntryLabels is not properly aligned to corresponding bar.
Code:
BARENTRY = new ArrayList<>();
BarEntryLabels = new ArrayList<String>();
AddValuesToBARENTRY();
AddValuesToBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY, "Projects");
BARDATA = new BarData(BarEntryLabels, Bardataset);
Bardataset.setColors(new int[]{Color.parseColor("#701112")});
horizontalBarChart.setData(BARDATA);
horizontalBarChart.setDrawBarShadow(false);
horizontalBarChart.setDrawValueAboveBar(true);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
horizontalBarChart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
horizontalBarChart.setPinchZoom(false);
// draw shadows for each bar that show the maximum value
// mChart.setDrawBarShadow(true);
horizontalBarChart.setDrawGridBackground(false);
horizontalBarChart.setDescription("");
Bardataset.setBarSpacePercent(10f);
barChart.setData(BARDATA);
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
barChart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
barChart.setPinchZoom(false);
// draw shadows for each bar that show the maximum value
// mChart.setDrawBarShadow(true);
barChart.setDrawGridBackground(false);
barChart.setDescription("");
For the "project" legend part try this
Legend legend = mBarChart.getLegend();
legend.setEnabled(false);
To make touch disable set setTouchEnabled(boolean enabled) on your bar chart object like this
mBarChart.setTouchEnabled(false);
and if you want to show all label you should try
axis.setLabelCount(...)
mBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
and also try official documents
BARENTRY1 = new ArrayList<>();
BarEntryLabels1 = new ArrayList<String>();
AddValuesToHorizontalBARENTRY();
AddValuesToHorizontalBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY1, "Projects");
BARDATA = new BarData(BarEntryLabels1, Bardataset);
Bardataset.setColors(new int[]{Color.parseColor("#701112")});
horizontalBarChart.setData(BARDATA);
horizontalBarChart.setDrawBarShadow(false);
horizontalBarChart.setDrawValueAboveBar(true);
horizontalBarChart.setPinchZoom(false);
horizontalBarChart.setDrawGridBackground(false);
horizontalBarChart.setDescription("");
Bardataset.setBarSpacePercent(10f);
Legend legend = horizontalBarChart.getLegend();
legend.setEnabled(false);
horizontalBarChart.setTouchEnabled(false);
XAxis xAxis1 = horizontalBarChart.getXAxis();
xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis1.setTextSize(8);
xAxis1.setSpaceBetweenLabels(8);
xAxis1.setTextColor(Color.parseColor("#701112"));
xAxis1.setTypeface(tf);
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.setEnabled(false);
Do like this.The following issue will be fix.
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 am using mpandroidchart library to draw Bar chart for my ANDROID app. I deleted all background lines except horizontal lines. How to delete them?
Try with this:
XAxis xAxis = mChart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.disableGridDashedLine();
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setDrawAxisLine(false); //If you do not want Axis Line.
leftAxis.setDrawGridLines(false); //If you do not want Grid Line.
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawAxisLine(false); //If you do not want Axis Line.
rightAxis.setDrawGridLines(false); //If you do not want Grid Line.
Hope this answer will help you.
To remove horizontal lines, use
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
and
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawGridLines(false);
To remove vertical lines, use
XAxis xAxis = mChart.getXAxis();
xAxix.setDrawGridLines(false);