Manipuler la taille de la position et le graphique du concepteur

Position et taille du graphique

Parfois, vous souhaitez modifier la position ou la taille du graphique nouveau ou existant dans la feuille de calcul. Aspose.Cells fournit leChart.ChartObject propriété pour y parvenir. Vous pouvez utiliser ses sous-propriétés pour redimensionner le graphique avec de nouvellesla taille etlargeur ou repositionnez-le avec de nouveaux** X** et**Coordonnées Y**.

Contrôle de la position et de la taille du graphique

Pour modifier la position (coordonnées X, Y) ou la taille (hauteur, largeur) du graphique, utilisez ces propriétés :

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

L’exemple suivant explique l’utilisation des API ci-dessus, il charge le classeur existant qui contient un graphique dans sa première feuille de calcul. Ensuite, il redimensionne et repositionne le graphique en utilisant 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");

Manipulation des graphiques de concepteur

Il arrive parfois que vous ayez besoin de manipuler ou de modifier des graphiques dans des fichiers de modèle de concepteur. Aspose.Cells prend entièrement en charge la manipulation du contenu et des éléments des graphiques de concepteur. Les données, le contenu du graphique, l’image d’arrière-plan et les mises en forme peuvent être conservés avec précision.

Manipulation de graphiques Designer dans des fichiers modèles

Pour manipuler les graphiques de concepteur dans les fichiers de modèle, utilisez le graphique associé API. Par exemple, vous pouvez utiliser la propriété Worksheet.Charts pour obtenir la collection de graphiques existante dans le fichier de modèle.

Création d’un graphique

L’exemple suivant montre comment créer un diagramme pyramidal. Nous manipulerons ce graphique plus tard.

// 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");

Manipulation du graphique

L’exemple suivant montre comment manipuler le graphique existant. Dans cet exemple, nous modifions le graphique créé ci-dessus. Dans la sortie générée, notez que l’étiquette de date d’un point de données a été définie sur “Royaume-Uni, 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");

Manipulation d’un graphique en courbes dans le modèle Designer

Dans cet exemple, nous allons manipuler un graphique linéaire. Nous ajouterons des séries de données au graphique existant et modifierons les couleurs de leurs lignes.

// 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");