I'm trying to put a radar chart in my Android application using MPAndroidChart library.
I've seen this very similar question and tried that approach but it didn't work for me
Here's my code
RadarChart chart = findViewById(R.id.activity_family_member_detail_chart);
Drawable iconTotalEarnedPoints = getDrawable(R.drawable.ic_star_border);
Drawable iconEarnedMoney = getDrawable(R.drawable.ic_attach_money);
Drawable iconTextPosts = getDrawable(R.drawable.ic_message);
Drawable iconImagePosts = getDrawable(R.drawable.ic_photo_size_select_actual);
Drawable iconAudioPosts = getDrawable(R.drawable.ic_audiotrack);
List<RadarEntry> entries = new ArrayList<>();
entries.add(new RadarEntry(member.getTotalEarnedPoints(), iconTotalEarnedPoints));
entries.add(new RadarEntry(member.getEarnedMoney(), iconEarnedMoney));
entries.add(new RadarEntry(member.getTextPosts(), iconTextPosts));
entries.add(new RadarEntry(member.getImagePosts(), iconImagePosts));
entries.add(new RadarEntry(member.getAudioPosts(), iconAudioPosts ));
RadarDataSet radarDataSet = new RadarDataSet(entries, "");
RadarData radarData = new RadarData(radarDataSet);
chart.setData(radarData);
chart.getLegend().setEnabled(false);
chart.getDescription().setEnabled(false);
chart.getYAxis().setEnabled(false);
chart.animateXY(1000, 1000);
chart.invalidate();
What i am getting as a result is a radar that shows the correct values, but the labels are numbers (from 0 to 4) while instead of numbers i would like to have the specified drawables, how can i achieve that?
Related
I'm using MPAndroidchart pie chart and used my own colors for charts. Now the problem is the pie chart is blurred. Its edges are in blur. I want to remove the blur and make it sharp edges.How to do it?
I don't have 10 reputations to post image.
https://drive.google.com/open?id=1umms-1LZj0XuuKuDpOWZqcpRCOn6rh-X
Here is the code for chart displaying.
private void showChart(BudgetResponse.Budgets[] items) {
chart.setVisibility(View.VISIBLE);
chart.setUsePercentValues(true);
chart.getDescription().setEnabled(false);
chart.setExtraOffsets(5, 10, 5, 5);
chart.setDragDecelerationFrictionCoef(0.95f);
//chart.setCenterTextTypeface(tfLight);
//chart.setCenterText(generateCenterSpannableText());
chart.setDrawHoleEnabled(true);
chart.setHoleColor(Color.TRANSPARENT);
chart.setTransparentCircleColor(Color.WHITE);
chart.setTransparentCircleAlpha(110);
chart.setHoleRadius(58f);
chart.setTransparentCircleRadius(61f);
chart.setDrawCenterText(true);
SimpleSpanBuilder sb=new SimpleSpanBuilder();
sb.append("Total budget",new RelativeSizeSpan(1.0f));
float budget=0;
try{
for (int i=0;i<items.length;i++){
budget+=Float.parseFloat(items[i].getCurrentAmount());
}
}catch (Exception e){
e.printStackTrace();
}
sb.append("\n"+budget,new RelativeSizeSpan(2.0f),
new ForegroundColorSpan(activity.getResources().getColor(R.color.colorPrimary)));
chart.setCenterText(sb.build());
chart.setCenterTextColor(activity.getResources().getColor(R.color.titleColor));
chart.setCenterTextSizePixels(12f);
chart.setRotationAngle(0);
// enable rotation of the chart by touch
chart.setRotationEnabled(false);
chart.setHighlightPerTapEnabled(true);
chart.animateY(1400, Easing.EaseInOutQuad);
chart.getLegend().setEnabled(false);
ArrayList<PieEntry> entries = new ArrayList<>();
ArrayList<Integer> pieColors=new ArrayList<>();
for (BudgetResponse.Budgets budgets:items){
entries.add(new PieEntry(Float.parseFloat(budgets.getCurrentAmount()), budgets.getCategoryGroupName()));
if(budgets.getCategoryGroupName().contains("Bills")){
pieColors.add(getAppColor(R.color.lightViolet));
}else if(budgets.getCategoryGroupName().contains("Entertainment")){
pieColors.add(getAppColor(R.color.lightPink));
}else if(budgets.getCategoryGroupName().contains("Food")){
pieColors.add(getAppColor(R.color.lightBlue));
}else if(budgets.getCategoryGroupName().contains("Auto")){
pieColors.add(getAppColor(R.color.darkViolet));
}else if(budgets.getCategoryGroupName().contains("Shopping")){
pieColors.add(getAppColor(R.color.lightOrangeVariant));
}else if(budgets.getCategoryGroupName().contains("Transfers")){
pieColors.add(getAppColor(R.color.lightGreenVariant));
}
}
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<>(pieColors);
PieDataSet dataSet = new PieDataSet(entries, "Budget Spends");
dataSet.setDrawIcons(false);
dataSet.setSliceSpace(3f);
dataSet.setIconsOffset(new MPPointF(0, 40));
dataSet.setSelectionShift(1f);
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
//dataSet.setSelectionShift(0f);
PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter(chart));
data.setValueTextSize(11f);
data.setValueTextColor(Color.TRANSPARENT);
//data.setValueTypeface(tfLight);
chart.setData(data);
//to remove labels over pie chart
chart.setDrawEntryLabels(false);
//to remove percentage over pie chart
chart.setUsePercentValues(false);
//chart.setDrawCenterText(false);
// undo all highlights
chart.highlightValues(null);
MyMarkerView mv = new MyMarkerView(activity, R.layout.custom_marker_view);
mv.setChartView(chart); // For bounds control
chart.setMarker(mv); //
chart.invalidate();
}
I want to remove blurriness around chart.
In activity the donut chat looks great. But in viewpager it looks blur.
I am working on MPAndroid chart library where I am displaying single bar religion values using bar graph. I want to give a color to each BarEntry as per religion name like "Islam" = green, "Christianity" = Blue etc. I have searched many examples on this forum but not able to solve this.
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 20,"Islam"));
entries.add(new BarEntry(1f, 20,"Christianity"));
entries.add(new BarEntry(2f, 20,"Judaism"));
entries.add(new BarEntry(3f,20,"Sikhism"));
entries.add(new BarEntry(4f,20,"Hinduism"));
BarDataSet bSet = new BarDataSet(entries, "Marks");
bSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
ArrayList<String> barFactors = new ArrayList<>();
barFactors.add("Islam");
barFactors.add("Christianity");
barFactors.add("Judaism");
barFactors.add("Sikhism");
barFactors.add("Hinduism");
XAxis xAxis = chart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
BarData data = new BarData(bSet);
data.setBarWidth(1f); // set custom bar width
data.setValueTextSize(12);
Description description = new Description();
description.setTextColor(R.color.colorPrimary);
description.setText("Religion analysis");
chart.setDescription(description);
chart.setData(data);
chart.setFitBars(true); // make the x-axis fit exactly all bars
chart.invalidate(); // refresh
chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(barFactors));
I am using this library link:
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
I want bar colors like bottom colors in this picture. How can I solve this issue?
You need to define colors in your colors.xml file as follow:
<color name="Islam">#1abc9c</color>
<color name="Christianity">#2ecc71</color>
<color name="Judaism">#3498db</color>
<color name="Sikhism">#9b59b6</color>
<color name="Hinduism">#16a085</color>
Also provide your respective color codes in above code.
After that you need to tell bardataset about these colors as follow:
barDataSet.setColors(new int[]
{
R.color.Islam, R.color.Christianity, R.color.Judaism,
R.color.Sikhism, R.color.Hinduism
}, getContext());
This will do the job just give the order of colors in an order in which you are giving values to bar dataset.
I am using mpandroidchart and I want to draw a circle only for one Entry in the LineDataSet but not the rest. I have tried to accomplish it using two LineDataSets but it hasn't worked so far. Here's my code:
LineDataSet scoreDataSet = new LineDataSet(values, "Score");
scoreDataSet.setDrawCircles(false);
// Entry with max value is the last one
Entry circleEntry = scoreDataSet.getEntryForXIndex(scoreDataSet.getEntryCount()-1);
LineDataSet circularDataSet = new LineDataSet(values, "Score");
circularDataSet.setDrawCircles(true);
int size = circularDataSet.getEntryCount()-1;
for (int i=0; i<size; i++) {
if (i != circleEntry.getXIndex()) {
circularDataSet.removeEntry(circularDataSet.getEntryForXIndex(i));
}
}
.
.
.
ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(scoreDataSet);
dataSets.add(circularDataSet);
This just prints a dot(circularDataSet) and not the scoreDataSet. I was expecting it to merge both the DataSets so as to mark the highest value with a circle and drawing the rest as a line chart.
Your approach looks good in general, there are just a few flaws.
Don't create the "circularDataSet" and initialize it with the same values like the "scoreDataSet" and then remove all except one.
What you should do instead is find the largest entry in your "scoreDataSet", and then only add this single entry to the "circularDataSet".
I'm using MPChartlib for a basic "Barchart" (3 bars and values between 0 and 100).
the background of the app is dark so I'd like to put the text in white but when I set the text with color code "FFFFFF" in chart_color stored in string.xml but the text appear in dark blue.
//Axe X
XAxis x = barchart.getXAxis();
x.setPosition(XAxisPosition.BOTTOM);
x.setTextColor(R.color.chart_color);
x.setAxisLineColor(R.color.chart_color);
// Design
barchart.setDragEnabled(false);
barchart.setDrawGridBackground(false);
barchart.setTouchEnabled(false);
barchart.setHighlightEnabled(false);
barchart.setMaxVisibleValueCount(101);
barchart.setDescription(null);
barchart.setGridBackgroundColor(R.color.chart_color);
barchart.invalidate(); // refresh
//Axe Y
barchart.getAxisLeft().setAxisMaxValue(100);
barchart.getAxisLeft().setDrawTopYLabelEntry(true);
barchart.getAxisLeft().setDrawAxisLine(false);
barchart.getAxisLeft().setDrawGridLines(false);
barchart.getAxisLeft().setAxisLineColor(R.color.chart_color);
barchart.getAxisLeft().setTextColor(R.color.chart_color);
barchart.getAxisRight().setAxisMaxValue(100);
barchart.getAxisRight().setDrawTopYLabelEntry(true);
barchart.getAxisRight().setAxisLineColor(R.color.chart_color);
barchart.getAxisRight().setTextColor(R.color.chart_color);
I tried lots of things and research but couldn't find the issue, does the lib doesn't use the same kind of color code or something ?
Thanks for your help,
Alex
You are passing the resource id to the library, not the actual color.
Use this to get the color:
int color = ContextCompat.getColor(context, R.color.chart_color);
LineDataSet dataSet = ...;
dataSet.setColor(color);
You can also find this in the documentation.
if you want change bars color prefer pass context as well like example below
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(87f, 0));
entries.add(new BarEntry(90f, 1));
ArrayList<String> labels = new ArrayList<>();
labels.add("title 1");
labels.add("title 2);
BarDataSet dataSet = new BarDataSet(entries, "# of Calls ");
BarData barData = new BarData(labels, dataSet);
dataSet.setColors(new int[]{R.color.color1 , R.color.color2} , context);
barChart.setData(barData);
barChart.animateY(3000 , Easing.EasingOption.EaseOutBack );
I am trying to customize legend but not able to do so.My purpose is to give different legend labels.I ma using MPChart library to do so.
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(4f, 0));
entries.add(new BarEntry(8f, 1));
entries.add(new BarEntry(6f, 2));
entries.add(new BarEntry(12f, 3));
entries.add(new BarEntry(18f, 4));
mColors.add(R.color.red);
mColors.add(R.color.text_color_gray);
mColors.add(R.color.text_color_blue);
mColors.add(R.color.green);
mColors.add(R.color.black);
BarDataSet dataset = new BarDataSet(entries, null);
ArrayList<String> labels = new ArrayList<String>();
labels.add("05");
labels.add("06");
labels.add("07");
labels.add("08");
labels.add("09");
BarData data = new BarData(labels, dataset);
Legend legend = mChart.getLegend();
legend.setEnabled(true);
legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
legend.setForm(Legend.LegendForm.SQUARE);
legend.setColors(mColors);
legend.setLabels(mLabels);
mChart.setData(data);
mChart.animateY(2000);
LimitLine line = new LimitLine(10f);
YAxis yAxis = mChart.getAxisLeft();
yAxis.addLimitLine(line);
yAxis.setDrawAxisLine(true);
mChart.setDrawValueAboveBar(true);
mChart.setDrawBarShadow(false);
mChart.setVisibleXRange(4);
mChart.moveViewToX(2);
mChart.setDrawValueAboveBar(false);
mChart.invalidate();
Please let me know any solution for this.
you can custom your legend like this
LegendEntry legendEntryA = new LegendEntry();
legendEntryA.label = "a";
legendEntryA.formColor = Color.GREEN;
And add them to legend of the chart
legend.setCustom(Arrays.asList(legendEntryA, legendEntryB, legendEntryC, legendEntryD));
If you want to have 4 legend labels, you need 4 BarDataSet objects.
Having different colours will only group the different colours on the one legend that will be generated.
And you need to pass the colors to the DataSet and it will be mapped with the Legend.
Finally, your DataSets need a label which will be used for the legend. You can specify the label as second parameter in the constructor.