2012-08-15 88 views
4

JFreeChart我試圖根據y值爲XY線圖/曲線的不同區域着色。我重寫XYLineAndShapeRenderergetItemPaint(int row, int col),但我不確定它是如何處理x之間的線的顏色,因爲它只在x(整數值)上獲得itemPaintJFreeChart對於相同的數據在不同地區的不同顏色系列

final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer() { 
    @Override 

    @Override 
    public Paint getItemPaint(int row, int col) { 
     System.out.println(col+","+dataset.getY(row, col)); 
     double y=dataset.getYValue(row, col); 
     if(y<=3)return ColorUtil.hex2Rgb("#7DD2F7"); 
     if(y<=4)return ColorUtil.hex2Rgb("#9BCB3B"); 
     if(y<=5)return ColorUtil.hex2Rgb("#FFF100"); 
     if(y<=6)return ColorUtil.hex2Rgb("#FAA419"); 
     if(y<=10)return ColorUtil.hex2Rgb("#ED1B24"); 

     //getPlot().getDataset(col). 
     return super.getItemPaint(row,col); 
    } 
} 

回答

7

它看起來像線之間的着色的處理中drawFirstPassShape

線條顏色被實現似乎是基於前一個點

enter image description here

此修改到您XYLineAndShapeRenderer使用漸變填充來混合線條顏色。

XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(){ 
     @Override 
     public Paint getItemPaint(int row, int col) { 
      Paint cpaint = getItemColor(row, col); 
      if (cpaint == null) { 
       cpaint = super.getItemPaint(row, col); 
      } 
      return cpaint; 
     } 

    public Color getItemColor(int row, int col) { 
     System.out.println(col + "," + dataset.getY(row, col)); 
     double y = dataset.getYValue(row, col); 
     if(y<=3) return Color.black; 
     if(y<=4) return Color.green;; 
     if(y<=5) return Color.red;; 
     if(y<=6) return Color.yellow;; 
     if(y<=10) return Color.orange;; 
     return null; 
    } 

    @Override 
    protected void drawFirstPassShape(Graphics2D g2, int pass, int series, 
     int item, Shape shape) { 
     g2.setStroke(getItemStroke(series, item)); 
     Color c1 = getItemColor(series, item); 
     Color c2 = getItemColor(series, item - 1); 
     GradientPaint linePaint = new GradientPaint(0, 0, c1, 0, 300, c2); 
     g2.setPaint(linePaint); 
     g2.draw(shape); 
    } 
}; 

enter image description here

我已經刪除ColorUtil.hex2Rgb,因爲我沒有訪問到類/方法。您可能需要修改GradientPaint以考慮點之間的距離/坡度。

+0

這是一個美妙的解決方案! – user121196 2012-08-15 13:51:49

相關問題