【问题标题】:How to set the category axis format of a bar chart to vertical?如何将条形图的类别轴格式设置为垂直?
【发布时间】:2014-05-13 16:03:22
【问题描述】:

我正在使用 dynamicReports 框架生成条形图,该图表已正确生成,但我想要这样的垂直类别标签(我的是水平的):

这是我的代码:

report().setTemplate(Templates.reportTemplate)
        .columns(itemColumn, quantityColumn, unitPriceColumn)
        .title(Templates.createTitleComponent("BarChart"))
        .summary(cht.barChart()
            .setTitle("Bar chart")
            .setTitleFont(boldFont)
            .setCategory(itemColumn)
            .series(cht.serie(quantityColumn) )
                   .series(cht.serie(unitPriceColumn))
                  .setCategoryAxisFormat(cht.axisFormat().setVerticalTickLabels(true).setLabel("Item")))
                .pageFooter(Templates.footerComponent)
                .setDataSource(createDataSource())
                .show();

【问题讨论】:

    标签: java jasper-reports reporting dynamic-reports


    【解决方案1】:

    您必须使用 ChartCustomizer 来完成此任务。

    private class ChartCustomizer implements DRIChartCustomizer, Serializable {  
       public void customize(JFreeChart chart, ReportParameters reportParameters) {  
          CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();  
          domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI/2));
       }  
    }
    

    createUpRotationLabelPositions 需要一个辐射角。

    您可以通过将其添加到图表中来使用此定制器,如下所示:

    .addCustomizer(new ChartCustomizer())
    

    【讨论】:

      【解决方案2】:
      public class BarChartReport 
      {
      public BarChartReport() {
      build();
      }
      
      private void build() {
          FontBuilder boldFont = stl.fontArialBold().setFontSize(12);
          StyleBuilder boldStyle = stl.style().setFontName("Arial");
          StyleBuilder boldCenteredStyle = stl.style(boldStyle)
                  .setHorizontalAlignment(HorizontalAlignment.CENTER);
          StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle)
                  .setBorder(stl.pen1Point())
                  .setBackgroundColor(Color.LIGHT_GRAY);
      
          TextColumnBuilder<String> itemColumn = col.column("Item", "item", type.stringType());
          TextColumnBuilder<Integer> quantityColumn = col.column("Quantity", "quantity", type.integerType());
          TextColumnBuilder<BigDecimal> unitPriceColumn = col.column("Unit price", "unitprice", type.bigDecimalType());
          try {
              report()
                      .setColumnTitleStyle(columnTitleStyle)
                      .setTemplate(Templates.reportTemplate)
                      //  .columns(itemColumn, quantityColumn, unitPriceColumn)
                      .title(Templates.createTitleComponent("BarChart"))
                      .summary(
                      cht.barChart()
                      .setTitle("Bar chart")
                      .setTitleFont(boldFont)
                      .setCategory(itemColumn)
                      .addCustomizer(new ChartCustomizer())
                      .series(
                      cht.serie(quantityColumn), cht.serie(unitPriceColumn)).setShowValues(Boolean.TRUE).setHeight(700)
                      .addCustomizer(new ChartCustomizer()))
                      .pageFooter(Templates.footerComponent)
                      .setDataSource(createDataSource())
                      .show();
          } catch (DRException e) {
              e.printStackTrace();
          }
      }
      
      private JRDataSource createDataSource() {
          DRDataSource dataSource = new DRDataSource("item", "quantity", "unitprice");
          dataSource.add("Tablet", 350, new BigDecimal(300));
          dataSource.add("Laptop", 300, new BigDecimal(2000));
          dataSource.add("Smartphone", 450, new BigDecimal(250));
          return dataSource;
      }
      
      private class ChartCustomizer implements DRIChartCustomizer, Serializable {
      
          public void customize(JFreeChart chart, ReportParameters reportParameters) {
      
              ValueAxis axis = chart.getCategoryPlot().getRangeAxis();
              TickUnits tickUnits = new TickUnits();
      
              tickUnits.add(new NumberTickUnit(300)); //sets value with which y-axis gets incremented
              //   axis.setVerticalTickLabels(true);
              axis.setStandardTickUnits(tickUnits);
      
              //rotate x-axis value to vertical
              CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
              domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2));
      
              //this is to rotate values on each bar generated
              BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
              renderer.setShadowPaint(Color.LIGHT_GRAY);
              renderer.setShadowVisible(true);
              for (int x = 0; x < 5; x++) {
                  renderer.setSeriesItemLabelFont(x, new Font("Times New Roman", Font.PLAIN, 10));
                  renderer.setSeriesPositiveItemLabelPosition(x,
                          //    new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.BOTTOM_LEFT, TextAnchor.BOTTOM_LEFT, -45.0));  
                          new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER, TextAnchor.CENTER, -55.0));
      
      
              }
          }
      }
      
      public static void main(String[] args) {
      
          new BarChartReport();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多