使用 Python 在演示文稿中自定义图表坐标轴

概述

本文介绍了如何在 Aspose.Slides 中自定义图表坐标轴。它展示了如何获取实际坐标轴值、在坐标轴之间交换数据、隐藏折线图的垂直或水平坐标轴、更改类别坐标轴类型、设置类别坐标轴值的日期格式、旋转坐标轴标题、设置坐标轴位置以及设置值坐标轴的显示单位。

获取图表垂直坐标轴的最大值

Aspose.Slides for Python via Java 允许您获取垂直坐标轴的最小值和最大值。请按以下步骤操作:

  1. 创建 Presentation 类的实例。
  2. 访问第一张幻灯片。
  3. 添加一个含默认数据的图表。
  4. 获取坐标轴的实际最大值。
  5. 获取坐标轴的实际最小值。
  6. 获取坐标轴的实际主单位。
  7. 获取坐标轴的实际次单位。
  8. 获取坐标轴的实际主单位比例。
  9. 获取坐标轴的实际次单位比例。

此示例代码——上述步骤的实现——展示了如何在 Python 中获取所需的值:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat

presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.Area, 100, 100, 500, 350)
    chart.validateChartLayout()

    max_value = chart.getAxes().getVerticalAxis().getActualMaxValue()
    min_value = chart.getAxes().getVerticalAxis().getActualMinValue()

    major_unit = chart.getAxes().getVerticalAxis().getActualMajorUnit()
    minor_unit = chart.getAxes().getVerticalAxis().getActualMinorUnit()

    major_unit_scale = chart.getAxes().getVerticalAxis().getActualMajorUnitScale()
    minor_unit_scale = chart.getAxes().getVerticalAxis().getActualMinorUnitScale()

    # 保存演示文稿
    presentation.save("MaxValuesVerticalAxis_out.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

在坐标轴之间交换数据

Aspose.Slides 允许您快速在坐标轴之间交换数据——垂直坐标轴(y 轴)上的数据移动到水平坐标轴(x 轴),反之亦然。

下面的 Python 代码演示了在图表坐标轴之间执行数据交换的操作:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat

presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 400, 300)

    # 将图表的默认数据加载到工作簿 — switchRowColumn 会转置工作簿,因此必须先填充它
    workbook = chart.getChartData().getChartDataWorkbook()

    # 交换行和列
    chart.getChartData().switchRowColumn()

    # 保存演示文稿
    presentation.save("SwitchChartRowColumns_out.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

禁用折线图的垂直坐标轴

下面的 Python 代码演示了如何隐藏折线图的垂直坐标轴:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat

presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.Line, 100, 100, 400, 300)
    chart.getAxes().getVerticalAxis().setVisible(False)

    presentation.save("chart.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

禁用折线图的水平坐标轴

下面的代码演示了如何隐藏折线图的水平坐标轴:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat

presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.Line, 100, 100, 400, 300)
    chart.getAxes().getHorizontalAxis().setVisible(False)

    presentation.save("chart.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

更改类别坐标轴

使用 setCategoryAxisType 方法,您可以指定首选的类别坐标轴类型(datetext)。以下 Python 代码演示了此操作:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, Chart, SaveFormat, CategoryAxisType, TimeUnitType

presentation = Presentation("ExistingChart.pptx")
try:
    if presentation.getSlides().size() > 0 and presentation.getSlides().get_Item(0).getShapes().size() > 0:
        chart = presentation.getSlides().get_Item(0).getShapes().get_Item(0)
        if isinstance(chart, Chart):
            chart.getAxes().getHorizontalAxis().setCategoryAxisType(CategoryAxisType.Date)
            chart.getAxes().getHorizontalAxis().setAutomaticMajorUnit(False)
            chart.getAxes().getHorizontalAxis().setMajorUnit(1)
            chart.getAxes().getHorizontalAxis().setMajorUnitScale(TimeUnitType.Months)
            presentation.save("ChangeChartCategoryAxis_out.pptx", SaveFormat.Pptx)
        else:
            print("The first shape is not a chart.")
    else:
        print("The presentation has no first shape to update.")
finally:
    presentation.dispose()

为类别坐标轴值设置日期格式

Aspose.Slides for Python via Java 允许您为类别坐标轴值设置日期格式。以下 Python 代码演示了该操作:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat, CategoryAxisType

from datetime import datetime

def convert_to_oa_date(date):
    base_date = datetime(1899, 12, 30)
    return (date - base_date).total_seconds() / 86400


presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.Area, 50, 50, 450, 300)

    workbook = chart.getChartData().getChartDataWorkbook()
    workbook.clear(0)

    chart.getChartData().getCategories().clear()
    chart.getChartData().getSeries().clear()
    category_date = datetime(2015, 2, 1)
    category_value = convert_to_oa_date(category_date)
    category_cell = workbook.getCell(0, "A2", category_value)
    chart.getChartData().getCategories().add(category_cell)
    category_date = datetime(2016, 2, 1)
    category_value = convert_to_oa_date(category_date)
    category_cell = workbook.getCell(0, "A3", category_value)
    chart.getChartData().getCategories().add(category_cell)
    category_date = datetime(2017, 2, 1)
    category_value = convert_to_oa_date(category_date)
    category_cell = workbook.getCell(0, "A4", category_value)
    chart.getChartData().getCategories().add(category_cell)
    category_date = datetime(2018, 2, 1)
    category_value = convert_to_oa_date(category_date)
    category_cell = workbook.getCell(0, "A5", category_value)
    chart.getChartData().getCategories().add(category_cell)

    series = chart.getChartData().getSeries().add(ChartType.Line)
    value_cell = workbook.getCell(0, "B2", 1.0)
    series.getDataPoints().addDataPointForLineSeries(value_cell)
    value_cell = workbook.getCell(0, "B3", 2.0)
    series.getDataPoints().addDataPointForLineSeries(value_cell)
    value_cell = workbook.getCell(0, "B4", 3.0)
    series.getDataPoints().addDataPointForLineSeries(value_cell)
    value_cell = workbook.getCell(0, "B5", 4.0)
    series.getDataPoints().addDataPointForLineSeries(value_cell)
    chart.getAxes().getHorizontalAxis().setCategoryAxisType(CategoryAxisType.Date)
    chart.getAxes().getHorizontalAxis().setNumberFormatLinkedToSource(False)
    chart.getAxes().getHorizontalAxis().setNumberFormat("yyyy")

    presentation.save("output.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

为图表坐标轴标题设置旋转角度

Aspose.Slides for Python via Java 允许您为图表坐标轴标题设置旋转角度。以下 Python 代码演示了该操作:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat

presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 450, 300)

    chart.getAxes().getVerticalAxis().setTitle(True)
    chart.getAxes().getVerticalAxis().getTitle().getTextFormat().getTextBlockFormat().setRotationAngle(90)

    presentation.save("output.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

在类别或数值坐标轴上设置坐标轴位置

Aspose.Slides for Python via Java 允许您在类别或数值坐标轴上设置坐标轴位置。以下 Python 代码展示了如何完成此任务:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat

presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 450, 300)

    chart.getAxes().getHorizontalAxis().setAxisBetweenCategories(True)

    presentation.save("output.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

为图表数值坐标轴设置显示单位

Aspose.Slides for Python via Java 允许您为图表数值坐标轴设置显示单位。然后坐标轴会按该单位对刻度标签进行缩放:使用 DisplayUnitType.Millions 时,最高到 60,000,000 的坐标轴会标记为 0 到 60。以下 Python 代码演示了该操作:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ChartType, SaveFormat, DisplayUnitType

presentation = Presentation()
try:
    chart = presentation.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 450, 300)

    chart.getAxes().getVerticalAxis().setDisplayUnit(DisplayUnitType.Millions)

    presentation.save("output.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

常见问题

如何设置一个坐标轴与另一个坐标轴的交叉值(坐标轴交叉)?

坐标轴提供了 crossing setting:您可以选择在零点、最大类别/数值或特定数值处交叉。这对于将 X 轴上移或下移或突出基线非常有用。

如何相对于坐标轴定位刻度线(交叉、外部、内部)?

tick mark position 设置为 “cross”、 “outside” 或 “inside”。这会影响可读性,并有助于节省空间,尤其是在小型图表中。