位置、サイズ、およびデザインチャートを操作

チャートの位置とサイズ

時には、ワークシート内で新しいチャートまたは既存のチャートの位置またはサイズを変更したいことがあります。 Aspose.Cellsはこれを実現するための Chart.ChartObject プロパティを提供しています。 新しい 高さ を使用してグラフのサイズを変更したり、新しい XY 座標を使用して再配置したりできます。

チャートの位置とサイズの制御

チャートの位置(X、Y座標)またはサイズ(高さ、幅)を変更するには、これらのプロパティを使用します。

  1. Chart.ChartObject.X
  2. Chart.ChartObject.Y
  3. Chart.ChartObject.Height
  4. Chart.ChartObject.Width

上記の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");

チャートの操作

次の例は、既存のチャートの操作方法を示しています。この例では、上記で作成したチャートを変更します。生成された出力では、1つのデータポイントの日付ラベルが ‘United Kingdom, 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");