Show image instead of text in MPAndroidChart pie chart - android

I have a pie chart in my android application that i draw with MPAndroidChart.
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/chart"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
/>
PieDataSet pieDataSet = new PieDataSet(entries, "expenses");
pieDataSet.setColors(ColorTemplate.PASTEL_COLORS);
pieDataSet.setDrawValues(false);
PieData data = new PieData(xValues, pieDataSet);
chart.setData(data);
chart.getLegend().setEnabled(false);
chart.setDescription("");
chart.animateY(800, Easing.EasingOption.EaseInBounce);
chart.setDrawHoleEnabled(false);
chart.setTouchEnabled(true);
chart.setDrawCenterText(false);
chart.setDrawSliceText(true);
chart.setOnChartValueSelectedListener(this);
chart.invalidate(); // refresh
The pie chart is drawn and the x values are show as text.
I would like to replace the text in each pie slice to show a small image, depending on the x value.
I checked the docs and didn't find anything, is it possible to do this with this charting library ?

No, that is not possible by default. However, it should not be too hard customize the library in a way to support that feature.
Just add a Bitmap to each entry in the PieChart and draw it on the location where the labels would usually be.

Related

How to set background color according to x-axis partition values in chart with MPAndroidChart library by PhilJay?

I would like to set background color according to x-axis partition values in chart using together with line chart. Is there any way to set like this picture in android with MPAndroid Chart?
Line Chart with MPAndroid Chart
when you set bar data then use this one.
val barDataSet = BarDataSet(mListOfBarEntry, "Colors")
barDataSet.color = resources.getColor(R.color.colorPrimary)
In this i have changed the background color of bar.

Pie chart values overlap and get clipped MPAndroidChart

I am using the MPAndroidChart library to draw a pie chart in my app but if a value gets a little bit bigger than others all the other values and their labels get overlapped. I am also having some problems with labels in which some text gets clipped if it is a bit longer. I have seen apps(examples are given below) that adjust the size of the pie chart dynamically if they are provided with the same data scenario as I am having.
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/main_dashboard_pie_chart"
android:layout_width="match_parent"
android:layout_height="400dp">
My Java code
pieDataSet.setColors(colors);
pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
pieDataSet.setValueTextColor(Color.BLACK);
pieDataSet.setSliceSpace(2);
pieDataSet.setValueLinePart1OffsetPercentage(10); //starting of the line from center of the chart offset
pieDataSet.setValueLinePart1Length(0.6f);
pieDataSet.setValueLinePart2Length(0.5f);
PieData pieData = new PieData(pieDataSet);
pieData.setValueTextSize(13);
pieData.setValueFormatter(new PercentFormatter());
pieChart.setUsePercentValues(true);
pieChart.setData(pieData);
pieChart.setTransparentCircleRadius(45);
pieChart.setHoleRadius(40);
pieChart.setUsePercentValues(true);
pieChart.setCenterText("Expenses");
pieChart.setEntryLabelTextSize(10);
pieChart.animateY(1000, Easing.EasingOption.EaseInOutCubic);
pieChart.getDescription().setEnabled(false);
pieChart.setEntryLabelColor(Color.BLACK);
pieChart.setEntryLabelTextSize(13);
pieChart.setExtraBottomOffset(50f);
pieChart.setExtraTopOffset(50f);
pieChart.setExtraLeftOffset(50f);
pieChart.setExtraRightOffset(50f);
Legend l = pieChart.getLegend();
l.setDrawInside(false);
l.setEnabled(false);
Use This Library to solve the problem https://github.com/huzipiaopiao/MPAndroidChartFix
in XML Define your chart as shown below.
<com.teaanddogdog.mpandroidchartutil.PieChartFixCover
android:id="#+id/pie_chart"
android:layout_width="450dp"
android:layout_height="450dp"
app:mp_chart_out_value_place_mode="AVG"
app:mp_chart_auto_adapt_text_size="true"
/>
And you can access it in java code just as you access MPANdroidChart and can modify all the properties. Don't forget to upvote the question and answer if it helps you. All of the overlapping problems will be handled for you automatically. Thank you.

How to set the rectangular box outside the chart using mpchart library in android?

I am trying to plot a graph using MPchart library in android.
But I am not able to show a rectangular box outside the chart with background color as green as shown in the picture:.
I tried using these methods of MPchart library:
setDrawGridBackground(true);
setGridBackgroundColor(Color.BLUE);
Instead of setting the color outside chart it is setting the background color of the chart as shown in the picture below: .
chart.setDrawGridBackground(true);
chart.setGridBackgroundColor(Color.BLUE);
Please provide any suggestions so that I can get my graph output as expected in picture 1
In xml file add background attribute to barchart element.
<com.github.mikephil.charting.charts.BarChart
android:id="#+id/bar_chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00AAA7"/>
use below method of mpchart library:
setDrawGridBackground(true);
output looks like in the image link
Barchart
I used mpchart library 'com.github.PhilJay:MPAndroidChart:v2.2.4'
//for refrence I used static values
// X co-ordinate
ArrayList sales=new ArrayList();
sales.add(new BarEntry(900f,0));
sales.add(new BarEntry(1150f,1));
sales.add(new BarEntry(780f,2));
sales.add(new BarEntry(1050f,3));
sales.add(new BarEntry(1180f,4));
sales.add(new BarEntry(1200f,5));
sales.add(new BarEntry(1160f,6));
sales.add(new BarEntry(1220f,7));
sales.add(new BarEntry(1100f,8));
// Y co-ordinate
ArrayList<String> month=new ArrayList<>();
month.add("June");
month.add("July");
month.add("Aug.");
month.add("Sept.");
month.add("Oct.");
month.add("Nov.");
month.add("Dec.");
BarDataSet barDataSet=new BarDataSet(sales,"Total Sales");
//animate
barChart.animateY(2000);
//set colors
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData=new BarData(month,barDataSet);
barChart.setData(barData);
//xml in layout
<com.github.mikephil.charting.charts.BarChart
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bar_chart">
</com.github.mikephil.charting.charts.BarChart>
Hope this answer will help you.
Output :

