1

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?

ratatosk
  • 373
  • 5
  • 19

1 Answers1

1

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?

ratatosk
  • 373
  • 5
  • 19
  • Can you please add your instantiation for the QualityChartRender class and what are the arguments you're using like high, low and series? – Droid Diva Feb 05 '21 at 08:46