Subtitles and Legend on a Bar Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jeffk
Posts: 4
Joined: Fri Oct 15, 2010 5:18 pm
antibot: No, of course not.

Subtitles and Legend on a Bar Chart

Post by jeffk » Fri Oct 22, 2010 7:56 pm

I have a bar chart and would like a legend and a couple subtitles.

If you run this code, you'll get a NPE on the line that reads: LegendTitle legendTitle2 = chart.getLegend();

Code: Select all

        JFreeChart chart = ChartFactory.createBarChart(
                "Chart Title", "X Title", "Range Title",  dataset,
                PlotOrientation.VERTICAL, true, false, false);

        LegendTitle legendTitle = chart.getLegend();

        List<Title> subTitles = new ArrayList<Title>();
        subTitles.add(new TextTitle("SubTitle 1"));
        subTitles.add(new TextTitle("SubTitle 2"));
        chart.setSubtitles(subTitles);

        LegendTitle legendTitle2 = chart.getLegend();
 
However, I found this workaround, which appears to work fine:

Code: Select all

        JFreeChart chart = ChartFactory.createBarChart(
                "Chart Title", "X Title", "Range Title",  dataset,
                PlotOrientation.VERTICAL, true, false, false);

        LegendTitle legendTitle = chart.getLegend();

        List<Title> subTitles = new ArrayList<Title>();
        subTitles.add(new TextTitle("SubTitle 1"));
        subTitles.add(new TextTitle("SubTitle 2"));
        chart.setSubtitles(subTitles);

        chart.addLegend(legendTitle);
        LegendTitle legendTitle2 = chart.getLegend();
I just thought I'd mention it in case someone else happened to come across it. It may be a simple case of my misunderstanding of how the subtitles and legend are supposed to interact.

Locked