How to draw a slice of piechart with an image file instead of filling with color in MPAndroidChart?

currently, I'm using MPAndroidChart library (v3.0.3) for developing an Android App that could visualize the user' steps in a Piechart. In our design, we want to draw a slice of the chart with an image instead of filling it with a specific color. I searched many posts but didn't find useful information.
Our UI design is as this image of the piechart. We want to draw the slice with an image of "dots patterns." as an indicator for encouraging the user to achieve the daily goal. Is it possible to do that with MPAndroidChart? If it's yes, could you give me some hints or references to try on? Thank you very much.
It can be done. All you have to do is create two entries for pieChart. Give 1 entry the percentage you want to show and the other entry remaining percentage from hundred.Then set color of first entry to your required color and set color of seconentry as transparent. Also apply center text whatever you want to write in center.
Follow code below it will do the job:
PieChart pieChart = (PieChart) findViewById(R.id.chart);
ArrayList<PieEntry> pieEntries = new ArrayList<PieEntry>();
pieEntries.add(new PieEntry(66, "Today"));
pieEntries.add(new PieEntry(44, ""));
PieDataSet pieDataSet = new PieDataSet(pieEntries, "");
pieDataSet.setColors(Color.BLUE,Color.TRANSPARENT);
pieDataSet.setHighlightEnabled(true);
pieDataSet.setValueTextSize(12);
pieDataSet.setValueTextColor(Color.TRANSPARENT);
PieData pieData = new PieData(pieDataSet);
pieChart.getDescription().setText("");
pieChart.setEntryLabelColor(Color.TRANSPARENT);
pieChart.getDescription().setTextSize(12);
pieChart.setDrawCenterText(true);
pieChart.setCenterText("1600 Steps");
pieChart.setCenterTextSize(22);
pieChart.setCenterTextColor(Color.BLUE);
pieChart.setEntryLabelTextSize(12);
pieChart.animateY(1000);
pieChart.setData(pieData);

MPandroidchart Double YAxis with different scales

I want that my chart has two YAxis with different scales like the image . How I can change the scale for each axis?
Version 2.0.0-beta was released over the weekend with a CombinedChart, which allows plotting Line-, Bar-, Scatter- and CandleData in one chart. So the feature is finally available.
One note about what I found during my upgrade, you can't simply addDataSet() for each one, you have to generate the individual DataSet types (Bar, Line, Scatter, etc) and then use SetData() for the appropriate type, to trigger rendering.
Here is the example code.
The author of MPAndroidChart library clearly specifies here, (MPChart - Scatter Chart and LineChart in same plot), that this function is not yet available. So, I am also looking for a nicer approach.
As such, you can display two different chart types as one, still using this library, only by overlapping them.
I have done this on my side as it follows:
1. Edit the activity layout xml, having a FrameLayout as charts container.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
.......
<FrameLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/root_view">
</FrameLayout>
.......
</LinearLayout>
Now, in the Activity class, build the chart objects and attach them to the previous container:
// construct the charts, as different objects
// make sure to have valid Data Sets
LineChartItem lineChart = new LineChartItem(lineChartDataset, getApplicationContext());
BarChartItem barChart = new BarChartItem(barChartDataset, getApplicationContext());
// get the charts' container
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.root_view);
// populate the container with the charts' views
frameLayout.addView(lineChart.getView(0, null, getApplicationContext()));
frameLayout.addView(barChart.getView(1, null, getApplicationContext()));
PROs/CONs for such a practice:
PROs
You have more combined charts with MPAndroidChart.
CONs
You need to take care of X and Y Labels alignment.
User Events: only the upper (in Z order) Chart View will get the user input, so you need to make sure that all the other chart views behave in the same manner (either processing OR ignoring the input, like zooming the chart or value selection).
I hope for a quick release of this great lib having this option. Thank you in advance Philipp Jahoda!
In my case, I'd every thing as suggested by the mpandroidcharts
Still I was getting same scale on "y axis right" and "y axis left"
the culprit was
combinedChart.axisLeft?.axisMinimum = //some calculations
combinedChart.axisLeft?.axisMaximum = //some calculations
combinedChart.axisRight?.axisMinimum = //some calculations
combinedChart.axisRight?.axisMaximum = //some calculations
Basically a copy paste error when you copy your perfect code from bar or line chart where setting the axis minimum and maximum where required.

Categories

Resources