Grafiğin Veri Kaynağını Ayarlama
Önceki konularımızda, grafikleriniz için bir veri kaynağı nasıl belirleneceğini zaten birçok örnek vermiştik, ancak bu konuda, bir grafik için belirlenebilecek veri türleri hakkında daha fazla ayrıntı sağlayacağız.
Grafik Verisi Ayarlama
Aspose.Cells for Python via .NET ile grafiklerde çalışırken iki tür veriyle ilgilenirsiniz:
- Grafik verisi.
- Kategori verisi.
Grafik Verisi
Grafik verisi, grafiklerimizi oluşturmak için veri kaynağı olarak kullandığımız veridir. Hücrelerin bir rangını (grafik verisi içeren) SeriesCollection nesnesinin add yöntemi çağrılarak ekleyebiliriz.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding sample values to cells | |
worksheet.cells.get("A1").put_value(50) | |
worksheet.cells.get("A2").put_value(100) | |
worksheet.cells.get("A3").put_value(170) | |
worksheet.cells.get("A4").put_value(300) | |
worksheet.cells.get("B1").put_value(160) | |
worksheet.cells.get("B2").put_value(32) | |
worksheet.cells.get("B3").put_value(50) | |
worksheet.cells.get("B4").put_value(40) | |
# Adding sample values to cells as category data | |
worksheet.cells.get("C1").put_value("Q1") | |
worksheet.cells.get("C2").put_value("Q2") | |
worksheet.cells.get("C3").put_value("Y1") | |
worksheet.cells.get("C4").put_value("Y2") | |
# Adding a chart to the worksheet | |
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5) | |
# Accessing the instance of the newly added chart | |
chart = worksheet.charts[chartIndex] | |
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B4" | |
chart.n_series.add("A1:B4", True) | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Kategori Verisi
Kategori verisi, grafik verisinin etiketlenmesi için kullanılır ve SeriesCollection‘nin category_data özelliği kullanılarak eklenir. Aşağıda, grafik ve kategori verisinin kullanımını gösteren tam bir örnek verilmiştir. Yukarıdaki örnek kodu çalıştırdıktan sonra, çalışma sayfasına sütun grafiği aşağıdaki gibi eklenecektir.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding sample values to cells | |
worksheet.cells.get("A1").put_value(10) | |
worksheet.cells.get("A2").put_value(100) | |
worksheet.cells.get("A3").put_value(170) | |
worksheet.cells.get("A4").put_value(200) | |
worksheet.cells.get("B1").put_value(120) | |
worksheet.cells.get("B2").put_value(320) | |
worksheet.cells.get("B3").put_value(50) | |
worksheet.cells.get("B4").put_value(40) | |
# Adding sample values to cells as category data | |
worksheet.cells.get("C1").put_value("Q1") | |
worksheet.cells.get("C2").put_value("Q2") | |
worksheet.cells.get("C3").put_value("Y1") | |
worksheet.cells.get("C4").put_value("Y2") | |
# Adding a chart to the worksheet | |
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5) | |
# Accessing the instance of the newly added chart | |
chart = worksheet.charts[chartIndex] | |
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B4" | |
chart.n_series.add("A1:B4", True) | |
# Setting the data source for the category data of SeriesCollection | |
chart.n_series.category_data = "C1:C4" | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |