Tạo hoặc Cập nhật Biểu đồ Bản trình bày PowerPoint trong .NET
Tổng quan
Bài viết này cung cấp hướng dẫn toàn diện về cách tạo và tùy chỉnh biểu đồ bằng Aspose.Slides for .NET. Bạn sẽ học cách lập trình để thêm biểu đồ vào một slide, đưa dữ liệu vào và áp dụng các tùy chọn định dạng khác nhau để đáp ứng yêu cầu thiết kế cụ thể của mình. Suốt bài viết, các ví dụ mã chi tiết minh họa từng bước, từ việc khởi tạo đối tượng Presentation và Chart cho đến cấu hình series, trục và legend. Khi làm theo hướng dẫn này, bạn sẽ nắm vững cách tích hợp việc tạo biểu đồ động vào các ứng dụng .NET, giúp đơn giản hoá quá trình tạo các bản trình bày dựa trên dữ liệu.
Tạo biểu đồ
Biểu đồ giúp người dùng nhanh chóng hình dung dữ liệu và rút ra những hiểu biết có thể không hiển thị ngay trong bảng hoặc bảng tính.
Tại sao nên tạo biểu đồ?
Sử dụng biểu đồ, bạn có thể:
- tổng hợp, nén hoặc tóm tắt một lượng lớn dữ liệu trên một slide duy nhất;
- phát hiện các mẫu và xu hướng trong dữ liệu;
- suy ra hướng và động lực của dữ liệu theo thời gian hoặc theo một đơn vị đo lường cụ thể;
- phát hiện các giá trị bất thường, sai lệch, lỗi và dữ liệu vô lý;
- truyền đạt hoặc trình bày dữ liệu phức tạp.
Trong PowerPoint, bạn có thể tạo biểu đồ thông qua chức năng Insert, cung cấp các mẫu để thiết kế nhiều loại biểu đồ. Khi dùng Aspose.Slides, bạn có thể tạo cả biểu đồ thông thường (dựa trên các loại biểu đồ phổ biến) và biểu đồ tùy chỉnh.
Tạo biểu đồ Cột nhóm (Clustered Column)
Phần này giải thích cách tạo biểu đồ cột nhóm bằng Aspose.Slides for .NET. Bạn sẽ học cách khởi tạo một presentation, thêm biểu đồ và tùy chỉnh các yếu tố như tiêu đề, dữ liệu, series, danh mục và kiểu dáng. Thực hiện các bước dưới đây để xem cách một biểu đồ cột nhóm tiêu chuẩn được tạo ra:
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với một số dữ liệu và chỉ định loại
ChartType.ClusteredColumn. - Thêm tiêu đề cho biểu đồ.
- Truy cập worksheet dữ liệu của biểu đồ.
- Xóa tất cả series và danh mục mặc định.
- Thêm series và danh mục mới.
- Thêm dữ liệu biểu đồ mới cho các series.
- Áp dụng màu nền cho các series.
- Thêm nhãn cho các series.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# sau minh họa cách tạo biểu đồ cột nhóm:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
// Khởi tạo lớp Presentation.
using (Presentation presentation = new Presentation())
{
// Truy cập slide đầu tiên.
ISlide slide = presentation.Slides[0];
// Thêm biểu đồ cột nhóm với dữ liệu mặc định.
IChart chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
// Đặt tiêu đề biểu đồ.
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
// Đặt chỉ mục của sheet dữ liệu biểu đồ.
int worksheetIndex = 0;
// Lấy workbook dữ liệu biểu đồ.
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Xóa các series và danh mục được tạo mặc định.
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
// Thêm series mới.
chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 0, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 0, 2, "Series 2"), chart.Type);
// Thêm danh mục mới.
chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 1, 0, "Category 1"));
chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 2, 0, "Category 2"));
chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 3, 0, "Category 3"));
// Lấy series biểu đồ đầu tiên.
IChartSeries series = chart.ChartData.Series[0];
// Điền dữ liệu cho series.
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 1, 30));
// Đặt màu nền cho series.
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Red;
// Lấy series biểu đồ thứ hai.
series = chart.ChartData.Series[1];
// Điền dữ liệu cho series.
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 2, 30));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 2, 10));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 2, 60));
// Đặt màu nền cho series.
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Green;
// Đặt nhãn đầu tiên để hiển thị tên danh mục.
IDataLabel label = series.DataPoints[0].Label;
label.DataLabelFormat.ShowCategoryName = true;
label = series.DataPoints[1].Label;
label.DataLabelFormat.ShowSeriesName = true;
// Đặt series để hiển thị giá trị cho nhãn thứ ba.
label = series.DataPoints[2].Label;
label.DataLabelFormat.ShowValue = true;
label.DataLabelFormat.ShowSeriesName = true;
label.DataLabelFormat.Separator = "/";
// Lưu bản trình bày vào đĩa dưới dạng tệp PPTX.
presentation.Save("AsposeChart_out.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Scatter
Biểu đồ scatter (còn gọi là scatter plot hoặc đồ thị x‑y) thường được dùng để kiểm tra mẫu hoặc thể hiện mối tương quan giữa hai biến.
Sử dụng biểu đồ scatter khi:
- Bạn có dữ liệu số cặp đôi.
- Bạn có hai biến có quan hệ chặt chẽ với nhau.
- Bạn muốn xác định liệu hai biến có liên quan hay không.
- Bạn có một biến độc lập có nhiều giá trị cho một biến phụ thuộc.
Mã C# này cho bạn thấy cách tạo biểu đồ scatter với một loạt marker khác nhau:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
// Khởi tạo lớp Presentation.
using (Presentation presentation = new Presentation())
{
// Truy cập slide đầu tiên.
ISlide slide = presentation.Slides[0];
// Tạo biểu đồ scatter mặc định.
IChart chart = slide.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 20, 20, 500, 300);
// Đặt chỉ mục của sheet dữ liệu biểu đồ.
int worksheetIndex = 0;
// Lấy workbook dữ liệu biểu đồ.
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Xóa series mặc định.
chart.ChartData.Series.Clear();
// Thêm series mới.
chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 1, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 1, 3, "Series 2"), chart.Type);
// Lấy series biểu đồ đầu tiên.
IChartSeries series = chart.ChartData.Series[0];
// Thêm một điểm mới (1:3) vào series.
series.DataPoints.AddDataPointForScatterSeries(workbook.GetCell(worksheetIndex, 2, 1, 1), workbook.GetCell(worksheetIndex, 2, 2, 3));
// Thêm một điểm mới (2:10).
series.DataPoints.AddDataPointForScatterSeries(workbook.GetCell(worksheetIndex, 3, 1, 2), workbook.GetCell(worksheetIndex, 3, 2, 10));
// Thay đổi kiểu series.
series.Type = ChartType.ScatterWithStraightLinesAndMarkers;
// Thay đổi marker của series biểu đồ.
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Star;
// Lấy series biểu đồ thứ hai.
series = chart.ChartData.Series[1];
// Thêm một điểm mới (5:2) vào series biểu đồ.
series.DataPoints.AddDataPointForScatterSeries(workbook.GetCell(worksheetIndex, 2, 3, 5), workbook.GetCell(worksheetIndex, 2, 4, 2));
// Thêm một điểm mới (3:1).
series.DataPoints.AddDataPointForScatterSeries(workbook.GetCell(worksheetIndex, 3, 3, 3), workbook.GetCell(worksheetIndex, 3, 4, 1));
// Thêm một điểm mới (2:2).
series.DataPoints.AddDataPointForScatterSeries(workbook.GetCell(worksheetIndex, 4, 3, 2), workbook.GetCell(worksheetIndex, 4, 4, 2));
// Thêm một điểm mới (5:1).
series.DataPoints.AddDataPointForScatterSeries(workbook.GetCell(worksheetIndex, 5, 3, 5), workbook.GetCell(worksheetIndex, 5, 4, 1));
// Thay đổi marker của series biểu đồ.
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Circle;
// Lưu bản trình bày vào đĩa dưới dạng tệp PPTX.
presentation.Save("AsposeChart_out.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Pie
Biểu đồ pie thích hợp để hiển thị mối quan hệ phần‑trong‑toàn của dữ liệu, đặc biệt khi dữ liệu có nhãn phân loại kèm giá trị số. Tuy nhiên, nếu dữ liệu của bạn có quá nhiều phần hoặc nhãn, bạn có thể cân nhắc dùng biểu đồ cột thay thế.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.Pie. - Truy cập workbook dữ liệu của biểu đồ (IChartDataWorkbook).
- Xóa series và danh mục mặc định.
- Thêm series và danh mục mới.
- Thêm dữ liệu biểu đồ mới cho các series.
- Thêm các điểm mới cho biểu đồ và áp dụng màu tùy chỉnh cho các khu vực của biểu đồ pie.
- Đặt nhãn cho các series.
- Bật các đường dẫn (leader lines) cho nhãn series.
- Đặt góc quay cho biểu đồ pie.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ pie:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
// Khởi tạo lớp Presentation.
using (Presentation presentation = new Presentation())
{
// Truy cập slide đầu tiên.
ISlide slide = presentation.Slides[0];
// Thêm biểu đồ với dữ liệu mặc định.
IChart chart = slide.Shapes.AddChart(ChartType.Pie, 20, 20, 500, 300);
// Đặt tiêu đề biểu đồ.
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
// Đặt series đầu tiên để hiển thị giá trị.
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
// Đặt chỉ mục của sheet dữ liệu biểu đồ.
int worksheetIndex = 0;
// Lấy workbook dữ liệu biểu đồ.
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Xóa các series và danh mục được tạo mặc định.
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
// Thêm danh mục mới.
chart.ChartData.Categories.Add(workbook.GetCell(0, 1, 0, "1st Qtr"));
chart.ChartData.Categories.Add(workbook.GetCell(0, 2, 0, "2nd Qtr"));
chart.ChartData.Categories.Add(workbook.GetCell(0, 3, 0, "3rd Qtr"));
// Thêm series mới.
IChartSeries series = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 1, "Series 1"), chart.Type);
// Điền dữ liệu cho series.
series.DataPoints.AddDataPointForPieSeries(workbook.GetCell(worksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForPieSeries(workbook.GetCell(worksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForPieSeries(workbook.GetCell(worksheetIndex, 3, 1, 30));
// Đặt màu cho sector.
chart.ChartData.SeriesGroups[0].IsColorVaried = true;
IChartDataPoint point = series.DataPoints[0];
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = Color.Cyan;
// Đặt viền cho sector.
point.Format.Line.FillFormat.FillType = FillType.Solid;
point.Format.Line.FillFormat.SolidFillColor.Color = Color.Gray;
point.Format.Line.Width = 3.0;
point.Format.Line.Style = LineStyle.ThinThick;
point.Format.Line.DashStyle = LineDashStyle.LargeDash;
IChartDataPoint point1 = series.DataPoints[1];
point1.Format.Fill.FillType = FillType.Solid;
point1.Format.Fill.SolidFillColor.Color = Color.Brown;
// Đặt viền cho sector.
point1.Format.Line.FillFormat.FillType = FillType.Solid;
point1.Format.Line.FillFormat.SolidFillColor.Color = Color.Blue;
point1.Format.Line.Width = 3.0;
point1.Format.Line.Style = LineStyle.Single;
point1.Format.Line.DashStyle = LineDashStyle.LargeDashDot;
IChartDataPoint point2 = series.DataPoints[2];
point2.Format.Fill.FillType = FillType.Solid;
point2.Format.Fill.SolidFillColor.Color = Color.Coral;
// Đặt viền cho sector.
point2.Format.Line.FillFormat.FillType = FillType.Solid;
point2.Format.Line.FillFormat.SolidFillColor.Color = Color.Red;
point2.Format.Line.Width = 2.0;
point2.Format.Line.Style = LineStyle.ThinThin;
point2.Format.Line.DashStyle = LineDashStyle.LargeDashDotDot;
// Tạo nhãn tùy chỉnh cho mỗi danh mục trong series mới.
IDataLabel label1 = series.DataPoints[0].Label;
label1.DataLabelFormat.ShowValue = true;
IDataLabel label2 = series.DataPoints[1].Label;
label2.DataLabelFormat.ShowValue = true;
label2.DataLabelFormat.ShowLegendKey = true;
label2.DataLabelFormat.ShowPercentage = true;
IDataLabel label3 = series.DataPoints[2].Label;
label3.DataLabelFormat.ShowSeriesName = true;
label3.DataLabelFormat.ShowPercentage = true;
// Đặt series để hiển thị đường dẫn (leader lines) cho biểu đồ.
series.Labels.DefaultDataLabelFormat.ShowLeaderLines = true;
// Đặt góc quay cho các sector của biểu đồ tròn.
chart.ChartData.SeriesGroups[0].FirstSliceAngle = 180;
// Lưu bản trình bày vào đĩa dưới dạng tệp PPTX.
presentation.Save("PieChart_out.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Line
Biểu đồ line (còn gọi là line graph) thích hợp trong các tình huống bạn muốn thể hiện sự thay đổi của giá trị theo thời gian. Với biểu đồ line, bạn có thể so sánh một lượng lớn dữ liệu cùng lúc, theo dõi các thay đổi và xu hướng theo thời gian, làm nổi bật các bất thường trong series, và nhiều hơn nữa.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.Line. - Truy cập workbook dữ liệu của biểu đồ (IChartDataWorkbook).
- Xóa series và danh mục mặc định.
- Thêm series và danh mục mới.
- Thêm dữ liệu biểu đồ mới cho các series.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ line:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
IChart lineChart = presentation.Slides[0].Shapes.AddChart(ChartType.Line, 20, 20, 500, 300);
presentation.Save("lineChart.pptx", SaveFormat.Pptx);
}
Mặc định, các điểm trên biểu đồ line được nối bằng các đoạn thẳng liên tục. Nếu bạn muốn các điểm được nối bằng các đoạn gạch, bạn có thể chỉ định kiểu gạch mong muốn như sau:
using Aspose.Slides;
using Aspose.Slides.Charts;
using (Presentation presentation = new Presentation())
{
IChart lineChart = presentation.Slides[0].Shapes.AddChart(ChartType.Line, 20, 20, 500, 300);
foreach (IChartSeries series in lineChart.ChartData.Series)
{
series.Format.Line.DashStyle = LineDashStyle.Dash;
}
}
Kết quả:

Tạo biểu đồ Tree Map
Biểu đồ tree map thích hợp cho dữ liệu bán hàng khi bạn muốn hiển thị kích thước tương đối của các danh mục dữ liệu và nhanh chóng thu hút sự chú ý tới các mục có đóng góp lớn trong mỗi danh mục.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.Treemap. - Truy cập workbook dữ liệu của biểu đồ (IChartDataWorkbook).
- Xóa series và danh mục mặc định.
- Thêm series và danh mục mới.
- Thêm dữ liệu biểu đồ mới cho các series.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ tree map:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.Treemap, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
// Nhánh 1
IChartCategory leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C1", "Leaf1"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem1");
leaf.GroupingLevels.SetGroupingItem(2, "Branch1");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C2", "Leaf2"));
leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C3", "Leaf3"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem2");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C4", "Leaf4"));
// Nhánh 2
leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C5", "Leaf5"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem3");
leaf.GroupingLevels.SetGroupingItem(2, "Branch2");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C6", "Leaf6"));
leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C7", "Leaf7"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem4");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C8", "Leaf8"));
IChartSeries series = chart.ChartData.Series.Add(ChartType.Treemap);
series.Labels.DefaultDataLabelFormat.ShowCategoryName = true;
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D1", 4));
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D2", 5));
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D3", 3));
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D4", 6));
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D5", 9));
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D6", 9));
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D7", 4));
series.DataPoints.AddDataPointForTreemapSeries(workbook.GetCell(0, "D8", 3));
series.ParentLabelLayout = ParentLabelLayoutType.Overlapping;
presentation.Save("Treemap.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Stock
Biểu đồ stock được dùng để hiển thị dữ liệu tài chính như giá mở cửa, cao nhất, thấp nhất và đóng cửa, hỗ trợ phân tích xu hướng thị trường và độ biến động. Chúng cung cấp những hiểu biết quan trọng về hiệu suất cổ phiếu, giúp nhà đầu tư và nhà phân tích đưa ra quyết định có căn cứ.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.OpenHighLowClose. - Truy cập workbook dữ liệu của biểu đồ (IChartDataWorkbook).
- Xóa series và danh mục mặc định.
- Thêm series và danh mục mới.
- Thêm dữ liệu biểu đồ mới cho các series.
- Xác định định dạng HiLowLines.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ stock:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.OpenHighLowClose, 20, 20, 500, 300, false);
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
chart.ChartData.Categories.Add(workbook.GetCell(0, 1, 0, "A"));
chart.ChartData.Categories.Add(workbook.GetCell(0, 2, 0, "B"));
chart.ChartData.Categories.Add(workbook.GetCell(0, 3, 0, "C"));
chart.ChartData.Series.Add(workbook.GetCell(0, 0, 1, "Open"), chart.Type);
chart.ChartData.Series.Add(workbook.GetCell(0, 0, 2, "High"), chart.Type);
chart.ChartData.Series.Add(workbook.GetCell(0, 0, 3, "Low"), chart.Type);
chart.ChartData.Series.Add(workbook.GetCell(0, 0, 4, "Close"), chart.Type);
IChartSeries series = chart.ChartData.Series[0];
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 1, 1, 72));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 2, 1, 25));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 3, 1, 38));
series = chart.ChartData.Series[1];
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 1, 2, 172));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 2, 2, 57));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 3, 2, 57));
series = chart.ChartData.Series[2];
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 1, 3, 12));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 2, 3, 12));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 3, 3, 13));
series = chart.ChartData.Series[3];
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 1, 4, 25));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 2, 4, 38));
series.DataPoints.AddDataPointForStockSeries(workbook.GetCell(0, 3, 4, 50));
chart.ChartData.SeriesGroups[0].UpDownBars.HasUpDownBars = true;
chart.ChartData.SeriesGroups[0].HiLowLinesFormat.Line.FillFormat.FillType = FillType.Solid;
foreach (IChartSeries ser in chart.ChartData.Series)
{
ser.Format.Line.FillFormat.FillType = FillType.NoFill;
}
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
presentation.Save("Stock-chart.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Box và Whisker
Biểu đồ Box và Whisker dùng để hiển thị phân bố dữ liệu bằng cách tóm tắt các chỉ số thống kê chính như trung vị, các phần tư và các ngoại lệ tiềm năng. Chúng rất hữu ích trong phân tích dữ liệu khám phá và các nghiên cứu thống kê để nhanh chóng hiểu biến động dữ liệu và xác định bất thường.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.BoxAndWhisker. - Truy cập workbook dữ liệu của biểu đồ (IChartDataWorkbook).
- Xóa series và danh mục mặc định.
- Thêm series và danh mục mới.
- Thêm dữ liệu biểu đồ mới cho các series.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ box và whisker:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.BoxAndWhisker, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
chart.ChartData.Categories.Add(workbook.GetCell(0, "A1", "Category 1"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A2", "Category 2"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A3", "Category 3"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A4", "Category 4"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A5", "Category 5"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A6", "Category 6"));
IChartSeries series = chart.ChartData.Series.Add(ChartType.BoxAndWhisker);
series.QuartileMethod = QuartileMethodType.Exclusive;
series.ShowMeanLine = true;
series.ShowMeanMarkers = true;
series.ShowInnerPoints = true;
series.ShowOutlierPoints = true;
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(workbook.GetCell(0, "B1", 15));
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(workbook.GetCell(0, "B2", 41));
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(workbook.GetCell(0, "B3", 16));
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(workbook.GetCell(0, "B4", 10));
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(workbook.GetCell(0, "B5", 23));
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(workbook.GetCell(0, "B6", 16));
presentation.Save("BoxAndWhisker.pptx", SaveFormat.Pptx);
}
Tạo biểu đồ Funnel
Biểu đồ funnel dùng để hình dung quy trình gồm các giai đoạn tuần tự, trong đó khối lượng dữ liệu giảm dần khi tiến từ bước này sang bước tiếp theo. Chúng đặc biệt hữu ích để phân tích tỷ lệ chuyển đổi, xác định các nút thắt và theo dõi hiệu quả của quy trình bán hàng hoặc tiếp thị.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.Funnel. - Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ funnel:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation("test.pptx"))
{
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.Funnel, 50, 50, 500, 400);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
chart.ChartData.Categories.Add(workbook.GetCell(0, "A1", "Category 1"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A2", "Category 2"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A3", "Category 3"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A4", "Category 4"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A5", "Category 5"));
chart.ChartData.Categories.Add(workbook.GetCell(0, "A6", "Category 6"));
IChartSeries series = chart.ChartData.Series.Add(ChartType.Funnel);
series.DataPoints.AddDataPointForFunnelSeries(workbook.GetCell(0, "B1", 50));
series.DataPoints.AddDataPointForFunnelSeries(workbook.GetCell(0, "B2", 100));
series.DataPoints.AddDataPointForFunnelSeries(workbook.GetCell(0, "B3", 200));
series.DataPoints.AddDataPointForFunnelSeries(workbook.GetCell(0, "B4", 300));
series.DataPoints.AddDataPointForFunnelSeries(workbook.GetCell(0, "B5", 400));
series.DataPoints.AddDataPointForFunnelSeries(workbook.GetCell(0, "B6", 500));
presentation.Save("Funnel.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Sunburst
Biểu đồ sunburst dùng để hiển thị dữ liệu phân cấp, với các mức được thể hiện dưới dạng các vòng đồng tâm. Chúng giúp minh họa các mối quan hệ phần‑trong‑toàn và là lựa chọn lý tưởng để biểu diễn các danh mục và phân danh mục lồng nhau một cách rõ ràng, gọn gàng.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.Sunburst. - Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ sunburst:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.Sunburst, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
// Nhánh 1
IChartCategory leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C1", "Leaf1"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem1");
leaf.GroupingLevels.SetGroupingItem(2, "Branch1");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C2", "Leaf2"));
leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C3", "Leaf3"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem2");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C4", "Leaf4"));
// Nhánh 2
leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C5", "Leaf5"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem3");
leaf.GroupingLevels.SetGroupingItem(2, "Branch2");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C6", "Leaf6"));
leaf = chart.ChartData.Categories.Add(workbook.GetCell(0, "C7", "Leaf7"));
leaf.GroupingLevels.SetGroupingItem(1, "Stem4");
chart.ChartData.Categories.Add(workbook.GetCell(0, "C8", "Leaf8"));
IChartSeries series = chart.ChartData.Series.Add(ChartType.Sunburst);
series.Labels.DefaultDataLabelFormat.ShowCategoryName = true;
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D1", 4));
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D2", 5));
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D3", 3));
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D4", 6));
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D5", 9));
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D6", 9));
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D7", 4));
series.DataPoints.AddDataPointForSunburstSeries(workbook.GetCell(0, "D8", 3));
presentation.Save("Sunburst.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Histogram
Biểu đồ histogram dùng để biểu diễn phân bố dữ liệu số bằng cách nhóm các giá trị thành các khoảng (bins). Chúng đặc biệt hữu ích để nhận dạng các mẫu dữ liệu như tần suất, độ lệch và phạm vi, cũng như phát hiện các ngoại lệ trong bộ dữ liệu.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với một số dữ liệu và chỉ định loại
ChartType.Histogram. - Truy cập workbook dữ liệu của biểu đồ (IChartDataWorkbook).
- Xóa series và danh mục mặc định.
- Thêm series và danh mục mới.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ histogram:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.Histogram, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
IChartSeries series = chart.ChartData.Series.Add(ChartType.Histogram);
series.DataPoints.AddDataPointForHistogramSeries(workbook.GetCell(0, "A1", 15));
series.DataPoints.AddDataPointForHistogramSeries(workbook.GetCell(0, "A2", -41));
series.DataPoints.AddDataPointForHistogramSeries(workbook.GetCell(0, "A3", 16));
series.DataPoints.AddDataPointForHistogramSeries(workbook.GetCell(0, "A4", 10));
series.DataPoints.AddDataPointForHistogramSeries(workbook.GetCell(0, "A5", -23));
series.DataPoints.AddDataPointForHistogramSeries(workbook.GetCell(0, "A6", 16));
chart.Axes.HorizontalAxis.AggregationType = AxisAggregationType.Automatic;
presentation.Save("Histogram.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Radar
Biểu đồ radar dùng để hiển thị dữ liệu đa biến trong một định dạng hai chiều, cho phép so sánh nhiều biến cùng lúc. Chúng đặc biệt hữu ích để xác định các mẫu, điểm mạnh và điểm yếu trên nhiều chỉ số hiệu suất hoặc thuộc tính.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với một số dữ liệu và chỉ định loại
ChartType.Radar. - Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ radar:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
presentation.Slides[0].Shapes.AddChart(ChartType.Radar, 20, 20, 500, 300);
presentation.Save("Radar-chart.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Multi‑Category
Biểu đồ Multi‑Category dùng để hiển thị dữ liệu có hơn một nhóm phân loại, cho phép so sánh giá trị trên nhiều chiều cùng lúc. Chúng đặc biệt hữu ích khi bạn cần phân tích xu hướng và mối quan hệ trong các bộ dữ liệu phức tạp, nhiều lớp.
- Tạo một thể hiện của lớp Presentation.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Thêm một biểu đồ với dữ liệu mặc định và chỉ định loại
ChartType.ClusteredColumn. - Truy cập workbook dữ liệu của biểu đồ (IChartDataWorkbook).
- Xóa series và danh mục mặc định.
- Thêm series và danh mục mới.
- Thêm dữ liệu biểu đồ mới cho các series.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách tạo biểu đồ multi‑category:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
int worksheetIndex = 0;
IChartCategory category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c2", "A"));
category.GroupingLevels.SetGroupingItem(1, "Group1");
category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c3", "B"));
category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c4", "C"));
category.GroupingLevels.SetGroupingItem(1, "Group2");
category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c5", "D"));
category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c6", "E"));
category.GroupingLevels.SetGroupingItem(1, "Group3");
category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c7", "F"));
category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c8", "G"));
category.GroupingLevels.SetGroupingItem(1, "Group4");
category = chart.ChartData.Categories.Add(workbook.GetCell(0, "c9", "H"));
// Thêm một series.
IChartSeries series = chart.ChartData.Series.Add(workbook.GetCell(0, "D1", "Series 1"), ChartType.ClusteredColumn);
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D2", 10));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D3", 20));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D4", 30));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D5", 40));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D6", 50));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D7", 60));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D8", 70));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, "D9", 80));
// Lưu bản trình bày kèm biểu đồ.
presentation.Save("AsposeChart_out.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Map
Biểu đồ map dùng để hiển thị dữ liệu địa lý bằng cách gán thông tin vào các vị trí cụ thể như quốc gia, tiểu bang hoặc thành phố. Chúng rất hữu ích để phân tích xu hướng khu vực, dữ liệu dân số và phân bố không gian một cách rõ ràng, trực quan.
Mã C# này cho bạn thấy cách tạo biểu đồ map:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.Map, 20, 20, 500, 300);
presentation.Save("mapChart.pptx", SaveFormat.Pptx);
}
Kết quả:

Tạo biểu đồ Combination
Biểu đồ combination (hay combo chart) kết hợp hai hoặc nhiều loại biểu đồ trong cùng một đồ thị. Biểu đồ này cho phép bạn làm nổi bật, so sánh hoặc kiểm tra sự khác biệt giữa hai hoặc nhiều bộ dữ liệu, giúp xác định các mối quan hệ giữa chúng.

Mã C# dưới đây cho thấy cách tạo biểu đồ combination như hình trên trong một bản PowerPoint:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
private static void CreateComboChart()
{
using (Presentation presentation = new Presentation())
{
IChart chart = CreateChartWithFirstSeries(presentation.Slides[0]);
AddSecondSeriesToChart(chart);
AddThirdSeriesToChart(chart);
SetPrimaryAxesFormat(chart);
SetSecondaryAxesFormat(chart);
presentation.Save("combo-chart.pptx", SaveFormat.Pptx);
}
}
private static IChart CreateChartWithFirstSeries(ISlide slide)
{
IChart chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
// Đặt tiêu đề biểu đồ
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("Chart Title");
chart.ChartTitle.Overlay = false;
IPortionFormat portionFormat =
chart.ChartTitle.TextFrameForOverriding.Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
portionFormat.FontBold = NullableBool.False;
portionFormat.FontHeight = 18f;
// Đặt chú giải cho biểu đồ
chart.Legend.Position = LegendPositionType.Bottom;
chart.Legend.TextFormat.PortionFormat.FontHeight = 12f;
// Xóa series và danh mục được tạo mặc định
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int worksheetIndex = 0;
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Thêm danh mục mới
chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 1, 0, "Category 1"));
chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 2, 0, "Category 2"));
chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 3, 0, "Category 3"));
chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 4, 0, "Category 4"));
// Thêm series đầu tiên
IChartSeries series = chart.ChartData.Series.Add(
workbook.GetCell(worksheetIndex, 0, 1, "Series 1"), chart.Type);
series.ParentSeriesGroup.Overlap = -25;
series.ParentSeriesGroup.GapWidth = 220;
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 1, 4.3));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 1, 2.5));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 1, 3.5));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 4, 1, 4.5));
return chart;
}
private static void AddSecondSeriesToChart(IChart chart)
{
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
const int worksheetIndex = 0;
IChartSeries series = chart.ChartData.Series.Add(
workbook.GetCell(worksheetIndex, 0, 2, "Series 2"), ChartType.ClusteredColumn);
series.ParentSeriesGroup.Overlap = -25;
series.ParentSeriesGroup.GapWidth = 220;
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 2, 2.4));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 2, 4.4));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 2, 1.8));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 4, 2, 2.8));
}
private static void AddThirdSeriesToChart(IChart chart)
{
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
const int worksheetIndex = 0;
IChartSeries series = chart.ChartData.Series.Add(
workbook.GetCell(worksheetIndex, 0, 3, "Series 3"), ChartType.Line);
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(worksheetIndex, 1, 3, 2.0));
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(worksheetIndex, 2, 3, 2.0));
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(worksheetIndex, 3, 3, 3.0));
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(worksheetIndex, 4, 3, 5.0));
series.PlotOnSecondAxis = true;
}
private static void SetPrimaryAxesFormat(IChart chart)
{
// Đặt trục ngang
IAxis horizontalAxis = chart.Axes.HorizontalAxis;
horizontalAxis.TextFormat.PortionFormat.FontHeight = 12f;
horizontalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(horizontalAxis, "X Axis");
// Đặt trục dọc
IAxis verticalAxis = chart.Axes.VerticalAxis;
verticalAxis.TextFormat.PortionFormat.FontHeight = 12f;
verticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(verticalAxis, "Y Axis 1");
// Đặt màu lưới chính dọc
ILineFillFormat majorGridLinesFormat = verticalAxis.MajorGridLinesFormat.Line.FillFormat;
majorGridLinesFormat.FillType = FillType.Solid;
majorGridLinesFormat.SolidFillColor.Color = Color.FromArgb(217, 217, 217);
}
private static void SetSecondaryAxesFormat(IChart chart)
{
// Đặt trục ngang phụ
IAxis secondaryHorizontalAxis = chart.Axes.SecondaryHorizontalAxis;
secondaryHorizontalAxis.Position = AxisPositionType.Bottom;
secondaryHorizontalAxis.CrossType = CrossesType.Maximum;
secondaryHorizontalAxis.IsVisible = false;
secondaryHorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
secondaryHorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
// Đặt trục dọc phụ
IAxis secondaryVerticalAxis = chart.Axes.SecondaryVerticalAxis;
secondaryVerticalAxis.Position = AxisPositionType.Right;
secondaryVerticalAxis.TextFormat.PortionFormat.FontHeight = 12f;
secondaryVerticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
secondaryVerticalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
secondaryVerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(secondaryVerticalAxis, "Y Axis 2");
}
private static void SetAxisTitle(IAxis axis, string axisTitle)
{
axis.HasTitle = true;
axis.Title.Overlay = false;
IPortionFormat titlePortionFormat =
axis.Title.AddTextFrameForOverriding(axisTitle).Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
titlePortionFormat.FontBold = NullableBool.False;
titlePortionFormat.FontHeight = 12f;
}
Cập nhật biểu đồ
Aspose.Slides for .NET cho phép bạn cập nhật các biểu đồ PowerPoint bằng cách chỉnh sửa dữ liệu, định dạng và kiểu dáng của biểu đồ. Tính năng này đơn giản hoá quá trình giữ cho các bản trình bày luôn cập nhật với nội dung động và đảm bảo biểu đồ phản ánh đúng dữ liệu hiện tại và tiêu chuẩn hình ảnh.
- Tạo một thể hiện của lớp Presentation đại diện cho bản presentation chứa biểu đồ.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Duyệt qua tất cả các shape để tìm biểu đồ.
- Truy cập worksheet dữ liệu của biểu đồ.
- Sửa đổi series dữ liệu của biểu đồ bằng cách thay đổi giá trị series.
- Thêm một series mới và điền dữ liệu cho nó.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách cập nhật một biểu đồ:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
const string chartName = "My chart";
// Khởi tạo lớp Presentation đại diện cho tệp PPTX.
using (Presentation presentation = new Presentation("ExistingChart.pptx"))
{
// Truy cập slide đầu tiên.
ISlide slide = presentation.Slides[0];
foreach (IShape shape in slide.Shapes)
{
if (shape is IChart chart && chart.Name == chartName)
{
// Đặt chỉ mục của sheet dữ liệu biểu đồ.
int worksheetIndex = 0;
// Lấy workbook dữ liệu biểu đồ.
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// Thay đổi tên các danh mục biểu đồ.
workbook.GetCell(worksheetIndex, 1, 0, "Modified Category 1");
workbook.GetCell(worksheetIndex, 2, 0, "Modified Category 2");
// Lấy series biểu đồ đầu tiên.
IChartSeries series = chart.ChartData.Series[0];
// Cập nhật dữ liệu series.
workbook.GetCell(worksheetIndex, 0, 1, "New_Series 1"); // Đang sửa đổi tên series.
series.DataPoints[0].Value.Data = 90;
series.DataPoints[1].Value.Data = 123;
series.DataPoints[2].Value.Data = 44;
// Lấy series biểu đồ thứ hai.
series = chart.ChartData.Series[1];
// Cập nhật dữ liệu series.
workbook.GetCell(worksheetIndex, 0, 2, "New_Series 2"); // Đang sửa đổi tên series.
series.DataPoints[0].Value.Data = 23;
series.DataPoints[1].Value.Data = 67;
series.DataPoints[2].Value.Data = 99;
// Thêm một series mới.
series = chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 0, 3, "Series 3"), chart.Type);
// Điền dữ liệu cho series.
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 3, 20));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 3, 50));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 3, 30));
chart.Type = ChartType.ClusteredCylinder;
}
}
// Lưu bản trình bày kèm biểu đồ.
presentation.Save("AsposeChartModified_out.pptx", SaveFormat.Pptx);
}
Đặt phạm vi dữ liệu cho biểu đồ
Để kiểm tra phạm vi đã được biểu đồ hiện có sử dụng, xem Retrieve a Chart’s Data Range.
Aspose.Slides for .NET cung cấp tính linh hoạt để định nghĩa một phạm vi dữ liệu cụ thể từ worksheet làm nguồn cho dữ liệu của biểu đồ. Điều này cho phép bạn trực tiếp ánh xạ một phần của worksheet tới biểu đồ, kiểm soát các ô nào sẽ góp phần vào series và danh mục của biểu đồ. Nhờ đó, bạn có thể dễ dàng cập nhật và đồng bộ hóa các biểu đồ với những thay đổi dữ liệu mới nhất trong worksheet, đảm bảo bản PowerPoint luôn phản ánh thông tin hiện tại và chính xác.
- Tạo một thể hiện của lớp Presentation đại diện cho bản presentation chứa biểu đồ.
- Lấy tham chiếu tới một slide bằng chỉ mục của nó.
- Duyệt qua tất cả các shape để tìm biểu đồ.
- Truy cập dữ liệu biểu đồ và đặt phạm vi.
- Lưu bản presentation đã chỉnh sửa dưới dạng file PPTX.
Mã C# này cho bạn thấy cách đặt phạm vi dữ liệu cho biểu đồ:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
const string chartName = "My chart";
// Khởi tạo lớp Presentation đại diện cho một tệp PPTX.
using (Presentation presentation = new Presentation("ExistingChart.pptx"))
{
// Truy cập slide đầu tiên.
ISlide slide = presentation.Slides[0];
foreach (IShape shape in slide.Shapes)
{
if (shape is IChart chart && chart.Name == chartName)
{
chart.ChartData.SetRange("Sheet1!A1:B4");
}
}
presentation.Save("SetDataRange_out.pptx", SaveFormat.Pptx);
}
Sử dụng các Marker mặc định trong biểu đồ
Khi bạn sử dụng các marker mặc định trong biểu đồ, mỗi series sẽ tự động nhận một ký hiệu marker mặc định khác nhau.
Mã C# này cho bạn thấy cách tự động đặt marker cho một series biểu đồ:
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IChart chart = slide.Shapes.AddChart(ChartType.LineWithMarkers, 10, 10, 400, 400);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
IChartSeries series = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 1, "Series 1"), chart.Type);
chart.ChartData.Categories.Add(workbook.GetCell(0, 1, 0, "C1"));
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 1, 1, 24));
chart.ChartData.Categories.Add(workbook.GetCell(0, 2, 0, "C2"));
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 2, 1, 23));
chart.ChartData.Categories.Add(workbook.GetCell(0, 3, 0, "C3"));
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 3, 1, -10));
chart.ChartData.Categories.Add(workbook.GetCell(0, 4, 0, "C4"));
series.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 4, 1, null));
IChartSeries series2 = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 2, "Series 2"), chart.Type);
// Điền dữ liệu cho series.
series2.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 1, 2, 30));
series2.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 2, 2, 10));
series2.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 3, 2, 60));
series2.DataPoints.AddDataPointForLineSeries(workbook.GetCell(0, 4, 2, 40));
chart.HasLegend = true;
chart.Legend.Overlay = false;
presentation.Save("DefaultMarkersInChart.pptx", SaveFormat.Pptx);
}
FAQ
Các loại biểu đồ nào được Aspose.Slides for .NET hỗ trợ?
Aspose.Slides for .NET hỗ trợ nhiều loại biểu đồ, bao gồm bar, line, pie, area, scatter, histogram, radar và nhiều loại khác. Tính linh hoạt này cho phép bạn chọn loại biểu đồ phù hợp nhất cho nhu cầu trực quan hoá dữ liệu của mình.
Làm thế nào để thêm một biểu đồ mới vào slide?
Để thêm biểu đồ, trước tiên bạn tạo một thể hiện của lớp Presentation, lấy slide mong muốn bằng chỉ mục, sau đó gọi phương thức thêm biểu đồ, chỉ định loại biểu đồ và dữ liệu ban đầu. Quá trình này tích hợp biểu đồ trực tiếp vào bản presentation của bạn.
Làm sao tôi có thể cập nhật dữ liệu hiển thị trong biểu đồ?
Bạn có thể cập nhật dữ liệu của biểu đồ bằng cách truy cập workbook dữ liệu của nó (IChartDataWorkbook), xóa bất kỳ series và danh mục mặc định nào, và sau đó thêm dữ liệu tùy chỉnh của bạn. Điều này cho phép bạn làm mới biểu đồ một cách lập trình để phản ánh dữ liệu mới nhất.
Có thể tùy chỉnh giao diện của biểu đồ không?
Có, Aspose.Slides for .NET cung cấp các tùy chọn tùy chỉnh phong phú. Bạn có thể thay đổi màu sắc, phông chữ, nhãn, legend và các yếu tố định dạng khác để điều chỉnh giao diện biểu đồ sao cho phù hợp với yêu cầu thiết kế cụ thể của mình.