How to remove bottom space in a pie chart? - android

I am using Pie Chart in my application, Right now Pie Chart is displaying properly ,only issue is getting space at bottom of Pie Chart. Following is code of Pie Chart and output what I want and What I am trying to get.
Using this library for piechart
https://github.com/PhilJay/MPAndroidChart
PieChart pieChart = (PieChart) view.findViewById(R.id.fragment_chart);
pieChart.setUsePercentValues(true);
Display display = getActivity().getWindowManager().getDefaultDisplay();
int height = display.getHeight(); // deprecated
int offset = (int)(height * 0.65); /* percent to move */
/* RelativeLayout.LayoutParams rlParams =
(RelativeLayout.LayoutParams)pieChart.getLayoutParams();
rlParams.setMargins(0, 0, 0, -offset);
pieChart.setLayoutParams(rlParams);*/
// IMPORTANT: In a PieChart, no values (Entry) should have the same
// xIndex (even if from different DataSets), since no values can be
// drawn above each other.
ArrayList<PieEntry> yvalues = new ArrayList<PieEntry>();
yvalues.add(new PieEntry(8f, 0));
yvalues.add(new PieEntry(15f, 1));
yvalues.add(new PieEntry(12f, 2));
PieDataSet dataSet = new PieDataSet(yvalues, "Reward Points");
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("January");
xVals.add("February");
xVals.add("March");
xVals.add("April");
xVals.add("May");
xVals.add("June");
PieData data = new PieData( dataSet);
data.setValueFormatter(new PercentFormatter());
pieChart.setData(data);
//pieChart.setDescription("This is Pie Chart");
pieChart.setBackgroundColor(Color.TRANSPARENT);
pieChart.setHoleColor(Color.WHITE);
pieChart.setTransparentCircleColor(Color.WHITE);
pieChart.setTransparentCircleAlpha(0);
pieChart.setHoleRadius(18f);
pieChart.setDrawCenterText(true);
pieChart.isRotationEnabled();
pieChart.isHighlightPerTapEnabled();
pieChart.setCenterTextOffset(0f,-20f);
pieChart.setEntryLabelColor(Color.WHITE);
pieChart.setEntryLabelTypeface(Typeface.DEFAULT);
pieChart.setEntryLabelTextSize(16f);
pieChart.setTransparentCircleRadius(11f);
pieChart.setDrawHoleEnabled(true);
pieChart.setMaxAngle(180.0f);
pieChart.setRotationAngle(180.0f);
pieChart.setCenterTextSize(30);
dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
data.setValueTextSize(13f);
data.setValueTextColor(Color.DKGRAY);
//pieChart.setOnChartValueSelectedListener(getActivity());
pieChart.animateY(1400, Easing.EaseInOutQuad);
Expected Output
Getting Output

Use below code.
pieChart.setHoleRadius(64f);//18f
pieChart.setCenterTextSize(10);//30
pieChart.setCenterText("Reward Points: 150");//new line
pieChart.setExtraOffsets(5f,0f,10f,-100f);
Adjust value of bottom offset in setExtraOffsets(in above code value -100f) in order to adjust space.

To get the expected output,
You need to add a negative bottom margin
// Use LinearLayout.LayoutParams since you use a linear layout
// set buttom margin (I used -450 change this as you need)
LinearLayout.LayoutParams rlParams =
(LinearLayout.LayoutParams) pieChart.getLayoutParams();
rlParams.setMargins(0, 0, 0, -450);
pieChart.setLayoutParams(rlParams);
Disable the description
pieChart.getDescription().setEnabled(false); // remove the description
Increase hole radius
pieChart.setHoleRadius(50f); // increase hole radius
Reduce center font size and set text
pieChart.setCenterTextSize(15); // reduce font size
pieChart.setCenterText("Rewarded points: 140"); // set center text
Also change the height to 400dp in your xml file.
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/fragment_chart"
android:layout_width="match_parent"
android:layout_height="400dp" />

Related

Android MP Pie Chart how to remove text and values from Pie chart

