One Legend Missing question

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.

One Legend Missing question

Post by lucky7456969 » Tue Sep 17, 2013 1:10 pm

Just wonder why there is a legend missing here

Image

Code: Select all

public abstract class GenericChart extends JDialog
{
   
    public GenericChart(String str, String xaxisname, String yaxisname)
    {   
        this.setPreferredSize(new Dimension(500,400));
        name = str;
        this.xaxisname = xaxisname;
        this.yaxisname = yaxisname;
        
    }
    
    
    protected abstract DefaultPieDataset createPieDataset(ResultSet _rs);
    protected abstract CategoryDataset createBarDataset(ResultSet _rs);
    
     
    public JPanel createPanel(ResultSet _rs, int type)
    {
        
         
        // add the chart to a panel...
         
        if (type == 0)
        {
            CategoryDataset dataset = createBarDataset(_rs);
            //chart = create3DPieChart(dataset);
            chart = create2DBarChart(dataset);
            chtChart = new ChartPanel(chart);
            chtChart.setPreferredSize(new java.awt.Dimension(500,400));
             Translator t = new Translator(chtChart);
            t.start();
        
        }
        else
        {
            DefaultPieDataset dataset = createPieDataset(_rs);
            chart =  create2DPieChart(dataset);
            
            chtChart = new ChartPanel(chart);
            chtChart.setOpaque(false);
            chtChart.setPreferredSize(new java.awt.Dimension(500, 400));
        }
        
        
        return chtChart;
          
     
    }
    
    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("宋体", java.awt.Font.PLAIN, 12));
        final CategoryPlot plot = achart.getCategoryPlot();
        
        plot.getDomainAxis().setLabelFont(new Font("宋体", 0, 12));
        plot.getDomainAxis().setTickLabelFont(new Font("宋体", 0, 12));

       plot.getRangeAxis().setLabelFont(new Font("宋体", 0, 12));

       LegendTitle title = (LegendTitle) achart.getSubtitle(0);
       title.setItemFont(new Font("宋体", 0, 12));
        
        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)
        );
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return achart;
        
        
       
        // OPTIONAL CUSTOMISATION COMPLETED.
       //Tween.to(chart.getCategoryPlot(), tweenType, duration)

         
         
    }
  
    protected JFreeChart chart;
     
    protected String name;
    protected String xaxisname;
    protected String yaxisname;
    
    protected ChartPanel chtChart;
    
  
}

Code: Select all

@Override
    protected CategoryDataset createBarDataset(ResultSet _rs) {
          final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

          List<CustomerArea2> lCustomerArea = new ArrayList<>();
          int totalArea = 0;
          
         try {
            while (_rs.next())
            {
                CustomerArea2 area = new CustomerArea2();
                String areaName = _rs.getString(1);
                int noOfPeople = _rs.getInt(2);
                
                area.setAreaName(areaName);
                area.setNoOfPeople(noOfPeople);
                
                lCustomerArea.add(area);
                
                //totalArea += noOfPeople;
                
                        
               
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(AddressDistributionChart.class.getName()).log(Level.SEVERE, null, ex);
        }
         
         for (CustomerArea2 anArea : lCustomerArea)
          {                
              String name = anArea.getAreaName();
                dataset.setValue(anArea.getNoOfPeople(),name, name);
           }
           
          return dataset;
    }

Thanks

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

Re: One Legend Missing question

Post by paradoxoff » Thu Sep 19, 2013 7:58 am

Looks like a series key "" to me.

Locked