gpt4 book ai didi

java - Apache POI 图表 - 标题格式

转载 作者:行者123 更新时间:2023-12-02 11:13:59 30 4
gpt4 key购买 nike

我创建了一个折线图,当我将标题添加到图表中时,它与我的数据重叠。如何在图表上方添加标题?另外,如何调整标题文本的字体/样式?我想让文字小一点。

    Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);

Chart chart = drawing.createChart(anchor);
chart.setTitleText("This is my title");

// Use a category axis for the bottom axis.
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);

ChartDataSource<Integer> test = DataSources.fromArray([2011,2012,2013,2014,2015,2016,2017] as Integer[]);
ChartDataSource<Integer> test2 = DataSources.fromArray([4805, 7351, 5333, 7183, 6230, 4050, 6963] as Integer[]);

LineChartData data = chart.getChartDataFactory().createLineChartData();
data.addSeries(test, test2);
chart.plot(data, bottomAxis, leftAxis);

enter image description here

因此,从这个示例中,我寻找的是标题所在的 8000 以上的额外填充/边距。

最佳答案

所以您不希望标题覆盖绘图区域?

问题是 apache poi 是使用 Excel 2007 进行测试的。但在此版本之后,后续版本中的多个默认设置已更改。

例如,如果未明确设置,则在 Excel 2007 中,覆盖设置默认为 false(不覆盖)。我认为这是一个不错的选择。在更高版本中,如果未显式设置,则默认值为 true(进行覆盖)。在我看来这是无稽之谈。但谁在乎我的意见。

因此,如果我们不希望标题覆盖绘图区域,则必须明确设置。

只能使用低级底层对象来设置标题字体的样式。使用此功能,我们需要将运行属性添加到标题的第一段和第一个文本运行中。然后我们可以设置粗体、斜体和字体大小(单位1/100 pt)。然后我们为拉丁文和复杂的脚本字符添加字体。

使用Java的示例代码。 (问题中的代码似乎是 Groovy,但它没有这样标记,并且没有回答我关于此差异的问题。)

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.xssf.usermodel.*;

public class LineChartProblem {

public static void main(String[] args) throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {

Sheet sheet = wb.createSheet("linechart");
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);

Chart chart = drawing.createChart(anchor);
((XSSFChart)chart).setTitleText("This is my title");

//set "the title overlays the plot area" to false explicitly
((XSSFChart)chart).getCTChart().getTitle().addNewOverlay().setVal(false);

//set font style for title - low level
//add run properties to title's first paragraph and first text run. Set bold.
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).addNewRPr().setB(true);
//set italic
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().setI(true);
//set font size 20pt
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().setSz(2000);
//add type face for latin and complex script characters
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().addNewLatin().setTypeface("Times New Roman");
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().addNewCs().setTypeface("Times New Roman");

// Use a category axis for the bottom axis.
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);

ChartDataSource<Integer> test = DataSources.fromArray(new Integer[]{2011,2012,2013,2014,2015,2016,2017});
ChartDataSource<Integer> test2 = DataSources.fromArray(new Integer[]{4805, 7351, 5333, 7183, 6230, 4050, 6963});

LineChartData data = chart.getChartDataFactory().createLineChartData();
data.addSeries(test, test2);
chart.plot(data, bottomAxis, leftAxis);

// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
wb.write(fileOut);
}
}
}
}

关于java - Apache POI 图表 - 标题格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50418856/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com