1

我在我的 struts 应用程序中使用 Jfree 图表。Y 轴是超运件数,X 轴是供应商代码,辅助轴用于超运百分比。我在这里面临的问题是,如果 Over Shipment % 值全部归零,它也会隐藏 X 轴供应商值。我该如何克服才能避免这种情况。我在这里附上了屏幕截图供您考虑。感谢大家阅读这个问题。

在此处输入图像描述

为了您的清楚理解,我将代码粘贴在这里,

/**
     * 
     * @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;
    }
4

1 回答 1

1

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

renderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
于 2013-01-28T14:45:10.127 回答