Page 1 of 1

Switching chart types loses legend

Posted: Wed Jan 02, 2013 9:41 pm
by 16959570
Within an Android app I am developing, I have a spinner where you can select different views of different data. Currently most of the graph types are Pie and this works ok. If I also switch from Pie to Line, that works. But if I switch from that Line chart back to a Pie chart, the chart displays ok, but the legend just displays 'Series 0'.

Here is how it looks:
teechart_issue.png
teechart_issue.png (19.87 KiB) Viewed 10167 times
This is the code which sets up the chart basics:

Code: Select all

//set up the TeeChart basics
LinearLayout group = (LinearLayout) view.findViewById(R.id.linearLayoutTchart);
chart = new TChart(PainRecordSummary_ViewPager.this);
group.addView(chart);
chart.getPanel().setTransparent(true);
               
//default 3D to true (will need to reset per chart)
chart.getAspect().setView3D(true);
This is the code which deals with switching the chart types:

Code: Select all

switch (graphType) {
case 0: // Location PIE CHART
if(PLS.getLocation() != null){
 Pie pieLocation = new Pie(chart.getChart());
		       
chart.removeLegendResolver();
chart.removeAllSeries();
chart.getAspect().setView3D(true);
try {
series = Series.createNewSeries(chart.getChart(), Pie.class,  null);
                } catch (InstantiationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
		    
for(int i=0; i<PLS.getLocation().size(); i++){ 
    series.add(PLS.getLocation().get(i).getItemPercent(), PLS.getLocation().get(i).getLocation());
} 
pieLocation.add(series);
ColorPalettes.applyPalette(chart.getChart(), Theme.ModernPalette);
chart.getLegend().setAlignment(LegendAlignment.BOTTOM);
chart.getHeader().setVisible(false);
chart.getPanel().setTransparent(true);
chart.getWalls().getBack().setVisible(true);
((com.steema.teechart.styles.Pie)chart.getSeries(0)).setCircled(true);
((com.steema.teechart.styles.Pie)chart.getSeries(0)).getMarks().setVisible(false);
((com.steema.teechart.styles.Pie)chart.getSeries(0)).setExplodeBiggest(12);
((com.steema.teechart.styles.Pie)chart.getSeries(0)).getPen().setVisible(false);
}else{
//show no data 
}
break;
case 5: //this will display a TIME SERIES of Pain Locations AVG. SEVERITY 
chart.removeAllSeries();
chart.getAspect().setView3D(false);
//X AXIS
//set the x-axis date display to e.g. '01-JAN'
chart.getAxes().getBottom().getLabels().setDateTimeFormat("dd-MMM");
//set the angle of the date display
chart.getAxes().getBottom().getLabels().setAngle(45);
//Y AXIS
//set up the y axis title
AxisTitle yTitle;
yTitle=chart.getAxes().getLeft().getTitle();
yTitle.setCaption("Severity");
//end title
//set y to increment by 1
//override the auto min/max and set to 0/10
chart.getAxes().getLeft().setIncrement(1.00);

for(int i=0; i<PLR.size(); i++){
    Series ln = new Line(chart.getChart());
    ln.setTitle(PLR.get(i).getName());
    SeriesPointer tmpPointer;
    tmpPointer = ((CustomPoint)ln).getPointer();
    tmpPointer.setInflateMargins(true);
    tmpPointer.setStyle(PointerStyle.CIRCLE);
    tmpPointer.setVisible(true);
    for(int j=0; j<PLR.get(i).getItems().size(); j++){
        DateTime dt = new DateTime(
	DateDiff.getYearFromString(PLR.get(i).getItems().get(j).getDateRecord()),
	DateDiff.getMonthFromString(PLR.get(i).getItems().get(j).getDateRecord()),
	DateDiff.getDayFromString(PLR.get(i).getItems().get(j).getDateRecord()));
		            
	ln.add(dt, (double)PLR.get(i).getItems().get(j).getSeverity().intValue());
	 }
    ((com.steema.teechart.styles.Line)chart.getSeries(i)).getLinePen().setWidth(2);
}
Anyone have any ideas why that Legend isn't repopulating?

Many thanks

Re: Switching chart types loses legend

Posted: Thu Jan 03, 2013 10:08 am
by yeray
Hello Dave,

Try forcing the LegendStyle to LegendStyle.SERIES when showing your Line series:

Code: Select all

tChart1.getLegend().setLegendStyle(LegendStyle.SERIES);
And to LegendStyle.VALUES when showing your Pie series:

Code: Select all

tChart1.getLegend().setLegendStyle(LegendStyle.VALUES);

Re: Switching chart types loses legend

Posted: Thu Jan 03, 2013 8:47 pm
by 16959570
Fantastic! That worked perfectly.

Thanks Yeray.

Re: Switching chart types loses legend

Posted: Fri Jan 04, 2013 8:05 am
by yeray
Hi Dave,

Glad to hear it! :D