如何克服JFreeChart问题?

时间:2012-12-05 06:35:32

标签: java charts struts report jfreechart

我在struts应用程序中使用Jfree图表。 Y轴是过度装运件,X轴是供应商代码,次轴用于过度装运%。我在这里遇到的问题是,如果Over Shipment%值全部归零,它也会隐藏X轴供应商价值。我怎样克服以避免这种情况。我附上了屏幕截图供您考虑。感谢所有人阅读这个问题。

enter image description here

我在此粘贴了我的代码,以便您清楚地理解,

/**
     * 
     * @param response
     * @throws IOException
     */
    public void writeOccurrenceBasedParetoChart(HttpServletResponse response) throws IOException
    {
        String METHOD_NAME = "writeOccurrenceBasedParetoChart";
        log.entering(CLASS_NAME, METHOD_NAME);

         CategoryDataset dataset1 = createDataSetForPercentBasedChart1();
         CategoryDataset dataset2 = createDataSetForPercentBasedChart2();
         String rangeAxisLabel = "";
         String numOfSupplrs = "";
         //Behind Schedule "4"
         if(searchRatingElement.equalsIgnoreCase("4"))
         {           
             System.out.println("*******************searchRatingElement"+searchRatingElement);
             rangeAxisLabel = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpcs");
         }
         else 
         {
             rangeAxisLabel = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpcs");
         }

         JFreeChart chart = ChartFactory.createBarChart(
                "",       // chart title
                "",               // domain axis label
                rangeAxisLabel,                  // range axis label
                dataset1,                  // data
                PlotOrientation.VERTICAL, // orientation
                true,                     // include legend
                true,                     // tooltips?
                false                     // URLs?
            );

            // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

            // set the background color for the chart...
            chart.setBackgroundPaint(Color.white);
            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.lightGray);


            plot.setDataset(1, dataset2);
            plot.mapDatasetToRangeAxis(1, 1);

            CategoryItemRenderer renderer1 = plot.getRenderer();
            renderer1.setSeriesItemLabelGenerator(0,new CategoryItemLabelGenerator() {

                public String generateRowLabel(final CategoryDataset arg0, final int arg1) {
                    // TODO Auto-generated method stub
                    return null;
                }

                public String generateLabel(final CategoryDataset dataset1,final int series,final int category) {
                    String result = null;
                    //CHAPTER 12. ITEM LABELS 91
                    final Number value = dataset1.getValue(series, category);
                    if (value != null) {
                    final double v = value.doubleValue();
                    if (v > 0) {
                    result = value.toString(); // could apply formatting here
                    }
                    }
                    return result;
                    }


                public String generateColumnLabel(final CategoryDataset arg0, final int arg1) {
                    // TODO Auto-generated method stub
                    return null;
                }
            });
            renderer1.setSeriesItemLabelsVisible(0,true);

            BarRenderer renderer = (BarRenderer) plot.getRenderer();
            renderer.setDrawBarOutline(false);
            renderer.setSeriesOutlinePaint(0,Color.BLACK);
            renderer.setDrawBarOutline(true);
            renderer.setMaximumBarWidth(0.02);
            renderer.setSeriesPaint(0,new Color(170, 0, 85));          

            CategoryAxis domainAxis = plot.getDomainAxis();
            domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(.6));  
            domainAxis.setLabelFont(new Font("Helvetica", Font.BOLD, 14));
            domainAxis.setTickLabelFont(new Font("Helvetica", Font.BOLD, 14));

             //Behind Schedule "4"
             if(searchRatingElement.equalsIgnoreCase("4"))
             {           
                 System.out.println("||||||||||||||||||||||||||||||||searchRatingElement"+searchRatingElement);
                 numOfSupplrs = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.bhndschpctforchart");
             }
             else 
             {
                 numOfSupplrs = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpctforchart");
             }

            ValueAxis axis2 = new NumberAxis(numOfSupplrs);
            axis2.setLabelFont(new Font("Helvetica", Font.BOLD, 14));    
            axis2.setTickLabelFont(new Font("Helvetica", Font.PLAIN, 14));
            //if(!this.isValueDataZeros)
                //axis2.setRange(0,13);
            plot.setRangeAxis(1, axis2);

            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
           // if(!this.isNumberDataZeros)
                //rangeAxis.setRange(0,90);
           //rangeAxis.setTickUnit(new NumberTickUnit(1)); 
           rangeAxis.setLabelFont(new Font("Helvetica", Font.BOLD, 14));
           rangeAxis.setTickLabelFont(new Font("Helvetica", Font.PLAIN, 14));

            LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
            renderer2.setSeriesPaint(0, Color.BLUE);
            /*Start */
            renderer2.setSeriesItemLabelGenerator(0,new CategoryItemLabelGenerator() {

                public String generateRowLabel(final CategoryDataset arg0, final int arg1) {
                    // TODO Auto-generated method stub
                    return null;
                }

                public String generateLabel(final CategoryDataset dataset1,final int series,final int category) {
                    String result = null;
                    //CHAPTER 12. ITEM LABELS 91
                    final Number value = dataset1.getValue(series, category);
                    if (value != null) {
                    final double v = value.doubleValue();
                    if (v > 0) {
                    result = value.toString(); // could apply formatting here
                    }
                    }
                    return result;
                    }


                public String generateColumnLabel(final CategoryDataset arg0, final int arg1) {
                    // TODO Auto-generated method stub
                    return null;
                }
            });
            renderer2.setSeriesItemLabelsVisible(0,true);
            /* End */
            plot.setRenderer(1, renderer2);
            plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

            ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
                    1150, 450);
    }
    /**
     * 
     */
    public DefaultCategoryDataset createDataSetForPercentBasedChart1()
    {
        String METHOD_NAME = "createDataSetForPercentBasedChart1";
        log.entering(CLASS_NAME, METHOD_NAME);

        DefaultCategoryDataset dataSetForOccurrenceBasedChart = new DefaultCategoryDataset();

        boolean flagForPercentPareto = false;
        String occurrenceCountLabelName = null;
        try
        {
            if(paretoReportBasedDataQO != null && paretoReportBasedDataQO.size()>0)
            {
                //Behind Schedule "4"
                if(searchRatingElement.equalsIgnoreCase("4"))
                {            
                    occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.bhndschpctforchart");
                }
                else 
                {
                    occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpcs");
                }
                if(!flagForPercentPareto)
                {
                    for(int i =0;i<paretoReportBasedDataQO.size();i++)
                    {
                        dataSetForOccurrenceBasedChart.addValue(paretoReportBasedDataQO.get(i).getOverShipmentPiecesCount(), occurrenceCountLabelName, paretoReportBasedDataQO.get(i).getSupplierName());
                    }
                }
            }
        }
        catch(Exception exceptionOccurrenceBasedChart)
        {
            flagForPercentPareto = true;
            System.out.println("Exception In createDataSetForPercentBasedChart1 : "+exceptionOccurrenceBasedChart.getMessage());
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
        return dataSetForOccurrenceBasedChart;
    }
    /**
     * 
     */
    public DefaultCategoryDataset createDataSetForPercentBasedChart2()
    {
        String METHOD_NAME = "createDataSetForPercentBasedChart2";
        log.entering(CLASS_NAME, METHOD_NAME);

        DefaultCategoryDataset dataSetForOccurrenceBasedChart = new DefaultCategoryDataset();

        boolean flagForPercentPareto = false;
        String occurrenceCountLabelName = null;
        try
        {
            if(paretoReportBasedDataQO != null && paretoReportBasedDataQO.size()>0)
            {
                if(searchRatingElement.equalsIgnoreCase("4"))
                {            
                    occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.bhndschpctforchart");
                }
                else 
                {
                    occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpctforchart");
                }
                if(!flagForPercentPareto)
                {
                    for(int i =0;i<paretoReportBasedDataQO.size();i++)
                    {
                        dataSetForOccurrenceBasedChart.addValue(paretoReportBasedDataQO.get(i).getOverShipmentPercentageCount(), occurrenceCountLabelName, paretoReportBasedDataQO.get(i).getSupplierName());
                    }
                }
            }
        }
        catch(Exception exceptionOccurrenceBasedChart)
        {
            flagForPercentPareto = true;
            System.out.println("Exception In createDataSetForPercentBasedChart2 : "+exceptionOccurrenceBasedChart.getMessage());
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
        return dataSetForOccurrenceBasedChart;
    }

1 个答案:

答案 0 :(得分:1)

如果您将标签放在另一个位置怎么办? 例如:

renderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));