7

In my JFreeChart timeseries plots I find the legends lines too thin to see the colour accurately. Another post [ https://stackoverflow.com/questions/7306901/jfreechart-change-sample-of-colors-in-legend ] suggested overriding a renderer method as follows:

renderer = new XYLineAndShapeRenderer()
{
    private static final long serialVersionUID = 1L;
    public Shape lookupLegendShape(int series)
    {
        return new Rectangle(15, 15);
    }
};

this approach works fine until you do what I did

renderer.setSeriesShapesVisible(i, false);

Once I did that the legend reverts back to a line. Is there any way round this?

The solution I adopted is close to that suggested by TrashGod I overrode the getLegendItem() method, forcing the legend shape to the desired box.

    renderer = new XYLineAndShapeRenderer()
    {
        private static final long serialVersionUID = 1L;

        public LegendItem getLegendItem(int datasetIndex, int series)
        {
            LegendItem legend = super.getLegendItem(datasetIndex, series);
            return new LegendItem(legend.getLabel(), legend.getDescription(), legend.getToolTipText(), legend.getURLText(), Plot.DEFAULT_LEGEND_ITEM_BOX, legend.getFillPaint());
        }
    };
Richard B
  • 895
  • 13
  • 39

2 Answers2

8

You're going to have to override getLegendItem() to get the LegendItem you want in place of the one the renderer creates.

Addendum: Here's a simple example that should help you get started.

XYPlot plot = (XYPlot) chart.getPlot();
plot.setRenderer(new MyRenderer());
...
private static class MyRenderer extends XYLineAndShapeRenderer {

    @Override
    public LegendItem getLegendItem(int dataset, int series) {
        LegendItem legendItem = super.getLegendItem(dataset, series);
        System.out.println(dataset + " " + series + " " + legendItem.getShape());
        // modify legendItem here
        return legendItem;
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I'm probably being a bit slow here but could you give me a clue how to do this? I'd be happy simply substituting the legend graphic for Plot.DEFAULT_LEGEND_ITEM_BOX but I'm not sure how to keep the rest of the legend information consistent with the original. Do I simply get all the attributes and set them in a new legend? – Richard B Sep 01 '12 at 16:50
  • @RichardB: I've outlined the approach above. – trashgod Sep 01 '12 at 21:30
  • Thanks. I modified your approach slightly(see above) – Richard B Sep 02 '12 at 19:52
2

Get the renderer and do the following:

XYItemRenderer renderer = plot.getRenderer();
BasicStroke thickLine = new BasicStroke( 4.0f ); 
renderer.setSeriesStroke(0, thickLine); 

This will make your line thicker.

fonZ
  • 2,428
  • 4
  • 21
  • 40
  • If the problem is that the line is to thin, you adjust the thickness of the rendered line. So yea thats ok. – fonZ Sep 02 '12 at 19:57
  • Ah, that makes sense. One only needs to override `getLegendItem()` to get a `LegendItem` that _differs_ from the renderer's. – trashgod Sep 02 '12 at 20:20
  • I thought that the rendered line on the chart would be the same as the line in the legend. So i assumed that changing the chart line would also change the line in the legend. – fonZ Sep 02 '12 at 20:30