Apache POI图表-标题格式 [英] Apache POI Charts - Title formatting

查看:123
本文介绍了Apache POI图表-标题格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个折线图,当我将标题添加到图表中时,它就会覆盖我的数据.如何在图表上方添加标题?另外,如何调整标题文本的字体/样式?我想把文本缩小一些.

I created a line chart and when I add the title to my chart it overlaps my data. How do I add the title above my chart? Also how can I adjust the font/style of the title text? I want to make the text a little smaller.

    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);

因此,从这个示例中,我想要的是在标题会获得的8000以上的额外填充/边距.

So from this example what I looking for is extra padding/margin above the 8000 where my title would go.

推荐答案

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

So you don't want the title overlaying the plot area?

问题是使用Excel 2007测试了apache poi.但是,此版本之后,在更高版本中已更改了多个默认设置.

The problem is that apache poi was tested using Excel 2007. But after this version multiple default settings have been changed in later versions.

例如,如果未明确设置Excel 2007中的覆盖设置,则默认为false(不覆盖).我认为这是一个不错的选择.在更高版本中,如果未明确设置,则默认值为true(覆盖).我认为那是胡说八道.但是谁在乎我的意见.

The setting of overlays for example had defaulted to false (do not overlay) in Excel 2007 if it was not explicit set. This was a good choice in my opinion. In later versions the default is true (do overlay) if it is not explicit set. That's nonsense in my opinion. But who cares my opinion.

因此,如果我们不希望标题覆盖绘图区域,则必须进行显式设置.

So if we not want the title overlaying the plot area, we have to set this explicitly.

仅可使用底层底层对象来设置标题字体的样式.使用此方法,我们需要将运行属性添加到标题的第一段和第一行文本中.然后我们可以设置粗体,斜体和字体大小(单位1/100磅).然后为拉丁字母和复杂的脚本字符添加类型face.

Styling the title font is possible only using the low level underlying objects. Using this we need add run properties to the title's first paragraph and first text run. Then we can set bold, italic and font size (unit 1/100 pt). And then we add type face for latin and complex script characters.

使用Java的示例代码. (问题中的代码似乎是Groovy,但没有这样标记,并且我的问题也没有答案.)

Example code using Java. (The code in the question seems to be Groovy, but it is not tagged as such and there is no answer to my question about this discrepancy.)

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);
   }
  }
 }
}

这篇关于Apache POI图表-标题格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