Formes dans les graphiques

Ajout de contrôle d’étiquette au graphique

Les étiquettes fournissent un moyen de donner des informations aux utilisateurs sur le contenu d’une feuille de calcul. Aspose.Cells vous permet d’ajouter et de manipuler des étiquettes même dans des graphiques.

La classe Aspose.Cells.Drawing.ShapeCollection fournit une méthode nommée AddLabelInChart, utilisée pour ajouter un contrôle d’étiquette à un graphique. Voici une liste des paramètres utilisés pour la méthode :

  • haut – le décalage vertical de l’étiquette depuis le coin supérieur gauche en unités de 1/4000 de la zone du graphique.
  • gauche – le décalage horizontal de l’étiquette depuis le coin supérieur gauche en unités de 1/4000 de la zone du graphique.
  • hauteur – la hauteur de l’étiquette, en unités de 1/4000 de la zone du graphique.
  • largeur – la largeur de l’étiquette, en unités de 1/4000 de la zone du graphique.

La méthode retourne un objet Aspose.Cells.Drawing.Label. La classe Label représente une étiquette dans le graphique. Elle a quelques membres importants :

  • Text (propriété) - spécifie la chaîne de légende d’une étiquette.
  • Fill (propriété) - spécifie les attributs de couleur de remplissage.

L’exemple suivant montre comment ajouter une étiquette au graphique. L’exemple utilise un fichier de concepteur (exp_piechart.xls) qui contient un graphique. Nous utilisons ce fichier pour insérer une étiquette dans le graphique. Ci-dessous se trouve le code d’origine pour ajouter une étiquette au graphique. La sortie suivante est générée lors de l’exécution du code.

// 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 + "chart.xls");
// Get the designer chart in the second sheet.
Worksheet sheet = workbook.Worksheets[1];
Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
// Add a new label to the chart.
Aspose.Cells.Drawing.Label label = chart.Shapes.AddLabelInChart(100, 100, 350, 900);
// Set the caption of the label.
label.Text = "A Label In Chart";
// Set the Placement Type, the way the
// Label is attached to the cells.
label.Placement = Aspose.Cells.Drawing.PlacementType.FreeFloating;
// Save the excel file.
workbook.Save(dataDir + "chart.out.xls");

Ajout d’un contrôle TextBox au graphique

Une manière de mettre en évidence des informations importantes dans un rapport est d’utiliser une zone de texte. Par exemple, saisissez du texte pour mettre en valeur le nom de l’entreprise ou pour indiquer la région géographique avec le plus de ventes. La classe Aspose.Cells.Drawing.ShapeCollection fournit une méthode appelée AddTextBoxInChart, qui est utilisée pour ajouter un contrôle de zone de texte à un graphique. Voici la liste des paramètres utilisés pour la méthode :

  • top - le décalage vertical de la zone de texte depuis le coin supérieur gauche en unités de 1/4000 de la zone du graphique.
  • left - le décalage vertical de la zone de texte depuis le coin supérieur gauche en unités de 1/4000 de la zone du graphique.
  • height - la hauteur de la zone de texte, en unités de 1/4000 de la zone du graphique.
  • width - la largeur de la zone de texte, en unités de 1/4000 de la zone du graphique.

La méthode renvoie un objet Aspose.Cells.Drawing.TextBox. La classe TextBox représente une zone de texte dans le graphique.

L’exemple suivant montre comment ajouter une zone de texte à un graphique. L’exemple utilise le fichier de concepteur précédent (exp_piechart.xls) qui contient un graphique. Nous utilisons ce fichier pour insérer une zone de texte dans le graphique pour afficher le titre du graphique. Ci-dessous se trouve le code d’origine pour ajouter une zone de texte au graphique.

// 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 + "chart.xls");
// Get the designer chart in the second sheet.
Worksheet sheet = workbook.Worksheets[1];
Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
// Add a new textbox to the chart.
Aspose.Cells.Drawing.TextBox textbox0 = chart.Shapes.AddTextBoxInChart(100, 1100, 350, 2550);
// Fill the text.
textbox0.Text = "Sales By Region";
// Get the textbox text frame.
// Aspose.Cells.Drawing.MsoTextFrame textframe0 = textbox0.TextFrame;
// Set the textbox to adjust it according to its contents.
// textframe0.AutoSize = true;
// Set the font color.
textbox0.Font.Color = Color.Maroon;
// Set the font to bold.
textbox0.Font.IsBold = true;
// Set the font size.
textbox0.Font.Size = 14;
// Set font attribute to italic.
textbox0.Font.IsItalic = true;
// Get the filformat of the textbox.
Aspose.Cells.Drawing.FillFormat fillformat = textbox0.Fill;
// Get the lineformat type of the textbox.
Aspose.Cells.Drawing.LineFormat lineformat = textbox0.Line;
// Set the line weight.
lineformat.Weight = 2;
// Set the dash style to solid.
lineformat.DashStyle = Aspose.Cells.Drawing.MsoLineDashStyle.Solid;
// Save the excel file.
workbook.Save(dataDir + "chart.out.xls");

Ajout d’une image au graphique

Aspose.Cells vous permet d’insérer des images dans un graphique. Par exemple, ajoutez une image pour mettre en avant ou donner plus de sens à un graphique ou à son contenu, ou insérez un fichier image de marque.

La classe Aspose.Cells.Drawing.ShapeCollection fournit une méthode nommée AddPictureInChart, qui est utilisée pour ajouter un objet image au graphique. Voici la liste des paramètres utilisés pour la méthode :

  • top - le décalage vertical de l’image depuis le coin supérieur gauche en unités de 1/4000 de la zone du graphique.
  • left - le décalage vertical de l’image depuis le coin supérieur gauche en unités de 1/4000 de la zone du graphique.
  • stream - un objet flux qui contient les données de l’image.
  • widthScale - l’échelle de la largeur de l’image, une valeur en pourcentage.
  • heightScale - l’échelle de la hauteur de l’image, une valeur en pourcentage.

La méthode renvoie un objet Aspose.Cells.Drawing.Picture. La classe Picture représente un objet image dans le graphique.

L’exemple suivant montre comment ajouter une image au graphique. L’exemple utilise le fichier de conception précédent (exp_piechart.xls) qui contient un graphique. Nous utilisons ce fichier pour insérer une image dans le graphique. Ci-dessous se trouve le code d’origine pour ajouter une image au graphique.

// 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 + "chart.xls");
// Get an image file to the stream.
FileStream stream = new FileStream(dataDir + "logo.jpg", FileMode.Open, FileAccess.Read);
// Get the designer chart in the second sheet.
Worksheet sheet = workbook.Worksheets[0];
Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
// Add a new picture to the chart.
Aspose.Cells.Drawing.Picture pic0 = chart.Shapes.AddPictureInChart(50, 50, stream, 40, 40);
// Get the lineformat type of the picture.
Aspose.Cells.Drawing.LineFormat lineformat = pic0.Line;
// Set the dash style.
lineformat.DashStyle = Aspose.Cells.Drawing.MsoLineDashStyle.Solid;
// Set the line weight.
lineformat.Weight = 4;
// Save the excel file.
workbook.Save(dataDir + "chart.out.xls");

Ajout d’une case à cocher dans le graphique

Aspose.Cells vous permet d’insérer des cases à cocher dans une feuille de graphique en utilisant l’énumération MsoDrawingType. L’exemple suivant montre comment ajouter une case à cocher à une feuille de graphique.

L’image suivante montre la feuille de graphique avec la case à cocher dans le fichier de sortie.

todo:image_alt_text

Le fichier de sortie généré par le snippet de code suivant est joint à titre de référence.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a chart to the worksheet
int index = workbook.Worksheets.Add(SheetType.Chart);
Worksheet sheet = workbook.Worksheets[index];
sheet.Charts.AddFloatingChart(ChartType.Column, 0, 0, 1024, 960);
sheet.Charts[0].NSeries.Add("{1,2,3}", false);
// Add checkbox to the chart.
sheet.Charts[0].Shapes.AddShapeInChart(MsoDrawingType.CheckBox, PlacementType.Move, 400, 400, 1000, 600);
sheet.Charts[0].Shapes[0].Text = "CheckBox 1";
// Save the excel file.
workbook.Save(outputDir + "InsertCheckboxInChartSheet_out.xlsx");

Sujets avancés