Reputation: 45
I've been trying to display multiple charts in one activity. Currently, the first chart displays but the second does not (or maybe just can't be scrolled to). If I remove android:fillViewport="true" from the ScrollView then nothing displays. I modified the demo aChartEngine code to return a View instead of an intent. I don't know if the error is in XML, my custom Chart class, or my Activity. I really appreciate any help. Thanks!
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/linearParent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/chartOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/chartTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>
</ScrollView>
Activity:
//setup Views
myWindow = (LinearLayout)findViewById(R.id.paidLayoutLinearParent);
row1 = (LinearLayout) findViewById(R.id.paidLayoutChartOne);
row2 = (LinearLayout) findViewById(R.id.paidLayoutChartTwo);
//Pie Charts
pie = new PieChartView(this,dens);
pieTotalCases = pie.getPieChart("Cases", gameStatsDb.getNumCasesPassed(), gameStatsDb.getNumCasesFailed());
pieTotalCombos = pie.getPieChart("Combos", gameStatsDb.getNumCombosPassed(), gameStatsDb.getNumCombosFailed());
//add charts to layout
row1.addView(pieTotalCases,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
row2.addView(pieTotalCombos,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Custom PieChartView
public class PieChartView{
/** Colors to be used for the pie slices. */
private static int[] COLORS = new int[] { Color.GREEN, Color.RED,Color.BLACK,Color.LTGRAY };
private CategorySeries mSeries;
private DefaultRenderer mRenderer;
private Context myContext;
float screenDens;
public PieChartView(Context ctx,float screenDensity){
myContext=ctx;
screenDens=screenDensity;
}
public GraphicalView getPieChart(String title,int numPassed,int numFailed){
mSeries = new CategorySeries("");
mRenderer = new DefaultRenderer();
stylizeChart();
GraphicalView mChartView = ChartFactory.getPieChartView(myContext, mSeries, mRenderer);
mRenderer.setChartTitle(title);
if(numPassed==0 & numFailed==0){
mSeries.add("None ",1);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[3]);
mRenderer.addSeriesRenderer(renderer);
mRenderer.setDisplayValues(false);
mRenderer.setStartAngle(90);
}else{
mSeries.add("Passed ",numPassed);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[0]);
mRenderer.addSeriesRenderer(renderer);
mSeries.add("Failed ", numFailed);
SimpleSeriesRenderer renderer2 = new SimpleSeriesRenderer();
renderer2.setColor(COLORS[1]);
mRenderer.addSeriesRenderer(renderer2);
}
return mChartView;
}
private void stylizeChart(){
//stylize chart
mRenderer.setZoomButtonsVisible(false);
mRenderer.setStartAngle(180);
mRenderer.setDisplayValues(true);
mRenderer.setClickEnabled(false);
mRenderer.setChartTitleTextSize(22*screenDens);
mRenderer.setLabelsTextSize(17*screenDens);
mRenderer.setLabelsColor(COLORS[2]);
mRenderer.setShowLegend(false);
mRenderer.setPanEnabled(false);
mRenderer.setZoomEnabled(false);
mRenderer.setZoomRate(6.0f);
mRenderer.setInScroll(true);
}
}
Upvotes: 2
Views: 576
Reputation: 3255
TRy this xml (give some height to graph containers)
<LinearLayout
android:id="@+id/linearParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/graphHolder"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#a11111"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/graphHolder2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#ea1111"
android:orientation="vertical" />
</LinearLayout>
Upvotes: 3