[]
        
(Showing Draft Content)

轴线和其他线

轴是指一个图表元素,用于显示绘图区域单个维度的比例。

使用GcExcel Java,可以为图表中的轴和其他线配置标题、主刻度线、次刻度线、刻度线标签、主网格线和次网格线。

通常,二维图表包含两种类型的轴—类别轴和值轴。分类轴也称为水平轴(x轴),可用于表示参数。值轴也称为纵轴(y轴),它表示工作表中行和列的数据值。


但是,在三维图表中,除了水平轴和垂直轴之外,还有一个轴。此轴称为系列轴。三维图表可以有以下三种类型的轴:

  1. 分类轴-在水平轴上显示所有类型图表的分类。例外情况是条形图,其中类别沿y轴(即纵轴)显示。

  2. 值轴-以垂直轴显示系列值。条形图除外,其中系列值沿x轴(即水平轴)显示。

  3. 系列轴-显示三维图表的数据系列,包括三维柱形图、三维面积图、三维折线图和曲面图。

可以使用 IAxis 接口的方法来配置图表中的分类轴、值轴和系列轴。


要在图表中配置轴,请参阅以下示例代码。

// Use IAxis.CategoryType to set category axis's scale type
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);
worksheet.getRange("A2:A6").setNumberFormat("m/d/yyyy");
IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);

// Category axis's category type is automatic scale
category_axis.setCategoryType(CategoryType.AutomaticScale);

// Category axis's actual category type is time scale
CategoryType actualcategorytype = category_axis.getActualCategoryType();

IWorksheet worksheet1 = workbook.getWorksheets().add();
worksheet1.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 } 
});
IShape shape2 = worksheet1.getShapes().addChart(ChartType.Line, 250, 20, 360, 230);
shape2.getChart().getSeriesCollection().add(worksheet1.getRange("A1:D6"), RowCol.Columns, true, true);
IAxis category_axis1 = shape2.getChart().getAxes().item(AxisType.Category);

// Set category axis's format.
category_axis1.getFormat().getFill().getColor().setObjectThemeColor(ThemeColor.Accent1);
category_axis1.getFormat().getLine().getColor().setRGB(Color.GetLightSkyBlue());
category_axis1.getFormat().getLine().setWeight(3);
category_axis1.getFormat().getLine().setStyle(LineStyle.Single);

IAxis value_axis = shape2.getChart().getAxes().item(AxisType.Value);

// Set value axis's format.
value_axis.getFormat().getLine().getColor().setRGB(Color.FromArgb(91, 155, 213));
value_axis.getFormat().getLine().setWeight(2);
value_axis.getFormat().getLine().setStyle(LineStyle.Single);

// Configure time scale category axis's units.
worksheet1.getRange("A8:A12").setNumberFormat("m/d/yyyy");
worksheet1.getRange("A7:D12").setValue(new Object[][]
{
    {null, "S1", "S2", "S3"},
    {new GregorianCalendar(2015, 9, 7), 10, 25, 25},
    {new GregorianCalendar(2015, 9, 24), 51, 36, 27},
    {new GregorianCalendar(2015, 10, 8), 52, 85, 30},
    {new GregorianCalendar(2015, 10, 25), 22, 65, 65},
    {new GregorianCalendar(2015, 11, 10), 23, 69, 69}
});

IShape shape3 = worksheet1.getShapes().addChart(ChartType.ColumnClustered, 200, 450, 300, 300);
shape3.getChart().getSeriesCollection().add(worksheet1.getRange("A7:D12"), RowCol.Columns, true, true);
IAxis category_axis2 = shape3.getChart().getAxes().item(AxisType.Category);

category_axis2.setMaximumScale(DateInfo.ToOADate(new GregorianCalendar(2019, 9, 1)));
category_axis2.setMinimumScale(DateInfo.ToOADate(new GregorianCalendar(2015, 9, 1)));

category_axis2.setBaseUnit(TimeUnit.Years);
category_axis2.setMajorUnitScale(TimeUnit.Months);
category_axis2.setMajorUnit(4);
category_axis2.setMinorUnitScale(TimeUnit.Days);
category_axis2.setMinorUnit(60);

// Configure value axis's units.
IShape shape4 = 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, 90, -30 }, 
{ "Item4", 22, 65, 50 }, 
{ "Item5", 23, 55, 69 } 
});
shape4.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
IAxis value_axis1 = shape4.getChart().getAxes().item(AxisType.Value);
value_axis1.setMaximumScale(100);
value_axis1.setMinimumScale(-100);
value_axis1.setMajorUnit(30);
value_axis1.setMinorUnit(6);
 
// Set axis crosses at.
IAxis value_axis_cross = shape.getChart().getAxes().item(AxisType.Value);
value_axis_cross.setCrosses(AxisCrosses.Maximum);

// Set axis's scale type.
IAxis value_axis_scale = shape.getChart().getAxes().item(AxisType.Value);
value_axis_scale.setScaleType(ScaleType.Logarithmic);
value_axis_scale.setLogBase(5);

// Set axis's tick mark.
IAxis category_axis_tick = shape.getChart().getAxes().item(AxisType.Category);
category_axis_tick.getFormat().getLine().getColor().setRGB(Color.GetGreen());
category_axis_tick.setMajorTickMark(TickMark.Inside);
category_axis_tick.setMinorTickMark(TickMark.Cross);
category_axis_tick.setTickMarkSpacing(2);

// Configure axis title
category_axis.setHasTitle(true);
category_axis.getAxisTitle().setText("CategoryAxisTitle");
category_axis.getAxisTitle().getFont().setSize(18);
category_axis.getAxisTitle().getFont().getColor().setRGB(Color.GetOrange());