MPAndroidChart how to set label color? - android

got the following code:
graph = (LineChart) convertView.findViewById(R.id.graph);
graph.getPaint(Chart.PAINT_LEGEND_LABEL).setColor(Color.BLUE);
graph.getPaint(Chart.PAINT_YLABEL).setColor(Color.BLUE);
graph.getPaint(Chart.PAINT_XLABEL).setColor(Color.BLUE);
But still the color of X/Y-value-lables and also the lable of the legend stay in black color. Do I miss something? Is there an other way to set the color of these?
This line for example works and results in blue value-lables for each data-point:
graph.getPaint(Chart.PAINT_VALUES).setColor(Color.BLUE);
Thanks in advance!

Try this to set the colors of axis and legend:
chart.getAxisLeft().setTextColor(...); // left y-axis
chart.getXAxis().setTextColor(...);
chart.getLegend().setTextColor(...);
chart.getDescription().setTextColor(...);
If you want to set the color of the drawn values inside the chart, check out the setting colors documentation or the setting data documentation.

Incase for a pie chat to change its label colors you can do this
pieChart.getLegend().setTextColor(Color.WHITE);

Related

How to set pie chart's label color?

I am using mpandroidchart library for creating a pie chart.
I wanted to format text labels on the pie chart but I don't know how to do it. I tried it using
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black));
but, it only changes the value of data, not the labels.
also, I wanted to be the label inside the pie chart I also tried that using the following code,
PieEntry entry2=new PieEntry(50-scratches,"Remaining \nScratches");
But, it didn't work.
My code is as follows:
private void setPiechart(float scratches) {
List<PieEntry> values = new ArrayList<>();
PieEntry entry1=new PieEntry(scratches,"Scratches");
PieEntry entry2=new PieEntry(50-scratches,"Remaining \nScratches");
values.add(entry1);
values.add(entry2);
PieDataSet dataSet = new PieDataSet(values,"");
dataSet.setColors(ContextCompat.getColor(getActivity(),R.color.color_veridoc_gradient1),
ContextCompat.getColor(getActivity(),R.color.colorBgChat));
dataSet.setHighlightEnabled(true);
dataSet.setAutomaticallyDisableSliceSpacing(true);
dataSet.setSliceSpace(10);
PieData data=new PieData(dataSet);
data.setValueTextSize(20f);
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black));
pieChart.setData(data);
Description description=new Description();
description.setText("Scratches ");
pieChart.setDescription(description);
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleRadius(10f);
pieChart.animateY(500);
Legend legend = pieChart.getLegend();
legend.setEnabled(true);
legend.setTextColor(Color.BLACK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
pieChart.setElevation(10);
}
}
and the result I get is:
What I want to do is in nutshell
change the label color inside the pie chart (from white to black)
Prevent the label from going outside the pie chart. (e.g. Remaining Scratches as shown in image)
Can anybody help me with it?
I also tried these solutions mentioned in followed links.
link 1
link 2
but none of them seems working.
use
pieChart.setEntryLabelColor(Color.BLACK);
To change the color of the label inside the pie chart use below code:
pieChart.setEntryLabelColor(Color.BLACK);
I assumed that pieChart is the view of your Pie chart graph view of XML i.e. pieChart = (PieChart) findViewById(R.id.chart1); If not then replace object of your view with pieChart
For value text color, you have already changed the color of it by the following code:
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black)); or you can use like data.setValueTextColor(Color.BLACK);
Now, regarding the display label text in two line:
I think it is not possible to do it because the chart is drawn on canvas and canvas doesn't support multiline. If you want multiline you have to do some trick as describe here. To do it you have to modify MPAndroidChartLibrary.
So as an alternative, you can change the text size of the label as below:
pieChart.setEntryLabelTextSize(12f); // You can increase or decrease value as per your need in argument
Also, you can apply TypeFace(Font) to that label as below:
pieChart.setEntryLabelTypeface(yourTypeFace);

MPAndroidChart limit line color not quite correct

MPAndroidCharts is so great.
I've changed my LineChart Limit line (and Text) color to an accent color used all over my app. However, the color of the line (and text) is much darker when compared to the rest. I suspect this is because the color is mixing with the chart background color have set - not sure though
What I've tried so far.
1) Set the drawable in the xml and programmatically. No change.
2) setDrawLimitLinesBehindData(false); No change.
3) changing the background color to a different shade. No apparent change.
The color you want to apply, add that in your colors.xml. After that add following line:
ll.setLineColor(getContext().getResources().getColor(R.color.your_color));

How to change particular label colour in Horizontal Bar Chart using Mp Chart in Android?

