2012-08-15 68 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

相关问题