Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:43:42+00:00 2026-05-26T02:43:42+00:00

Is there a way to include some arbitrary text in the legend in a

  • 0

Is there a way to include some arbitrary text in the legend in a JFreeChart PieChart? I know it’s possible to assign a PieSectionLabelGenerator in order to customize the labels of each of the Pie’s sections that appear on the chart’s legend.

I’d like to insert some text into the legend, completely unrelated to any of the pie sections, like, for instance, “Legend”.

I’m building the Chart like this:

private JFreeChart constructChart() {
    List<Object[]> llistaValorsArr;

    ParamsDTO dto = (ParamsDTO) getModelObject();
    List llistaValors = statisticsService.getStatistics(dto);
    if (!llistaValors.isEmpty() && !(llistaValors.get(0) instanceof Object[])){
        llistaValorsArr = new ArrayList<Object[]>();
        llistaValorsArr.add(new Object[]{llistaValors.get(0), ""});
    }
    else{
        llistaValorsArr = (List<Object[]>) llistaValors;
    }
    DefaultPieDataset dataSet = new DefaultPieDataset();
    for (Object[] objects : llistaValorsArr) {
        dataSet.setValue((Comparable) objects[1], (Number)objects[0]);
    }

    String title = "Total: " +  new Double(DatasetUtilities.calculatePieDatasetTotal(dataSet)).intValue();
    JFreeChart chart = ChartFactory.createPieChart(title, dataSet, true, false, true);

    final PiePlot plot = (PiePlot) chart.getPlot();
    plot.setForegroundAlpha(0.5f);
    plot.setNoDataMessage("No data");

    PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0} - {1} ({2})"){
        @Override
        protected Object[] createItemArray(PieDataset dataset, Comparable key) {
            // TODO Auto-generated method stub
            Object[] array = super.createItemArray(dataset, key);
            array[0] = getEntityLabel(key);
            return array;
        }
    };
    plot.setLabelGenerator(labelGenerator);
    plot.setLegendLabelGenerator(labelGenerator);        
    //plot.setStartAngle(290);
    boolean circular = true;
    plot.setCircular(circular);
    return chart;
}

UPDATE: I just found out JFreeChart.addSubtitle(), hoping it would allow to position it just above the legend, but it will just add a subtitle next to the chart’s Title.

UPDATE 2: I’ve been trying to place a TextTitle inside the LegendTitle‘s wrapper, but it appears to be null at chart construction time.

LegendTitle legend = chart.getLegend();
BlockContainer container = legend.getWrapper();
container.add(new TextTitle("Legend"));

It shouldn’t really be that complicated to add a ‘Legend’ text to decorate the legend.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:43:43+00:00Added an answer on May 26, 2026 at 2:43 am

    Looking at the source code of org.jfree.chart.JFreeChart, and seeing that addLegend() is nothing more than addSubtitle() behind the scenes, everything indicates that this should be achieved using addSubtitle().

    Looking at the part where org.jfree.chart.JFreeChart adds its own LegendTitle item, we can find the setup JFreeChart uses for placing the Legend here.

    So, the solution is to add, for instance, a TextTitle to the Chart in an analogous way. The relevant setting here is setPosition(RECTANGLE.BOTTOM).

    TextTitle legendText = new TextTitle("This is LEGEND: ");
    legendText.setPosition(RectangleEdge.BOTTOM);
    chart.addSubtitle(legendText);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to include some aspx/ascx markup in a DLL and use
Is there a way to include an entire text file as a string in
Is there a way to include a default empty option (or with text) if
I was wondering wether there is a way to include some html content inside
Is there any way to include the SVN repository revision number in the version
Is there a way to include a javascript-loaded advertisement last on the page, but
Is there any way to include php file using require and calling a php
Is there a way to include a LIKE expression in a GROUP BY query?
Is there a way to not be forced to include members with the MustOverride
Is there any way to configure Doxygen to include the log from SVN for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.