8

I'm creating an Excel workbook from scratch. One of the sheets contains a chart, and I want to set the chart title.

Apache POI has a setChartTitle method on HSSFChart, but neither XSSFChart nor the format-agnostic Chart have methods to set the chart title. Since I need to create .xlsx files this is a problem for me.

After much poking around in POI code and OOXML specifications I managed to come up with this code for setting the title on a newly created Chart:

    if (chart instanceof XSSFChart) {
        XSSFChart xchart = (XSSFChart) chart;
        CTChart ctChart = xchart.getCTChart();
        CTTitle title = ctChart.addNewTitle();
        CTTx tx = title.addNewTx();
        CTTextBody rich = tx.addNewRich();
        rich.addNewBodyPr();  // body properties must exist, but can be empty
        CTTextParagraph para = rich.addNewP();
        CTRegularTextRun r = para.addNewR();
        r.setT("My chart title");
    }

This seems to work - I can load the resulting file in Excel 2013 and the chart is there with the correct title.

Is there an easier way to do this? What gotchas would I need to look out for when changing a chart title in a workbook created in Excel?

3
  • Have you thought about submitting a patch to Apache POI to add in this missing functionality to the XSSF and SS chart methods? See here for details on patches Commented May 29, 2015 at 15:53
  • I submitted a patch to Apache POI months ago but it has not been acted on. See the patch in Bugzilla. Commented Feb 3, 2016 at 17:02
  • Patch now committed in POI, so you can now add an answer referencing your contribution :) Commented Feb 21, 2016 at 21:18

2 Answers 2

0

Before this question remains unanswered for six years, here is the answer:

You can use the XSSFChart.setTitleText(String title) method.

Example:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFDrawing;

public class Chart {

    public static void main(String[] args) throws Exception {
        try (Workbook wb = new XSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("chart.xlsx");) {
            Sheet sheet = wb.createSheet("Sheet1");

            XSSFDrawing drawing = (XSSFDrawing)sheet.createDrawingPatriarch();
            XSSFChart chart = drawing.createChart(drawing.createAnchor(0, 0, 0, 0, 0, 0, 12, 12));
    
            
            // --------------------->     Here we set the title
            chart.setTitleText("My chart title");
            // <---------------------

            
            XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromArray(new String[]{"A","B","C","D"});
            XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromArray(new Double[]{1d, 2d, 3d, 4d});

            XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
            data.setVaryColors(true);
            XDDFChartData.Series series = data.addSeries(cat, val);
            series.setTitle("Series", null);
            chart.plot(data);

            wb.write(fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I understand that XSSFChart.setTitleText("My chart title") was not available, when this question was asked.

Sign up to request clarification or add additional context in comments.

Comments

0

Using Apache Poi 3.7 (itself an older version), this works fine:

XSSFChart xchart = (XSSFChart) chart;
xchart.setTitleText("Your title");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.