java – 可以在烛台jfreechart中显示空心蜡烛吗?

大多数金融图表库使用一些规则来着色蜡烛:

>如果关闭> =打开,则蜡烛是空心的(未填充)
>如果关闭 >如果先前关闭< =关闭,则蜡烛为绿色
>如果先前关闭>关闭,然后蜡烛是红色的

jFreeChart似乎有自己的着色规则,可以改变它们.默认情况下,图表看起来像这样(某些颜色已更改):

如你所见,可以改变蜡烛,背景,情节边界等的颜色.

我想在图表中实施规则1和2(上面).可能吗?

结果应该是这样的:

这是我在示例中使用的当前CandlestickRenderer:

public class CandlestickRenderer extends org.jfree.chart.renderer.xy.CandlestickRenderer {

    public CandlestickRenderer() {
        setDrawVolume(false);
    }

    @Override
    public Paint getItemPaint(int series, int item) {
        OHLCDataset highLowData = (OHLCDataset)getPlot().getDataset(series);
        Number curClose = highLowData.getClose(series, item);
        Number prevClose = highLowData.getClose(series, item>0 ? item-1 : 0);
        if (prevClose.doubleValue() <=  curClose.doubleValue()) {
            return getUpPaint();
        }
        else {
            return getDownPaint();
        }
    }   
}

更新1

似乎可以通过将油漆设置为黑色并使用颜色不改变项目裤来完成.

public class CandlestickRenderer extends org.jfree.chart.renderer.xy.CandlestickRenderer {

    public CandlestickRenderer() {
        setDrawVolume(false);
        setUseOutlinePaint(false);
        setUpPaint(Color.BLACK); // <-- rule 1. (plot is also black so it looks not filled)
    }

    @Override
    public Paint getItemPaint(int series, int item) {
        OHLCDataset highLowData = (OHLCDataset) getPlot().getDataset(series);
        Number curClose = highLowData.getClose(series, item);
        Number prevClose = highLowData.getClose(series, item>0 ? item-1 : 0);

        if (prevClose.doubleValue() <=  curClose.doubleValue()) {
            return Color.GREEN; // <-- rule 3.
        } else {
            return Color.RED; // <-- rule 4.
        }
    }

}

但主要问题持续:

>根据jFreeChart,down(downPaint)和up(upPaint)是什么意思?它比较上一个.收盘价与收盘价相比还是开盘价与收盘价相比
>在jFreeChart的源代码中,我可以找到upPaint& amp;使用downPaint?

现在这是一个奇怪的结果:

最佳答案 我做到了这是完整的渲染器:

public class CandlestickRenderer extends org.jfree.chart.renderer.xy.CandlestickRenderer {

    private final Paint colorRaising = Color.GREEN;
    private final Paint colorFalling = Color.RED;
    private final Paint colorUnknown = Color.GRAY;
    private final Paint colorTransparent = Color.BLACK;

    public CandlestickRenderer() {
        setDrawVolume(false);
        setUpPaint(colorUnknown); // use unknown color if error
        setDownPaint(colorUnknown); // use unknown color if error
    }


    @Override
    public Paint getItemPaint(int series, int item) {
        OHLCDataset highLowData = (OHLCDataset) getPlot().getDataset(series);
        Number curClose = highLowData.getClose(series, item);
        Number prevClose = highLowData.getClose(series, item>0 ? item-1 : 0);

        if (prevClose.doubleValue() <=  curClose.doubleValue()) {
            return Color.GREEN;
        } else {
            return Color.RED;
        }
    }


    @Override
    public void drawItem(Graphics2D g2, XYItemRendererState state,
            Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
            ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
            int series, int item, CrosshairState crosshairState, int pass) {

        OHLCDataset highLowData = (OHLCDataset) dataset;
        double yOpen = highLowData.getOpenValue(series, item);
        double yClose = highLowData.getCloseValue(series, item);        

        // set color for filled candle
        if (yClose >= yOpen) {
            setUpPaint(colorRaising);
            setDownPaint(colorFalling);
        }

        // set color for hollow (not filled) candle
        else {
            setUpPaint(colorTransparent);
            setDownPaint(colorTransparent);
        }

        // call parent method
        super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass);
    }   

}

结果图表:

如果您需要所有示例代码,请随时与我联系.

点赞