2017-04-26 79 views
1

我希望有人可以幫助我設置自定義標籤的域軸刻度標籤由JASPER報告創建jFreeChart內。我試過了我在網上找到的所有東西,仍然沒有骰子。這裏是我的代碼:jFreeChart自定義域軸標籤

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.Paint; 
import java.util.List; 

import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.CategoryAxis; 
import org.jfree.chart.axis.SymbolAxis; 
import org.jfree.chart.axis.ValueAxis; 
import org.jfree.chart.labels.CategoryItemLabelGenerator; 
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 
import org.jfree.chart.plot.CategoryPlot; 
import org.jfree.chart.renderer.category.BarRenderer; 
import org.jfree.data.Range; 
import org.jfree.data.category.CategoryDataset; 
import org.jfree.data.category.DefaultCategoryDataset; 
import org.jfree.text.TextBlock; 
import org.jfree.text.TextUtilities; 
import org.jfree.ui.RectangleEdge; 

import net.sf.jasperreports.engine.JRChart; 
import net.sf.jasperreports.engine.JRChartCustomizer; 

public class ChartCustomizer implements JRChartCustomizer{ 
    public class CustomColorRenderer extends BarRenderer { 
     private static final long serialVersionUID = -9045170581109026224L; 

     @Override 
     public Paint getItemPaint(int row, int col) { 
      CategoryDataset currentDataset = getPlot().getDataset(); 

      String columnKey = (String) currentDataset.getColumnKey(col); 
      String[] columnKeyValues = columnKey.split(":"); 

      if(columnKeyValues.length < 2) return getSeriesPaint(row); 

      String columnActualEstimated = columnKeyValues[2]; 
      if(columnActualEstimated.equals("A")) { 
       return Color.RED; 
      } else if(columnActualEstimated.equals("E")) { 
       return Color.BLUE; 
      }  

      return getSeriesPaint(row); 
     } 
    } 

    public void customize(JFreeChart chart, JRChart jasperChart) 
    { 
     if(jasperChart.getChartType() == JRChart.CHART_TYPE_BAR) { 
      CategoryPlot plot = chart.getCategoryPlot(); 
      CategoryDataset currentDataset = plot.getDataset(); 
      double maxValue = Double.MIN_VALUE; 

      // Scan to get total max value for the chart in order to set chart height appropriately 
      for(int i = 0; i < currentDataset.getRowCount(); i++) { 
       //System.out.println(i); 
       for(int j = 0; j < currentDataset.getColumnCount(); j++) { 
        Number numberValue = currentDataset.getValue(i, j); 

        //System.out.println("Column " + j + " key: " + currentDataset.getColumnKey(j)); 

        double value = numberValue == null ? Double.NaN : numberValue.doubleValue(); 
        if(value > maxValue) { 
         maxValue = value; 
        } 
       } 
      } 

      // Add 10% to top margin 
      double tenPercent = maxValue * 0.1; 
      maxValue = (Math.round((maxValue * 1.1)/tenPercent) * tenPercent) + tenPercent; 

      // Set max bar height to max value 
      ValueAxis yAxis = plot.getRangeAxis(); 
      yAxis.setAutoRange(false); 
      yAxis.setRange(0, maxValue); 

      CategoryAxis xAxis = plot.getDomainAxis(); 

      // Set label font size 
      xAxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 4)); 

      // Will set single bar colors by value with a custom renderer 
      CustomColorRenderer customRenderer = new CustomColorRenderer(); 

      // Set the chart to apply the custom renderer 
      plot.setRenderer(customRenderer); 
     } 
    } 
} 

這裏是我的圖表看起來像目前:

Chart example

注意域軸顯示鍵,例如 「1:N:A」。在這種情況下,1表示訂單,N表示11月份,A表示這兩個系列的值爲「實際」與「預計」。我想要做的就是將可見刻度標籤更改爲「Nov」,即「1:N:A」示例。像自定義標籤生成器這樣的東西會更改圖表其他部分的標籤,而不是標籤。我可以成功設置刻度標籤字體,但似乎無法讓標籤本身發生變化。

編輯:關於這種情況的另一個棘手的部分是,要求顯示包括前11個,當前和即將到來的13個月。即將到來的月份總是一個估計值,因此是「A」和「E」系列)。這使得它很痛苦,因爲這意味着總是有重複的月份,因此需要合併的列。

任何幫助,將不勝感激。讓我知道是否需要更多信息。

Crossposted到http://www.jfree.org/forum/viewtopic.php?f=3&t=117811

回答

1

定製CategoryItemLabelGenerator,這通常是用來標記吧,可能是不適合這個正確的選擇。如here所示,CategoryAxis通過圖的getCategoriesForAxis()方法從CategoryDataset的列鍵中獲得類別標籤的文本。您可以在創建數據集時指定所需的鍵。

+0

根據您的建議,我從現有的數據集鍵中創建了一個SymbolAxis,但這樣做的問題是似乎沒有辦法將新的SymbolAxis設置爲域軸,因爲圖的'setDomainAxis()'只需要一個'CategoryAxis',而圖的'setRangeAxis()'確實需要'SymbolAxis'作爲輸入。我錯過了什麼嗎? – Kettch19

+0

糟糕,我的錯誤; 「SymbolAxis」不適合您的類別圖的域軸 – trashgod