สร้างหรืออัปเดตแผนภูมิการนำเสนอ PowerPoint ใน Python
ภาพรวม
บทความนี้ให้คำแนะนำอย่างครบถ้วนเกี่ยวกับวิธีสร้างและปรับแต่งแผนภูมิด้วย Aspose.Slides for Python via Java คุณจะได้เรียนรู้วิธีการเพิ่มแผนภูมิลงในสไลด์โดยโปรแกรม, เติมข้อมูลลงในแผนภูมิ, และใช้ตัวเลือกการจัดรูปแบบต่าง ๆ เพื่อให้ตรงกับความต้องการในการออกแบบของคุณ ตลอดบทความ จะมีตัวอย่างโค้ดอย่างละเอียดแสดงขั้นตอนแต่ละขั้นตอน ตั้งแต่การเริ่มต้น Presentation และอ็อบเจกต์แผนภูมิ ไปจนถึงการกำหนด Series, Axis, และ Legend การทำตามคำแนะนำนี้ คุณจะเข้าใจวิธีการรวมการสร้างแผนภูมิดิจิทัลลงในแอปพลิเคชัน Python ของคุณได้อย่างมั่นใจ ทำให้การสร้างงานนำเสนอที่อิงข้อมูลเป็นเรื่องง่ายและมีประสิทธิภาพ
สร้างแผนภูมิ
แผนภูมิช่วยให้ผู้ใช้มองเห็นข้อมูลได้อย่างรวดเร็วและสังเกตแนวคิดที่อาจไม่ชัดเจนจากตารางหรือสเปรดชีต
ทำไมต้องสร้างแผนภูมิ?
การใช้แผนภูมิ คุณสามารถ:
- รวม, ย่อ, หรือสรุปข้อมูลจำนวนมากลงในสไลด์เดียวของงานนำเสนอ;
- เปิดเผยรูปแบบและแนวโน้มในข้อมูล;
- สรุปทิศทางและโมเมนตัมของข้อมูลตามเวลา หรือเทียบกับหน่วยวัดที่กำหนด;
- ตรวจจับค่าที่อยู่นอกกรอบ, ความเบี่ยงเบน, ความผิดพลาด, และข้อมูลที่ไม่มีความหมาย;
- สื่อสารหรือแสดงข้อมูลที่ซับซ้อน
ใน PowerPoint คุณสามารถสร้างแผนภูมิผ่านฟังก์ชัน Insert ซึ่งมีเทมเพลตสำหรับออกแบบหลายประเภทของแผนภูมิ โดยใช้ Aspose.Slides คุณสามารถสร้างแผนภูมิปกติ (อ้างอิงจากประเภทแผนภูมิที่นิยม) และแผนภูมิแบบกำหนดเองได้
Note
ใช้ enumeration ChartType ในแพคเกจ asposeslides.api ค่าต่าง ๆ ใน enumeration นี้สอดคล้องกับประเภทแผนภูมิที่หลากหลายสร้างแผนภูมิคอลัมน์แบบคลัสเตอร์
ตัวอย่างนี้ใช้ addChart เพื่อสร้างแผนภูมิคอลัมน์แบบคลัสเตอร์ที่เปรียบเทียบสอง Series บนสาม Category Series ใช้สีเติมแดงและสีเขียว, และป้ายกำกับแสดงชื่อ Category, ชื่อ Series, และค่า Worksheet index 0 หมายถึงแผ่นแรกในเวิร์กบุ๊คของแผนภูมิที่ฝังไว้
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, NullableBool, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 300)
chart.getChartTitle().addTextFrameForOverriding("Sample Title")
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True_)
chart.getChartTitle().setHeight(20)
chart.setTitle(True)
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
series_name_cell = workbook.getCell(worksheet_index, 0, 1, "Series 1")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series_name_cell = workbook.getCell(worksheet_index, 0, 2, "Series 2")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
category_cell = workbook.getCell(worksheet_index, 1, 0, "Category 1")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(worksheet_index, 2, 0, "Category 2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(worksheet_index, 3, 0, "Category 3")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().get_Item(0)
value_cell = workbook.getCell(worksheet_index, 1, 1, 20)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 1, 50)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 1, 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
series.getFormat().getFill().setFillType(FillType.Solid)
series.getFormat().getFill().getSolidFillColor().setColor(Color.RED)
series = chart.getChartData().getSeries().get_Item(1)
value_cell = workbook.getCell(worksheet_index, 1, 2, 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 2, 10)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 2, 60)
series.getDataPoints().addDataPointForBarSeries(value_cell)
series.getFormat().getFill().setFillType(FillType.Solid)
color = Color(0, 128, 0)
series.getFormat().getFill().getSolidFillColor().setColor(color)
label = series.getDataPoints().get_Item(0).getLabel()
label.getDataLabelFormat().setShowCategoryName(True)
label = series.getDataPoints().get_Item(1).getLabel()
label.getDataLabelFormat().setShowSeriesName(True)
label = series.getDataPoints().get_Item(2).getLabel()
label.getDataLabelFormat().setShowValue(True)
label.getDataLabelFormat().setShowSeriesName(True)
label.getDataLabelFormat().setSeparator("/")
presentation.save("AsposeChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิกระจาย
แผนภูมิกระจาย (หรือ scatter plot, x‑y graph) มักใช้เพื่อตรวจสอบรูปแบบหรือแสดงความสัมพันธ์ระหว่างสองตัวแปร
ใช้แผนภูมิกระจายเมื่อ:
- คุณมีข้อมูลตัวเลขคู่
- มีสองตัวแปรที่สัมพันธ์กันได้ดี
- คุณต้องการตรวจสอบว่าตัวแปรสองตัวนั้นเกี่ยวข้องกันหรือไม่
- มีตัวแปรอิสระที่มีค่าหลายค่าเพื่อกำหนดค่าตัวแปรตาม
ตัวอย่างนี้พล็อตชุดค่า x‑y สองชุด Series แรกใช้เส้นตรงพร้อมเครื่องหมายดาว, ส่วนสองใช้เส้นโค้งพร้อมเครื่องหมายวงกลม; ทั้งสองใช้ขนาดเครื่องหมาย 10 โดยกำหนดด้วย setSize วิธี setSymbol ควบคุมรูปแบบเครื่องหมาย
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, MarkerStyleType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 20, 20, 500, 300)
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
chart.getChartData().getSeries().clear()
series_name_cell = workbook.getCell(worksheet_index, 1, 1, "Series 1")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series_name_cell = workbook.getCell(worksheet_index, 1, 3, "Series 2")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series = chart.getChartData().getSeries().get_Item(0)
x_value_cell = workbook.getCell(worksheet_index, 2, 1, 1)
y_value_cell = workbook.getCell(worksheet_index, 2, 2, 3)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 3, 1, 2)
y_value_cell = workbook.getCell(worksheet_index, 3, 2, 10)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
series.setType(ChartType.ScatterWithStraightLinesAndMarkers)
series.getMarker().setSize(10)
series.getMarker().setSymbol(MarkerStyleType.Star)
series = chart.getChartData().getSeries().get_Item(1)
x_value_cell = workbook.getCell(worksheet_index, 2, 3, 5)
y_value_cell = workbook.getCell(worksheet_index, 2, 4, 2)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 3, 3, 3)
y_value_cell = workbook.getCell(worksheet_index, 3, 4, 1)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 4, 3, 2)
y_value_cell = workbook.getCell(worksheet_index, 4, 4, 2)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 5, 3, 5)
y_value_cell = workbook.getCell(worksheet_index, 5, 4, 1)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
series.getMarker().setSize(10)
series.getMarker().setSymbol(MarkerStyleType.Circle)
presentation.save("AsposeChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิวงกลม
แผนภูมิวงกลมเหมาะสำหรับแสดงความสัมพันธ์ส่วนต่อส่วนทั้งหมดของข้อมูล โดยเฉพาะเมื่อข้อมูลมีป้ายกำกับเชิงหมวดหมู่พร้อมค่าตัวเลข อย่างไรก็ตาม หากข้อมูลมีส่วนหรือป้ายกำกับจำนวนมาก ควรพิจารณาใช้แผนภูมิบาร์แทน
ตัวอย่างนี้สร้างแผนภูมิกลับที่มีค่าครึ่งปีสามค่า แต่ละส่วนมีการเติมสีและเส้นขอบกำหนดเอง, ป้ายกำกับแสดงการรวมค่าต่าง ๆ, ร้อยละ, และข้อมูล Series วิธี setFirstSliceAngle ตั้งมุมเริ่มต้นของส่วนแรกที่ 180 องศา
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, LineDashStyle, LineStyle, NullableBool, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Pie, 20, 20, 500, 300)
chart.getChartTitle().addTextFrameForOverriding("Sample Title")
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True_)
chart.getChartTitle().setHeight(20)
chart.setTitle(True)
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(True)
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
category_cell = workbook.getCell(0, 1, 0, "1st Qtr")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 2, 0, "2nd Qtr")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 3, 0, "3rd Qtr")
chart.getChartData().getCategories().add(category_cell)
series_name_cell = workbook.getCell(0, 0, 1, "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
value_cell = workbook.getCell(worksheet_index, 1, 1, 20)
series.getDataPoints().addDataPointForPieSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 1, 50)
series.getDataPoints().addDataPointForPieSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 1, 30)
series.getDataPoints().addDataPointForPieSeries(value_cell)
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(True)
first_slice = series.getDataPoints().get_Item(0)
first_slice.getFormat().getFill().setFillType(FillType.Solid)
first_slice.getFormat().getFill().getSolidFillColor().setColor(Color.CYAN)
first_slice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid)
first_slice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
first_slice.getFormat().getLine().setWidth(3.0)
first_slice.getFormat().getLine().setStyle(LineStyle.ThinThick)
first_slice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDash)
second_slice = series.getDataPoints().get_Item(1)
second_slice.getFormat().getFill().setFillType(FillType.Solid)
color = Color(165, 42, 42)
second_slice.getFormat().getFill().getSolidFillColor().setColor(color)
second_slice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid)
second_slice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
second_slice.getFormat().getLine().setWidth(3.0)
second_slice.getFormat().getLine().setStyle(LineStyle.Single)
second_slice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDot)
third_slice = series.getDataPoints().get_Item(2)
third_slice.getFormat().getFill().setFillType(FillType.Solid)
color = Color(255, 127, 80)
third_slice.getFormat().getFill().getSolidFillColor().setColor(color)
third_slice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid)
third_slice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED)
third_slice.getFormat().getLine().setWidth(2.0)
third_slice.getFormat().getLine().setStyle(LineStyle.ThinThin)
third_slice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDotDot)
first_label = series.getDataPoints().get_Item(0).getLabel()
first_label.getDataLabelFormat().setShowValue(True)
second_label = series.getDataPoints().get_Item(1).getLabel()
second_label.getDataLabelFormat().setShowValue(True)
second_label.getDataLabelFormat().setShowLegendKey(True)
second_label.getDataLabelFormat().setShowPercentage(True)
third_label = series.getDataPoints().get_Item(2).getLabel()
third_label.getDataLabelFormat().setShowSeriesName(True)
third_label.getDataLabelFormat().setShowPercentage(True)
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(True)
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180)
presentation.save("PieChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิเส้น
แผนภูมิเส้น (หรือ line graph) เหมาะสำหรับแสดงการเปลี่ยนแปลงของค่าเมื่อเวลาผ่านไป โดยใช้แผนภูมิเส้นคุณสามารถเปรียบเทียบข้อมูลจำนวนมากพร้อมกัน, ติดตามการเปลี่ยนแปลงและแนวโน้มตามเวลา, เน้นความแปลกปลอมใน Series ฯลฯ
ตัวอย่างนี้ใช้ addChart เพื่อสร้างแผนภูมิเส้นด้วย Series และ Category เริ่มต้น, จากนั้นบันทึก Presentation เป็น PPTX
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
line_chart = slide.getShapes().addChart(ChartType.Line, 20, 20, 500, 300)
presentation.save("line_chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
โดยค่าเริ่มต้น จุดบนแผนภูมิเส้นจะเชื่อมต่อด้วยเส้นตรงต่อเนื่อง หากต้องการให้จุดเชื่อมด้วยเส้นขีด, ให้เรียก setDashStyle พร้อมค่าจาก LineDashStyle
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, LineDashStyle, Presentation
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
line_chart = slide.getShapes().addChart(ChartType.Line, 20, 20, 500, 300)
for series in line_chart.getChartData().getSeries():
series.getFormat().getLine().setDashStyle(LineDashStyle.Dash)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิตรีแมป
แผนภูมิตรีแมปเหมาะสำหรับข้อมูลการขายเมื่อคุณต้องการแสดงขนาดสัมพัทธ์ของหมวดหมู่ข้อมูลและดึงความสนใจไปยังรายการที่เป็นส่วนสำคัญในแต่ละหมวดหมู่
ตัวอย่างนี้สร้าง treemap ที่มีใบแปดใบจัดเป็นสาขาสี่สาขาและสองกิ่ง วิธี setGroupingItem กำหนดสาเหตุที่ระดับกลุ่ม 1 และกิ่งที่ระดับ 2 ค่าใช้ในการกำหนดขนาดสี่เหลี่ยมสัมพัทธ์, ป้ายกำกับหมวดหมู่และป้ายกำกับพาเรนท์ที่ซ้อนกันบ่งบอกลำดับขั้น
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, ParentLabelLayoutType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Treemap, 20, 20, 500, 300)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
# สาขา 1
category_cell = workbook.getCell(0, "C1", "Leaf1")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem1")
leaf.getGroupingLevels().setGroupingItem(2, "Branch1")
category_cell = workbook.getCell(0, "C2", "Leaf2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C3", "Leaf3")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem2")
category_cell = workbook.getCell(0, "C4", "Leaf4")
chart.getChartData().getCategories().add(category_cell)
# สาขา 2
category_cell = workbook.getCell(0, "C5", "Leaf5")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem3")
leaf.getGroupingLevels().setGroupingItem(2, "Branch2")
category_cell = workbook.getCell(0, "C6", "Leaf6")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C7", "Leaf7")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem4")
category_cell = workbook.getCell(0, "C8", "Leaf8")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.Treemap)
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(True)
value_cell = workbook.getCell(0, "D1", 4)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D2", 5)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D3", 3)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D4", 6)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D5", 9)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D6", 9)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D7", 4)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D8", 3)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
series.setParentLabelLayout(ParentLabelLayoutType.Overlapping)
presentation.save("Treemap.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิน้ำตก
แผนภูมิน้ำตกแสดงการเปลี่ยนแปลงบวกและลบที่มีผลต่อค่ากลาง ตัวอย่างเช่น การสรุปกำไรอาจเริ่มจากรายได้, ลบต้นทุน, แล้วบวกรายได้อื่น ๆ คอลัมน์การเปลี่ยนแปลงจะลอยระหว่างค่าก่อนหน้าและค่าที่อัปเดต, ส่วนคอลัมน์รวมจะขยายจากศูนย์ถึงค่าที่สะสม
ตัวอย่างต่อไปใช้จำนวนเงินเป็นพันดอลลาร์สหรัฐ:
| Category | Workbook value | Column type |
|---|---|---|
| Revenue | 1000 | Increase |
| Cost of sales | -400 | Decrease |
| Gross profit | 600 | Intermediate total |
| Operating expenses | -200 | Decrease |
| Other income | 50 | Increase |
| Net profit | 450 | Final total |
เพื่อกำหนดจุดเป็นยอดรวมระหว่างหรือสุดท้าย ให้เรียกเมธอด setSetAsTotal ของจุดนั้นด้วย True และใส่จำนวนที่สะสมจริงในเซลล์เวิร์กบุ๊คของจุดนั้น: 600 สำหรับกำไรขั้นต้น (1000 - 400) และ 450 สำหรับกำไรสุทธิ (600 - 200 + 50) เมธอดนี้ทำเครื่องหมายคอลัมน์ว่าเป็นยอดรวม; มันไม่ได้คำนวณหรือเติมค่าลงในเซลล์ อย่าใช้ศูนย์เป็นตัวแทนหรือใส่ค่าเปลี่ยนแปลงอื่นสำหรับจุดยอดรวม คอลัมน์ยอดรวมจะแสดงค่าที่สะสมโดยไม่เพิ่มอีกครั้งต่อค่าปัจจุบัน
ตัวอย่าง Python เต็มรูปแบบนี้สร้างแผนภูมิโดยใช้สมาชิก Waterfall ของ ChartType, ล้างข้อมูลเริ่มต้น, และเติมเวิร์กบุ๊คที่ฝังไว้ Worksheet index 0 เลือกแผ่นงานแรก; คอลัมน์ A มีชื่อ Category, คอลัมน์ B มีค่า รายการค่าแต่ละค่าเพิ่มด้วย addDataPointForWaterfallSeries ดัชนีจุดศูนย์ฐาน 2 และ 5 ระบุยอดรวมระหว่างและสุดท้าย การเรียก setShowValue ด้วย True แสดงจำนวนบนคอลัมน์ ไม่จำเป็นต้องมี Presentation หรือเวิร์กบุ๊คต้นทาง
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Waterfall, 20, 20, 680, 200)
chart.setTitle(True)
chart.getChartTitle().addTextFrameForOverriding("Profit breakdown (USD thousands)")
chart.getChartTitle().setOverlay(False)
chart.setLegend(False)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
workbook.clear(worksheet_index)
series_name_cell = workbook.getCell(worksheet_index, 0, 1, "Profit")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.Waterfall)
categories = ["Revenue", "Cost of sales", "Gross profit", "Operating expenses", "Other income", "Net profit"]
values = [1000, -400, 600, -200, 50, 450]
for i in range(len(categories)):
category_cell = workbook.getCell(worksheet_index, i + 1, 0, categories[i])
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(worksheet_index, i + 1, 1, values[i])
series.getDataPoints().addDataPointForWaterfallSeries(value_cell)
# จุดที่สามและหกเป็นยอดรวม ไม่ใช่การเปลี่ยนแปลงเพิ่มเติม.
series.getDataPoints().get_Item(2).setSetAsTotal(True)
series.getDataPoints().get_Item(5).setSetAsTotal(True)
series.setShowConnectorLines(True)
series.getLabels().getDefaultDataLabelFormat().setShowValue(True)
presentation.save("WaterfallChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
เรียกเมธอดระดับ Series setShowConnectorLines ด้วย True เพื่อแสดงเส้นเชื่อมคอลัมน์ต่อเนื่องและทำให้ค่ารันนิ่งตามลำดับง่ายขึ้น หากต้องการซ่อนเส้นเหล่านี้ ให้แทนที่การเรียกเมธอดในตัวอย่างด้วย series.setShowConnectorLines(False) ก่อนบันทึก วิธีนี้จะส่งผลต่อการมองเห็นเส้นเท่านั้น ไม่กระทบข้อมูลหรือยอดรวม
Presentation ที่บันทึกแสดงกำไรขั้นต้น 600 และกำไรสุทธิ 450, ทั้งสองยอดรวมเริ่มจากศูนย์:

สร้างแผนภูมิเส้นหุ้น
แผนภูมิเส้นหุ้นใช้แสดงข้อมูลการเงิน เช่น ราคาเปิด, สูง, ต่ำ, ปิด, ช่วยวิเคราะห์แนวโน้มตลาดและความผันผวน ให้ข้อมูลเชิงลึกสำคัญเกี่ยวกับการแสดงผลของหุ้นเพื่อช่วยนักลงทุนและนักวิเคราะห์ในการตัดสินใจ
ตัวอย่างนี้สร้างแผนภูมิ OHLC (Open‑High‑Low‑Close) ที่มีสี่ Series ตามลำดับและสาม Category เมธอด setUpDownBars เปิดใช้งานแถบขึ้น/ลง, และ getHiLowLinesFormat ควบคุมเส้น high‑low ที่แสดงช่วงราคา, ในขณะที่เส้น Series แยกต่างหากและเส้นกริดแนวนอนย่อยถูกซ่อน
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.OpenHighLowClose, 20, 20, 500, 300, False)
workbook = chart.getChartData().getChartDataWorkbook()
category_cell = workbook.getCell(0, 1, 0, "A")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 2, 0, "B")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 3, 0, "C")
chart.getChartData().getCategories().add(category_cell)
series_name_cell = workbook.getCell(0, 0, 1, "Open")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series_name_cell = workbook.getCell(0, 0, 2, "High")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series_name_cell = workbook.getCell(0, 0, 3, "Low")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series_name_cell = workbook.getCell(0, 0, 4, "Close")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series = chart.getChartData().getSeries().get_Item(0)
value_cell = workbook.getCell(0, 1, 1, 72)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 1, 25)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 1, 38)
series.getDataPoints().addDataPointForStockSeries(value_cell)
series = chart.getChartData().getSeries().get_Item(1)
value_cell = workbook.getCell(0, 1, 2, 172)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 2, 57)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 2, 57)
series.getDataPoints().addDataPointForStockSeries(value_cell)
series = chart.getChartData().getSeries().get_Item(2)
value_cell = workbook.getCell(0, 1, 3, 12)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 3, 12)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 3, 13)
series.getDataPoints().addDataPointForStockSeries(value_cell)
series = chart.getChartData().getSeries().get_Item(3)
value_cell = workbook.getCell(0, 1, 4, 25)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 4, 38)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 4, 50)
series.getDataPoints().addDataPointForStockSeries(value_cell)
chart.getChartData().getSeriesGroups().get_Item(0).getUpDownBars().setUpDownBars(True)
chart.getChartData().getSeriesGroups().get_Item(0).getHiLowLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid)
for stock_series in chart.getChartData().getSeries():
stock_series.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
presentation.save("Stock-chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิกล่องและคีบ
แผนภูมิกล่องและคีบใช้แสดงการกระจายของข้อมูลโดยสรุปมาตรวัดสถิติสำคัญ เช่น มัธยฐาน, ควอร์ไทล์, และค่าอิสระที่อยู่นอกกลุ่ม เหมาะกับการวิเคราะห์ข้อมูลสำรวจและการศึกษาเชิงสถิติเพื่อทำความเข้าใจความแปรปรวนของข้อมูลและระบุความผิดปกติอย่างรวดเร็ว
ตัวอย่างนี้สร้างแผนภูมิกล่องและคีบจากหกค่า เมธอด setQuartileMethod เลือกการคำนวณควอร์ไทล์แบบ exclusive ตัวอย่างยังเปิดใช้เส้นค่าเฉลี่ย, เครื่องหมายค่าเฉลี่ย, จุดภายใน, และจุดอิสระ
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, QuartileMethodType, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.BoxAndWhisker, 20, 20, 500, 300)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
category_cell = workbook.getCell(0, "A1", "Category 1")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A2", "Category 2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A3", "Category 3")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A4", "Category 4")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A5", "Category 5")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A6", "Category 6")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.BoxAndWhisker)
series.setQuartileMethod(QuartileMethodType.Exclusive)
series.setShowMeanLine(True)
series.setShowMeanMarkers(True)
series.setShowInnerPoints(True)
series.setShowOutlierPoints(True)
value_cell = workbook.getCell(0, "B1", 15)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B2", 41)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B3", 16)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B4", 10)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B5", 23)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B6", 16)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
presentation.save("BoxAndWhisker.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
สร้างแผนภูมิกรวย
แผนภูมิกรวยใช้แสดงกระบวนการที่มีขั้นตอนต่อเนื่อง โดยปริมาณข้อมูลจะลดลงเมื่อดำเนินการจากขั้นตอนหนึ่งไปยังขั้นตอนถัดไป เหมาะสำหรับการวิเคราะห์อัตราการแปลง, ระบุคอขวด, และติดตามประสิทธิภาพของกระบวนการขายหรือการตลาด
ตัวอย่างนี้ใช้ addChart เพิ่มแผนภูมิกรวยลงในสไลด์แรกของ Presentation ที่มีอยู่แล้ว แทนที่ข้อมูลเริ่มต้นด้วยหก Category และค่าตั้งแต่ 50 ถึง 500, จากนั้นบันทึก Presentation ที่อัปเดต Presentation ต้องมีสไลด์อย่างน้อยหนึ่งหน้า
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation("test.pptx")
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Funnel, 50, 50, 500, 400)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
category_cell = workbook.getCell(0, "A1", "Category 1")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A2", "Category 2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A3", "Category 3")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A4", "Category 4")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A5", "Category 5")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A6", "Category 6")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.Funnel)
value_cell = workbook.getCell(0, "B1", 50)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B2", 100)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B3", 200)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B4", 300)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B5", 400)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B6", 500)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
presentation.save("Funnel.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิดวงอาทิตย์
แผนภูมิดวงอาทิตย์ใช้แสดงข้อมูลเชิงลำดับชั้น โดยแสดงระดับต่าง ๆ เป็นวงแหวนศูนย์กลม ช่วยอธิบายความสัมพันธ์ส่วนต่อส่วนทั้งหมดและเหมาะสำหรับแสดงหมวดหมู่ย่อยและหมวดหมู่ย่อยในรูปแบบที่กระชับและชัดเจน
ตัวอย่างนี้สร้างแผนภูมิดวงอาทิตย์ที่มีใบแปดใบจัดเป็นสี่สาเหตุและสองกิ่ง วิธี setGroupingItem กำหนดสาเหตุที่ระดับกลุ่ม 1 และกิ่งที่ระดับ 2 ป้ายกำกับหมวดหมู่บ่งบอกส่วนต่าง ๆ, ค่ากำหนดขนาดสัมพัทธ์ของแต่ละส่วน
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Sunburst, 20, 20, 500, 300)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
# สาขา 1
category_cell = workbook.getCell(0, "C1", "Leaf1")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem1")
leaf.getGroupingLevels().setGroupingItem(2, "Branch1")
category_cell = workbook.getCell(0, "C2", "Leaf2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C3", "Leaf3")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem2")
category_cell = workbook.getCell(0, "C4", "Leaf4")
chart.getChartData().getCategories().add(category_cell)
# สาขา 2
category_cell = workbook.getCell(0, "C5", "Leaf5")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem3")
leaf.getGroupingLevels().setGroupingItem(2, "Branch2")
category_cell = workbook.getCell(0, "C6", "Leaf6")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C7", "Leaf7")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem4")
category_cell = workbook.getCell(0, "C8", "Leaf8")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.Sunburst)
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(True)
value_cell = workbook.getCell(0, "D1", 4)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D2", 5)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D3", 3)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D4", 6)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D5", 9)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D6", 9)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D7", 4)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D8", 3)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
presentation.save("Sunburst.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิเฮสต์แกรม
แผนภูมิเฮสต์แกรมแสดงการกระจายของค่าตัวเลขตามช่วง (bins) สำหรับรายงานรายเดือนของเวลาประมวลผลคำสั่งซื้อ ให้ใช้ความกว้างของ bin และเกณฑ์เดียวกันเพื่อให้ช่วงสอดคล้องและทำให้การเปรียบเทียบการกระจายง่ายขึ้น
เรียก setAggregationType บนแกนแนวนอนด้วยโหมดจาก AxisAggregationType setBinWidth ใช้ได้เฉพาะกับ ByBinWidth; setNumberOfBins ใช้ได้เฉพาะกับ ByNumberOfBins ใช้ Automatic เพื่อให้แผนภูมิกำหนด bin เอง
ใช้ความกว้าง Bin คงที่
ตัวอย่างนี้จัดกลุ่มเวลาประมวลผลเป็นช่วง 10 นาที, มี bin แยกสำหรับเวลาที่ ≤ 10 นาทีและ > 40 นาที เปิดใช้งาน bin เหล่านี้ด้วย setUnderflowBin และ setOverflowBin เมื่อกำหนดเกณฑ์ชัดเจนผ่าน setUnderflowBin และ setOverflowBin ให้เรียก setAutomaticUnderflowBin และ setAutomaticOverflowBin ด้วย False
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AxisAggregationType, ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Histogram, 20, 20, 680, 200)
chart.setLegend(False)
chart.setTitle(False)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
series = chart.getChartData().getSeries().add(ChartType.Histogram)
values = [5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50]
for i, value in enumerate(values):
value_cell = workbook.getCell(0, i, 0, float(value))
series.getDataPoints().addDataPointForHistogramSeries(value_cell)
axis = chart.getAxes().getHorizontalAxis()
axis.setAggregationType(AxisAggregationType.ByBinWidth)
axis.setBinWidth(10.0)
axis.setUnderflowBin(True)
axis.setAutomaticUnderflowBin(False)
axis.setUnderflowBin(10.0)
axis.setOverflowBin(True)
axis.setAutomaticOverflowBin(False)
axis.setOverflowBin(40.0)
presentation.save("Histogram-bin-width.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Presentation ที่แสดงใน PowerPoint มีจำนวนในแต่ละ bin 3, 4, 4, 4, และ 2 bin underflow มีค่า 5, 10, 10 ดังนั้นเกณฑ์เป็น inclusive bin overflow มีค่า 41, 50 ดังนั้นเกณฑ์เป็น exclusive; ค่าที่เท่ากับ 40 ยังคงอยู่ในช่วงก่อนหน้า bin ภายในมีค่ามากกว่าขอบล่างและน้อยกว่าหรือเท่ากับขอบบน

กำหนดจำนวน Bin
หากต้องการระดับรายละเอียดคงที่แทนช่วงคงที่ ให้กำหนดจำนวน bin เป็น 5 สำหรับข้อมูลเดียวกัน ตัวอย่างนี้ปิดใช้งาน bin underflow และ overflow แผนภูมิคำนวณความกว้างจากช่วงข้อมูล ดังนั้นขอบเขตอาจเปลี่ยนแปลงระหว่างรายงาน
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AxisAggregationType, ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Histogram, 20, 20, 680, 200)
chart.setLegend(False)
chart.setTitle(False)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
series = chart.getChartData().getSeries().add(ChartType.Histogram)
values = [5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50]
for i, value in enumerate(values):
value_cell = workbook.getCell(0, i, 0, float(value))
series.getDataPoints().addDataPointForHistogramSeries(value_cell)
axis = chart.getAxes().getHorizontalAxis()
axis.setAggregationType(AxisAggregationType.ByNumberOfBins)
axis.setNumberOfBins(5)
presentation.save("Histogram-bin-count.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ห้าชุด bin มีจำนวน 4, 4, 4, 4, และ 1 เปรียบเทียบการกระจายนี้กับผลลัพธ์ความกว้างคงที่ข้างต้น: ค่าข้อมูลเท่าเดิม แต่ขอบเขตต่างกัน

สร้างแผนภูมิเรดาร์
แผนภูมิเรดาร์ใช้แสดงข้อมูลหลายตัวแปรในรูปแบบสองมิติ ทำให้เปรียบเทียบตัวแปรหลาย ๆ ตัวพร้อมกันได้ง่าย เหมาะสำหรับระบุรูปแบบ, จุดแข็ง, จุดอ่อนในหลายเมตริกหรือคุณลักษณะการทำงาน
ตัวอย่างนี้ใช้ addChart สร้างแผนภูมิเหมือนเรดาร์ด้วย Series และ Category เริ่มต้น, แล้วบันทึก Presentation เป็น PPTX
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
slide.getShapes().addChart(ChartType.Radar, 20, 20, 500, 300)
presentation.save("Radar-chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูมิหลายหมวดหมู่
แผนภูมิหลายหมวดหมู่ใช้แสดงข้อมูลที่มีการจัดกลุ่มเชิงหมวดหมู่หลายระดับ ช่วยให้คุณเปรียบเทียบค่าในหลายมิติพร้อมกัน เหมาะเมื่อจำเป็นต้องวิเคราะห์แนวโน้มและความสัมพันธ์ในชุดข้อมูลหลายชั้นที่ซับซ้อน
ตัวอย่างนี้สร้างแผนภูมิคอลัมน์แบบคลัสเตอร์ที่มีแปด Category จัดเป็นสี่คู่ เมธอด setGroupingItem ให้แต่ละคู่มีป้ายกำกับร่วมที่ระดับกลุ่ม 1 ทำให้แกน Category มีทั้งป้ายกำกับเดี่ยวและป้ายกำกับกลุ่ม
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 300)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
worksheet_index = 0
category_cell = workbook.getCell(0, "c2", "A")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group1")
category_cell = workbook.getCell(0, "c3", "B")
category = chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "c4", "C")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group2")
category_cell = workbook.getCell(0, "c5", "D")
category = chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "c6", "E")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group3")
category_cell = workbook.getCell(0, "c7", "F")
category = chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "c8", "G")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group4")
category_cell = workbook.getCell(0, "c9", "H")
category = chart.getChartData().getCategories().add(category_cell)
series_name_cell = workbook.getCell(0, "D1", "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.ClusteredColumn)
value_cell = workbook.getCell(worksheet_index, "D2", 10)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D3", 20)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D4", 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D5", 40)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D6", 50)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D7", 60)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D8", 70)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D9", 80)
series.getDataPoints().addDataPointForBarSeries(value_cell)
presentation.save("AsposeChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

สร้างแผนภูม 지도
แผนภูม 지도ใช้แสดงข้อมูลเชิงภูมิศาสตร์โดยแมปข้อมูลไปยังตำแหน่งเฉพาะ เช่น ประเทศ, รัฐ, หรือเมือง เหมาะสำหรับวิเคราะห์แนวโน้มภูมิภาค, ข้อมูลประชากร, และการกระจายเชิงพื้นที่ในรูปแบบที่ชัดเจนและดึงดูดสายตา
ตัวอย่างนี้ใช้ addChart สร้างแผนภูมิกรีนที่มีข้อมูลภูมิศาสตร์เริ่มต้นและบันทึกเพื่อดูใน PowerPoint
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Map, 20, 20, 500, 300)
presentation.save("mapChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ผลลัพธ์:

Note
รูปภาพด้านบนแสดง Presentation ที่บันทึกและเปิดใน PowerPoint Aspose.Slides เขียนแผนภูมิแผนที่และข้อมูลได้อย่างถูกต้อง แต่ไม่ได้วาดแผนภูมิแผนที่เอง: เมื่อสไลด์ที่มีแผนภูมินี้ถูกเรนเดอร์เป็นภาพหรือแปลงเป็น PDF หรือ SVG พื้นที่แผนภูมิจะเป็นช่องว่าง รูปทรงอื่นบนสไลด์เดียวกันไม่ถูกกระทบสร้างแผนภูมิผสม
แผนภูมผสม (หรือ combo chart) ผสานประเภทแผนภูมิสองประเภทขึ้นไปในกราฟเดียว ทำให้คุณสามารถเน้น, เปรียบเทียบ, หรือวิเคราะห์ความแตกต่างระหว่างชุดข้อมูลหลายชุด เพื่อระบุความสัมพันธ์ระหว่างกัน

ตัวอย่างนี้ผสาน Series คอลัมน์แบบคลัสเตอร์สอง Series กับ Series เส้นหนึ่ง Series ผ่านสี่ Category คอลัมน์ใช้แกนแนวตั้งหลัก, ขณะที่ setPlotOnSecondAxis วางเส้นบนแกนแนวตั้งที่สองด้านขวา ฟังก์ชันช่วยเหลือกำหนดค่าชื่อเรื่อง, legend, ป้ายแกน, และเส้นกริด
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AxisPositionType, ChartType, CrossesType, FillType, LegendPositionType, NullableBool, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
def create_combo_chart():
presentation = Presentation()
slide = presentation.getSlides().get_Item(0)
try:
chart = create_chart_with_first_series(slide)
add_second_series_to_chart(chart)
add_third_series_to_chart(chart)
set_primary_axes_format(chart)
set_secondary_axes_format(chart)
presentation.save("combo-chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
def create_chart_with_first_series(slide):
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 600, 400)
# ตั้งค่าชื่อแผนภูมิ.
chart.setTitle(True)
chart.getChartTitle().addTextFrameForOverriding("Chart Title")
chart.getChartTitle().setOverlay(False)
title_paragraph = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0)
title_format = title_paragraph.getParagraphFormat().getDefaultPortionFormat()
title_format.setFontBold(NullableBool.False_)
title_format.setFontHeight(18.0)
# ตั้งค่าคำอธิบายแผนภูมิ.
chart.getLegend().setPosition(LegendPositionType.Bottom)
chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12.0)
# ลบซีรีส์และหมวดหมู่ที่สร้างโดยอัตโนมัติ.
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
worksheet_index = 0
workbook = chart.getChartData().getChartDataWorkbook()
# เพิ่มหมวดหมู่ใหม่.
cell = workbook.getCell(worksheet_index, 1, 0, "Category 1")
chart.getChartData().getCategories().add(cell)
cell = workbook.getCell(worksheet_index, 2, 0, "Category 2")
chart.getChartData().getCategories().add(cell)
cell = workbook.getCell(worksheet_index, 3, 0, "Category 3")
chart.getChartData().getCategories().add(cell)
cell = workbook.getCell(worksheet_index, 4, 0, "Category 4")
chart.getChartData().getCategories().add(cell)
# เพิ่มซีรีส์แรก.
series_name_cell = workbook.getCell(worksheet_index, 0, 1, "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series.getParentSeriesGroup().setOverlap(jpype.JByte(-25))
series.getParentSeriesGroup().setGapWidth(220)
cell = workbook.getCell(worksheet_index, 1, 1, 4.3)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 2, 1, 2.5)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 3, 1, 3.5)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 4, 1, 4.5)
series.getDataPoints().addDataPointForBarSeries(cell)
return chart
def add_second_series_to_chart(chart):
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
series_name_cell = workbook.getCell(worksheet_index, 0, 2, "Series 2")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.ClusteredColumn)
series.getParentSeriesGroup().setOverlap(jpype.JByte(-25))
series.getParentSeriesGroup().setGapWidth(220)
cell = workbook.getCell(worksheet_index, 1, 2, 2.4)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 2, 2, 4.4)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 3, 2, 1.8)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 4, 2, 2.8)
series.getDataPoints().addDataPointForBarSeries(cell)
def add_third_series_to_chart(chart):
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
series_name_cell = workbook.getCell(worksheet_index, 0, 3, "Series 3")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.Line)
cell = workbook.getCell(worksheet_index, 1, 3, 2.0)
series.getDataPoints().addDataPointForLineSeries(cell)
cell = workbook.getCell(worksheet_index, 2, 3, 2.0)
series.getDataPoints().addDataPointForLineSeries(cell)
cell = workbook.getCell(worksheet_index, 3, 3, 3.0)
series.getDataPoints().addDataPointForLineSeries(cell)
cell = workbook.getCell(worksheet_index, 4, 3, 5.0)
series.getDataPoints().addDataPointForLineSeries(cell)
series.setPlotOnSecondAxis(True)
def set_primary_axes_format(chart):
# ตั้งค่าแกนแนวนอน.
horizontal_axis = chart.getAxes().getHorizontalAxis()
horizontal_axis.getTextFormat().getPortionFormat().setFontHeight(12.0)
horizontal_axis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
set_axis_title(horizontal_axis, "X Axis")
# ตั้งค่าแกนแนวตั้ง.
vertical_axis = chart.getAxes().getVerticalAxis()
vertical_axis.getTextFormat().getPortionFormat().setFontHeight(12.0)
vertical_axis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
set_axis_title(vertical_axis, "Y Axis 1")
# ตั้งค่าสีเส้นกริดหลักของแกนแนวตั้ง.
major_grid_lines_format = vertical_axis.getMajorGridLinesFormat().getLine().getFillFormat()
major_grid_lines_format.setFillType(FillType.Solid)
color = Color(217, 217, 217)
major_grid_lines_format.getSolidFillColor().setColor(color)
def set_secondary_axes_format(chart):
# ตั้งค่าแกนแนวนอนรอง.
secondary_horizontal_axis = chart.getAxes().getSecondaryHorizontalAxis()
secondary_horizontal_axis.setPosition(AxisPositionType.Bottom)
secondary_horizontal_axis.setCrossType(CrossesType.Maximum)
secondary_horizontal_axis.setVisible(False)
secondary_horizontal_axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
secondary_horizontal_axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
# ตั้งค่าแกนแนวตั้งรอง.
secondary_vertical_axis = chart.getAxes().getSecondaryVerticalAxis()
secondary_vertical_axis.setPosition(AxisPositionType.Right)
secondary_vertical_axis.getTextFormat().getPortionFormat().setFontHeight(12.0)
secondary_vertical_axis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
secondary_vertical_axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
secondary_vertical_axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
set_axis_title(secondary_vertical_axis, "Y Axis 2")
def set_axis_title(axis, axis_title):
axis.setTitle(True)
axis.getTitle().setOverlay(False)
title_paragraph = axis.getTitle().addTextFrameForOverriding(axis_title).getParagraphs().get_Item(0)
title_format = title_paragraph.getParagraphFormat().getDefaultPortionFormat()
title_format.setFontBold(NullableBool.False_)
title_format.setFontHeight(12.0)
create_combo_chart()
อัปเดตแผนภูมิ
Aspose.Slides for Python via Java ทำให้คุณอัปเดตแผนภูมิ PowerPoint โดยแก้ไขข้อมูลแผนภูมิ, รูปแบบ, และสไตล์ การทำเช่นนี้ช่วยให้การรักษา Presentation ให้ทันสมัยด้วยเนื้อหาไดนามิกเป็นเรื่องง่ายและทำให้แผนภูมสะท้อนข้อมูลและมาตรฐานภาพล่าสุดอย่างแม่นยำ
ตัวอย่างนี้อัปเดตแผนภูมิชื่อ “My chart” บนสไลด์แรกของ Presentation ที่มีอยู่แล้ว ต้องมี Series คอลัมน์สอง Series แต่ละ Series มีสามจุดข้อมูล, ป้าย Category อยู่ในคอลัมน์ A และชื่อ Series อยู่ในแถวแรกของ Worksheet แรก ตัวอย่างใช้ getCell เพื่อเปลี่ยนค่าในเซลล์เวิร์กบุ๊ค, อัปเดตค่า Series, เพิ่ม Series ที่สาม, และเปลี่ยนประเภทแผนภูมิเป็น clustered cylinders ผ่าน setType
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Chart, ChartType, Presentation, SaveFormat
chart_name = "My chart"
presentation = Presentation("ExistingChart.pptx")
try:
slide = presentation.getSlides().get_Item(0)
for shape in slide.getShapes():
if isinstance(shape, Chart) and chart_name == shape.getName():
chart = shape
worksheet_index = 0
workbook = chart.getChartData().getChartDataWorkbook()
workbook.getCell(worksheet_index, 1, 0, "Modified Category 1")
workbook.getCell(worksheet_index, 2, 0, "Modified Category 2")
series = chart.getChartData().getSeries().get_Item(0)
workbook.getCell(worksheet_index, 0, 1, "New_Series 1")
series.getDataPoints().get_Item(0).getValue().setData(90)
series.getDataPoints().get_Item(1).getValue().setData(123)
series.getDataPoints().get_Item(2).getValue().setData(44)
series = chart.getChartData().getSeries().get_Item(1)
workbook.getCell(worksheet_index, 0, 2, "New_Series 2")
series.getDataPoints().get_Item(0).getValue().setData(23)
series.getDataPoints().get_Item(1).getValue().setData(67)
series.getDataPoints().get_Item(2).getValue().setData(99)
series_name_cell = workbook.getCell(worksheet_index, 0, 3, "Series 3")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
value_cell = workbook.getCell(worksheet_index, 1, 3, 20)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 3, 50)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 3, 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
chart.setType(ChartType.ClusteredCylinder)
presentation.save("AsposeChartModified.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
กำหนดช่วงข้อมูลสำหรับแผนภูมิ
หากต้องการตรวจสอบช่วงที่แผนภูมิมีอยู่แล้ว ให้ดู Retrieve a Chart’s Data Range
Aspose.Slides for Python via Java ให้ความยืดหยุ่นในการกำหนดช่วงข้อมูลเฉพาะจาก Worksheet เป็นแหล่งข้อมูลของแผนภูมิ ซึ่งหมายความว่าคุณสามารถแมปส่วนหนึ่งของ Worksheet ไปยังแผนภูมิได้โดยตรง, ควบคุมเซลล์ใดบ้างที่มีส่วนร่วมใน Series และ Category ของแผนภูมิ ผลก็คือคุณสามารถอัปเดตและซิงโครไนซ์แผนภูมิของคุณกับการเปลี่ยนแปลงข้อมูลล่าสุดใน Worksheet ได้อย่างง่ายดาย ทำให้ Presentation ของคุณสะท้อนข้อมูลที่เป็นปัจจุบันและแม่นยำเสมอ
ตัวอย่างนี้ใช้ setRange เพื่อกำหนดแหล่งข้อมูลของแผนภูมิชื่อ “My chart” บนสไลด์แรกของ Presentation ที่มีอยู่แล้วเป็น Sheet1!A1:B4 Worksheet ของแผนภูมิต้องมี Worksheet นั้นและช่วงข้อมูลที่เต็มด้วยหัวข้อ Series ในแถวแรก, ป้าย Category ในคอลัมน์ A, และค่าสามค่าในคอลัมน์ B
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Chart, Presentation, SaveFormat
chart_name = "My chart"
presentation = Presentation("ExistingChart.pptx")
try:
slide = presentation.getSlides().get_Item(0)
for shape in slide.getShapes():
if isinstance(shape, Chart) and chart_name == shape.getName():
chart = shape
chart.getChartData().setRange("Sheet1!A1:B4")
presentation.save("SetDataRange.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
ใช้ Markers เริ่มต้นในแผนภูมิ
เมื่อคุณใช้ Markers เริ่มต้นในแผนภูมิ แต่ละ Series จะได้รับสัญลักษณ์ Marker เริ่มต้นที่แตกต่างกันโดยอัตโนมัติ
ตัวอย่างนี้ใช้ addChart สร้างแผนภูมิกับสอง Series เส้นและ legend การตั้งค่า Marker ที่เข้าถึงผ่าน getMarker รักษาสัญลักษณ์ Marker ที่กำหนดอัตโนมัติ Series แรกรวมค่าติดลบและค่าที่หายไป (แสดงด้วย None), ส่วนที่สองมีค่าทุก Category
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 10, 10, 400, 400)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
series_name_cell = workbook.getCell(0, 0, 1, "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
category_cell = workbook.getCell(0, 1, 0, "C1")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 1, 1, 24)
series.getDataPoints().addDataPointForLineSeries(value_cell)
category_cell = workbook.getCell(0, 2, 0, "C2")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 2, 1, 23)
series.getDataPoints().addDataPointForLineSeries(value_cell)
category_cell = workbook.getCell(0, 3, 0, "C3")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 3, 1, -10)
series.getDataPoints().addDataPointForLineSeries(value_cell)
category_cell = workbook.getCell(0, 4, 0, "C4")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 4, 1, None)
series.getDataPoints().addDataPointForLineSeries(value_cell)
series_name_cell = workbook.getCell(0, 0, 2, "Series 2")
second_series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
value_cell = workbook.getCell(0, 1, 2, 30)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
value_cell = workbook.getCell(0, 2, 2, 10)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
value_cell = workbook.getCell(0, 3, 2, 60)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
value_cell = workbook.getCell(0, 4, 2, 40)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
chart.setLegend(True)
chart.getLegend().setOverlay(False)
presentation.save("DefaultMarkersInChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
คำถามที่พบบ่อย
Aspose.Slides for Python via Java รองรับประเภทแผนภูมิใดบ้าง?
Aspose.Slides for Python via Java รองรับประเภทแผนภูมิมากมาย รวมถึง bar, line, pie, area, scatter, histogram, radar, และอื่น ๆ อีกหลายประเภท ซึ่งทำให้คุณเลือกประเภทแผนภูมิที่เหมาะกับการแสดงผลข้อมูลของคุณได้อย่างยืดหยุ่น
ฉันจะเพิ่มแผนภูมิใหม่ลงในสไลด์อย่างไร?
เพื่อเพิ่มแผนภูมิ ก่อนอื่นให้สร้างอินสแตนซ์ของคลาส Presentation แล้วดึงสไลด์ที่ต้องการโดยใช้ดัชนี, จากนั้นเรียกเมธอดเพื่อเพิ่มแผนภูมิ โดยระบุประเภทแผนภูมิและข้อมูลเริ่มต้น วิธีนี้ทำให้แผนภูมิเข้าร่วมใน Presentation ของคุณโดยตรง
ฉันสามารถอัปเดตข้อมูลที่แสดงในแผนภูมิได้อย่างไร?
คุณสามารถอัปเดตข้อมูลของแผนภูมิได้โดยเข้าถึงเวิร์กบุ๊คข้อมูลของแผนภูมิ (ChartDataWorkbook), ล้าง Series และ Category เริ่มต้น, แล้วเพิ่มข้อมูลที่กำหนดเอง วิธีนี้ช่วยให้คุณรีเฟรชแผนภูมิด้วยข้อมูลล่าสุดโดยอัตโนมัติ
สามารถปรับแต่งลักษณะของแผนภูมิได้หรือไม่?
ได้, Aspose.Slides for Python via Java มีตัวเลือกการปรับแต่งที่หลากหลาย คุณสามารถแก้ไขสี, ฟอนต์, ป้ายกำกับ, legend, และองค์ประกอบการจัดรูปแบบอื่น ๆ เพื่อให้แผนภูมิเหมาะกับความต้องการออกแบบของคุณอย่างแม่นยำ