ratatosk
ratatosk

Reputation: 393

Manipulate color of shapes in XYPlot of JFreeChart

Is it possible to have different coloured shapes in an XYPlot without using different series?

One idea is to extend the XYLineAndShapeRenderer but where can I change the colour of single shapes when they are drawn?

Upvotes: 1

Views: 365

Answers (1)

ratatosk
ratatosk

Reputation: 393

I found a solution myself

public class QualityChartRenderer extends XYLineAndShapeRenderer {

    private int dataSeries;

    public QualityChartRenderer(double high, double low, int dataSeries) {
        this.dataSeries = dataSeries;
    }

    @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) {

        Paint paint = getSeriesPaint(series);

        if (series == dataSeries && item < 2) {
            setSeriesPaint(series, Color.RED);
        }
        super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis,
                dataset, series, item, crosshairState, pass);
        setSeriesPaint(series, paint);
    }

}

For every shape drawn, I check for my condition (here only item < 2) and change the color of the whole series. I change it back after the drawing.

This feels like a hack. Is there a more elegant solution within the framework?

Upvotes: 1

Related Questions