Grafik Oluşturma ve Özelleştirme

Olası Kullanım Senaryoları

Bir grafik, bilgilerin görsel bir şekilde gösterimidir. Aspose.Cells geliştiricilere, Microsoft Excel’in yaptığı gibi bilgileri grafiklerde görselleştirmeyi sağlar. Bilgilerin grafiklerde sunulması, karar vericilerin hızlı ve zamanında kararlar almasına yardımcı olur. Grafikler aracılığıyla verilerdeki karşılaştırmalar, desenler ve trendler hızlı bir şekilde görülebilir. Aspose.Cells’ın faydalı bir özelliği, elektronik tablodaki verilere dayalı olarak çalışma zamanında grafikler oluşturmaktır. Aspose.Cells, Hem Standart Hem de Özelleştirilmiş grafikler oluşturmayı destekler. Aşağıda, Aspose.Cells API’sini kullanarak yaygın MS-Excel grafik tiplerini oluşturmak için birkaç örnek dosya ile bazı örnekleri göstereceğiz.

Piramit Grafiği

Örnek kod çalıştırıldığında, bir piramit grafik çalışma sayfasına eklenir. Lütfen aşağıdaki örnek kodun çıktı Excel dosyasını inceleyin.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypePyramid = outDir + u"outputChartTypePyramid.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(50);
worksheet.GetCells().Get(u"A2").PutValue(100);
worksheet.GetCells().Get(u"A3").PutValue(150);
worksheet.GetCells().Get(u"B1").PutValue(4);
worksheet.GetCells().Get(u"B2").PutValue(20);
worksheet.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Pyramid, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the Excel file
workbook.Save(outputChartTypePyramid);
Aspose::Cells::Cleanup();

Çizgi Grafiği

Yukarıdaki örnekte, sadece ChartType‘yu ChartType::Line‘a değiştirerek bir çizgi grafiği oluşturulur. Tam kaynak aşağıda sağlanmıştır. Kod çalıştırıldığında, bir çizgi grafiği çalışma sayfasına eklenir. Lütfen aşağıdaki örnek kodun çıktı Excel dosyasını inceleyin.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeLine = testPath + u"outputChartTypeLine.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(50);
worksheet.GetCells().Get(u"A2").PutValue(100);
worksheet.GetCells().Get(u"A3").PutValue(150);
worksheet.GetCells().Get(u"B1").PutValue(4);
worksheet.GetCells().Get(u"B2").PutValue(20);
worksheet.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Line, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the Excel file
workbook.Save(outputChartTypeLine);
Aspose::Cells::Cleanup();

Kabarcık Grafiği

Baloncuk grafik oluşturmak için, ChartType ChartType_Bubble olarak ayarlanmalı ve SetBubbleSizes & SetXValues gibi ekstra özellikler uygun şekilde ayarlanmalıdır. Aşağıdaki kodu çalıştırdığınızda, bir baloncuk grafik çalışma sayfasına eklenir. Lütfen aşağıdaki örnek kodun çıktı Excel dosyasını inceleyin.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeBubble = outDir + u"outputChartTypeBubble.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Fill in data for chart's series
// Y Values
worksheet.GetCells().Get(0, 0).PutValue(u"Y Values");
worksheet.GetCells().Get(0, 1).PutValue(2);
worksheet.GetCells().Get(0, 2).PutValue(4);
worksheet.GetCells().Get(0, 3).PutValue(6);
// Bubble Size
worksheet.GetCells().Get(1, 0).PutValue(u"Bubble Size");
worksheet.GetCells().Get(1, 1).PutValue(2);
worksheet.GetCells().Get(1, 2).PutValue(3);
worksheet.GetCells().Get(1, 3).PutValue(1);
// X Values
worksheet.GetCells().Get(2, 0).PutValue(u"X Values");
worksheet.GetCells().Get(2, 1).PutValue(1);
worksheet.GetCells().Get(2, 2).PutValue(2);
worksheet.GetCells().Get(2, 3).PutValue(3);
// Set first column width
worksheet.GetCells().SetColumnWidth(0, 12);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Bubble, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from B1 to D1
chart.GetNSeries().Add(u"B1:D1", true);
// Set bubble sizes
chart.GetNSeries().Get(0).SetBubbleSizes(u"B2:D2");
// Set X axis values
chart.GetNSeries().Get(0).SetXValues(u"B3:D3");
// Set Y axis values
chart.GetNSeries().Get(0).SetValues(u"B1:D1");
// Saving the Excel file
workbook.Save(outputChartTypeBubble);
Aspose::Cells::Cleanup();

Özel Grafikler Oluşturma

Şimdiye kadar, grafikleri tartışırken, kendi standart biçim ayarlarına sahip standart grafiklere bakmıştık. Yalnızca veri kaynağını tanımlarız, birkaç özellik ayarları yaparız ve grafik, varsayılan biçim ayarlarıyla oluşturulur. Ancak Aspose.Cells API’leri, geliştiricilere kendi biçim ayarları ile grafik oluşturmayı destekler. Geliştiriciler, Aspose.Cells’ı kullanarak çalışma zamanında özel grafikler oluşturabilir.

Bir grafik, bir veri serisinden oluşur. Özel bir grafik oluştururken, geliştiriciler farklı veri serileri için farklı grafik türlerini kullanmada özgürlüğe sahiptir.

Aşağıdaki örnek kod, özel grafikler oluşturmayı nasıl gösterir. Bu örnekte, ilk veri serisi için bir sütun grafiği ve ikinci serisi için bir çizgi grafiği kullanacağız. Sonuç olarak, bir çalışma sayfasına sütun grafiği ve çizgi grafiği eklenir. Lütfen aşağıdaki örnek kodun çıktı Excel dosyasını inceleyin.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeCustom = outDir + u"outputChartTypeCustom.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(50);
worksheet.GetCells().Get(u"A2").PutValue(100);
worksheet.GetCells().Get(u"A3").PutValue(150);
worksheet.GetCells().Get(u"A4").PutValue(110);
worksheet.GetCells().Get(u"B1").PutValue(260);
worksheet.GetCells().Get(u"B2").PutValue(12);
worksheet.GetCells().Get(u"B3").PutValue(50);
worksheet.GetCells().Get(u"B4").PutValue(100);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from A1 to B4
chart.GetNSeries().Add(u"A1:B4", true);
// Setting the chart type of 2nd NSeries to display as line chart
chart.GetNSeries().Get(1).SetType(Aspose::Cells::Charts::ChartType::Line);
// Saving the Excel file
workbook.Save(outputChartTypeCustom);
Aspose::Cells::Cleanup();