操纵头寸规模和设计师图表

图表位置和大小

有时,您想要更改工作表中新图表或现有图表的位置或大小。 Aspose.Cells 提供了图表.ChartObject财产来实现这一点。您可以使用它的子属性来重新调整图表的大小高度宽度或用新的重新定位XY 坐标。

控制图表位置和大小

要更改图表的位置(X、Y 坐标)或大小(高度、宽度),请使用以下属性:

  1. 图表.ChartObject.X
  2. 图表.ChartObject.Y
  3. Chart.ChartObject.高度
  4. Chart.ChartObject.宽度

以下示例解释了上述 API 的用法,它加载了现有工作簿,该工作簿的第一个工作表中包含一个图表。然后它使用 Aspose.Cells 重新调整图表的大小和位置。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Workbook workbook = new Workbook(dataDir + "chart.xls");
Worksheet worksheet = workbook.Worksheets[1];
// Load the chart from source worksheet
Chart chart = worksheet.Charts[0];
// Resize the chart
chart.ChartObject.Width = 400;
chart.ChartObject.Height = 300;
// Reposition the chart
chart.ChartObject.X = 250;
chart.ChartObject.Y = 150;
// Output the file
workbook.Save(dataDir + "chart.out.xls");

操纵设计师图表

有时您需要操作或修改设计器模板文件中的图表。 Aspose.Cells 完全支持操纵设计器图表内容和元素。可以准确保存数据、图表内容、背景图像和格式。

在模板文件中操作设计器图表

要在模板文件中操作设计器图表,请使用图表相关的 API。例如,您可以使用 Worksheet.Charts 属性来获取模板文件中现有的图表集合。

创建图表

以下示例显示了如何创建金字塔图。稍后我们将操作此图表。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding sample values to cells
worksheet.Cells["A1"].PutValue(50);
worksheet.Cells["A2"].PutValue(100);
worksheet.Cells["A3"].PutValue(150);
worksheet.Cells["B1"].PutValue(4);
worksheet.Cells["B2"].PutValue(20);
worksheet.Cells["B3"].PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5);
// Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3", true);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

操纵图表

以下示例显示如何操作现有图表。在这个例子中,我们修改上面创建的图表。在生成的输出中,请注意一个数据点的日期标签已设置为“英国,30K”。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing file.
Workbook workbook = new Workbook(dataDir + "piechart.xls");
// Get the designer chart in the second sheet.
Worksheet sheet = workbook.Worksheets[1];
Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
// Get the data labels in the data series of the third data point.
Aspose.Cells.Charts.DataLabels datalabels = chart.NSeries[0].Points[2].DataLabels;
// Change the text of the label.
datalabels.Text = "Unided Kingdom, 400K ";
// Save the excel file.
workbook.Save(dataDir + "output.xls");

在设计器模板中操作折线图

在这个例子中,我们将操作一个折线图。我们将向现有图表添加一些数据系列并更改它们的线条颜色。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing file.
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Get the designer chart in the first worksheet.
Aspose.Cells.Charts.Chart chart = workbook.Worksheets[0].Charts[0];
// Add the third data series to it.
chart.NSeries.Add("{60, 80, 10}", true);
// Add another data series (fourth) to it.
chart.NSeries.Add("{0.3, 0.7, 1.2}", true);
// Plot the fourth data series on the second axis.
chart.NSeries[3].PlotOnSecondAxis = true;
// Change the Border color of the second data series.
chart.NSeries[1].Border.Color = Color.Green;
// Change the Border color of the third data series.
chart.NSeries[2].Border.Color = Color.Red;
// Make the second value axis visible.
chart.SecondValueAxis.IsVisible = true;
// Save the excel file.
workbook.Save(dataDir + "output.xls");