簡體   English   中英

Apache POI圖表-標題格式

[英]Apache POI Charts - Title formatting

我創建了一個折線圖,當我在圖表中添加標題時,它就會與數據重疊。 如何在圖表上方添加標題? 另外,如何調整標題文本的字體/樣式? 我想把文本縮小一些。

    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的位置上的額外填充/邊距。

因此,您不希望標題覆蓋繪圖區域嗎?

問題是apache poi已使用Excel 2007進行了測試。 但是,此版本之后,多個默認設置已在更高版本中更改。

例如,如果未明確設置疊加層設置,則在Excel 2007默認將其設置為false(不疊加)。 我認為這是一個不錯的選擇。 在更高版本中,如果未明確設置,則默認值為true(做覆蓋)。 我認為那是胡扯。 但是誰在乎我的意見。

因此,如果我們不希望標題覆蓋繪圖區域,則必須進行顯式設置。

只能使用底層底層對象來樣式化標題字體。 使用此方法,我們需要在標題的第一段和第一段文本中添加運行屬性。 然后我們可以設置粗體,斜體和字體大小(單位1/100磅)。 然后,我們為拉丁和復雜腳本字符添加type face。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM