java - JFreeChart 同一数据系列在不同区域的不同颜色

标签 java jfreechart renderer

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

最佳答案

看起来线间着色的处理是在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 以考虑点之间的距离/梯度。

关于java - JFreeChart 同一数据系列在不同区域的不同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11962836/

相关文章:

java - 如何将 Glassfish 4.1 Server 运行时添加到 Eclipse Indigo?

java - 几乎相同的程序。一个有效,一个无效

math - 使用贝塞尔曲线的圆近似

java - Tapestry动态生成图像

enums - 为 Vaadin 14 Web 应用程序中的枚举支持的单选按钮生成标签

java - 如何使 Java 组合框中的项目居中

java - Eclipse juno 在 Suse 64 位中给出错误

java - Blackberry 中的快速重置是什么?

java - Android 如何从 Parse 列表中删除选定的项目

java - 哪种类型的 Jfreechart 更适合 Histograms?