Question

There are methods in the CandlestickRenderer class that change the border color of all candles to a desired color, but I need to set all the Up candle borders to green and all the Down candle borders to red. Any advice or example code is greatly appreciated.

Was it helpful?

Solution

I got this by digging into the CandlestickRenderer class source

First, make a subclass

public class MyCandlestickRenderer extends CandlestickRenderer {

    @Override
    public Paint getItemPaint(int row, int column) {

        //determine up or down candle 
        XYDataset dataset = getPlot().getDataset();
        OHLCDataset highLowData = (OHLCDataset) dataset;
        int series = row, item = column;
        Number yOpen = highLowData.getOpen(series, item);
        Number yClose = highLowData.getClose(series, item);
        boolean isUpCandle = yClose.doubleValue() > yOpen.doubleValue();

        //return the same color as that used to fill the candle
        if (isUpCandle) {
            return getUpPaint();
        }
        else {
            return getDownPaint();
        }
    }
}

Then set your chart to use it

chart.getXYPlot().setRenderer(new MyCandlestickRenderer());

Result:

chart

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top