3D Stacked Bar Chart not showing labels

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jmhwhite2001
Posts: 3
Joined: Fri Jul 11, 2008 2:53 pm

3D Stacked Bar Chart not showing labels

Post by jmhwhite2001 » Mon Apr 12, 2010 10:22 pm

Hello everyone. This is my first time posting in the forum, but I do have the developer's guide and basically referenced the code. So, I'm creating a 3D Stacked Bar Chart and the labels are not visible for all the stacked bars. I'm submitting code and a pic. Before I forget, I'm essentially calling a servlet and using the "saveChartAsPNG" method in the Servlet Utilities.

Code: Select all

// This method returns the filename to reference on the client-side
public String getUtilizationBarChart(HttpServletRequest request) throws MaintenancePlannerException {
		try {
			DefaultCategoryDataset discoveredDataSet = new DefaultCategoryDataset();
			parseUtilizationDataSet(discoveredDataSet);		
			
	        //create the base chart with the dataset above
	        JFreeChart chart = ChartFactory.createStackedBarChart3D("Utilization Rates",
	                "Aircrafts", "Flight Hours", discoveredDataSet,
	                PlotOrientation.VERTICAL, 
	                true,
	                true,
	                false);
	        
	        //decorate the chart
	        decorateBarChart(chart);        
	        //output the chart as PNG
	        String chartName = ServletUtilities.saveChartAsPNG(chart, 800, 370, null, request.getSession());
	        String chartBase = "/displayChart?filename=";
	        return chartBase + chartName;
		} catch (Exception e) {
			new LogServiceWrapper().log(LogLevel.SEVERE,
					this.getClass().getName(), e.getMessage(), e);
			throw new MaintenancePlannerException(e);
		}
	}
Here'd the code that parses the values from the data object.

Code: Select all

private void parseUtilizationDataSet(DefaultCategoryDataset utilizationDataSet) {      
        
        Iterator<AircraftStatusBoardRow> listIter = xmlData.iterator();
        int count = 1;

        while (listIter.hasNext()) {
            AircraftStatusBoardRow listOrder = listIter.next();
                        
            String aircraftID = "Aircraft " + count;
            System.out.println(aircraftID);
            System.out.println("Daily: " + listOrder.getDailyFltHrs());
            System.out.println("Monthly: " + listOrder.getMonthlyFltHrs());
            
            utilizationDataSet.addValue(listOrder.getDailyFltHrs(), 
            		"Daily Flight Hours", 
            		aircraftID);
            utilizationDataSet.addValue(listOrder.getMonthlyFltHrs(), 
            		"Monthly Flight Hours", 
            		aircraftID);
           
            count++;
        
        }

    }
Here's the code that "decorates" the chart:

Code: Select all

protected void decorateBarChart(JFreeChart chart) {
        chart.setBackgroundPaint(new Color(Integer.parseInt("DFE8F6", 16)));
        CategoryPlot plot = (CategoryPlot) chart.getPlot();        
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER, TextAnchor.CENTER));
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER, TextAnchor.CENTER));
        
        //renderer.setItemLabelsVisible(true);
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
                CategoryLabelPositions.createUpRotationLabelPositions(
                        Math.PI / 6.0));
      
    }
Finally, here's the System.out to get an idea of the data I'm using:

Aircraft 1
Daily: 3.1
Monthly: 16.4
Aircraft 2
Daily: 0.0
Monthly: 7.8
Aircraft 3
Daily: 0.6
Monthly: 21.3
Aircraft 4
Daily: 2.4
Monthly: 15.8
Aircraft 5
Daily: 1.3
Monthly: 34.1
Aircraft 6
Daily: 3.1
Monthly: 16.4
Aircraft 7
Daily: 1.3
Monthly: 34.1
Aircraft 8
Daily: 0.3
Monthly: 4.5
Aircraft 9
Daily: 0.0
Monthly: 7.8
Aircraft 10
Daily: 1.3
Monthly: 34.1
Aircraft 11
Daily: 4.0
Monthly: 16.5
Aircraft 12
Daily: 8.0
Monthly: 27.2
Aircraft 13
Daily: 7.1
Monthly: 28.4
Aircraft 14
Daily: 0.0
Monthly: 0.0
Aircraft 15
Daily: 0.6
Monthly: 21.3
Aircraft 16
Daily: 3.1
Monthly: 16.4
Aircraft 17
Daily: 4.6
Monthly: 16.4
Aircraft 18
Daily: 1.4
Monthly: 11.5
Aircraft 19
Daily: 0.0
Monthly: 0.0
Aircraft 20
Daily: 0.0
Monthly: 7.8
Aircraft 21
Daily: 1.3
Monthly: 34.1
Aircraft 22
Daily: 3.1
Monthly: 16.4

Image

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: 3D Stacked Bar Chart not showing labels

Post by remiohead » Mon Apr 12, 2010 10:43 pm

You also need to set a fallback position which applies if the label does not fit the available space:

Code: Select all

renderer.setPositiveItemLabelPositionFallback(ItemLabelPosition position);
renderer.setNegativeItemLabelPositionFallback(ItemLabelPosition position);

jmhwhite2001
Posts: 3
Joined: Fri Jul 11, 2008 2:53 pm

Re: 3D Stacked Bar Chart not showing labels

Post by jmhwhite2001 » Tue Apr 13, 2010 1:35 pm

Thanks, that did it!

Locked