Custom view of Limit Line in MPAndroidChart Barchart - android

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

Related

Draw extra upper/lower limit in ListViewMultiChartActivity

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 –

MPAndroidChart Pie is drawing incorrect chart when one value is very big

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

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.

MPAndroidChart how to set x-axis to the bottom of the chart?

I have downloaded the MPAndroidChart library to draw a LineChart, i have noticed that this LineChart always draw the xAxis on top of yAxis, me i need to draw xAxis on bottom of yAxis
this is how i initialize the chart
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setDescription("");
mChart.setNoDataTextDescription("You need to provide data for the chart.");
// enable value highlighting
mChart.setHighlightEnabled(true);
// enable touch gestures
mChart.setTouchEnabled(true);
mChart.setDragDecelerationFrictionCoef(0.9f);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
mChart.setHighlightPerDragEnabled(true);
mChart.setBackgroundColor(Color.WHITE);
XAxis xAxis = mChart.getXAxis();
xAxis.setDrawGridLines(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setAxisMaxValue(200f);
leftAxis.setDrawGridLines(false);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawAxisLine(false);
rightAxis.setTextColor(Color.WHITE);
rightAxis.setDrawGridLines(false);
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
// set the marker to the chart
mChart.setMarkerView(mv);
Try this: xAxis.setPosition(XAxisPosition.BOTTOM)
More that in the documentation.

How to remove padding from horizontal barchart of mpandroidchart?

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

Categories

Resources