如何通过Apache POI在Excel Sunburst Chart中设置各个数据标签的文本属性? [英] How to set the text attributes of the individual data labels in an Excel Sunburst Chart through Apache POI?

查看:132
本文介绍了如何通过Apache POI在Excel Sunburst Chart中设置各个数据标签的文本属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此

In this post a solution was provided to set the graphical attributes of the data points.

我现在需要访问各个数据点标签并更改文本属性.我的首要任务是调整字体大小,但是,也可以使用调整其他属性(例如,字体,粗体,斜体,对齐方式等)的功能.此方法设置数据点填充颜色.要为该数据点设置数据标签的字体大小需要添加什么内容?

I now need to access the individual data point labels and change the text attributes as well. My priority is to adjust the font size, however, the ability to adjust other attributes (e.g. font, bold, italic, alignment, etc) as well would be useful. This method sets the data point fill colour. What needs to be added to set the font size of the data label for that data point?

private static void setDataPointColor(XmlObject series, int number, String colorHex) {
    XmlCursor cursor = series.newCursor();
    cursor.toLastChild();
    cursor.beginElement(new QName("http://schemas.microsoft.com/office/drawing/2014/chartex", "dataPt", "cx"));
    cursor.insertAttributeWithValue("idx", "" + number);
    cursor.beginElement(new QName("http://schemas.microsoft.com/office/drawing/2014/chartex", "spPr", "cx"));
    cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "solidFill", "a"));
    cursor.beginElement(new  Name("http://schemas.openxmlformats.org/drawingml/2006/main", "srgbClr", "a"));
    cursor.insertAttributeWithValue("val", colorHex);

    cursor.dispose();
}

推荐答案

已在问题中告知,这与

As told in the question already, this is related to How to change the graphical attributes of a point in an Excel sunburst chart through Apache POI. In my answer there was told already that sunburst chart is of type application/vnd.ms-office.chartex+xml and so cannot be a XSSFChartas this is of type application/vnd.openxmlformats-officedocument.drawingml.chart+xml. My answer there also provides a method to nevertheless get the XML of the sunburst chart in read and write mode.

但是,当然,如果需要更改 XML ,则必须知道 XML 的含义.可以从 2.24.3.11 CT_ChartSpace .但是我的方法如下:

But of course if one needs changing the XML one must know about what the XML is about. One could read Microsofts documentation starting with 2.24.3.11 CT_ChartSpace. But my approach is the following:

*.xlsx 文件只不过是 ZIP 存档.因此,我使用 Excel 创建了一个简单的森伯斯特图表,并将其保存在 *.xlsx 文件中.然后我解压缩该 *.xlsx 文件,并查看/xl/charts/chartEx1.xml .在那里,我可以看到使用的 XML .现在,我使用 Excel 进一步格式化旭日形图,保存并查看/xl/charts/chartEx1.xml 中的 XML 已经改变.因此,我可以确定所使用的 XML 的含义.

The *.xlsx file is nothing else than a ZIP archive. So I create a simple sunburst chart using Excel and save this in a *.xlsx file. Then I unzip that *.xlsx file and have a look at /xl/charts/chartEx1.xml. There I can see the XML used. Now I do additional formatting the sunburst chart using Excel, save and have a look at how the XML in /xl/charts/chartEx1.xml has changed. So I can determine the meaning of the used XML.

使用这种方法,我得出的结论是,每个单独的数据标签都可以使用< cx:dataLabel idx ="0"> 进行格式化,其中 idx 与数据点 idx 相同.格式是在< cx:txPr> 中完成的,其中包含段落< a:p> 和文本行< a:r> 然后进行格式化.

Using this approach I come to the conclusion that each single data label can be formatted using a <cx:dataLabel idx="0"> where the idx is the same as the data point idx. The formatting is done in <cx:txPr> which contains paragraph <a:p> and text run <a:r> which is formatted then.

知道了这一点,我们来看看以下方法:

Knowing this, we come to following method:

...
 private static void setDataLabelFontSettings(XmlObject series, int number,
  int fontSizePt, boolean b, boolean i, String u, String strike, String typeface, String colorHex) {

  XmlObject[] xmlObjects = series.selectPath(
   "declare namespace cx='http://schemas.microsoft.com/office/drawing/2014/chartex' " +
   ".//cx:dataLabels");
  if (xmlObjects.length == 1) {
   XmlObject dataLabels = xmlObjects[0];
   XmlCursor cursor = dataLabels.newCursor();
   cursor.toLastChild();
   cursor.beginElement(new QName("http://schemas.microsoft.com/office/drawing/2014/chartex", "dataLabel", "cx"));
   cursor.insertAttributeWithValue("idx", "" + number);

   cursor.beginElement(new QName("http://schemas.microsoft.com/office/drawing/2014/chartex", "txPr", "cx"));

   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "p", "a"));

   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "r", "a"));

   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "t", "a"));
   cursor.toParent();
   cursor.setTextValue("dummy");

   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "rPr", "a"));
   cursor.insertAttributeWithValue("sz", "" + (fontSizePt * 100));
   cursor.insertAttributeWithValue("b", ((b)?"1":"0"));
   cursor.insertAttributeWithValue("i", ((i)?"1":"0"));
   cursor.insertAttributeWithValue("u", u);
   cursor.insertAttributeWithValue("strike", strike);
   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "latin", "a"));
   cursor.insertAttributeWithValue("typeface", typeface);
   cursor.toParent();
   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "solidFill", "a"));
   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "srgbClr", "a"));
   cursor.insertAttributeWithValue("val", colorHex);

   cursor.toParent();
   cursor.toParent();
   cursor.toParent();
   cursor.toParent();
   cursor.toParent();
   cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "bodyPr", "a"));

   cursor.dispose();
  }
 }
...

可以在我的代码中在上面相关链接的Q/A中使用,如下所示:

which can be used in my code in the related Q/A linked above as follows:

...
      //setDataLabelFontSettings(XmlObject series, int number, int fontSizePt, boolean b, boolean i, 
      // String u, String strike, String typeface, String colorHex)
      setDataLabelFontSettings(series, 0, 14, false, true, "sng", "noStrike", "Calibri", "FFFFFF");
      setDataLabelFontSettings(series, 4, 12, false, true, "none", "sngStrike", "Calibri", "FFFFFF");
      setDataLabelFontSettings(series, 5, 8, false, true, "dbl", "noStrike", "Calibri", "FFFFFF");
      setDataLabelFontSettings(series, 6, 8, false, true, "none", "dblStrike", "Calibri", "FFFFFF");
...

这篇关于如何通过Apache POI在Excel Sunburst Chart中设置各个数据标签的文本属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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