Open suvansh007 opened 4 years ago
i used already a PieChart
use this code :
package com.example.chakib.myapplication;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.WindowManager;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.PercentFormatter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private PieChart mChart;
private Typeface tf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mChart = (PieChart) findViewById(R.id.chart1);
//mChart.setUsePercentValues(true);
mChart.setDescription("");
mChart.setExtraOffsets(5, 10, 5, 5);
mChart.setDragDecelerationFrictionCoef(0.95f);
mChart.setCenterText(generateCenterSpannableText());
mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.WHITE);
mChart.setTransparentCircleColor(Color.WHITE);
mChart.setTransparentCircleAlpha(110);
mChart.setHoleRadius(58f);
mChart.setTransparentCircleRadius(61f);
mChart.setDrawCenterText(true);
mChart.setRotationAngle(0);
// enable rotation of the chart by touch
mChart.setRotationEnabled(true);
mChart.setHighlightPerTapEnabled(true);
setData(3, 100);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
Legend l = mChart.getLegend();
l.setPosition(LegendPosition.RIGHT_OF_CHART_CENTER);
l.setTextSize(60);
l.setFormSize(20f);
l.setYEntrySpace(60f);
l.setYOffset(0f);
}
private void setData(int count, float range) {
float mult = range;
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
// 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.
for (int i = 0; i < count ; i++) {
yVals1.add(new Entry((float) 33.3, i));
}
ArrayList<String> xVals = new ArrayList<String>();
String [] mParties = {"En-dessous", "Dans la cible", "Au-dessus"};
for (int i = 0; i < count ; i++)
xVals.add(mParties[i]);
PieDataSet dataSet = new PieDataSet(yVals1, "");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<Integer>();
colors.add(Color.RED);
colors.add(Color.GREEN);
int orange = Color.rgb(255, 165, 0);
colors.add(orange);
dataSet.setColors(colors);
//dataSet.setSelectionShift(0f);
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
//data.setValueTypeface(tf);
List<Integer> textColor = new ArrayList<>();
textColor.add(Color.BLUE);
textColor.add(Color.BLUE);
textColor.add(Color.BLUE);
data.setValueTextColors(textColor);
mChart.setData(data);
// undo all highlights
mChart.highlightValues(null);
mChart.invalidate();
}
private SpannableString generateCenterSpannableText() {
SpannableString s = new SpannableString("Durée dans la cible\n70-180 mg/dL");
s.setSpan(new RelativeSizeSpan(1.7f), 0, 19, 0);
return s;
}
}
I am trying to make a piechart but it is not getting displayed on the layout unless i click on the layout while all other charts are working fine. here's the code:
`private fun populateDonutChart(responseDonutChart: List){
// if(color_index<7) // pieDataSet.setColor(getColor(piechart.context, getColorID(color_index))) // else // pieDataSet.setColor(getColorID(color_index)) pieDataSet.setColor(getColor(piechart.context, getColorID(color_index))) data.addDataSet(pieDataSet) index++ color_index++ } addDatatoPieChart(data) }
// piechart.setDescription(description); piechart.setDrawHoleEnabled(true) piechart.setHoleRadius(58f) // dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS); // data.setValueTextSize(13f); // data.setValueTextColor(Color.DKGRAY); // piechart.isLayoutRequested piechart.setTouchEnabled(false) piechart.setd piechart.requestLayout() // piechart.post { // piechart.invalidate() // }
I have already by using piechart.requestLayout() , invalidate(), invalidate in post{....} as you can see in the above code.
The dependency in android is: implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
Please help!