Ovaj članak vodi o tome kako stvoriti trakasti grafikon u PowerPoint s C# REST API. Naučit ćete kako izraditi stupčasti grafikon u PowerPointu uz C# RESTful Service koristeći Cloud SDK temeljen na .NET-u. Sadrži potpune informacije o dodavanju grafikona i podataka o postavkama.
Preduvjet
preuzimanje Aspose.Slides Cloud SDK for Dotnet to add a chart
Postavite C# projekt s gornjim SDK-om za rad s grafikonima u prezentaciji
Koraci za dodavanje stupčastog grafikona u PowerPoint s API-jem temeljenim na C# .NET
- Inicijalizirajte SlidesApi s ID-om klijenta i tajnom za dodavanje grafikona
- Prenesite prezentacijsku datoteku u oblak
- Stvorite novi konfiguracijski objekt grafikona pomoću klase Grafikon i postavite vrstu grafikona
- Dodajte naslov grafikona i inicijalizirajte kategorije za grafikon
- Inicijalizirajte seriju podataka za grafikon, stvorite seriju podataka i dodajte prvu seriju podataka
- Inicijalizirajte više serija podataka za grafikon ako je potrebno
- Dodajte grafikon na prvi slajd prezentacije pomoću metode CreateShape() i preuzmite ga ako je potrebno
Ovi koraci opisuju kako stvoriti trakasti grafikon u programu PowerPoint s API-jem temeljenim na C# .NET. Stvorite objekt SlidesApi, prenesite prezentaciju, izradite objekt grafikona, postavite vrstu grafikona, poziciju, dimenzije, naslov, kategorije i željenu seriju podataka. Dodajte niz podataka na grafikon i dodajte grafikon na prvi slajd.
Kod za izradu stupčastog grafikona u programu PowerPoint s C# REST sučeljem
using Aspose.Slides.Cloud.Sdk; | |
using Aspose.Slides.Cloud.Sdk.Model; | |
using System; | |
using System.IO; | |
namespace AsposeTestCodes | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // For adding a custom shape to a presentation | |
{ | |
// Initialize the Slides API using client ID and secret key | |
SlidesApi slidesApi = new SlidesApi("Client ID", "Secret"); | |
// Upload the presentation file to the cloud | |
var uploadResponse = slidesApi.UploadFile("MyPresentation.pptx", new MemoryStream(File.ReadAllBytes("MyPresentation.pptx"))); | |
// Create a new chart configuration object | |
Chart chartConfig = new Chart(); | |
// Define chart type as a stacked bar chart | |
chartConfig.ChartType = Chart.ChartTypeEnum.StackedBar; | |
// Set the position and dimensions of the chart on the slide | |
chartConfig.X = 150; // Horizontal position (pixels) | |
chartConfig.Y = 150; // Vertical position (pixels) | |
chartConfig.Width = 500; // Chart width (pixels) | |
chartConfig.Height = 350; // Chart height (pixels) | |
// Add a title to the chart | |
chartConfig.Title = new ChartTitle { Text = "Bar Chart Example" }; | |
// Initialize categories (x-axis labels) for the chart | |
chartConfig.Categories = new System.Collections.Generic.List<ChartCategory>(); | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group A" }); // First category | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group B" }); // Second category | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group C" }); // Third category | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group D" }); // Fourth category | |
// Initialize data series for the chart | |
chartConfig.Series = new System.Collections.Generic.List<Series>(); | |
// Create the first data series | |
OneValueSeries firstSeries = new OneValueSeries(); | |
firstSeries.DataPoints = new System.Collections.Generic.List<OneValueChartDataPoint>(); | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 15 }); // Value for Group A | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 40 }); // Value for Group B | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 25 }); // Value for Group C | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 10 }); // Value for Group D | |
// Add the first data series to the chart | |
chartConfig.Series.Add(firstSeries); | |
// Create the second data series | |
OneValueSeries secondSeries = new OneValueSeries(); | |
secondSeries.DataPoints = new System.Collections.Generic.List<OneValueChartDataPoint>(); | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 35 }); // Value for Group A | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 20 }); // Value for Group B | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 45 }); // Value for Group C | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 15 }); // Value for Group D | |
// Add the second data series to the chart | |
chartConfig.Series.Add(secondSeries); | |
// Add the chart to the first slide of the presentation | |
Chart createdChart = (Chart)slidesApi.CreateShape("MyPresentation.pptx", 1, chartConfig); | |
// Print the number of categories in the chart to the console | |
Console.WriteLine(createdChart.Categories.Count); | |
// Download the modified presentation from the cloud | |
Stream stream = slidesApi.DownloadFile("MyPresentation.pptx"); | |
// Save the downloaded file to the local system | |
var fs = new FileStream("Downloaded.pptx", FileMode.Create, FileAccess.Write); | |
stream.CopyTo(fs); // Copy the stream data to the local file | |
} | |
} | |
} |
Ovaj kôd demonstrira proces kako napraviti trakasti grafikon u PowerPointu s C# Low Code API. Možete postaviti različite vrste grafikona kao što su ClusteredColumn, Column3D, ClusteredCone i Line itd., koristeći ChartTypeEnum i druge parametre u objektu Chart uključujući ShowDataLabelsOverMaximum, BackWall, SideWall, Floor i Legend itd.
Ovaj nas je članak naučio izraditi trakasti grafikon u PowerPointu. Za pretvaranje HTML-a u PowerPoint pogledajte članak na Pretvorite HTML u PowerPoint pomoću NET REST API-ja.