Sesuaikan Sumbu Diagram dalam Presentasi Menggunakan Python
Overview
Artikel ini menjelaskan cara menyesuaikan sumbu diagram di Aspose.Slides. Menampilkan cara mendapatkan nilai sumbu aktual, menukar data antara sumbu, menyembunyikan sumbu vertikal atau horizontal untuk diagram garis, mengubah jenis sumbu kategori, mengatur format tanggal untuk nilai sumbu kategori, memutar judul sumbu, mengatur posisi sumbu, dan mengatur unit tampilan sumbu nilai.
Get the Maximum Values on the Vertical Axis of a Chart
Aspose.Slides for Python via Java memungkinkan Anda memperoleh nilai minimum dan maksimum pada sumbu vertikal. Ikuti langkah-langkah berikut:
- Buat instance dari kelas Presentation.
- Akses slide pertama.
- Tambahkan diagram dengan data default.
- Dapatkan nilai maksimum aktual pada sumbu.
- Dapatkan nilai minimum aktual pada sumbu.
- Dapatkan satuan mayor aktual pada sumbu.
- Dapatkan satuan minor aktual pada sumbu.
- Dapatkan skala satuan mayor aktual pada sumbu.
- Dapatkan skala satuan minor aktual pada sumbu.
Kode contoh—implementasi langkah-langkah di atas—menunjukkan cara mendapatkan nilai yang diperlukan dalam 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()
# Menyimpan presentasi
presentation.save("MaxValuesVerticalAxis_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Swap the Data between Axes
Aspose.Slides memungkinkan Anda dengan cepat menukar data antara sumbu—data yang ditampilkan pada sumbu vertikal (sumbu y) dipindahkan ke sumbu horizontal (sumbu x) dan sebaliknya.
Kode Python ini menunjukkan cara melakukan penukaran data antara sumbu pada diagram:
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)
# Memuat data default diagram ke dalam workbook — switchRowColumn mentransposisi workbook,
# jadi harus diisi terlebih dahulu
workbook = chart.getChartData().getChartDataWorkbook()
# Menukar baris dan kolom
chart.getChartData().switchRowColumn()
# Menyimpan presentasi
presentation.save("SwitchChartRowColumns_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Disable the Vertical Axis for Line Charts
Kode Python ini menunjukkan cara menyembunyikan sumbu vertikal untuk diagram garis:
import jpide
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()
Disable the Horizontal Axis for Line Charts
Kode ini menunjukkan cara menyembunyikan sumbu horizontal untuk diagram garis:
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()
Change a Category Axis
Dengan menggunakan metode setCategoryAxisType Anda dapat menentukan jenis sumbu kategori yang diinginkan (date atau text). Kode berikut dalam Python mendemonstrasikan operasi tersebut:
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()
Set the Date Format for Category Axis Values
Aspose.Slides for Python via Java memungkinkan Anda mengatur format tanggal untuk nilai sumbu kategori. Operasi ini ditunjukkan dalam kode Python berikut:
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()
Set a Rotation Angle for a Chart Axis Title
Aspose.Slides for Python via Java memungkinkan Anda mengatur sudut rotasi untuk judul sumbu diagram. Kode Python ini mendemonstrasikan operasi tersebut:
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()
Set the Axis Position on a Category or Value Axis
Aspose.Slides for Python via Java memungkinkan Anda mengatur posisi sumbu pada sumbu kategori atau nilai. Kode Python ini menunjukkan cara melakukan tugas tersebut:
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()
Set the Display Unit on a Chart Value Axis
Aspose.Slides for Python via Java memungkinkan Anda mengatur unit tampilan sumbu nilai diagram. Sumbu kemudian menskalakan label tick‑nya dengan unit tersebut: dengan DisplayUnitType.Millions, sebuah sumbu yang berjalan sampai 60.000.000 diberi label 0 hingga 60. Kode Python ini mendemonstrasikan operasi tersebut:
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()
FAQ
How do I set the value at which one axis crosses the other (axis crossing)?
Sumbu menyediakan crossing setting: Anda dapat memilih untuk memotong di nol, pada kategori/nilai maksimum, atau pada nilai numerik tertentu. Ini berguna untuk menggeser sumbu X ke atas atau ke bawah atau untuk menekankan baseline.
How can I position tick marks relative to the axis (crossing, outside, inside)?
Atur tick mark position ke “cross”, “outside”, atau “inside”. Ini memengaruhi keterbacaan dan membantu menghemat ruang, terutama pada diagram kecil.