สร้างหรืออัปเดตแผนภูมิการนำเสนอ PowerPoint ด้วย JavaScript
ภาพรวม
บทความนี้ให้คำแนะนำอย่างละเอียดเกี่ยวกับวิธีการสร้างและปรับแต่งแผนภูมิด้วย Aspose.Slides for Node.js via Java คุณจะได้เรียนรู้วิธีการเพิ่มแผนภูมิลงในสไลด์โดยโปรแกรมเมติก กรอกข้อมูลในแผนภูมิ และใช้ตัวเลือกการจัดรูปแบบต่าง ๆ เพื่อให้ตรงกับข้อกำหนดการออกแบบของคุณ ตลอดทั้งบทความ ตัวอย่างโค้ดที่ละเอียดจะอธิบายขั้นตอนแต่ละขั้น ตั้งแต่การเริ่มต้น Presentation และอ็อบเจกต์แผนภูมิ ไปจนถึงการกำหนดซีรีส์ แกน และคำอธิบาย legend หากทำตามคำแนะนำนี้ คุณจะเข้าใจวิธีการรวมการสร้างแผนภูมิแบบไดนามิกลงในแอปพลิเคชัน Node.js ของคุณได้อย่างมั่นใจและทำให้กระบวนการสร้างงานนำเสนออิงข้อมูลเป็นเรื่องง่ายขึ้น
สร้างแผนภูมิ
แผนภูมิช่วยให้ผู้คนมองเห็นข้อมูลได้อย่างรวดเร็วและเข้าใจเชิงลึกที่อาจไม่ชัดเจนจากตารางหรือสแปรดชีต
ทำไมต้องสร้างแผนภูมิ?
- รวม, ย่อส่วน, หรือสรุปข้อมูลจำนวนมากบนสไลด์เดียวในงานนำเสนอ;
- เผยแพร่วิธีการและแนวโน้มของข้อมูล;
- สรุปทิศทางและแนวโน้มของข้อมูลตามเวลา หรือเทียบกับหน่วยวัดเฉพาะ;
- ตรวจจับค่าผิดปกติ, ความเบี่ยงเบน, ข้อผิดพลาด, และข้อมูลที่ไม่มีความหมาย;
- สื่อสารหรือแสดงข้อมูลที่ซับซ้อน.
ใน PowerPoint คุณสามารถสร้างแผนภูมิผ่านฟังก์ชัน Insert ที่ให้เทมเพลตสำหรับออกแบบแผนภูมิต่าง ๆ โดยใช้ Aspose.Slides คุณสามารถสร้างแผนภูมิปกติ (อิงจากประเภทแผนภูมิยอดนิยม) และแผนภูมิแบบกำหนดเองได้
Note
ใช้ enumeration ChartType ในโมดูล aspose.slides ค่าต่าง ๆ ใน enumeration นี้สอดคล้องกับประเภทแผนภูมิที่แตกต่างกันสร้างแผนภูมิคอลัมน์แบบกลุ่ม
ตัวอย่างนี้ใช้ addChart เพื่อสร้างแผนภูมิคอลัมน์แบบกลุ่มที่เปรียบเทียบสองซีรีส์ในสามหมวดหมู่ ซีรีส์ใช้สีเติมแบบแดงและเขียว และป้ายชื่อแต่ละรายการแสดงชื่อหมวดหมู่ ชื่อซีรีส์ และค่า ดัชนี Worksheet 0 หมายถึงชีตแรกในสมุดงานแผนภูมิที่ฝังอยู่
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(java.newByte(aspose.slides.NullableBool.True));
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
let workbook = chart.getChartData().getChartDataWorkbook();
let worksheetIndex = 0;
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 1, "Series 1");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
seriesNameCell = workbook.getCell(worksheetIndex, 0, 2, "Series 2");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
let categoryCell = workbook.getCell(worksheetIndex, 1, 0, "Category 1");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 2, 0, "Category 2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 3, 0, "Category 3");
chart.getChartData().getCategories().add(categoryCell);
let series = chart.getChartData().getSeries().get_Item(0);
let valueCell = workbook.getCell(worksheetIndex, 1, 1, 20);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 1, 50);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 1, 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
series = chart.getChartData().getSeries().get_Item(1);
valueCell = workbook.getCell(worksheetIndex, 1, 2, 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 2, 10);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 2, 60);
series.getDataPoints().addDataPointForBarSeries(valueCell);
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(java.newInstanceSync("java.awt.Color", 0, 128, 0));
let 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", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