How can i achieve this using MPAndroidChart ?
Using version : com.github.PhilJay:MPAndroidChart:v3.1.0-alpha
Adding code for Legend and piechart margin:
private void setPieChartData() {
//https://stackoverflow.com/a/50551488/1427037
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setExtraOffsets(5,5,10,5);
pieChart.setDragDecelerationFrictionCoef(0.9f);
pieChart.setDrawCenterText(false);
pieChart.setDrawHoleEnabled(false);
//pieChart.animateY(1000, Easing.EasingOption.EaseInOutCubic);
ArrayList<PieEntry> yValues = new ArrayList<>();
yValues.add(new PieEntry(34f,"Ilala"));
yValues.add(new PieEntry(56f,"Temeke"));
yValues.add(new PieEntry(66f,"Kinondoni"));
yValues.add(new PieEntry(45f,"Kigamboni"));
yValues.add(new PieEntry(45f,"Kigamboni2"));
pieChart.setDrawEntryLabels(false);
PieDataSet dataSet = new PieDataSet(yValues, "Desease Per Regions");
dataSet.setSliceSpace(0.1f);
dataSet.setDrawValues(false);
dataSet.setSelectionShift(5f);
dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
PieData pieData = new PieData((dataSet));
pieData.setValueTextSize(10f);
pieData.setValueTextColor(Color.BLACK);
pieChart.setData(pieData);
pieChart.setDrawSliceText(false);
setLegend();
}
private void setLegend() {
Legend l = pieChart.getLegend();
l.setFormSize(10f); // set the size of the legend forms/shapes
l.setForm(Legend.LegendForm.SQUARE); // set what type of form/shape should be used
l.setYOffset(5f);
//l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // set vertical alignment for legend
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setTextSize(12f);
l.setTextColor(Color.BLACK);
//l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
l.setYEntrySpace(1f); // set the space between the legend entries on the y-axis
// set custom labels and colors
List<LegendEntry> entries = new ArrayList<>();
ArrayList<PieEntry> yValues = new ArrayList<>();
yValues.add(new PieEntry(34f,"Ilala"));
yValues.add(new PieEntry(56f,"Temeke"));
yValues.add(new PieEntry(66f,"Kinondoni"));
yValues.add(new PieEntry(45f,"Kigamboni"));
yValues.add(new PieEntry(45f,"Kigamboni2"));
for (int i = 0; i < 3; i++) {
LegendEntry entry = new LegendEntry();
entry.formColor = ColorTemplate.VORDIPLOM_COLORS[i];
entry.label = yValues.get(i).getLabel() ;
entries.add(entry);
}
l.setCustom(entries);
}
[![code result][2]][2]
How to set position of legend same as actual image i am getting problem to set its layout.please correct code and share any link for help
Please share details to resolve this problem
How to resolve this UI issue .. legend label should be on right side same as Required mock
For removing entry value & labels use method :
myPieChart.setDrawSliceText(false); // To remove slice text
myPieChart.setDrawMarkers(false); // To remove markers when click
myPieChart.setDrawEntryLabels(false); // To remove labels from piece of pie
myPieChart.getDescription().setEnabled(false); // To remove description of pie
For showing legends beside your chart try below methods :
Legend l = myPieChart.getLegend(); // get legend of pie
l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER); // set vertical alignment for legend
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
l.setOrientation(Legend.LegendOrientation.VERTICAL); // set orientation for legend
l.setDrawInside(false); // set if legend should be drawn inside or not
Check out more from here & from main doc page of MPAndroidChart.
I hope it will help!
I did it,
pieChart.setDrawCenterText(true);
pieChart.setDrawEntryLabels(true);
pieChart.setDrawMarkers(false);
pieChart.getDescription().setEnabled(false);
pieChart.getLegend().setEnabled(false);
just change to false all and you shouldn't see any text(s)!
[ sorry if i had poor English ]

MPAndroidChart - How to draw bar at bottom of line chart

I want to draw stock's history price chart with volume, similar to this:
With MPAndroidChart, I managed to get this result using CombinedChart:
Here's my code snippet:
CombinedChart mChart = (CombinedChart) findViewById(R.id.chart);
final ArrayList<BarEntry> barList = new ArrayList<>();
final ArrayList<Entry> lineList = new ArrayList<>();
// add Bar & Line data entries
.....
// bar depends on LEFT y-axis
BarDataSet barSet = new BarDataSet(barList, "");
barSet.setAxisDependency(YAxis.AxisDependency.LEFT);
BarData barData = new BarData(barSet);
// line depends on RIGHT y-axis
LineDataSet lineSet = new LineDataSet(lineList, "");
lineSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
LineData lineData = new LineData(lineSet);
// set data to chart
....
float barYMax = barData.getYMax();
float lineYMin = lineData.getYMin();
// to make bar appears at bottom
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMaximum(barYMax * 10);
// to make line appears at top
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setAxisMinimum(lineYMin * 0.5f);
In order to make "Line Chart" appears above "Bar Chart":
Bar chart depends on Left y-axis. To make bar appears at bottom, I make it's scale larger, with leftAxis.setAxisMaximum(barYMax * 10).
Line chart depends on Right y-axis. To shift line up, I do rightAxis.setAxisMinimum(lineYMin * 0.5f).
I wonder is there a better recommended way to achieve this effect?
With my solution, line chart may still overlap with bar partially. Ideally I wish to achieve no overlapping at all, with line appears completely on top of bar.

MPAndroidChart bar chart issues in android

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.

MPAndroidChart - First and last bars not rendering correctly

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

MPAndroidChart vertical values

I want to delimit on vertical values between 13 and 14 not this is how it looks my linear chart, always starts on 0 as the starting limit, I want to be something around 13. Please see the image on the next link to see what I am talking about, I am attaching the piece of code for it enter image description here
LineChart lineChart = (LineChart) rootView.findViewById(R.id.chart);
LineData data = new LineData(labels, dataset);
dataset.setColors(ColorTemplate.COLORFUL_COLORS); //
dataset.setDrawCubic(true);
dataset.setDrawFilled(true);
lineChart.setData(data);
lineChart.animateY(5000);
Finally I got the answer about this, you can check it here
LineChart lineChart = (LineChart) rootView.findViewById(R.id.chart);
LineData data = new LineData(labels, dataset);
lineChart.getAxisLeft().setLabelCount(2, true);
lineChart.getAxisRight().setEnabled(false);
lineChart.getAxisLeft().setStartAtZero(false);
lineChart.setAutoScaleMinMaxEnabled(false);
lineChart.getAxisLeft().setAxisMaxValue(maxYval + 1);
lineChart.getAxisLeft().setAxisMinValue(minYval - 1);
dataset.setColors(ColorTemplate.COLORFUL_COLORS); //
dataset.setValueTextSize(10);
dataset.setDrawCubic(true);
dataset.setDrawFilled(true);
lineChart.setData(data);
lineChart.animateY(10);

Categories

Resources