این مقاله شما را در مورد نحوه ایجاد نمودار دایره ای در PowerPoint با C# REST API راهنمایی می کند. یاد خواهید گرفت چگونه با C# RESTful Service یک نمودار دایره ای در پاورپوینت بسازید با استفاده از Cloud SDK مبتنی بر NET. تمامی مراحل در کد نمونه به همراه نظرات تشریحی انجام شده است.
پیش نیاز
دانلود کنید Aspose.Slides Cloud SDK for Dotnet to make a Pie chart
راه اندازی پروژه C# با SDK فوق برای کار با نمودارهای Pie در یک اسلاید ارائه
مراحل ایجاد نمودار دایره ای در پاورپوینت با C#.NET-based API
- کلاینت API را با ID و KEY با استفاده از کلاس SlidesApi راه اندازی کنید
- فایل ارائه محلی را بخوانید و آن را در فضای ذخیره سازی ابری آپلود کنید
- نمودار دایره ای را با تعیین موقعیت، اندازه و نوع آن برای پیکربندی آن ایجاد کنید
- برچسب ها را به نمودار اضافه کنید و یک سری نقاط داده برای نمودار ایجاد کنید
- سری به نمودار اضافه کنید و با روش CreateShape() این نمودار را به اسلاید هدف در ارائه بارگذاری شده اضافه کنید.
- ارائه را در یک فایل محلی دانلود کنید
این مراحل نحوه ایجاد نمودار دایره ای در پاورپوینت با C# RESTful Service را شرح می دهد. شی نمودار را نمونهسازی کنید، ویژگیهای آن را پیکربندی کنید، دستهها را اضافه کنید و سری نقاط داده را برای افزودن به نمودار دایره ایجاد کنید. در نهایت نمودار Pie را به اسلاید مورد نظر در ارائه اضافه کنید و در دیسک محلی ذخیره کنید.
کد اضافه کردن Pie Graph در پاورپوینت با سرویس C# RESTful
using SlidesCloud.SDK; // Importing the SDK for managing presentations | |
using SlidesCloud.SDK.Model; // Importing models used for creating and manipulating presentation components | |
using System; | |
using System.IO; | |
namespace PresentationManager // Custom namespace for this program | |
{ | |
class ShapeAdder | |
{ | |
static void Main(string[] args) // Main entry point of the program | |
{ | |
// Initialize the API client with API key and secret | |
SlidesApi presentationApi = new SlidesApi("Client ID", "Secret"); | |
// Upload the presentation file to the server | |
// Reads the local presentation file and uploads it to the cloud for processing | |
var uploadResult = presentationApi.UploadFile("SampleDeck.pptx", new MemoryStream(File.ReadAllBytes("SampleDeck.pptx"))); | |
// Configure chart properties | |
// Creating a new chart object and defining its position, size, and type | |
Chart customChart = new Chart | |
{ | |
ChartType = Chart.ChartTypeEnum.Pie, // Specifies the type of chart (Pie Chart) | |
X = 150, // X-coordinate of the chart's position on the slide | |
Y = 120, // Y-coordinate of the chart's position on the slide | |
Width = 450, // Width of the chart | |
Height = 350, // Height of the chart | |
Title = new ChartTitle { Text = "Pie Chart" } // Setting the chart title | |
}; | |
// Adding categories (labels) to the chart | |
customChart.Categories = new System.Collections.Generic.List<ChartCategory> | |
{ | |
new ChartCategory { Value = "Category A" }, // First category | |
new ChartCategory { Value = "Category B" }, // Second category | |
new ChartCategory { Value = "Category C" } // Third category | |
}; | |
// Define data series and data points | |
// Creating a series of data points for the chart | |
OneValueSeries chartSeries = new OneValueSeries | |
{ | |
IsColorVaried = true, // Allows different colors for each data point | |
DataPoints = new System.Collections.Generic.List<OneValueChartDataPoint> | |
{ | |
new OneValueChartDataPoint { Value = 35 }, // Data point for Category A | |
new OneValueChartDataPoint { Value = 45 }, // Data point for Category B | |
new OneValueChartDataPoint { Value = 20 } // Data point for Category C | |
} | |
}; | |
// Adding the series to the chart | |
customChart.Series = new System.Collections.Generic.List<Series> { chartSeries }; | |
// Add the chart to the first slide of the presentation | |
Chart insertedChart = (Chart)presentationApi.CreateShape("SampleDeck.pptx", 1, customChart); | |
// Display the number of categories in the chart | |
Console.WriteLine("Number of Categories in the Chart: " + insertedChart.Categories.Count); | |
// Download the updated presentation file from the server | |
Stream updatedPresentationStream = presentationApi.DownloadFile("SampleDeck.pptx"); | |
// Save the downloaded presentation to a local file | |
// Writing the downloaded stream to a new file on disk | |
using (var fileStream = new FileStream("UpdatedDeck.pptx", FileMode.Create, FileAccess.Write)) | |
{ | |
updatedPresentationStream.CopyTo(fileStream); | |
} | |
// Notify the user that the process is complete | |
Console.WriteLine("Updated presentation saved as 'UpdatedDeck.pptx'."); | |
} | |
} | |
} |
کد بالا نحوه ایجاد نمودار دایره ای در PPT با C#.NET-based API* را نشان می دهد. میتوانید اطمینان حاصل کنید که به تعداد دستههای اضافه شده در نمودار، مجموعهای از مجموعه دادهها را اضافه میکنید. همچنین توجه داشته باشید که بارگیری ارائه در ابتدای او الزامی است، در غیر این صورت ممکن است در هنگام اجرای کد بدون آپلود ارائه منبع، استثناهایی دریافت کنید.
این مقاله اضافه کردن نمودار دایره ای در ارائه را به ما آموزش داده است. برای افزودن نمودار میلهای، به مقاله با C# REST API نمودار نواری در پاورپوینت ایجاد کنید مراجعه کنید.