สร้างแผนภูมิกระจาย
แผนภูมิกระจาย (หรือที่เรียกว่ากระจายจุดหรือกราฟ x‑y) มักใช้เพื่อตรวจสอบรูปแบบหรือแสดงความสัมพันธ์ระหว่างสองตัวแปร
ใช้แผนภูมิกระจายเมื่อ:
- คุณมีข้อมูลเชิงตัวเลขเป็นคู่.
- คุณมีตัวแปรสองตัวที่จับคู่กันได้อย่างดี.
- คุณต้องการตรวจสอบว่าตัวแปรสองตัวสัมพันธ์กันหรือไม่.
- คุณมีตัวแปรอิสระที่มีค่าหลายค่า สำหรับตัวแปรตาม.
ตัวอย่างนี้พล็อตค่าชุด x‑y สองชุด ชุดแรกใช้เส้นตรงพร้อมเครื่องหมายดาว ชุดที่สองใช้เส้นโค้งพร้อมเครื่องหมายวงกลม ทั้งสองใช้ขนาดเครื่องหมาย 10 โดยกำหนดผ่าน setSize วิธี setSymbol ควบคุมรูปแบบเครื่องหมาย
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.ScatterWithSmoothLines, 20, 20, 500, 300);
let workbook = chart.getChartData().getChartDataWorkbook();
let worksheetIndex = 0;
chart.getChartData().getSeries().clear();
let seriesNameCell = workbook.getCell(worksheetIndex, 1, 1, "Series 1");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
seriesNameCell = workbook.getCell(worksheetIndex, 1, 3, "Series 2");
chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
let series = chart.getChartData().getSeries().get_Item(0);
let xValueCell = workbook.getCell(worksheetIndex, 2, 1, 1);
let yValueCell = workbook.getCell(worksheetIndex, 2, 2, 3);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 3, 1, 2);
yValueCell = workbook.getCell(worksheetIndex, 3, 2, 10);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
series.setType(aspose.slides.ChartType.ScatterWithStraightLinesAndMarkers);
series.getMarker().setSize(10);
series.getMarker().setSymbol(aspose.slides.MarkerStyleType.Star);
series = chart.getChartData().getSeries().get_Item(1);
xValueCell = workbook.getCell(worksheetIndex, 2, 3, 5);
yValueCell = workbook.getCell(worksheetIndex, 2, 4, 2);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 3, 3, 3);
yValueCell = workbook.getCell(worksheetIndex, 3, 4, 1);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 4, 3, 2);
yValueCell = workbook.getCell(worksheetIndex, 4, 4, 2);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.getCell(worksheetIndex, 5, 3, 5);
yValueCell = workbook.getCell(worksheetIndex, 5, 4, 1);
series.getDataPoints().addDataPointForScatterSeries(xValueCell, yValueCell);
series.getMarker().setSize(10);
series.getMarker().setSymbol(aspose.slides.MarkerStyleType.Circle);
presentation.save("AsposeChart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

สร้างแผนภูมิวงกลม
แผนภูมิวงกลมเหมาะสำหรับแสดงความสัมพันธ์ส่วนต่อส่วนของข้อมูล โดยเฉพาะเมื่อข้อมูลมีป้ายชื่อเชิงหมวดหมู่พร้อมค่าตัวเลข อย่างไรก็ตาม หากข้อมูลของคุณมีหลายส่วนหรือหลายป้ายชื่อ คุณอาจพิจารณาใช้แผนภูมิบาร์แทน
ตัวอย่างนี้สร้างแผนภูมิวงกลมที่มีค่าไตรมาสสามค่า แต่ละชิ้นมีการเติมสีและเส้นขอบกำหนดเอง และป้ายชื่อแสดงการผสมค่าต่าง ๆ, เปอร์เซ็นต์, และข้อมูลซีรีส์ วิธี setFirstSliceAngle ตั้งให้ชิ้นแรกเริ่มที่ 180 องศา
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.Pie, 20, 20, 500, 300);
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(java.newByte(aspose.slides.NullableBool.True));
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
let workbook = chart.getChartData().getChartDataWorkbook();
let worksheetIndex = 0;
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
let categoryCell = workbook.getCell(0, 1, 0, "1st Qtr");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 2, 0, "2nd Qtr");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 3, 0, "3rd Qtr");
chart.getChartData().getCategories().add(categoryCell);
let seriesNameCell = workbook.getCell(0, 0, 1, "Series 1");
let series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
let valueCell = workbook.getCell(worksheetIndex, 1, 1, 20);
series.getDataPoints().addDataPointForPieSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 1, 50);
series.getDataPoints().addDataPointForPieSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 1, 30);
series.getDataPoints().addDataPointForPieSeries(valueCell);
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(true);
let firstSlice = series.getDataPoints().get_Item(0);
firstSlice.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstSlice.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "CYAN"));
firstSlice.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstSlice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));
firstSlice.getFormat().getLine().setWidth(3.0);
firstSlice.getFormat().getLine().setStyle(java.newByte(aspose.slides.LineStyle.ThinThick));
firstSlice.getFormat().getLine().setDashStyle(java.newByte(aspose.slides.LineDashStyle.LargeDash));
let secondSlice = series.getDataPoints().get_Item(1);
secondSlice.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondSlice.getFormat().getFill().getSolidFillColor().setColor(java.newInstanceSync("java.awt.Color", 165, 42, 42));
secondSlice.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondSlice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
secondSlice.getFormat().getLine().setWidth(3.0);
secondSlice.getFormat().getLine().setStyle(java.newByte(aspose.slides.LineStyle.Single));
secondSlice.getFormat().getLine().setDashStyle(java.newByte(aspose.slides.LineDashStyle.LargeDashDot));
let thirdSlice = series.getDataPoints().get_Item(2);
thirdSlice.getFormat().setFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
thirdSlice.getFormat().getFill().getSolidFillColor().setColor(java.newInstanceSync("java.awt.Color", 255, 127, 80));
thirdSlice.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
thirdSlice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
thirdSlice.getFormat().getLine().setWidth(2.0);
thirdSlice.getFormat().getLine().setStyle(java.newByte(aspose.slides.LineStyle.ThinThin));
thirdSlice.getFormat().getLine().setDashStyle(java.newByte(aspose.slides.LineDashStyle.LargeDashDotDot));
let firstLabel = series.getDataPoints().get_Item(0).getLabel();
firstLabel.getDataLabelFormat().setShowValue(true);
let secondLabel = series.getDataPoints().get_Item(1).getLabel();
secondLabel.getDataLabelFormat().setShowValue(true);
secondLabel.getDataLabelFormat().setShowLegendKey(true);
secondLabel.getDataLabelFormat().setShowPercentage(true);
let thirdLabel = series.getDataPoints().get_Item(2).getLabel();
thirdLabel.getDataLabelFormat().setShowSeriesName(true);
thirdLabel.getDataLabelFormat().setShowPercentage(true);
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true);
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180);
presentation.save("PieChart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

สร้างแผนภูมิเส้น
แผนภูมิเส้น (หรือที่เรียกว่ากราฟเส้น) เหมาะสำหรับแสดงการเปลี่ยนแปลงค่าตามเวลา โดยใช้แผนภูมิเส้นคุณสามารถเปรียบเทียบข้อมูลจำนวนมากพร้อมกัน ติดตามการเปลี่ยนแปลงและแนวโน้มตามเวลา ไฮไลต์ความผิดปกติในซีรีส์ และอื่น ๆ
ตัวอย่างนี้ใช้ addChart เพื่อสร้างแผนภูมิเส้นด้วยซีรีส์และหมวดหมู่เริ่มต้น จากนั้นบันทึก Presentation เป็น PPTX
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let lineChart = slide.getShapes().addChart(aspose.slides.ChartType.Line, 20, 20, 500, 300);
presentation.save("lineChart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
โดยปกติ จุดในแผนภูมิเส้นจะเชื่อมด้วยเส้นตรงต่อเนื่อง หากต้องการให้จุดเชื่อมด้วยเส้นประ ให้เรียก setDashStyle โดยระบุค่าจาก LineDashStyle
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let lineChart = slide.getShapes().addChart(aspose.slides.ChartType.Line, 20, 20, 500, 300);
for (let i = 0; i < lineChart.getChartData().getSeries().size(); i++)
{
let series = lineChart.getChartData().getSeries().get_Item(i);
series.getFormat().getLine().setDashStyle(java.newByte(aspose.slides.LineDashStyle.Dash));
}
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

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

สร้างแผนภูมิน้ำตก
แผนภูมิน้ำตกแสดงว่าการเปลี่ยนแปลงบวกและลบส่งผลต่อค่าขั้นสุดท้ายอย่างไร ตัวอย่างเช่น การสรุปกำไรอาจเริ่มจากรายได้ ลดต้นทุนขาย แล้วบวกรายได้อื่น คอลัมน์การเปลี่ยนแปลงลอยระหว่างค่าก่อนหน้าและค่าปัจจุบัน ส่วนคอลัมน์รวมจะต่อจากศูนย์ไปยังค่าที่สะสม
| ประเภท | ค่าชุดงาน | ประเภทคอลัมน์ |
|---|---|---|
| รายได้ | 1000 | เพิ่ม |
| ต้นทุนขาย | -400 | ลด |
| กำไรขั้นต้น | 600 | รวมย่อย |
| ค่าใช้จ่ายดำเนินการ | -200 | ลด |
| รายได้อื่น | 50 | เพิ่ม |
| กำไรสุทธิ | 450 | รวมสุดท้าย |
เพื่อทำเครื่องหมายจุดเป็นค่ากลางหรือค่าขั้นสุดท้าย ให้เรียกเมธอด setSetAsTotal ด้วย true และใส่จำนวนที่สะสมจริงลงในเซลล์ของชุดงานนั้น: 600 สำหรับกำไรขั้นต้น (1000 - 400) และ 450 สำหรับกำไรสุทธิ (600 - 200 + 50) เมธอดจะทำเครื่องหมายคอลัมน์เป็นค่ารวม แต่ไม่ได้คำนวณหรือเติมค่าเซลล์ ไม่ควรใช้ศูนย์เป็นค่าแทนหรือใส่การเปลี่ยนแปลงอื่น ๆ สำหรับจุดรวม คอลัมน์รวมจะแสดงค่าที่สะสมโดยไม่เพิ่มอีกครั้งในค่าที่กำลังดำเนินอยู่
ตัวอย่าง JavaScript นี้สร้างแผนภูมิน้ำตกโดยใช้สมาชิก Waterfall ของ ChartType, ล้างข้อมูลเริ่มต้น และเติมข้อมูลลงในสมุดงานฝังนั้น ดัชนี Worksheet 0 เลือกชีตแรก; คอลัมน์ A มีชื่อหมวด, คอลัมน์ B มีค่า แต่ละเซลล์ค่าถูกเพิ่มด้วย addDataPointForWaterfallSeries ดัชนีจุด 0‑ฐาน 2 และ 5 ระบุตำแหน่งค่ากลางและค่าขั้นสุดท้าย การเรียก setShowValue ด้วย true จะทำให้แสดงจำนวนบนคอลัมน์ ไม่จำเป็นต้องมี Presentation หรือ Workbook อินพุต
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.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();
let workbook = chart.getChartData().getChartDataWorkbook();
let worksheetIndex = 0;
workbook.clear(worksheetIndex);
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 1, "Profit");
let series = chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.Waterfall);
let categories = [ "Revenue", "Cost of sales", "Gross profit", "Operating expenses", "Other income", "Net profit" ];
let values = [ 1000, -400, 600, -200, 50, 450 ];
for (let i = 0; i < categories.length; i++)
{
let categoryCell = workbook.getCell(worksheetIndex, i + 1, 0, categories[i]);
chart.getChartData().getCategories().add(categoryCell);
let valueCell = workbook.getCell(worksheetIndex, i + 1, 1, values[i]);
series.getDataPoints().addDataPointForWaterfallSeries(valueCell);
}
// จุดที่สามและหกเป็นค่ารวม ไม่ใช่การเปลี่ยนแปลงเพิ่มเติม.
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", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
เรียกเมธอดระดับซีรีส์ setShowConnectorLines ด้วย true เพื่อแสดงเส้นเชื่อมคอลัมน์ต่อเนื่อง ทำให้ค่าที่กำลังดำเนินง่ายต่อการติดตาม หากต้องการซ่อนเส้นเหล่านี้ ให้แทนที่การเรียกเมธอดในตัวอย่างด้วย series.setShowConnectorLines(false); ก่อนบันทึก การเปลี่ยนแปลงนี้ส่งผลต่อการมองเห็นเส้นเท่านั้น ไม่กระทบข้อมูลหรือค่ารวม
ผลลัพธ์ที่บันทึกแสดงกำไรขั้นต้น 600 และกำไรสุทธิ 450 พร้อมทั้งค่ารวมทั้งสองที่เริ่มจากศูนย์:

สร้างแผนภูมิหุ้น
แผนภูมิจดหุ้นใช้แสดงข้อมูลการเงิน เช่น ราคาเปิด, สูง, ต่ำ, ปิด ช่วยวิเคราะห์แนวโน้มตลาดและความผันผวน ให้ข้อมูลเชิงลึกสำคัญเกี่ยวกับประสิทธิภาพหุ้น ช่วยนักลงทุนและนักวิเคราะห์ตัดสินใจอย่างมีข้อมูล
ตัวอย่างนี้สร้างแผนภูมิเปิด‑สูง‑ต่ำ‑ปิดด้วยซีรีส์สี่ชุดตามลำดับและหมวดหมู่สามชุด เมธอด setUpDownBars เปิดใช้งานบาร์ขึ้น‑ลง และ getHiLowLinesFormat ควบคุมเส้นสูง‑ต่ำที่แสดงช่วงราคา ส่วนเส้นของแต่ละซีรีส์และเส้นกริดแนวนอนย่อยถูกซ่อน
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.OpenHighLowClose, 20, 20, 500, 300, false);
let workbook = chart.getChartData().getChartDataWorkbook();
let categoryCell = workbook.getCell(0, 1, 0, "A");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 2, 0, "B");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, 3, 0, "C");
chart.getChartData().getCategories().add(categoryCell);
let seriesNameCell = workbook.getCell(0, 0, 1, "Open");
chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.OpenHighLowClose);
seriesNameCell = workbook.getCell(0, 0, 2, "High");
chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.OpenHighLowClose);
seriesNameCell = workbook.getCell(0, 0, 3, "Low");
chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.OpenHighLowClose);
seriesNameCell = workbook.getCell(0, 0, 4, "Close");
chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.OpenHighLowClose);
let series = chart.getChartData().getSeries().get_Item(0);
let valueCell = workbook.getCell(0, 1, 1, 72);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 1, 25);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 1, 38);
series.getDataPoints().addDataPointForStockSeries(valueCell);
series = chart.getChartData().getSeries().get_Item(1);
valueCell = workbook.getCell(0, 1, 2, 172);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 2, 57);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 2, 57);
series.getDataPoints().addDataPointForStockSeries(valueCell);
series = chart.getChartData().getSeries().get_Item(2);
valueCell = workbook.getCell(0, 1, 3, 12);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 3, 12);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 3, 13);
series.getDataPoints().addDataPointForStockSeries(valueCell);
series = chart.getChartData().getSeries().get_Item(3);
valueCell = workbook.getCell(0, 1, 4, 25);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 2, 4, 38);
series.getDataPoints().addDataPointForStockSeries(valueCell);
valueCell = workbook.getCell(0, 3, 4, 50);
series.getDataPoints().addDataPointForStockSeries(valueCell);
chart.getChartData().getSeriesGroups().get_Item(0).getUpDownBars().setUpDownBars(true);
chart.getChartData().getSeriesGroups().get_Item(0).getHiLowLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
for (let i = 0; i < chart.getChartData().getSeries().size(); i++)
{
let stockSeries = chart.getChartData().getSeries().get_Item(i);
stockSeries.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
}
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
presentation.save("Stock-chart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

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

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

สร้างแผนภูมิฮิสโตแกรม
แผนภูมิฮิสโตแกรมใช้แสดงการกระจายของข้อมูลเชิงตัวเลขโดยจัดกลุ่มค่าเป็นช่วงหรือบินส์ มีประโยชน์ในการระบุรูปแบบข้อมูล เช่น ความถี่, ความเอียง, การกระจาย และการตรวจจับค่าผิดปกติในชุดข้อมูล
ตัวอย่างนี้สร้างฮิสโตแกรมจากห้าค่าเชิงตัวเลขรวมถึงค่าติดลบ การเรียก setAggregationType ด้วยประเภทการรวมอัตโนมัติทำให้แผนภูมิกำหนดบินส์ที่ใช้แสดงการกระจายโดยอัตโนมัติ
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.Histogram, 20, 20, 500, 300);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
let workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
let series = chart.getChartData().getSeries().add(aspose.slides.ChartType.Histogram);
let valueCell = workbook.getCell(0, "A1", 15);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
valueCell = workbook.getCell(0, "A2", -41);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
valueCell = workbook.getCell(0, "A3", 16);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
valueCell = workbook.getCell(0, "A4", 10);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
valueCell = workbook.getCell(0, "A5", -23);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
valueCell = workbook.getCell(0, "A6", 16);
series.getDataPoints().addDataPointForHistogramSeries(valueCell);
chart.getAxes().getHorizontalAxis().setAggregationType(aspose.slides.AxisAggregationType.Automatic);
presentation.save("Histogram.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

สร้างแผนภูมิเรดาร์
แผนภูมิเรดาร์ใช้แสดงข้อมูลหลายตัวแปรในรูปแบบสองมิติ ช่วยให้เปรียบเทียบหลายตัวแปรพร้อมกันได้ง่าย เหมาะสำหรับระบุรูปแบบ, จุดแข็ง, จุดอ่อนของเมตริกหรือคุณลักษณะหลายประการ
ตัวอย่างนี้ใช้ addChart เพื่อสร้างแผนภูมิเบรดาร์ด้วยซีรีส์และหมวดหมู่เริ่มต้น จากนั้นบันทึก Presentation เป็น PPTX
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
slide.getShapes().addChart(aspose.slides.ChartType.Radar, 20, 20, 500, 300);
presentation.save("Radar-chart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

สร้างแผนภูมิหลายประเภท
แผนภูมิหลายประเภทใช้เพื่อแสดงข้อมูลที่มีการจัดกลุ่มเชิงหมวดหลายระดับ ช่วยให้เปรียบเทียบค่าในหลายมิติพร้อมกัน เหมาะเมื่อจำเป็นต้องวิเคราะห์แนวโน้มและความสัมพันธ์ภายในชุดข้อมูลหลายชั้น
ตัวอย่างนี้สร้างแผนภูมิคอลัมน์แบบกลุ่มที่มีแปดหมวดจัดเป็นสี่คู่ เมธอด setGroupingItem ให้แต่ละคู่มีป้ายร่วมที่ระดับการจัดกลุ่ม 1 ทำให้แกนหมวดมีทั้งป้ายรายบุคคลและป้ายกลุ่ม
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
let workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);
let worksheetIndex = 0;
let categoryCell = workbook.getCell(0, "c2", "A");
let category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group1");
categoryCell = workbook.getCell(0, "c3", "B");
category = chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "c4", "C");
category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group2");
categoryCell = workbook.getCell(0, "c5", "D");
category = chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "c6", "E");
category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group3");
categoryCell = workbook.getCell(0, "c7", "F");
category = chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(0, "c8", "G");
category = chart.getChartData().getCategories().add(categoryCell);
category.getGroupingLevels().setGroupingItem(1, "Group4");
categoryCell = workbook.getCell(0, "c9", "H");
category = chart.getChartData().getCategories().add(categoryCell);
let seriesNameCell = workbook.getCell(0, "D1", "Series 1");
let series = chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.ClusteredColumn);
let valueCell = workbook.getCell(worksheetIndex, "D2", 10);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D3", 20);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D4", 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D5", 40);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D6", 50);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D7", 60);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D8", 70);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, "D9", 80);
series.getDataPoints().addDataPointForBarSeries(valueCell);
presentation.save("AsposeChart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

สร้างแผนภูมิแผนที่
แผนภูมิแผนที่ใช้แสดงข้อมูลภูมิศาสตร์โดยแมปข้อมูลไปยังตำแหน่งเฉพาะ เช่น ประเทศ, รัฐ, หรือเมือง มีประโยชน์ในการวิเคราะห์แนวโน้มภูมิภาค, ข้อมูลประชากร, และการกระจายเชิงพื้นที่ในรูปแบบที่ชัดเจนและดึงดูดสายตา
ตัวอย่างนี้ใช้ addChart เพื่อสร้างแผนภูมิแผนที่ด้วยข้อมูลภูมิศาสตร์เริ่มต้นและบันทึกเพื่อดูใน PowerPoint
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.Map, 20, 20, 500, 300);
presentation.save("mapChart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ผลลัพธ์:

Note
ภาพด้านบนแสดง Presentation ที่บันทึกแล้วเปิดใน PowerPoint Aspose.Slides เขียนแผนภูมิแผนที่และข้อมูลได้อย่างถูกต้อง แต่ไม่วาดแผนภูมิแผนที่เอง: เมื่อสไลด์ที่มีแผนภูมินี้ถูกเรนเดอร์เป็นภาพหรือแปลงเป็น PDF หรือ SVG พื้นที่แผนภูมิกลายเป็นสีขาว รูปทรงอื่นบนสไลด์เดียวกันไม่ได้รับผลกระทบสร้างแผนภูมิแบบผสม

แผนภูมิแบบผสม (หรือ combo chart) รวมสองประเภทแผนภูมิเข้าด้วยกันในกราฟเดียว ทำให้คุณสามารถเน้น, เปรียบเทียบ, หรือวิเคราะห์ความแตกต่างระหว่างชุดข้อมูลสองชุดขึ้นไป เพื่อช่วยระบุความสัมพันธ์ระหว่างข้อมูล
ตัวอย่างนี้รวมสองซีรีส์คอลัมน์แบบกลุ่มกับซีรีส์เส้นในสี่หมวด คอลัมน์ใช้แกนแนวตั้งหลัก ส่วน setPlotOnSecondAxis วางเส้นบนแกนแนวตั้งรองด้านขวา ฟังก์ชันช่วยกำหนดหัวเรื่อง, คำอธิบาย, ป้ายแกน, และเส้นกริด
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
function createComboChart()
{
let presentation = new aspose.slides.Presentation();
try
{
let chart = createChartWithFirstSeries(presentation.getSlides().get_Item(0));
addSecondSeriesToChart(chart);
addThirdSeriesToChart(chart);
setPrimaryAxesFormat(chart);
setSecondaryAxesFormat(chart);
presentation.save("combo-chart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
}
function createChartWithFirstSeries(slide)
{
let chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 600, 400);
chart.setTitle(true);
chart.getChartTitle().addTextFrameForOverriding("Chart Title");
chart.getChartTitle().setOverlay(false);
let portionFormat = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
portionFormat.setFontBold(java.newByte(aspose.slides.NullableBool.False));
portionFormat.setFontHeight(18);
chart.getLegend().setPosition(aspose.slides.LegendPositionType.Bottom);
chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
let workbook = chart.getChartData().getChartDataWorkbook();
let worksheetIndex = 0;
let categoryCell = workbook.getCell(worksheetIndex, 1, 0, "Category 1");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 2, 0, "Category 2");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 3, 0, "Category 3");
chart.getChartData().getCategories().add(categoryCell);
categoryCell = workbook.getCell(worksheetIndex, 4, 0, "Category 4");
chart.getChartData().getCategories().add(categoryCell);
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 1, "Series 1");
let series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
series.getParentSeriesGroup().setOverlap(java.newByte(-25));
series.getParentSeriesGroup().setGapWidth(220);
let valueCell = workbook.getCell(worksheetIndex, 1, 1, 4.3);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 1, 2.5);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 1, 3.5);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 4, 1, 4.5);
series.getDataPoints().addDataPointForBarSeries(valueCell);
return chart;
}
function addSecondSeriesToChart(chart)
{
let workbook = chart.getChartData().getChartDataWorkbook();
let worksheetIndex = 0;
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 2, "Series 2");
let series = chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.ClusteredColumn);
series.getParentSeriesGroup().setOverlap(java.newByte(-25));
series.getParentSeriesGroup().setGapWidth(220);
let valueCell = workbook.getCell(worksheetIndex, 1, 2, 2.4);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 2, 4.4);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 2, 1.8);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 4, 2, 2.8);
series.getDataPoints().addDataPointForBarSeries(valueCell);
}
function addThirdSeriesToChart(chart)
{
let workbook = chart.getChartData().getChartDataWorkbook();
let worksheetIndex = 0;
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 3, "Series 3");
let series = chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.Line);
let valueCell = workbook.getCell(worksheetIndex, 1, 3, 2.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 3, 2.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 3, 3.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 4, 3, 5.0);
series.getDataPoints().addDataPointForLineSeries(valueCell);
series.setPlotOnSecondAxis(true);
}
function setPrimaryAxesFormat(chart)
{
let horizontalAxis = chart.getAxes().getHorizontalAxis();
horizontalAxis.getTextFormat().getPortionFormat().setFontHeight(12);
horizontalAxis.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
setAxisTitle(horizontalAxis, "X Axis");
let verticalAxis = chart.getAxes().getVerticalAxis();
verticalAxis.getTextFormat().getPortionFormat().setFontHeight(12);
verticalAxis.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
setAxisTitle(verticalAxis, "Y Axis 1");
let majorGridLinesFormat = verticalAxis.getMajorGridLinesFormat().getLine().getFillFormat();
majorGridLinesFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
majorGridLinesFormat.getSolidFillColor().setColor(java.newInstanceSync("java.awt.Color", 217, 217, 217));
}
function setSecondaryAxesFormat(chart)
{
let secondaryHorizontalAxis = chart.getAxes().getSecondaryHorizontalAxis();
secondaryHorizontalAxis.setPosition(aspose.slides.AxisPositionType.Bottom);
secondaryHorizontalAxis.setCrossType(aspose.slides.CrossesType.Maximum);
secondaryHorizontalAxis.setVisible(false);
secondaryHorizontalAxis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
secondaryHorizontalAxis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
let secondaryVerticalAxis = chart.getAxes().getSecondaryVerticalAxis();
secondaryVerticalAxis.setPosition(aspose.slides.AxisPositionType.Right);
secondaryVerticalAxis.getTextFormat().getPortionFormat().setFontHeight(12);
secondaryVerticalAxis.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
secondaryVerticalAxis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
secondaryVerticalAxis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
setAxisTitle(secondaryVerticalAxis, "Y Axis 2");
}
function setAxisTitle(axis, axisTitle)
{
axis.setTitle(true);
axis.getTitle().setOverlay(false);
let titlePortionFormat = axis.getTitle().addTextFrameForOverriding(axisTitle).getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
titlePortionFormat.setFontBold(java.newByte(aspose.slides.NullableBool.False));
titlePortionFormat.setFontHeight(12);
}
createComboChart();
อัปเดตแผนภูมิ
Aspose.Slides for Node.js via Java ช่วยให้คุณอัปเดตแผนภูมิ PowerPoint โดยปรับเปลี่ยนข้อมูลแผนภูมิ, การจัดรูปแบบ, และสไตล์ ความสามารถนี้ทำให้การรักษา Presentation ให้อัพ‑เดตด้วยเนื้อหาไดนามิกง่ายขึ้นและทำให้แผนภูมิสะท้อนข้อมูลและมาตรฐานการแสดงผลล่าสุดได้อย่างแม่นยำ
ตัวอย่างนี้อัปเดตแผนภูมิที่ชื่อ “My chart” บนสไลด์แรกของ Presentation ที่มีอยู่ จำเป็นต้องมีสองซีรีส์คอลัมน์ที่มีจุดข้อมูลสามจุดแต่ละชุด พร้อมป้ายหมวดในคอลัมน์ A และชื่อซีรีส์ในแถวแรกของชีตแรก ใช้ getCell เพื่อเปลี่ยนเซลล์ในชุดงาน อัปเดตค่าซีรีส์ เพิ่มซีรีส์ที่สาม และเปลี่ยนประเภทแผนภูมเป็นลูกบาศก์แบบกลุ่มด้วย setType
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
let chartName = "My chart";
let presentation = new aspose.slides.Presentation("ExistingChart.pptx");
try
{
let slide = presentation.getSlides().get_Item(0);
for (let i = 0; i < slide.getShapes().size(); i++)
{
let shape = slide.getShapes().get_Item(i);
if (java.instanceOf(shape, "com.aspose.slides.IChart") && chartName === shape.getName())
{
let chart = shape;
let worksheetIndex = 0;
let workbook = chart.getChartData().getChartDataWorkbook();
workbook.getCell(worksheetIndex, 1, 0, "Modified Category 1");
workbook.getCell(worksheetIndex, 2, 0, "Modified Category 2");
let series = chart.getChartData().getSeries().get_Item(0);
workbook.getCell(worksheetIndex, 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(worksheetIndex, 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);
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 3, "Series 3");
series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
let valueCell = workbook.getCell(worksheetIndex, 1, 3, 20);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 2, 3, 50);
series.getDataPoints().addDataPointForBarSeries(valueCell);
valueCell = workbook.getCell(worksheetIndex, 3, 3, 30);
series.getDataPoints().addDataPointForBarSeries(valueCell);
chart.setType(aspose.slides.ChartType.ClusteredCylinder);
}
}
presentation.save("AsposeChartModified.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
กำหนดช่วงข้อมูลสำหรับแผนภูมิ
เพื่อดูช่วงที่ใช้โดยแผนภูมิที่มีอยู่ ให้ดู ดึงข้อมูลช่วงของแผนภูมิ
Aspose.Slides for Node.js via Java รองรับความยืดหยุ่นในการกำหนดช่วงข้อมูลเฉพาะจาก Worksheet เป็นแหล่งข้อมูลสำหรับแผนภูมิของคุณ หมายความว่าคุณสามารถแม็พส่วนของ Worksheet ไปยังแผนภูมิได้โดยตรง ควบคุมว่าเซลล์ใดบ้างที่มีส่วนในซีรีส์และหมวดของแผนภูมิ ผลลัพธ์คือคุณสามารถอัปเดตและซิงโครไนซ์แผนภูมิของคุณกับการเปลี่ยนแปลงข้อมูลล่าสุดใน Worksheet ได้อย่างง่ายดาย ทำให้ Presentation ของคุณแสดงข้อมูลที่เป็นปัจจุบันและถูกต้อง
ตัวอย่างนี้ใช้ setRange เพื่อกำหนดแหล่งข้อมูลของแผนภูมิที่ชื่อ “My chart” บนสไลด์แรกของ Presentation ที่มีอยู่เป็น Sheet1!A1:B4 สมุดงานแผนภูมิต้องมี Worksheet นั้นและช่วงที่เต็มด้วยหัวซีรีส์ในแถวแรก, ป้ายหมวดในคอลัมน์ A, และค่าสามค่าในคอลัมน์ B
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
let chartName = "My chart";
let presentation = new aspose.slides.Presentation("ExistingChart.pptx");
try
{
let slide = presentation.getSlides().get_Item(0);
for (let i = 0; i < slide.getShapes().size(); i++)
{
let shape = slide.getShapes().get_Item(i);
if (java.instanceOf(shape, "com.aspose.slides.IChart") && chartName === shape.getName())
{
let chart = shape;
chart.getChartData().setRange("Sheet1!A1:B4");
}
}
presentation.save("SetDataRange.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
ใช้ตัวชี้เริ่มต้นในแผนภูมิ
เมื่อใช้ตัวชี้เริ่มต้นในแผนภูมิแต่ละซีรีส์จะได้รับสัญลักษณ์ตัวชี้เริ่มต้นที่แตกต่างโดยอัตโนมัติ
ตัวอย่างนี้ใช้ addChart เพื่อสร้างแผนภูมิที่มีสองซีรีส์เส้นและคำอธิบาย เมธอด getMarker รักษาสัญลักษณ์ตัวชี้ที่กำหนดอัตโนมัติ ซีรีส์แรกมีค่าลบและค่าที่หายไป (null) ส่วนซีรีส์ที่สองมีค่าทุกหมวด
const aspose = { slides: require("aspose.slides.via.java") };
let presentation = new aspose.slides.Presentation();
try
{
let slide = presentation.getSlides().get_Item(0);
let chart = slide.getShapes().addChart(aspose.slides.ChartType.LineWithMarkers, 10, 10, 400, 400);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
let workbook = chart.getChartData().getChartDataWorkbook();
let seriesNameCell = workbook.getCell(0, 0, 1, "Series 1");
let series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
let categoryCell = workbook.getCell(0, 1, 0, "C1");
chart.getChartData().getCategories().add(categoryCell);
let valueCell = workbook.getCell(0, 1, 1, 24);
series.getDataPoints().addDataPointForLineSeries(valueCell);
categoryCell = workbook.getCell(0, 2, 0, "C2");
chart.getChartData().getCategories().add(categoryCell);
valueCell = workbook.getCell(0, 2, 1, 23);
series.getDataPoints().addDataPointForLineSeries(valueCell);
categoryCell = workbook.getCell(0, 3, 0, "C3");
chart.getChartData().getCategories().add(categoryCell);
valueCell = workbook.getCell(0, 3, 1, -10);
series.getDataPoints().addDataPointForLineSeries(valueCell);
categoryCell = workbook.getCell(0, 4, 0, "C4");
chart.getChartData().getCategories().add(categoryCell);
valueCell = workbook.getCell(0, 4, 1, null);
series.getDataPoints().addDataPointForLineSeries(valueCell);
seriesNameCell = workbook.getCell(0, 0, 2, "Series 2");
let secondSeries = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
valueCell = workbook.getCell(0, 1, 2, 30);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(0, 2, 2, 10);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(0, 3, 2, 60);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
valueCell = workbook.getCell(0, 4, 2, 40);
secondSeries.getDataPoints().addDataPointForLineSeries(valueCell);
chart.setLegend(true);
chart.getLegend().setOverlay(false);
presentation.save("DefaultMarkersInChart.pptx", aspose.slides.SaveFormat.Pptx);
}
finally
{
presentation.dispose();
}
คำถามที่พบบ่อย
ประเภทแผนภูมิใดบ้างที่สนับสนุนโดย Aspose.Slides for Node.js via Java?
Aspose.Slides for Node.js via Java รองรับแผนภูมิดังหลายประเภท รวมถึงบาร์, เส้น, วงกลม, พื้นที่, กระจาย, ฮิสโตแกรม, เรดาร์ และอื่น ๆ อีกมากมาย ความยืดหยุ่นนี้ทำให้คุณเลือกประเภทแผนภูมิที่เหมาะสมกับความต้องการการแสดงผลข้อมูลของคุณได้
ฉันจะเพิ่มแผนภูมิใหม่ลงในสไลด์ได้อย่างไร?
เพื่อเพิ่มแผนภูมิ คุณต้องสร้างอินสแตนซ์ของคลาส Presentation ดึงสไลด์ที่ต้องการโดยใช้ดัชนี และเรียกเมธอดเพื่อเพิ่มแผนภูมิ โดยระบุประเภทแผนภูมิและข้อมูลเริ่มต้น กระบวนการนี้จะผสานแผนภูมิลงใน Presentation ของคุณโดยตรง
ฉันจะอัปเดตข้อมูลที่แสดงในแผนภูมิได้อย่างไร?
คุณสามารถอัปเดตข้อมูลของแผนภูมิได้โดยเข้าถึงสมุดงานข้อมูลของแผนภูมิ (ChartDataWorkbook) ล้างซีรีส์และหมวดเริ่มต้นใด ๆ แล้วเพิ่มข้อมูลที่กำหนดเอง วิธีนี้ทำให้คุณรีเฟรชแผนภูมิเพื่อแสดงข้อมูลล่าสุดได้โดยโปรแกรมเมติก
สามารถปรับแต่งรูปลักษณ์ของแผนภูมิได้หรือไม่?
ใช่ Aspose.Slides for Node.js via Java มีตัวเลือกการปรับแต่งอย่างกว้างขวาง คุณสามารถแก้ไขสี, ฟอนต์, ป้าย, คำอธิบาย, และองค์ประกอบการจัดรูปแบบอื่น ๆ เพื่อให้แผนภูมิตรงตามข้อกำหนดการออกแบบเฉพาะของคุณ