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

获取图表垂直轴的最大值

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

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

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

import aspose.slides.charts as charts
import aspose.slides as slides

with slides.Presentation() as pres:
	chart = pres.slides[0].shapes.add_chart(charts.ChartType.AREA, 100, 100, 500, 350)
	chart.validate_chart_layout()

	maxValue = chart.axes.vertical_axis.actual_max_value
	minValue = chart.axes.vertical_axis.actual_min_value

	majorUnit = chart.axes.horizontal_axis.actual_major_unit
	minorUnit = chart.axes.horizontal_axis.actual_minor_unit
	
	# 保存演示文稿
	pres.save("ErrorBars_out.pptx", slides.export.SaveFormat.PPTX)

在轴之间交换数据

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

此 Python 代码演示了如何在图表上执行轴之间的数据交换任务:

import aspose.slides.charts as charts
import aspose.slides as slides

# 创建空白演示文稿
with slides.Presentation() as pres:
    chart = pres.slides[0].shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 100, 100, 400, 300)

    #切换行和列
    chart.chart_data.switch_row_column()
            
    # 保存演示文稿
    pres.save("SwitchChartRowColumns_out.pptx", slides.export.SaveFormat.PPTX)

禁用折线图的垂直轴

此 Python 代码展示了如何隐藏折线图的垂直轴:

import aspose.slides.charts as charts
import aspose.slides as slides

with slides.Presentation() as pres:
    chart = pres.slides[0].shapes.add_chart(charts.ChartType.LINE, 100, 100, 400, 300)
    chart.axes.vertical_axis.is_visible = False
    
    pres.save("chart-is_visible.pptx", slides.export.SaveFormat.PPTX)

禁用折线图的水平轴

此代码展示了如何隐藏折线图的水平轴:

import aspose.slides.charts as charts
import aspose.slides as slides
 
with slides.Presentation() as pres:
    chart = pres.slides[0].shapes.add_chart(charts.ChartType.LINE, 100, 100, 400, 300)
    chart.axes.horizontal_axis.is_visible = False

    pres.save("chart-2.pptx", slides.export.SaveFormat.PPTX)

更改分类轴

使用 CategoryAxisType 属性,您可以指定首选的分类轴类型(datetext)。以下 Python 代码演示了该操作:

import aspose.slides.charts as charts
import aspose.slides as slides

with slides.Presentation(path + "ExistingChart.pptx") as presentation:
    chart = presentation.slides[0].shapes[0]
    chart.axes.horizontal_axis.category_axis_type = charts.CategoryAxisType.DATE
    chart.axes.horizontal_axis.is_automatic_major_unit = False
    chart.axes.horizontal_axis.major_unit = 1
    chart.axes.horizontal_axis.major_unit_scale = charts.TimeUnitType.MONTHS
    presentation.save("ChangeChartCategoryAxis_out.pptx", slides.export.SaveFormat.PPTX)

设置分类轴值的日期格式

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

import aspose.slides.charts as charts
import aspose.slides as slides
from datetime import date

def to_oadate(dt):
    delta = dt - date(1899, 12, 30)
    return delta.days + (delta.seconds + delta.microseconds / 1e6) / (24 * 3600)

with slides.Presentation() as pres:
    chart = pres.slides[0].shapes.add_chart(charts.ChartType.AREA, 50, 50, 450, 300)

    wb = chart.chart_data.chart_data_workbook

    wb.clear(0)

    chart.chart_data.categories.clear()
    chart.chart_data.series.clear()

    chart.chart_data.categories.add(wb.get_cell(0, "A2", to_oadate(date(2015, 1, 1))))
    chart.chart_data.categories.add(wb.get_cell(0, "A3", to_oadate(date(2016, 1, 1))))
    chart.chart_data.categories.add(wb.get_cell(0, "A4", to_oadate(date(2017, 1, 1))))
    chart.chart_data.categories.add(wb.get_cell(0, "A5", to_oadate(date(2018, 1, 1))))

    series = chart.chart_data.series.add(charts.ChartType.LINE)
    series.data_points.add_data_point_for_line_series(wb.get_cell(0, "B2", 1))
    series.data_points.add_data_point_for_line_series(wb.get_cell(0, "B3", 2))
    series.data_points.add_data_point_for_line_series(wb.get_cell(0, "B4", 3))
    series.data_points.add_data_point_for_line_series(wb.get_cell(0, "B5", 4))
    chart.axes.horizontal_axis.category_axis_type = charts.CategoryAxisType.DATE
    chart.axes.horizontal_axis.is_number_format_linked_to_source = False
    chart.axes.horizontal_axis.number_format = "yyyy"
    pres.save("test.pptx", slides.export.SaveFormat.PPTX)

设置图表轴标题的旋转角度

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

import aspose.slides.charts as charts
import aspose.slides as slides

with slides.Presentation() as pres:
    chart = pres.slides[0].shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 50, 50, 450, 300)
    chart.axes.vertical_axis.has_title = True
    chart.axes.vertical_axis.title.text_format.text_block_format.rotation_angle = 90

    pres.save("test.pptx", slides.export.SaveFormat.PPTX)

在分类轴或值轴中设置位置轴

Aspose.Slides for Python via .NET 允许您在分类轴或值轴中设置位置轴。以下 Python 代码展示了如何完成该任务:

import aspose.slides.charts as charts
import aspose.slides as slides

with slides.Presentation() as pres:
	chart = pres.slides[0].shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 50, 50, 450, 300)
	chart.axes.horizontal_axis.axis_between_categories = True

	pres.save("AsposeScatterChart.pptx", slides.export.SaveFormat.PPTX)

在图表数值轴上启用显示单位标签

Aspose.Slides for Python via .NET 允许您配置图表,在其数值轴上显示单位标签。以下 Python 代码演示了该操作:

import aspose.slides.charts as charts
import aspose.slides as slides

with slides.Presentation() as pres:
	chart = pres.slides[0].shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 50, 50, 450, 300)
	chart.axes.vertical_axis.display_unit = charts.DisplayUnitType.MILLIONS
	pres.save("Result.pptx", slides.export.SaveFormat.PPTX)

常见问题

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

轴提供了一个 crossing setting:您可以选择在零点、最大分类/数值处或特定数值处交叉。这对于上下移动 X 轴或强调基准线非常有用。

如何相对于轴定位刻度标签(旁边、外部、内部)?

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