Why isn't there any category labels?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lucky7456969
Posts: 15
Joined: Thu Nov 01, 2012 6:41 am
antibot: No, of course not.

Why isn't there any category labels?

Post by lucky7456969 » Sat Sep 07, 2013 8:37 am

Image

Code: Select all

   protected JFreeChart create2DBarChart(final CategoryDataset dataset) 
    {
        JFreeChart achart = ChartFactory.createBarChart(name, xaxisname, yaxisname, dataset, PlotOrientation.VERTICAL, true, true, false);
        achart.getTitle().setFont(new Font("新細明體", Font.BOLD, 22));
        
        
       achart.getLegend().setItemFont(new java.awt.Font("Sans-serif", java.awt.Font.PLAIN, 12));
        final CategoryPlot plot = achart.getCategoryPlot();
        
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        // ##### todo 
        //renderer.setMove(100 , true);
       
         //renderer.setDrawBarOutline(false);

        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
        );
         
        return achart;
         
         
    }

Code: Select all

    @Override
    protected CategoryDataset createBarDataset(ResultSet _rs) {
         final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
         List<AgeDistribution> lageDist = new ArrayList<>();
        try {
            while (_rs.next())   
            {
                AgeDistribution ageDist = new AgeDistribution();
                ageDist.setProductID(1);
                ageDist.setProductName("Test");
               
                ageDist.setAgeGroup(_rs.getString(2));
                 
                 ageDist.setNoOfPeople(_rs.getInt(4));
                  
                
               
               lageDist.add(ageDist);
               AgeDistributionFactory.addToCollection(ageDist);
               
            }
        } catch (SQLException ex) {
            Logger.getLogger(AgeDistributionChart.class.getName()).log(Level.SEVERE, null, ex);
        }
         
           
            
             
           
             for (AgeDistribution ad : lageDist)
             {
              
              
                 dataset.addValue(ad.getNoOfPeople(), ad.getAgeGroup(), "Series");
             }
         
          
         return dataset;
    }
I am using JFreeChart 1.0.5
Thanks
Jack

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Why isn't there any category labels?

Post by paradoxoff » Sun Sep 08, 2013 9:41 pm

AFAICS, you haven´t defined a CategoryItemLabelGenerator for the renderer.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Why isn't there any category labels?

Post by david.gilbert » Mon Sep 09, 2013 8:51 pm

You have constructed the dataset with multiple series, where each series has a single category that you've called "series". You should study the DefaultCategoryDataset API a bit more and understand the structure of this dataset.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

lucky7456969
Posts: 15
Joined: Thu Nov 01, 2012 6:41 am
antibot: No, of course not.

Re: Why isn't there any category labels?

Post by lucky7456969 » Tue Sep 10, 2013 10:42 am

Okay, everybody, I'll do that.
Thanks

Locked