How to set line thickness for multiple series in JFreeChart?
Asked Answered
T

3

8

I create a lot of charts. In each of them I need to call

renderer.setSeriesStroke( i, new BasicStroke( 2.0f ) );

for each series. (renderer is chart.getXYPlot().getRenderer()).

I wonder if there is any way to set the thickness globally.

Torpedoman answered 19/10, 2018 at 10:2 Comment(0)
M
7

Call the renderer's setBaseStroke() setDefaultStroke() method, like they say here, and change the autoPopulateSeriesStroke flag, like they say here.

//renderer.setBaseStroke(new BasicStroke(2.0f));
renderer. setDefaultStroke(new BasicStroke(2.0f));
renderer.setAutoPopulateSeriesStroke(false);

The answers here and here show the new method name when migrating to v1.5.

Mala answered 19/10, 2018 at 16:31 Comment(1)
My renderer was XYItemRenderer, so I needed to use ((AbstractRenderer) this.renderer).setAutoPopulateSeriesStroke(false); like in referenced link ;)Torpedoman
I
3

From Jfreechart 1.5.0 and Line Chart created with ChartFactory.createLineChart(...)

    JFreeChart lineChart = ChartFactory.createLineChart(...);

    LineAndShapeRenderer renderer = (LineAndShapeRenderer) lineChart.getCategoryPlot().getRenderer();
    renderer.setAutoPopulateSeriesStroke(false);
    renderer.setDefaultStroke(new BasicStroke(3.0f));
Indue answered 7/1, 2021 at 12:35 Comment(2)
is it actually different from the accepted answer? doesn't look like thatTorpedoman
Yes, From JFreechart 1.5.0 the accepted answer won't work as the method used is now deprecated. my answer will work for those who are using the newer version.Indue
H
1

For Jfreechart 1.5.0:

XYItemRenderer renderer = lineChart.getXYPlot().getRenderer();
renderer.setDefaultStroke(new BasicStroke(2.0f));
((AbstractRenderer) renderer).setAutoPopulateSeriesStroke(false);
Hodges answered 4/7, 2020 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.