[]
        
(Showing Draft Content)

配置图表轴

GcExcel Java提供了几个选项,可以帮助您配置图表轴。

插入表单的图表中的以下轴元素是可自定义的:

轴标题

用户可以使用IAxis 接口的  getAxisTitle 为轴标题设置自定义样式。

要配置图表轴标题的布局,请参阅以下示例代码。

IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
worksheet.getRange("A1:D6").setValue(new Object[][] { { null, "S1", "S2", "S3" }, { "Item1", 10, 25, 25 },
        { "Item2", 51, 36, 27 }, { "Item3", 52, 85, 30 }, { "Item4", 22, 65, 65 }, { "Item5", 23, 69, 69 } });
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
        
IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);
category_axis.setHasTitle(true);
category_axis.getAxisTitle().setText("CategoryAxisTitle");
category_axis.getAxisTitle().getFont().setSize(18);
category_axis.getAxisTitle().getFont().getColor().setRGB(Color.GetOrange());

您还可以将轴标题的方向设置为水平、垂直、旋转(90度或270度)以及堆叠(从左到右或从右到左读取文本)。IAxisTitle和IAxisTitle.ITextFrame接口中的setDirection方法允许您使用TextDirection枚举设置轴标题的方向。

请参考以下示例代码将轴标题方向设置为堆叠(stacked):

// 创建图表。
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);

// 显示轴标题。
category_axis.setHasTitle(true);

// 设置轴标题名称。
category_axis.getAxisTitle().setText("Category");

// 将轴标题方向设置为堆叠方向。
category_axis.getAxisTitle().getTextFrame().setDirection(TextDirection.Stacked);

// 或者
category_axis.getAxisTitle().setDirection(TextDirection.Stacked);

GcExcel还允许您通过使用IAxisTitle接口的setOrientation 方法来配置轴标题的文本角度。

请参考以下示例代码设置轴标题的文本角度:

// 创建图表。
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);

// 显示轴标题。
category_axis.setHasTitle(true);

// 设置轴标题名称。
category_axis.getAxisTitle().setText("Category");

// 将轴标题的方向设置为45度。
category_axis.getAxisTitle().setOrientation(45);

轴标题的方向和文本角度也可以导出或导入到JSON或PDF文档中。

type=warning

注意: 只有在setDirection方法的值为Horizontal时,setOrientation方法才适用。

网格线

用户还可以使用 IAxis 接口的  getMajorGridlines 方法, getMinorGridlines 方法, setHasMajorGridlines 方法和 setHasMinorGridlines方法自定义图表轴中主网格线和次网格线的样式。


要设置主要网格线和次要网格线的样式,请参阅以下示例代码。

IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
worksheet.getRange("A1:D6").setValue(
        new Object[][] { { null, "S1", "S2", "S3" }, { "Item1", 10, 25, 25 }, { "Item2", -51, -36, 27 },
                { "Item3", 52, -85, -30 }, { "Item4", 22, 65, 65 }, { "Item5", 23, 69, 69 } });
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

IAxis value_axis = shape.getChart().getAxes().item(AxisType.Value);
value_axis.setHasMajorGridlines(true);
value_axis.setHasMinorGridlines(true);
value_axis.getMajorGridlines().getFormat().getLine().getColor().setRGB(Color.GetGray());
value_axis.getMajorGridlines().getFormat().getLine().setWeight(1);
value_axis.getMinorGridlines().getFormat().getLine().getColor().setRGB(Color.GetLightGray());
value_axis.getMinorGridlines().getFormat().getLine().setWeight(0.75);
value_axis.setMajorUnit(40);
value_axis.setMinorUnit(8);
value_axis.getMinorGridlines().getFormat().getLine().setStyle(LineStyle.ThickThin);

显示单元标签

用户可以通过 IAxis 接口的  setDisplayUnit 方法, getDisplayUnitLabel 方法, setDisplayUnitCustom 方法和 setHasDisplayUnitLabel 方法配置轴的显示单元及其标签样式,自定义图表轴上的显示单元标签。


要配置轴的显示单位并设置自定义标签样式,请参阅以下示例代码。

IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
worksheet.getRange("A1:D6").setValue(
        new Object[][] { { null, "S1", "S2", "S3" }, { "Item1", 10, 25, 25 }, { "Item2", -51, -36, 27 },
                { "Item3", 52, -85, -30 }, { "Item4", 22, 65, 65 }, { "Item5", 23, 69, 69 } });
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

IAxis value_axis = shape.getChart().getAxes().item(AxisType.Value);
value_axis.setDisplayUnit(DisplayUnit.Custom);
value_axis.setDisplayUnitCustom(100);
value_axis.setHasDisplayUnitLabel(true);
value_axis.getDisplayUnitLabel().getFont().getColor().setRGB(Color.GetCornflowerBlue());
value_axis.getDisplayUnitLabel().getFormat().getFill().getColor().setRGB(Color.GetOrange());
value_axis.getDisplayUnitLabel().getFormat().getLine().getColor().setRGB(Color.GetCornflowerBlue());

刻度标签

用户可以使用 IAxis 接口的  setTickLabelPosition 方法, getTickLabels 方法, setTickLabelSpacing 方法配置刻度线标签的位置和布局,自定义图表轴上的刻度线标签。

请参阅以下示例代码以配置位置标签的位置和布局。

shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
        
IAxis value_axis = shape.getChart().getAxes().item(AxisType.Value);
IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);

category_axis.setTickLabelPosition(TickLabelPosition.NextToAxis);
category_axis.setTickLabelSpacing(2);
category_axis.getTickLabels().getFont().getColor().setRGB(Color.GetDarkOrange());
category_axis.getTickLabels().getFont().setSize(12);
category_axis.getTickLabels().setNumberFormat("#,##0.00");
value_axis.getTickLabels().setNumberFormat("#,##0;[Red]#,##0");

您还可以将刻度标签的方向设置为水平、垂直、旋转(90度或270度)和堆叠(从左到右或从右到左阅读文本)。ITickLabels 接口中的 setDirection 方法允许您使用 TextDirection 枚举设置刻度标签的方向。

请参考以下示例代码以设置垂直刻度标签:

// 创建图表。
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

// 设置分类轴。
var categoryAxis = shape.getChart().getAxes().item(AxisType.Category);

// 将分类刻度标签设置为垂直方向。
categoryAxis.getTickLabels().setDirection(TextDirection.Vertical);

GcExcel还允许您使用ITickLabels接口的setOrientation方法来配置刻度标签的文本角度。

请参考以下示例代码来设置刻度标签的文本角度:

shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);
    
// 配置刻度标签的角度
category_axis.getTickLabels().setOrientation(45);

// 保存为Excel文件
workbook.save("configtickmarklabelangle.xlsx");

刻度标签的方向(Direction)和角度(Orientation)也可以导出或导入到JSON或PDF文档中。

type=warning

注意: setOrientation 方法仅在 setDirection 方法的值为 Horizontal 时才适用。