Search code examples
androidchartspie-chart

In Android how to display percentage in pie chart with achartengine


I am using achartengine for displaying the pie chart and I have tried this :--

public class AChartEnginePieChartActivity extends Activity {   

private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE,Color.MAGENTA, Color.CYAN };  

private static double[] VALUES = new double[] { 10, 11, 12, 13 };  

private static String[] NAME_LIST = new String[] { "A", "B", "C", "D" };  

private CategorySeries mSeries = new CategorySeries("");  

private DefaultRenderer mRenderer = new DefaultRenderer();  

private GraphicalView mChartView;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.main);  

mRenderer.setApplyBackgroundColor(true);  
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));  
mRenderer.setChartTitleTextSize(20);  
mRenderer.setLabelsTextSize(15);  
mRenderer.setLegendTextSize(15);  
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });  
mRenderer.setZoomButtonsVisible(true);  
mRenderer.setStartAngle(90);  

for (int i = 0; i < VALUES.length; i++) {  
mSeries.add(NAME_LIST[i] + " " + VALUES[i], VALUES[i]);  
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();  
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);  
mRenderer.addSeriesRenderer(renderer);  
}  

if (mChartView != null) {  
mChartView.repaint();  
}  

}  


@SuppressLint("ShowToast")
@Override  
protected void onResume() {  
super.onResume();  
if (mChartView == null) {  
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);  
mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);  
mRenderer.setClickEnabled(true);  
mRenderer.setSelectableBuffer(10);  

mChartView.setOnClickListener(new View.OnClickListener() {  
@Override  
public void onClick(View v) {  
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();  

if (seriesSelection == null) {  
Toast.makeText(AChartEnginePieChartActivity.this,"No chart element was clicked",Toast.LENGTH_SHORT).show();  
} else {  
Toast.makeText(AChartEnginePieChartActivity.this,"Chart element data point index "+ (seriesSelection.getPointIndex()+1) + " was clicked" + " point value="+ seriesSelection.getValue(), Toast.LENGTH_SHORT).show();  
}  
}  
});  

mChartView.setOnLongClickListener(new View.OnLongClickListener() {  
@SuppressLint("ShowToast")
@Override  
public boolean onLongClick(View v) {  
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();  
if (seriesSelection == null) {  
Toast.makeText(AChartEnginePieChartActivity.this,"No chart element was long pressed", Toast.LENGTH_SHORT);  
return false;   
} else {  
Toast.makeText(AChartEnginePieChartActivity.this,"Chart element data point index "+ seriesSelection.getPointIndex()+ " was long pressed",Toast.LENGTH_SHORT);  
return true;         
}  
}  
});  
layout.addView(mChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));  
}  
else {  
mChartView.repaint();  
}  
}  
}

Here its displaying the pie chart Like this picture..But I want the value as percentage.. like A(10%) B(11%) in the picture.. How can I do that????? please suggest me


Solution

  • Try below code hope it will help to resolve your problem:-

    mSeries.add(NAME_LIST[i] + "(" + VALUES[i]+"%)", VALUES[i]);