可以为CTLegend设置特定的X,Y位置

时间:2018-04-02 16:58:17

标签: java charts apache-poi legend

我正在使用apache POI和OpenOffice库来创建条形图和折线图。我可以让组合条和折线图工作正常,并创造一个很好的传奇。但是我想使用STLegendPos(.L,.B,.R,.T,。TR)的枚举值之外的东西来定位它。我生成的电子表格包含大量带有图表的图纸,图例覆盖了Y轴。我将addNewOverlay()设置为true,因为我需要它在图表的顶部,但我只是想以编程方式向右移动一点而不需要用户进入并移动大约70个图例

我添加图表图例的代码如下:

private static void addChartLegend(CTChart ctChart) {
    // Define legends for the chart and set the position of the legend
    CTLegend    ctLegend = ctChart.addNewLegend();
    ctLegend.addNewLegendPos().setVal(STLegendPos.L);

    // Set border color
    ctLegend.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)0, (byte)0, (byte)0});

    // Set fill color
    ctLegend.getSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)255, (byte)255, (byte)255});
    ctLegend.addNewOverlay().setVal(true);  // true overlays it on top of chart; false creates it's own space
}

我试图找到一个没有运气设置确切位置的例子。

感谢您提供的任何帮助。 杰夫

1 个答案:

答案 0 :(得分:0)

如果您已经有CTLegend,那么您可以使用CTLegend.addNewLayout申请CTLayout。然后,必须使用CTLayout.addNewManualLayout应用CTManualLayout。然后在CTManualLayout中,必须设置CTLayoutMode s XModeYMode,然后设置双值XY

主要问题是获取信息XModeYMode以及XY的含义。为此,请参阅https://msdn.microsoft.com/en-us/library/ff534056(v=office.12).aspx

“对于 xMode / yMode 元素, edge 属性指定同胞元素 x / y 的值被解释为距离图表元素的左/上边缘从图表的左/上边缘作为图表宽度/高度的百分比。“

代码示例:

    //legend
    CTLegend ctLegend = ctChart.addNewLegend();
    ctLegend.addNewLegendPos().setVal(STLegendPos.L);
    ctLegend.addNewOverlay().setVal(false);
    CTManualLayout ctManualLayout = ctLegend.addNewLayout().addNewManualLayout();
    ctManualLayout.addNewXMode().setVal(STLayoutMode.EDGE);
    ctManualLayout.addNewYMode().setVal(STLayoutMode.EDGE);
    ctManualLayout.addNewX().setVal(0.00); //left edge of the chart
    ctManualLayout.addNewY().setVal(0.25); //25% of chart's height from top edge of the chart