I am trying to display aChartengine pie chart inside a listview using a custom adapter , it is executing without any error but not displaying chart ..please help.
I have a method openChart()..which is creating chart and returning view which I am setting in the get view through holder.
code
public View getView(int position, View convertView, ViewGroup arg2) {
.....
convertView = mInflater.inflate(R.layout.chart,null);
holder.vw=(ViewGroup) convertView.findViewById(id.chart_container);
//Log.w("In View", );
holder.vw.addView(openChart());
convertView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
return convertView;
}
....
private View openChart(){
// Pie Chart Section Names
String[] code = new String[] {
"Eclair & Older", "Froyo", "Gingerbread", "Honeycomb",
"IceCream Sandwich", "Jelly Bean"
};
// Pie Chart Section Value
double[] distribution = { 3.9, 12.9, 55.8, 1.9, 23.7, 1.8 } ;
// Color of each Pie Chart Sections
int[] colors = { Color.BLUE, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.RED,
Color.YELLOW };
// Instantiating CategorySeries to plot Pie Chart
CategorySeries distributionSeries = new CategorySeries("PIe");
for(int i=0 ;i < distribution.length;i++){
// Adding a slice with its values and name to the Pie Chart
distributionSeries.add(code[i], distribution[i]);
}
// Instantiating a renderer for the Pie Chart
DefaultRenderer defaultRenderer = new DefaultRenderer();
for(int i = 0 ;i<distribution.length;i++){
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
seriesRenderer.setColor(colors[i]);
seriesRenderer.setDisplayChartValues(true);
// Adding a renderer for a slice
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
defaultRenderer.setChartTitle("Android version distribution as on October 1, 2012 ");
defaultRenderer.setChartTitleTextSize(20);
defaultRenderer.setZoomButtonsVisible(true);
defaultRenderer.setBackgroundColor(Color.GRAY);
defaultRenderer.setDisplayValues(true);
// Getting a reference to LinearLayout of the MainActivity Layout
//LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
// Creating a Line Chart
mChart = ChartFactory.getPieChartView(mContext, distributionSeries , defaultRenderer);
// Adding the Line Chart to the LinearLayout
//chartContainer.addView(mChart);
// Creating an intent to plot bar chart using dataset and multipleRenderer
// Intent intent = ChartFactory.getPieChartIntent(getBaseContext(), distributionSeries , defaultRenderer, "AChartEnginePieChartDemo");
// Start Activity
// startActivity(intent);
Log.w("In Chart", mChart.toString() +"");
return mChart;
}
I went through the same issue a while ago. In my case I had to give the graph view a fixed height.
You can also give fixed height to one of the graph view's parents and then use match_parent
or wrap_content
on the graph view.
See my xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="88dp">
<LinearLayout
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="@dimen/list_item_margin"
android:layout_marginRight="@dimen/list_item_margin"
android:layout_alignBottom="@id/image"
android:layout_alignTop="@id/image"
android:layout_toRightOf="@id/image"/>
<!-- other views... -->
</RelativeLayout>
This is probably done because ListView wants to minimize the height of its items. If this is true, your graph was there, but its height was zero.