สร้างหรืออัปเดตแผนภูมิการนำเสนอ PowerPoint ใน C++
ภาพรวม
บทความนี้ให้คำแนะนำอย่างครบถ้วนเกี่ยวกับวิธีการสร้างและปรับแต่งแผนภูมิด้วย Aspose.Slides คุณจะได้เรียนรู้วิธีการเพิ่มแผนภูมิลงในสไลด์โดยโปรแกรมระบุข้อมูลลงไปและใช้ตัวเลือกการจัดรูปแบบต่างๆ เพื่อให้ตรงตามความต้องการออกแบบของคุณ ตลอดบทความจะมีตัวอย่างโค้ดโดยละเอียดแสดงขั้นตอนแต่ละขั้น ตั้งแต่การเริ่มต้น Presentation และวัตถุแผนภูมิ ไปจนถึงการกำหนด Series, Axes, และ Legends การทำตามคำแนะนำนี้จะทำให้คุณเข้าใจการผสานการสร้างแผนภูมิกระบวนการแบบไดนามิกเข้าไปในแอปพลิเคชันของคุณ ทำให้การสร้างงานนำเสนอที่ขับเคลื่อนด้วยข้อมูลเป็นเรื่องง่ายและรวดเร็ว
สร้างแผนภูมิ
แผนภูมิช่วยให้ผู้ใช้มองเห็นข้อมูลและค้นพบข้อสรุปได้อย่างรวดเร็ว ซึ่งอาจไม่เห็นได้ชัดจากตารางหรือสเปรดชีต
ทำไมต้องสร้างแผนภูมิ?
โดยใช้แผนภูมิคุณสามารถ
- รวมรวม ย่อหรือสรุปข้อมูลจำนวนมากในสไลด์เดียวของงานนำเสนอ
- เปิดเผยรูปแบบและแนวโน้มของข้อมูล
- สรุปทิศทางและโมเมนตัมของข้อมูลตามเวลา หรือเทียบกับหน่วยวัดเฉพาะ
- ระบุจุดที่เบี่ยงเบน ข้อผิดพลาด หรือข้อมูลที่ไม่มีความหมาย
- สื่อสารหรือแสดงข้อมูลที่ซับซ้อน
ใน PowerPoint คุณสามารถสร้างแผนภูมิได้ผ่านเมนู Insert ซึ่งให้เทมเพลตสำหรับออกแบบแผนภูมิหลายประเภท ด้วย Aspose.Slides คุณสามารถสร้างแผนภูมิตามประเภทมาตรฐานและแผนภูมิแบบกำหนดเองได้
สร้างแผนภูมิปกติ
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนีของสไลด์
- เพิ่มแผนภูมิพร้อมข้อมูลบางส่วนและระบุประเภทแผนภูมิที่ต้องการ
- เพิ่มชื่อเรื่องให้กับแผนภูมิ
- เข้าถึง worksheet ของข้อมูลแผนภูมิ
- ลบ Series และ Category ที่เป็นค่าเริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- เพิ่มข้อมูลใหม่ให้กับ Series ของแผนภูมิ
- กำหนดสีเติมให้กับ Series ของแผนภูมิ
- เพิ่มป้ายกำกับให้กับ Series ของแผนภูมิ
- บันทึกไฟล์ Presentation ที่แก้ไขไว้เป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิปกติ:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/NormalCharts_out.pptx";
// สร้างอินสแตนซ์ของคลาสการนำเสนอที่เป็นไฟล์ PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// เข้าถึงสไลด์แรก
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// เพิ่มแผนภูมิด้วยข้อมูลค่าเริ่มต้น
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);
// กำหนดดัชนีของแผ่นข้อมูลแผนภูมิ
int defaultWorksheetIndex = 0;
// ดึง worksheet ของข้อมูลแผนภูมิ
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();
// กำหนดชื่อเรื่องของแผนภูมิ
chart->get_ChartTitle()->AddTextFrameForOverriding(u"Sample Title");
chart->get_ChartTitle()->get_TextFrameForOverriding()->get_TextFrameFormat()->set_CenterText ( NullableBool::True);
chart->get_ChartTitle()->set_Height(20);
chart->set_HasTitle( true);
// ลบ Series และ Category ที่สร้างโดยค่าเริ่มต้น
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
int s = chart->get_ChartData()->get_Series()->get_Count();
s = chart->get_ChartData()->get_Categories()->get_Count();
// เพิ่ม Series ใหม่
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box<System::String>(u"Series 2")), chart->get_Type());
// เพิ่ม Category
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<System::String>(u"Caetegoty 1")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<System::String>(u"Caetegoty 2")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box<System::String>(u"Caetegoty 3")));
// ดึง Series แผนภูมิแรก
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);
// เติมข้อมูลให้ Series
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<double>(20)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(50)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(30)));
// กำหนดสีเติมให้ Series
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
// ดึง Series แผนภูมิที่สอง
series = chart->get_ChartData()->get_Series()->idx_get(1);
// เติมข้อมูลให้ Series
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 2, ObjectExt::Box<double>(30)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(10)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(60)));
// กำหนดสีเติมให้ Series
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Green());
// กำหนดให้ป้ายกำกับแรกแสดงชื่อ Category
SharedPtr<IDataLabel> lbl = series->get_DataPoints()->idx_get(0)->get_Label();
lbl->get_DataLabelFormat()->set_ShowCategoryName(true);
lbl = series->get_DataPoints()->idx_get(1)->get_Label();
lbl->get_DataLabelFormat()->set_ShowSeriesName (true);
// แสดงค่าบนป้ายกำกับที่สาม
lbl = series->get_DataPoints()->idx_get(2)->get_Label();
lbl->get_DataLabelFormat()->set_ShowValue (true);
lbl->get_DataLabelFormat()->set_ShowSeriesName(true);
lbl->get_DataLabelFormat()->set_Separator (u"/");
// บันทึกงานนำเสนอ
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิกระจาย (Scattered Charts)
แผนภูมิกระจาย (หรือ scatter plot, x‑y graph) มักใช้เพื่อตรวจสอบรูปแบบหรือแสดงความสัมพันธ์ระหว่างตัวแปรสองตัว
คุณอาจต้องการใช้แผนภูมิกระจายเมื่อ
- มีข้อมูลตัวเลขเป็นคู่
- มีสองตัวแปรที่สัมพันธ์กันอย่างดี
- ต้องการตรวจสอบว่าตัวแปรสองตัวมีความเกี่ยวข้องหรือไม่
- มีตัวแปรอิสระที่มีค่าหลายค่าเมื่อเทียบกับตัวแปรตาม
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิกระจายพร้อมชุดเครื่องหมายต่าง ๆ:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/ScatteredChart_out.pptx";
// สร้างอินสแตนซ์ของคลาสการนำเสนอที่เป็นไฟล์ PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// เข้าถึงสไลด์แรก
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// เพิ่มแผนภูมิด้วยข้อมูลค่าเริ่มต้น
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ScatterWithSmoothLines, 0, 0, 500, 500);
// กำหนดชื่อเรื่องของแผนภูมิ
chart->get_ChartTitle()->AddTextFrameForOverriding(u"Sample Title");
chart->get_ChartTitle()->get_TextFrameForOverriding()->get_TextFrameFormat()->set_CenterText(NullableBool::True);
chart->get_ChartTitle()->set_Height(20);
chart->set_HasTitle(true);
// ลบ Series ที่สร้างโดยค่าเริ่มต้น
chart->get_ChartData()->get_Series()->Clear();
// กำหนดดัชนีสำหรับแผ่นข้อมูลแผนภูมิ
int defaultWorksheetIndex = 0;
// ดึง worksheet ของข้อมูลแผนภูมิ
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();
// เพิ่ม Series ใหม่
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 1, 3, ObjectExt::Box<System::String>(u"Series 2")), chart->get_Type());
// ดึง Series แผนภูมิแรก
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);
// เพิ่มจุดใหม่ (1:3)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(1)), fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(3)));
// เพิ่มจุดใหม่ (2:10)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(10)));
// แก้ไขประเภท Series
series->set_Type (ChartType::ScatterWithStraightLinesAndMarkers);
// เปลี่ยน Marker ของ Series แผนภูมิ
series->get_Marker()->set_Size (10);
series->get_Marker()->set_Symbol(MarkerStyleType::Star);
// ดึง Series แผนภูมิที่สอง
series = chart->get_ChartData()->get_Series()->idx_get(1);
// เพิ่มจุดใหม่ (5:2)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 2, 4, ObjectExt::Box<double>(2)));
// เพิ่มจุดใหม่ (3:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 3, ObjectExt::Box<double>(3)), fact->GetCell(defaultWorksheetIndex, 3, 4, ObjectExt::Box<double>(1)));
// เพิ่มจุดใหม่ (2:2)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 4, 3, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 4, 4, ObjectExt::Box<double>(2)));
// เพิ่มจุดใหม่ (5:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 5, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 5, 4, ObjectExt::Box<double>(1)));
// เปลี่ยน Marker ของ Series แผนภูมิ
series->get_Marker()->set_Size ( 10);
series->get_Marker()->set_Symbol(MarkerStyleType::Circle);
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_IsColorVaried(true);
SharedPtr<IChartDataPoint> point = series->get_DataPoints()->idx_get(0);
point->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Cyan());
// กำหนดสีขอบเซกเตอร์
point->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Gray());
point->get_Format()->get_Line()->set_Width ( 3.0);
point->get_Format()->get_Line()->set_Style(LineStyle::ThinThick);
point->get_Format()->get_Line()->set_DashStyle(LineDashStyle::DashDot);
SharedPtr<IChartDataPoint> point1 = series->get_DataPoints()->idx_get(1);
point1->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point1->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Brown());
// กำหนดสีขอบเซกเตอร์
point1->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point1->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());
point1->get_Format()->get_Line()->set_Width (3.0);
point1->get_Format()->get_Line()->set_Style(LineStyle::Single);
point1->get_Format()->get_Line()->set_DashStyle(LineDashStyle::LargeDashDot);
SharedPtr<IChartDataPoint> point2 = series->get_DataPoints()->idx_get(2);
point2->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point2->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Coral());
// กำหนดสีขอบเซกเตอร์
point2->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point2->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
point2->get_Format()->get_Line()->set_Width ( 2.0);
point2->get_Format()->get_Line()->set_Style(LineStyle::ThickThin);
point2->get_Format()->get_Line()->set_DashStyle(LineDashStyle::LargeDashDotDot);
// สร้างป้ายกำกับที่กำหนดเองสำหรับแต่ละ Category ของ Series ใหม่
SharedPtr<IDataLabel> lbl1 = series->get_DataPoints()->idx_get(0)->get_Label();
// lbl.ShowCategoryName = true;
lbl1->get_DataLabelFormat()->set_ShowValue(true);
SharedPtr<IDataLabel> lbl2 = series->get_DataPoints()->idx_get(1)->get_Label();
lbl2->get_DataLabelFormat()->set_ShowValue(true);
lbl2->get_DataLabelFormat()->set_ShowLegendKey(true);
lbl2->get_DataLabelFormat()->set_ShowPercentage(true);
SharedPtr<IDataLabel> lbl3 = series->get_DataPoints()->idx_get(2)->get_Label();
lbl3->get_DataLabelFormat()->set_ShowSeriesName(true);
lbl3->get_DataLabelFormat()->set_ShowPercentage(true);
// แสดงเส้นนำสำหรับแผนภูมิ
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLeaderLines(true);
// กำหนดมุมการหมุนสำหรับเซกเตอร์ของแผนภูมิพาย
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_FirstSliceAngle(180);
// บันทึกงานนำเสนอ
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมวงกลม (Pie Charts)
แผนภูมวงกลมเหมาะที่สุดสำหรับแสดงความสัมพันธ์ส่วนต่อส่วนของข้อมูล โดยเฉพาะเมื่อข้อมูลมีป้ายชื่อแบบหมวดหมู่พร้อมค่าตัวเลข อย่างไรก็ตาม หากข้อมูลของคุณมีหลายส่วนหรือหลายป้ายชื่อ ควรพิจารณาใช้แผนภูมิแท่งแทน
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิพร้อมข้อมูลเริ่มต้นและระบุประเภทที่ต้องการ (
ChartType.Pie) - เข้าถึง IChartDataWorkbook ของแผนภูมิ
- ลบ Series และ Category เริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- เพิ่มข้อมูลใหม่ให้กับ Series ของแผนภูมิ
- เพิ่มจุดใหม่ให้แผนภูมิและกำหนดสีกำหนดเองสำหรับส่วนของแผนภูมวงกลม
- ตั้งค่าป้ายกำกับสำหรับ Series
- ตั้งค่า leader lines สำหรับป้ายกำกับ Series
- ตั้งค่ามุมการหมุนสำหรับสไลด์แผนภูมวงกลม
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมวงกลม:
// The path to the documents directory.
const String outPath = u"../out/PieChart_out.pptx";
//Instantiates a Presentation class that represents a PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
//Accesses first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Adds a chart with default data
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Pie, 0, 0, 500, 500);
// Sets the chart Title
chart->get_ChartTitle()->AddTextFrameForOverriding(u"Sample Title");
chart->get_ChartTitle()->get_TextFrameForOverriding()->get_TextFrameFormat()->set_CenterText(NullableBool::True);
chart->get_ChartTitle()->set_Height(20);
chart->set_HasTitle(true);
// Deletes the default generated series and categories
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
// Sets the index of chart data sheet
int defaultWorksheetIndex = 0;
// Gets the chart data worksheet
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();
// Adds Catrgories
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<System::String>(u"First Qtr")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<System::String>(u"2nd Qtr")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box<System::String>(u"3ed Qtr")));
// Adds a new series
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());
// Takes the first chart series
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);
// Populates the series data
series->get_DataPoints()->AddDataPointForPieSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<double>(20)));
series->get_DataPoints()->AddDataPointForPieSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(50)));
series->get_DataPoints()->AddDataPointForPieSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(30)));
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_IsColorVaried(true);
SharedPtr<IChartDataPoint> point = series->get_DataPoints()->idx_get(0);
point->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Cyan());
// Sets the Sector border
point->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Gray());
point->get_Format()->get_Line()->set_Width ( 3.0);
point->get_Format()->get_Line()->set_Style( LineStyle::ThinThick);
point->get_Format()->get_Line()->set_DashStyle ( LineDashStyle::DashDot);
SharedPtr<IChartDataPoint> point1 = series->get_DataPoints()->idx_get(1);
point1->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point1->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Brown());
// Sets the Sector border
point1->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point1->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());
point1->get_Format()->get_Line()->set_Width (3.0);
point1->get_Format()->get_Line()->set_Style(LineStyle::Single);
point1->get_Format()->get_Line()->set_DashStyle(LineDashStyle::LargeDashDot);
SharedPtr<IChartDataPoint> point2 = series->get_DataPoints()->idx_get(2);
point2->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point2->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Coral());
// Sets the Sector border
point2->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point2->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
point2->get_Format()->get_Line()->set_Width (2.0);
point2->get_Format()->get_Line()->set_Style(LineStyle::ThickThin);
point2->get_Format()->get_Line()->set_DashStyle(LineDashStyle::LargeDashDotDot);
// Creates custom labels for each of categories for new series
SharedPtr<IDataLabel> lbl1 = series->get_DataPoints()->idx_get(0)->get_Label();
// lbl.ShowCategoryName = true;
lbl1->get_DataLabelFormat()->set_ShowValue(true);
SharedPtr<IDataLabel> lbl2 = series->get_DataPoints()->idx_get(1)->get_Label();
lbl2->get_DataLabelFormat()->set_ShowValue(true);
lbl2->get_DataLabelFormat()->set_ShowLegendKey(true);
lbl2->get_DataLabelFormat()->set_ShowPercentage(true);
SharedPtr<IDataLabel> lbl3 = series->get_DataPoints()->idx_get(2)->get_Label();
lbl3->get_DataLabelFormat()->set_ShowSeriesName(true);
lbl3->get_DataLabelFormat()->set_ShowPercentage(true);
// Sets the series to show leader lines for the chart
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLeaderLines ( true);
// Sets the rotation angle for the pie chart sectors
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_FirstSliceAngle ( 180);
// Saves the presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมเส้น (Line Charts)
แผนภูมเส้น (หรือ line graph) เหมาะกับสถานการณ์ที่ต้องการแสดงการเปลี่ยนแปลงของค่าเมื่อเวลาผ่านไป การใช้แผนภูมเส้นช่วยให้เปรียบเทียบข้อมูลหลายชุดพร้อมกัน ติดตามแนวโน้มตามเวลา เน้นจุดที่ผิดปกติใน Series ฯลฯ
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิพร้อมข้อมูลเริ่มต้นและระบุประเภท (
ChartType::Line) - เข้าถึง IChartDataWorkbook ของแผนภูมิ
- ลบ Series และ Category เริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- เพิ่มข้อมูลใหม่ให้กับ Series ของแผนภูมิ
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมเส้น:
auto pres = System::MakeObject<Presentation>();
System::SharedPtr<IChart> lineChart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::Line, 10.0f, 50.0f, 600.0f, 350.0f);
pres->Save(u"lineChart.pptx", SaveFormat::Pptx);
โดยค่าเริ่มต้น จุดบนแผนภูมเส้นจะเชื่อมต่อด้วยเส้นตรงต่อเนื่อง หากต้องการให้จุดเชื่อมต่อด้วยเส้นประ สามารถกำหนด dash type ที่ต้องการได้ดังนี้:
System::SharedPtr<IChart> lineChart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::Line, 10.0f, 50.0f, 600.0f, 350.0f);
for (auto&& series : lineChart->get_ChartData()->get_Series())
{
series->get_Format()->get_Line()->set_DashStyle(LineDashStyle::Dash);
}
สร้างแผนภูมิ Tree Map
แผนภูมิ Tree Map เหมาะสำหรับข้อมูลการขายเมื่อคุณต้องการแสดงขนาดสัมพัทธ์ของหมวดหมู่ข้อมูลและในขณะเดียวกันดึงความสนใจไปยังรายการที่เป็นผู้มีส่วนร่วมมากในแต่ละหมวดหมู่
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิพร้อมข้อมูลเริ่มต้นและระบุประเภท (
ChartType.TreeMap) - เข้าถึง IChartDataWorkbook ของแผนภูมิ
- ลบ Series และ Category เริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- เพิ่มข้อมูลใหม่ให้กับ Series ของแผนภูมิ
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิ Tree Map:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/TreemapChart_out.pptx";
//Instantiates a Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Accesses the first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
System::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Treemap, 50, 50, 500, 400);
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();
wb->Clear(0);
// Branch 1
System::SharedPtr<IChartCategory> leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C1", System::ObjectExt::Box<System::String>(u"Leaf1")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem1"));
leaf->get_GroupingLevels()->SetGroupingItem(2, System::ObjectExt::Box<System::String>(u"Branch1"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C2", System::ObjectExt::Box<System::String>(u"Leaf2")));
leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C3", System::ObjectExt::Box<System::String>(u"Leaf3")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem2"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C4", System::ObjectExt::Box<System::String>(u"Leaf4")));
// Branch 2
leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C5", System::ObjectExt::Box<System::String>(u"Leaf5")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem3"));
leaf->get_GroupingLevels()->SetGroupingItem(2, System::ObjectExt::Box<System::String>(u"Branch2"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C6", System::ObjectExt::Box<System::String>(u"Leaf6")));
leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C7", System::ObjectExt::Box<System::String>(u"Leaf7")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem4"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C8", System::ObjectExt::Box<System::String>(u"Leaf8")));
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::Treemap);
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowCategoryName(true);
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D1", System::ObjectExt::Box<int32_t>(4)));
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D2", System::ObjectExt::Box<int32_t>(5)));
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D3", System::ObjectExt::Box<int32_t>(3)));
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D4", System::ObjectExt::Box<int32_t>(6)));
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D5", System::ObjectExt::Box<int32_t>(9)));
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D6", System::ObjectExt::Box<int32_t>(9)));
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D7", System::ObjectExt::Box<int32_t>(4)));
series->get_DataPoints()->AddDataPointForTreemapSeries(wb->GetCell(0, u"D8", System::ObjectExt::Box<int32_t>(3)));
series->set_ParentLabelLayout(Aspose::Slides::Charts::ParentLabelLayoutType::Overlapping);
// Saves the presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิหุ้น (Stock Charts)
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิค่าเปิด‑สูง‑ต่ำ‑ปิดด้วยประเภท
ChartType.OpenHighLowClose - เข้าถึง IChartDataWorkbook ของแผนภูมิ
- ลบ Series และ Category เริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- เพิ่มข้อมูลใหม่ให้กับ Series ของแผนภูมิ
- ระบุรูปแบบ HiLowLines
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ดตัวอย่าง C++ สำหรับสร้างแผนภูมิหุ้น:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/AddStockChart_out.pptx";
//Instantiates a Presentation class that represents a PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
//Accesses the first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Adds a chart with default data
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::OpenHighLowClose, 0, 0, 500, 500);
// Sets the index for the chart data sheet
int defaultWorksheetIndex = 0;
// Gets the chart data worksheet
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();
// Deletes the default generated series and categories
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
// Adds catrgories
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<System::String>(u"A")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<System::String>(u"B")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box<System::String>(u"C")));
// Adds a new series
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Open")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box<System::String>(u"High")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 3, ObjectExt::Box<System::String>(u"Low")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 4, ObjectExt::Box<System::String>(u"Close")), chart->get_Type());
// Takes the first chart series
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);
// Populates the first series data
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<double>(72)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(25)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(38)));
series = chart->get_ChartData()->get_Series()->idx_get(1);
// Populates the second series data
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 1, 2, ObjectExt::Box<double>(172)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(57)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(57)));
series = chart->get_ChartData()->get_Series()->idx_get(2);
// Populates the second series data
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 1, 3, ObjectExt::Box<double>(12)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 2, 3, ObjectExt::Box<double>(12)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 3, 3, ObjectExt::Box<double>(13)));
series = chart->get_ChartData()->get_Series()->idx_get(3);
// Populates the second series data
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 1, 4, ObjectExt::Box<double>(25)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 2, 4, ObjectExt::Box<double>(38)));
series->get_DataPoints()->AddDataPointForStockSeries(fact->GetCell(defaultWorksheetIndex, 3, 4, ObjectExt::Box<double>(50)));
// Sets the series group
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->get_UpDownBars()->set_HasUpDownBars (true);
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->get_HiLowLinesFormat()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
for(int i=0;i<chart->get_ChartData()->get_Series()->get_Count();i++)
{
series = chart->get_ChartData()->get_Series()->idx_get(i);
series->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
}
// Saves the presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิ Box and Whisker
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิกับประเภท
ChartType.BoxAndWhisker - เข้าถึง IChartDataWorkbook ของแผนภูมิ
- ลบ Series และ Category เริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- เพิ่มข้อมูลใหม่ให้กับ Series ของแผนภูมิ
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิ Box and Whisker:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/BoxAndWhisker_out.pptx";
//Instantiates a Presentation class that represents a PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
//Accesses the first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
System::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::BoxAndWhisker, 50, 50, 500, 400);
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();
wb->Clear(0);
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A1", System::ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A2", System::ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A3", System::ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A4", System::ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A5", System::ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A6", System::ObjectExt::Box<System::String>(u"Category 1")));
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::BoxAndWhisker);
series->set_QuartileMethod(Aspose::Slides::Charts::QuartileMethodType::Exclusive);
series->set_ShowMeanLine(true);
series->set_ShowMeanMarkers(true);
series->set_ShowInnerPoints(true);
series->set_ShowOutlierPoints(true);
series->get_DataPoints()->AddDataPointForBoxAndWhiskerSeries(wb->GetCell(0, u"B1", System::ObjectExt::Box<int32_t>(15)));
series->get_DataPoints()->AddDataPointForBoxAndWhiskerSeries(wb->GetCell(0, u"B2", System::ObjectExt::Box<int32_t>(41)));
series->get_DataPoints()->AddDataPointForBoxAndWhiskerSeries(wb->GetCell(0, u"B3", System::ObjectExt::Box<int32_t>(16)));
series->get_DataPoints()->AddDataPointForBoxAndWhiskerSeries(wb->GetCell(0, u"B4", System::ObjectExt::Box<int32_t>(10)));
series->get_DataPoints()->AddDataPointForBoxAndWhiskerSeries(wb->GetCell(0, u"B5", System::ObjectExt::Box<int32_t>(23)));
series->get_DataPoints()->AddDataPointForBoxAndWhiskerSeries(wb->GetCell(0, u"B6", System::ObjectExt::Box<int32_t>(16)));
// บันทึกงานนำเสนอ
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิ Funnel
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิกับประเภท
ChartType.Funnel - บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิ Funnel:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/FunnelChart_out.pptx";
//สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นไฟล์ PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>();
//เข้าถึงสไลด์แรก
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
System::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Funnel, 50, 50, 500, 400);
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();
wb->Clear(0);
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A1", System::ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A2", System::ObjectExt::Box<System::String>(u"Category 2")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A3", System::ObjectExt::Box<System::String>(u"Category 3")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A4", System::ObjectExt::Box<System::String>(u"Category 4")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A5", System::ObjectExt::Box<System::String>(u"Category 5")));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"A6", System::ObjectExt::Box<System::String>(u"Category 6")));
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::Funnel);
series->get_DataPoints()->AddDataPointForFunnelSeries(wb->GetCell(0, u"B1", System::ObjectExt::Box<int32_t>(50)));
series->get_DataPoints()->AddDataPointForFunnelSeries(wb->GetCell(0, u"B2", System::ObjectExt::Box<int32_t>(100)));
series->get_DataPoints()->AddDataPointForFunnelSeries(wb->GetCell(0, u"B3", System::ObjectExt::Box<int32_t>(200)));
series->get_DataPoints()->AddDataPointForFunnelSeries(wb->GetCell(0, u"B4", System::ObjectExt::Box<int32_t>(300)));
series->get_DataPoints()->AddDataPointForFunnelSeries(wb->GetCell(0, u"B5", System::ObjectExt::Box<int32_t>(400)));
series->get_DataPoints()->AddDataPointForFunnelSeries(wb->GetCell(0, u"B6", System::ObjectExt::Box<int32_t>(500)));
// บันทึกงานนำเสนอ
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิ Sunburst
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิกับประเภท
ChartType.sunburst - บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิ Sunburst:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/SunburstChart_out.pptx";
// สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นไฟล์ PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// เข้าถึงสไลด์แรก
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
System::SharedPtr<IChart> chart=slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Sunburst, 50, 50, 500, 400);
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();
wb->Clear(0);
// สาขา 1
System::SharedPtr<IChartCategory> leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C1", System::ObjectExt::Box<System::String>(u"Leaf1")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem1"));
leaf->get_GroupingLevels()->SetGroupingItem(2, System::ObjectExt::Box<System::String>(u"Branch1"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C2", System::ObjectExt::Box<System::String>(u"Leaf2")));
leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C3", System::ObjectExt::Box<System::String>(u"Leaf3")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem2"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C4", System::ObjectExt::Box<System::String>(u"Leaf4")));
// สาขา 2
leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C5", System::ObjectExt::Box<System::String>(u"Leaf5")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem3"));
leaf->get_GroupingLevels()->SetGroupingItem(2, System::ObjectExt::Box<System::String>(u"Branch2"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C6", System::ObjectExt::Box<System::String>(u"Leaf6")));
leaf = chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C7", System::ObjectExt::Box<System::String>(u"Leaf7")));
leaf->get_GroupingLevels()->SetGroupingItem(1, System::ObjectExt::Box<System::String>(u"Stem4"));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, u"C8", System::ObjectExt::Box<System::String>(u"Leaf8")));
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::Sunburst);
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowCategoryName(true);
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D1", System::ObjectExt::Box<int32_t>(4)));
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D2", System::ObjectExt::Box<int32_t>(5)));
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D3", System::ObjectExt::Box<int32_t>(3)));
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D4", System::ObjectExt::Box<int32_t>(6)));
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D5", System::ObjectExt::Box<int32_t>(9)));
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D6", System::ObjectExt::Box<int32_t>(9)));
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D7", System::ObjectExt::Box<int32_t>(4)));
series->get_DataPoints()->AddDataPointForSunburstSeries(wb->GetCell(0, u"D8", System::ObjectExt::Box<int32_t>(3)));
// เขียนไฟล์งานนำเสนอลงดิสก์
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิ Histogram
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิกับข้อมูลบางส่วนและระบุประเภท
ChartType.Histogram - เข้าถึง IChartDataWorkbook ของแผนภูมิ
- ลบ Series และ Category เริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิ Histogram:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/HistogramChart_out.pptx";
// สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นไฟล์ PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// เข้าถึงสไลด์แรก
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
System::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Histogram, 50, 50, 500, 400);
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();
wb->Clear(0);
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::Histogram);
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A1", System::ObjectExt::Box<int32_t>(15)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A2", System::ObjectExt::Box<int32_t>(-41)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A3", System::ObjectExt::Box<int32_t>(16)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A4", System::ObjectExt::Box<int32_t>(10)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A5", System::ObjectExt::Box<int32_t>(-23)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A6", System::ObjectExt::Box<int32_t>(16)));
chart->get_Axes()->get_HorizontalAxis()->set_AggregationType(Aspose::Slides::Charts::AxisAggregationType::Automatic);
// บันทึกงานนำเสนอ
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิ Radar
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิกับข้อมูลบางส่วนและระบุประเภท
ChartType.Radar - บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิ Radar:
System::SharedPtr<Presentation> presentation = System::MakeObject<Presentation>();
presentation->get_Slides()->idx_get(0)->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Radar, 20.0f, 20.0f, 400.0f, 300.0f);
presentation->Save(u"Radar-chart.pptx", Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิ Multi‑Category
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- เพิ่มแผนภูมิกับข้อมูลเริ่มต้นและระบุประเภท
ChartType.ClusteredColumn - เข้าถึง IChartDataWorkbook ของแผนภูมิ
- ลบ Series และ Category เริ่มต้นทั้งหมด
- เพิ่ม Series และ Category ใหม่
- เพิ่มข้อมูลใหม่ให้กับ Series ของแผนภูมิ
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิ Multi‑Category:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
const String outPath = u"../out/MultiCategoryChart_out.pptx";
// สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นไฟล์ PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// เข้าถึงสไลด์แรก
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// เพิ่มแผนภูมิกับข้อมูลค่าเริ่มต้น
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);
// กำหนดดัชนีสำหรับแผ่นข้อมูลแผนภูมิ
int defaultWorksheetIndex = 0;
// ดึง worksheet ของข้อมูลแผนภูมิ
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();
// ล้างเวิร์กบุ๊ก
fact->Clear(defaultWorksheetIndex);
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
// เพิ่ม Category
SharedPtr<IChartCategory> category = chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c2", ObjectExt::Box<System::String>(u"A")));
category->get_GroupingLevels()->SetGroupingItem(1, ObjectExt::Box<System::String>(u"Group1"));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c3", ObjectExt::Box<System::String>(u"B")));
category = chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c4", ObjectExt::Box<System::String>(u"C")));
category->get_GroupingLevels()->SetGroupingItem(1, ObjectExt::Box<System::String>(u"Group2"));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c5", ObjectExt::Box<System::String>(u"D")));
category = chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c6", ObjectExt::Box<System::String>(u"E")));
category->get_GroupingLevels()->SetGroupingItem(1, ObjectExt::Box<System::String>(u"Group3"));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c7", ObjectExt::Box<System::String>(u"F")));
category = chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c8", ObjectExt::Box<System::String>(u"G")));
category->get_GroupingLevels()->SetGroupingItem(1, ObjectExt::Box<System::String>(u"Group4"));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, u"c9", ObjectExt::Box<System::String>(u"H")));
// เพิ่ม Series ใหม่
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(fact->GetCell(0, u"D1", ObjectExt::Box<System::String>(u"Series 1")),
ChartType::ClusteredColumn);
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D2", ObjectExt::Box<double>(10)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D3", ObjectExt::Box<double>(20)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D4", ObjectExt::Box<double>(30)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D5", ObjectExt::Box<double>(40)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D6", ObjectExt::Box<double>(50)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D7", ObjectExt::Box<double>(60)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D8", ObjectExt::Box<double>(70)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, u"D9", ObjectExt::Box<double>(80)));
// บันทึกงานนำเสนอ
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
สร้างแผนภูมิแผนที่ (Map Charts)
แผนภูมิแผนที่เป็นการแสดงภาพข้อมูลบนพื้นที่ทางภูมิศาสตร์ เหมาะสำหรับเปรียบเทียบค่าต่าง ๆ ระหว่างภูมิภาค
โค้ด C++ นี้แสดงวิธีการสร้างแผนภูมิแผนที่:
auto pres = System::MakeObject<Presentation>();
auto slide = pres->get_Slides()->idx_get(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Map, 50.0f, 50.0f, 500.0f, 400.0f);
pres->Save(u"mapChart.pptx", SaveFormat::Pptx);
สร้างแผนภูมิปะทะ (Combination Charts)
แผนภูมิปะทะ (หรือ combo chart) ผสานประเภทแผนภูมิสองประเภทขึ้นไปในกราฟเดียว ช่วยให้คุณเน้น เปรียบเทียบ หรือสังเกตความแตกต่างระหว่างชุดข้อมูลหลายชุดได้ชัดเจน

