2017-06-21 232 views
2

在eclipse中我在JasperSoft 6.3.1中有一個堆疊條形圖,我試圖根據這個系列來顯示顏色。該圖表顯示隨機顏色,而不是爲特定系列分配單一顏色。如何動態設置堆疊條形圖系列顏色?

JRXML

<categorySeries> 
    <seriesExpression><![CDATA[$F{name}]]></seriesExpression> 
    <categoryExpression><![CDATA[$F{time}]]></categoryExpression> 
    <valueExpression><![CDATA[$F{value}]]></valueExpression> 
</categorySeries> 
</categoryDataset> 
    <barPlot> 
     <plot> 
      <seriesColor $F{name}.equals("JANUARY")?color="#756D72":color="" seriesOrder="0" /> 
      <seriesColor $F{name}.equals("MARCH")?color="#4B5154":color="" seriesOrder="1" /> 
      <seriesColor $F{name}.equals("JUNE")?color="#090A09":color="" seriesOrder="2"/> 
     </plot> 
    <itemLabel/> 
    <categoryAxisFormat> 
    .... 

我想使用if語句圖表系列的顏色分配給特定的系列名。我如何在碧玉報告中實現這一目標?

如果系列名稱爲JANUARY,顏色應該是黑色,如果1月份沒有數據,則不應使用黑色。

回答

1

正如我想你已經注意到了,你可以不如果XML標籤報表時,將JRXML簡單不能編譯,因爲它不是有效的XML了。

解決的辦法是實現自己的JRChartCustomizer

的Java

找到不同的系列名稱並設置Paint的渲染上的名稱的基礎

public class BarColorCustomizer implements JRChartCustomizer { 

    @Override 
    public void customize(JFreeChart jfchart, JRChart jrchart) { 
     //Get the plot 
     CategoryPlot plot = jfchart.getCategoryPlot(); 
     //Get the dataset 
     CategoryDataset dataSet = plot.getDataset(); 
     //Loop the row count (our series) 
     int rowCount = dataSet.getRowCount(); 
     for (int i = 0; i < rowCount; i++) { 
      Comparable<?> rowKey = dataSet.getRowKey(i); 
      //Get a custom paint for our series key 
      Paint p = getCustomPaint(rowKey); 
      if (p!=null){ 
       //set the new paint to the renderer 
       plot.getRenderer().setSeriesPaint(i, p); 
      } 
     } 

    } 

    //Example of simple implementation returning Color on basis of value 
    private Paint getCustomPaint(Comparable<?> rowKey) { 
     if ("JANUARY".equals(rowKey)){ 
      return Color.BLACK; 
     } 
     return null; 
    } 
} 

jrxml

設置customizerClass屬性全包名的圖表標籤

<barChart> 
    <chart evaluationTime="Report" customizerClass="my.custom.BarColorCustomizer"> 
    .... 
</barChart> 
+0

WOOW的作品完美。你救了我的一天。我經歷了很多鏈接,並且找不到解決方案。對不起,再次詢問是否可以隱藏類別顏色欄,如果它是空的。 – joseph

+0

@joseph隱藏類別顏色欄?,類別是一組系列?,我無法真正理解您的意思,無論如何,您可以根據您的喜好設置顏色條形和輪廓,因此將其設置爲背景並設置爲你不會看到它;) –

+0

@ Petter Friberg非常感謝你。你已經正確回答了我的問題,我想設置空白類別作爲背景。 – joseph

相關問題