How to remove padding from horizontal barchart of mpandroidchart? - android

I am trying to make a horizontal barchart that covers the entire parent layout but its not. Below is my code -
HorizontalBarChart barchart = new HorizontalBarChart(activity);
barchart.setLayoutParams(new LinearLayout.LayoutParams(0, 110, weight));
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
entries.add(new BarEntry(86.0f, 0));
BarDataSet dataset = new BarDataSet(entries, "");
dataset.setColor(Color.parseColor("#E0E0E0"));
ArrayList<String> labels = new ArrayList<String>();
labels.add("86.0");
BarData bardata = new BarData(labels, dataset);
barchart.setData(bardata);
barchart.setDescription("");
Legend legend = barchart.getLegend();
legend.setEnabled(false);
YAxis topAxis = barchart.getAxisLeft();
topAxis.setDrawLabels(false);
YAxis bottomAxis = barchart.getAxisRight();
bottomAxis.setDrawLabels(false);
XAxis rightAxis = barchart.getXAxis();
rightAxis.setDrawLabels(false);
bottomAxis.setDrawLabels(false);
barchart.setPadding(-1, -1, -1, -1);
barchart.setBackgroundColor(Color.CYAN);
return barchart;
I want my horizontal barchart (barchart) to fill the entire blue area. Can someone please help.
EDIT : #PhilippJahoda i tried your solution, but at first launch it shows up the same way it was, when i click/touch the chart then only it covers the entire area. Can you please tell me why i have to touch the chart to make it fill the entire space.
At first launch it looks like -
Screen shot:
After clicking it looks like -
Screen shot2:

Update to the latest version of the library if you have not already.
Then, just remove all offsets from the chart. It's in the documentation.
Call:
chart.setViewPortOffsets(0f, 0f, 0f, 0f);

The padding removal works with
chart.setViewPortOffsets(0f, 0f, 0f, 0f);
but if the effect appears only after touch, try to invalidate chart view (after you set data) this way:
post(new Runnable() {
#Override
public void run() {
chartView.invalidate();
}
});

YAxis yl = chart.getAxisLeft();
yl.setSpaceTop(20f);

Best way to control the padding what I feel is as:
Set all the offsets to 0
chart.setViewPortOffsets(0f, 0f, 0f, 0f);
Control the padding from the XML
com.github.mikephil.charting.charts.LineChart
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"

When you first set your ViePortOffsets and after that moveViewTo then it works.
Graph.setViewPortOffsets(0F,0F,0F,0F)
Graph.moveViewTo(0f,0f,YAxis.AxisDependency.RIGHT)
Graph.invalidate()

Related

Custom view of Limit Line in MPAndroidChart Barchart

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

How to remove bottom space in a pie chart?

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" />

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 - 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 - How to change color of value?

How to change color of value's which I painted red ? I used setValueTextColor but it didn't work. It is still black
LineDataSet d1 = new LineDataSet(e1, entityList.get(i).getName());
d1.setValueTextColor(Color.rgb(255, 255, 83));
sets.add(d1);
LineData cd = new LineData(dateList, sets);
cd.setValueTextColor(Color.rgb(223, 83, 83));
d1.setValueTextColor(Color.rgb(255, 255, 83));
is meant for the highlighting values within the graph.
The things that are underlined are YAxis left and right, XAxis, and Legend
For each of them you need to set the text color to get the desired effect.
Now to get the reference of them you need the chart object:
YAxis yAxisRight = mChart.getAxisRight();
YAxis yAxisLeft = mChart.getAxisLeft();
XAxis xAxis = mChart.getXAxis();
Legend l = mChart.getLegend();
and set the color for each of them by calling setTextColor(int color) method.
You can find this in the official documentation as well.

Categories

Resources