โค้ด C++ ต่อไปนี้แสดงวิธีการสร้างแผนภูมิปะทะตามที่แสดงในรูปด้านบนใน PowerPoint:
static SharedPtr<IChart> CreateChartWithFirstSeries(SharedPtr<ISlide> slide)
{
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50, 50, 600, 400);
// ตั้งค่าชื่อเรื่องของแผนภูมิ.
chart->set_HasTitle(true);
chart->get_ChartTitle()->AddTextFrameForOverriding(u"Chart Title");
chart->get_ChartTitle()->set_Overlay(false);
auto titleParagraph = chart->get_ChartTitle()->get_TextFrameForOverriding()->get_Paragraph(0);
auto titleFormat = titleParagraph->get_ParagraphFormat()->get_DefaultPortionFormat();
titleFormat->set_FontBold(NullableBool::False);
titleFormat->set_FontHeight(18.0);
// ตั้งค่าตัวอักษรอธิบายของแผนภูมิ.
chart->get_Legend()->set_Position(LegendPositionType::Bottom);
chart->get_Legend()->get_TextFormat()->get_PortionFormat()->set_FontHeight(12.0);
// ลบ Series และ Category ที่สร้างโดยค่าเริ่มต้น.
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
const int worksheetIndex = 0;
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
// เพิ่ม Category ใหม่.
chart->get_ChartData()->get_Categories()->Add(workbook->GetCell(worksheetIndex, 1, 0, ObjectExt::Box<String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(workbook->GetCell(worksheetIndex, 2, 0, ObjectExt::Box<String>(u"Category 2")));
chart->get_ChartData()->get_Categories()->Add(workbook->GetCell(worksheetIndex, 3, 0, ObjectExt::Box<String>(u"Category 3")));
chart->get_ChartData()->get_Categories()->Add(workbook->GetCell(worksheetIndex, 4, 0, ObjectExt::Box<String>(u"Category 4")));
// เพิ่ม Series แรก.
auto seriesNameCell = workbook->GetCell(worksheetIndex, 0, 1, ObjectExt::Box<String>(u"Series 1"));
auto series = chart->get_ChartData()->get_Series()->Add(seriesNameCell, chart->get_Type());
series->get_ParentSeriesGroup()->set_Overlap(-25);
series->get_ParentSeriesGroup()->set_GapWidth(220);
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 1, 1, ObjectExt::Box<double>(4.3)));
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 2, 1, ObjectExt::Box<double>(2.5)));
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 3, 1, ObjectExt::Box<double>(3.5)));
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 4, 1, ObjectExt::Box<double>(4.5)));
return chart;
}
static void AddSecondSeriesToChart(SharedPtr<IChart> chart)
{
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
const int worksheetIndex = 0;
auto seriesNameCell = workbook->GetCell(worksheetIndex, 0, 2, ObjectExt::Box<String>(u"Series 2"));
auto series = chart->get_ChartData()->get_Series()->Add(seriesNameCell, ChartType::ClusteredColumn);
series->get_ParentSeriesGroup()->set_Overlap(-25);
series->get_ParentSeriesGroup()->set_GapWidth(220);
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 1, 2, ObjectExt::Box<double>(2.4)));
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 2, 2, ObjectExt::Box<double>(4.4)));
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 3, 2, ObjectExt::Box<double>(1.8)));
series->get_DataPoints()->AddDataPointForBarSeries(workbook->GetCell(worksheetIndex, 4, 2, ObjectExt::Box<double>(2.8)));
}
static void AddThirdSeriesToChart(SharedPtr<IChart> chart)
{
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
const int worksheetIndex = 0;
auto seriesNameCell = workbook->GetCell(worksheetIndex, 0, 3, ObjectExt::Box<String>(u"Series 3"));
auto series = chart->get_ChartData()->get_Series()->Add(seriesNameCell, ChartType::Line);
series->get_DataPoints()->AddDataPointForLineSeries(workbook->GetCell(worksheetIndex, 1, 3, ObjectExt::Box<double>(2.0)));
series->get_DataPoints()->AddDataPointForLineSeries(workbook->GetCell(worksheetIndex, 2, 3, ObjectExt::Box<double>(2.0)));
series->get_DataPoints()->AddDataPointForLineSeries(workbook->GetCell(worksheetIndex, 3, 3, ObjectExt::Box<double>(3.0)));
series->get_DataPoints()->AddDataPointForLineSeries(workbook->GetCell(worksheetIndex, 4, 3, ObjectExt::Box<double>(5.0)));
series->set_PlotOnSecondAxis(true);
}
static void SetAxisTitle(SharedPtr<IAxis> axis, String axisTitle)
{
axis->set_HasTitle(true);
axis->get_Title()->set_Overlay(false);
auto titleParagraph = axis->get_Title()->AddTextFrameForOverriding(axisTitle)->get_Paragraph(0);
auto titleFormat = titleParagraph->get_ParagraphFormat()->get_DefaultPortionFormat();
titleFormat->set_FontBold(NullableBool::False);
titleFormat->set_FontHeight(12.0);
}
static void SetPrimaryAxesFormat(SharedPtr<IChart> chart)
{
// ตั้งค่าแกนแนวนอน.
auto horizontalAxis = chart->get_Axes()->get_HorizontalAxis();
horizontalAxis->get_TextFormat()->get_PortionFormat()->set_FontHeight(12.0);
horizontalAxis->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
SetAxisTitle(horizontalAxis, u"X Axis");
// ตั้งค่าแกนแนวตั้ง.
auto verticalAxis = chart->get_Axes()->get_VerticalAxis();
verticalAxis->get_TextFormat()->get_PortionFormat()->set_FontHeight(12.0);
verticalAxis->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
SetAxisTitle(verticalAxis, u"Y Axis 1");
// ตั้งค่าสีเส้นกริดหลักแนวตั้ง.
auto majorGridLinesFormat = verticalAxis->get_MajorGridLinesFormat()->get_Line()->get_FillFormat();
majorGridLinesFormat->set_FillType(FillType::Solid);
majorGridLinesFormat->get_SolidFillColor()->set_Color(Color::FromArgb(217, 217, 217));
}
static void SetSecondaryAxesFormat(SharedPtr<IChart> chart)
{
// ตั้งค่าแกนแนวนอนรอง.
auto secondaryHorizontalAxis = chart->get_Axes()->get_SecondaryHorizontalAxis();
secondaryHorizontalAxis->set_Position(AxisPositionType::Bottom);
secondaryHorizontalAxis->set_CrossType(CrossesType::Maximum);
secondaryHorizontalAxis->set_IsVisible(false);
secondaryHorizontalAxis->get_MajorGridLinesFormat()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
secondaryHorizontalAxis->get_MinorGridLinesFormat()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
// ตั้งค่าแกนแนวตั้งรอง.
auto secondaryVerticalAxis = chart->get_Axes()->get_SecondaryVerticalAxis();
secondaryVerticalAxis->set_Position(AxisPositionType::Right);
secondaryVerticalAxis->get_TextFormat()->get_PortionFormat()->set_FontHeight(12.0);
secondaryVerticalAxis->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
secondaryVerticalAxis->get_MajorGridLinesFormat()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
secondaryVerticalAxis->get_MinorGridLinesFormat()->get_Line()->get_FillFormat()->set_FillType(FillType::NoFill);
SetAxisTitle(secondaryVerticalAxis, u"Y Axis 2");
}
static void CreateComboChart()
{
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = CreateChartWithFirstSeries(slide);
AddSecondSeriesToChart(chart);
AddThirdSeriesToChart(chart);
SetPrimaryAxesFormat(chart);
SetSecondaryAxesFormat(chart);
presentation->Save(u"combo-chart.pptx", SaveFormat::Pptx);
presentation->Dispose();
}
อัปเดตแผนภูมิ
- สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นตัวแทนของงานนำเสนอที่มีแผนภูมิ
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- วนลูปผ่านรูปร่างทั้งหมดเพื่อค้นหาแผนภูมิที่ต้องการ
- เข้าถึง worksheet ของข้อมูลแผนภูมิ
- แก้ไขข้อมูล Series ของแผนภูมิโดยเปลี่ยนค่าใน Series
- เพิ่ม Series ใหม่และใส่ข้อมูลลงในนั้น
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการอัปเดตแผนภูมิ:
// สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นไฟล์ PPTX
System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"ExistingChart.pptx");
// เข้าถึงสไลด์แรก
System::SharedPtr<ISlide> sld = pres->get_Slides()->idx_get(0);
// เพิ่มแผนภูมิกับข้อมูลค่าเริ่มต้น
System::SharedPtr<IChart> chart = System::ExplicitCast<Aspose::Slides::Charts::IChart>(sld->get_Shapes()->idx_get(0));
// กำหนดดัชนีสำหรับแผ่นข้อมูลแผนภูมิ
int32_t defaultWorksheetIndex = 0;
// ดึง worksheet ของข้อมูลแผนภูมิ
System::SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();
// เปลี่ยนชื่อ Category ของแผนภูมิ
fact->GetCell(defaultWorksheetIndex, 1, 0, System::ObjectExt::Box<System::String>(u"Modified Category 1"));
fact->GetCell(defaultWorksheetIndex, 2, 0, System::ObjectExt::Box<System::String>(u"Modified Category 2"));
// ดึง Series แผนภูมิเชิงแรก
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);
// อัปเดตข้อมูล Series
fact->GetCell(defaultWorksheetIndex, 0, 1, System::ObjectExt::Box<System::String>(u"New_Series1"));
// แก้ไขชื่อ Series
series->get_DataPoints()->idx_get(0)->get_Value()->set_Data(System::ObjectExt::Box<int32_t>(90));
series->get_DataPoints()->idx_get(1)->get_Value()->set_Data(System::ObjectExt::Box<int32_t>(123));
series->get_DataPoints()->idx_get(2)->get_Value()->set_Data(System::ObjectExt::Box<int32_t>(44));
// ดึง Series แผนภูมิที่สอง
series = chart->get_ChartData()->get_Series()->idx_get(1);
// กำลังอัปเดตข้อมูล Series
fact->GetCell(defaultWorksheetIndex, 0, 2, System::ObjectExt::Box<System::String>(u"New_Series2"));
// แก้ไขชื่อ Series
series->get_DataPoints()->idx_get(0)->get_Value()->set_Data(System::ObjectExt::Box<int32_t>(23));
series->get_DataPoints()->idx_get(1)->get_Value()->set_Data(System::ObjectExt::Box<int32_t>(67));
series->get_DataPoints()->idx_get(2)->get_Value()->set_Data(System::ObjectExt::Box<int32_t>(99));
// ตอนนี้กำลังเพิ่ม Series ใหม่
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 3, System::ObjectExt::Box<System::String>(u"Series 3")), chart->get_Type());
// ดึง Series แผนภูมิที่สาม
series = chart->get_ChartData()->get_Series()->idx_get(2);
// กำลังเติมข้อมูลให้ Series
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 3, System::ObjectExt::Box<int32_t>(20)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 3, System::ObjectExt::Box<int32_t>(50)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 3, System::ObjectExt::Box<int32_t>(30)));
chart->set_Type(Aspose::Slides::Charts::ChartType::ClusteredCylinder);
// บันทึกงานนำเสนอพร้อมแผนภูมิ
pres->Save(u"AsposeChartModified_out.pptx", Aspose::Slides::Export::SaveFormat::Pptx);
กำหนดช่วงข้อมูลสำหรับแผนภูมิ
- เปิดอินสแตนซ์ของคลาส Presentation ที่มีแผนภูมิอยู่
- รับอ้างอิงสไลด์โดยใช้ดัชนี
- วนลูปผ่านรูปร่างทั้งหมดเพื่อค้นหาแผนภูมิที่ต้องการ
- เข้าถึงข้อมูลแผนภูมิและกำหนดช่วงข้อมูล
- บันทึกไฟล์ Presentation ที่แก้ไขเป็นไฟล์ PPTX
โค้ด C++ นี้แสดงวิธีการกำหนดช่วงข้อมูลสำหรับแผนภูมิ:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
String dataDir = GetDataPath();
// สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นไฟล์ PPTX
auto presentation = System::MakeObject<Presentation>(dataDir + u"ExistingChart.pptx");
// เข้าถึงสไลด์แรกและเพิ่มแผนภูมิกับข้อมูลค่าเริ่มต้น
auto slide = presentation->get_Slides()->idx_get(0);
auto chart = System::ExplicitCast<IChart>(slide->get_Shapes()->idx_get(0));
chart->get_ChartData()->SetRange(u"Sheet1!A1:B4");
presentation->Save(dataDir + u"SetDataRange_out.pptx", SaveFormat::Pptx);
ใช้ Marker เริ่มต้นในแผนภูมิ
เมื่อคุณใช้ Marker เริ่มต้นในแผนภูมิแต่ละ Series จะได้รับสัญลักษณ์ Marker เริ่มต้นที่แตกต่างกันโดยอัตโนมัติ
โค้ด C++ นี้แสดงวิธีการตั้งค่า Marker ของ Series อย่างอัตโนมัติ:
// เส้นทางไปยังไดเรกทอรีเอกสาร.
String dataDir = GetDataPath();
auto pres = System::MakeObject<Presentation>();
auto slide = pres->get_Slides()->idx_get(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::LineWithMarkers, 10.0f, 10.0f, 400.0f, 400.0f);
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
auto wb = chart->get_ChartData()->get_ChartDataWorkbook();
chart->get_ChartData()->get_Series()->Add(wb->GetCell(0, 0, 1, ObjectExt::Box<String>(u"Series 1")), chart->get_Type());
auto series = chart->get_ChartData()->get_Series()->idx_get(0);
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, 1, 0, ObjectExt::Box<String>(u"C1")));
series->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 1, 1, ObjectExt::Box<int32_t>(24)));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, 2, 0, ObjectExt::Box<String>(u"C2")));
series->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 2, 1, ObjectExt::Box<int32_t>(23)));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, 3, 0, ObjectExt::Box<String>(u"C3")));
series->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 3, 1, ObjectExt::Box<int32_t>(-10)));
chart->get_ChartData()->get_Categories()->Add(wb->GetCell(0, 4, 0, ObjectExt::Box<String>(u"C4")));
series->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 4, 1, nullptr));
chart->get_ChartData()->get_Series()->Add(wb->GetCell(0, 0, 2, ObjectExt::Box<String>(u"Series 2")), chart->get_Type());
// ดึง Series ของแผนภูมิที่สอง
auto series2 = chart->get_ChartData()->get_Series()->idx_get(1);
// เติมข้อมูลให้ Series
series2->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 1, 2, ObjectExt::Box<int32_t>(30)));
series2->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 2, 2, ObjectExt::Box<int32_t>(10)));
series2->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 3, 2, ObjectExt::Box<int32_t>(60)));
series2->get_DataPoints()->AddDataPointForLineSeries(wb->GetCell(0, 4, 2, ObjectExt::Box<int32_t>(40)));
chart->set_HasLegend(true);
chart->get_Legend()->set_Overlay(false);
pres->Save(dataDir + u"DefaultMarkersInChart.pptx", SaveFormat::Pptx);
คำถามที่พบบ่อย (FAQ)
Aspose.Slides รองรับประเภทแผนภูมิใดบ้าง?
Aspose.Slides รองรับแผนภูมิหลายประเภท ได้แก่ แถบ, เส้น, วงกลม, พื้นที่, กระจาย, Histogram, Radar และอื่น ๆ อีกมาก ทำให้คุณเลือกประเภทแผนภูมิที่เหมาะกับการแสดงผลข้อมูลของคุณได้ตามต้องการ
ฉันจะเพิ่มแผนภูมิใหม่ลงในสไลด์อย่างไร?
ให้สร้างอินสแตนซ์ของคลาส Presentation แล้วดึงสไลด์ที่ต้องการโดยใช้ดัชนี จากนั้นเรียกเมธอดเพิ่มแผนภูมิโดยระบุประเภทแผนภูมิและข้อมูลเริ่มต้น กระบวนการนี้จะฝังแผนภูมิเข้าไปในงานนำเสนอของคุณโดยตรง
ฉันจะอัปเดตข้อมูลที่แสดงในแผนภูมิได้อย่างไร?
คุณสามารถอัปเดตข้อมูลของแผนภูมิได้โดยเข้าถึง IChartDataWorkbook ของแผนภูมิ ลบ Series และ Category เริ่มต้น แล้วเพิ่มข้อมูลที่กำหนดเองของคุณเอง ซึ่งจะทำให้แผนภูมรีเฟรชและแสดงข้อมูลล่าสุดที่คุณต้องการ
สามารถปรับแต่งลักษณะของแผนภูมิได้หรือไม่?
ได้ Aspose.Slides มีตัวเลือกการปรับแต่งที่ครอบคลุม คุณสามารถแก้ไขสี, ฟอนต์, ป้ายกำกับ, คำอธิบาย, และองค์ประกอบการจัดรูปแบบอื่น ๆ เพื่อให้แผนภูมิตรงกับความต้องการออกแบบของคุณอย่างละเอียด