【问题标题】:JFreeChart different colors in different regions for the same dataSeriesJFreeChart同一个dataSeries不同区域不同颜色
【发布时间】:2012-08-15 00:58:12
【问题描述】:

JFreeChart 中,我正在尝试根据y 值对XY 折线图/曲线的不同区域进行着色。我正在覆盖XYLineAndShapeRenderergetItemPaint(int row, int col),但是我不确定它如何处理xs 之间的线的着色,因为它只在x 上获得itemPaint(整数值) .

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);
    }
}

【问题讨论】:

    标签: java jfreechart renderer


    【解决方案1】:

    貌似在drawFirstPassShape中实现了对行间着色的处理

    线条颜色似乎基于前面的点

    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);
        }
    };
    

    我已删除 ColorUtil.hex2Rgb,因为我无权访问该类/方法。您可能需要修改 GradientPaint 以考虑点之间的距离/梯度。

    【讨论】:

      猜你喜欢
      • 2013-08-31
      • 2012-04-24
      • 1970-01-01
      • 2012-04-15
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      相关资源
      最近更新 更多