How can i change the particular label text color in xaxis using Horizontal Bar Chart. Please show the attached screenshot and code.
[Here is the screen short]
I have changed the bar colour in to black but i have to change that text colour corresponding to that bar(For example:(You#) colour need to be change).The following code i have tried so far.
XAxis xAxis1 = horizontalBarChart.getXAxis();
xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis1.setTextSize(8);
xAxis1.setSpaceBetweenLabels(8);
xAxis1.setTypeface(tf);
YAxis leftAxis = horizontalBarChart.getAxisLeft();
leftAxis.setEnabled(false);
for(int j=0;j<BarEntryLabels1.size();j++){
if ((BarEntryLabels1.get(j).contains("You"))){
xAxis1.setTextColor(Color.parseColor("#000000"));
}else{
xAxis1.setTextColor(Color.parseColor("#701112"));
}
}
Any code and help would be appreciate, Thanks in advance.
If you wand to change the chart label color then try this code
chart.getAxisLeft().setTextColor(...); // left y-axis
chart.getXAxis().setTextColor(...);
chart.getLegend().setTextColor(...);
Or you can do by this way Check this answer.
For more detail about Axis please check this.

Android MPAndroidChart PieChart not getting hole tranparent color

In my Android application I implemented an MPAndroidChart PieChart. I'm getting everything according to my requirement. But I am not able to prevent transparent color inside Pie chart. How do I solve this issue?
This is my sample code
mChart.setDrawHoleEnabled(true);
mChart.setHoleColorTransparent(true);
mChart.setHoleRadius(50f);
mChart.setTransparentCircleRadius(10);
// enable rotation of the chart by touch
mChart.setRotationAngle(0);
mChart.setRotationEnabled(false);
mChart.setDrawCenterText(true);
Just comment-out this line:
//mChart.setTransparentCircleRadius(10);
Define a new color in colors.xml
<color name="Transparent">#00000000</color>
then set the Transparent color to your pie chart hole:
pieChart.setHoleColor(getColor(R.color.Transparent));

MPAndroidChart white Background

I am using the MPAndroidChart library. I am setting the multi line chart with data using MPAndroidChart. It's working great but the background is coming white. This is what i am doing
nhChart = new LineChart(getActivity());
nhChart.setDescription("Number of Hits View");
nhChart = (LineChart) hitsView.findViewById(R.id.line_chart_number_of_hits);
//int color = Color.parseColor("#80101010");
nhChart.setBackgroundColor(Color.parseColor("#80101010"));
//nhChart.setBackgroundResource(R.drawable.background_portrate);
//nhChart.setBackground(getResources().getDrawable(R.drawable.background_portrate));
nhChart.setStartAtZero(true);
nhChart.setDrawBorder(true);
nhChart.setNoDataTextDescription("No Data available for Charts");
nhChart.setDrawYValues(false);
nhChart.setDrawBorder(true);
nhChart.setScaleEnabled(true);
nhChart.setHighlightEnabled(false);
nhChart.setTouchEnabled(true);
//nhChart.setGridColor(Color.WHITE & 0x70FFFFFF);
//nhChart.setDragScaleEnabled(true);
nhChart.setPinchZoom(true);
setData(valueDate.size(),10000);
nhChart.animateX(2500);
Legend l = nhChart.getLegend();
l.setForm(LegendForm.CIRCLE);
l.setFormSize(6f);
l.setTextColor(Color.WHITE);
YLabels y = nhChart.getYLabels();
y.setTextColor(Color.WHITE);
y.setLabelCount(6);
XLabels x1 = nhChart.getXLabels();
x1.setCenterXLabelText(true);
x1.setPosition(XLabelPosition.BOTTOM);
x1.setTextColor(Color.WHITE);
I am plotting line graphs in post execute method of AsyncTask in a fragment viewpager. Other fragment showing the graph shows the same white background. I tried setting a color for the background but nothing works. I also left it blank, but its still showing me the white background. I also updated the latest Jar but its not works. Please help.
Here is the image how it looks
You want this for transparent:
chart.setDrawGridBackground(false);
And this for transparent bar:
chart.setDrawBarShadow(false);
Well by default, the backgroud of the chart is transparent which means that it will have whatever color you set for the view/layout below the chart.
If you want to change the background (color, or maybe drawable), you can do that in the following ways:
in .xml (android:background="...")
--> set the background color of the chart in xml or set the backgroud color of the layout the chart is in.
by calling setBackgroundColor(...) or setBackgroundResource(...)
I guess you want your background to be some kind of black?
What happens if you call:
chart.setBackgroundColor(Color.BLACK)?
Does setting the background-color work then? Also check the example code in the github repo here. There are some cases in the example app that change the background color.
You can do this with:
chart.setBackgroundColor(getResources().getColor(R.color.transparent));
Please define first:
<color name="transparent">#00000000</color>

Categories

Resources