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 pour Python via .NET permet d’ajouter et de manipuler des étiquettes même dans les graphiques.
La classe aspose.cells.drawing.ShapeCollection fournit une méthode nommée add_label_in_chart, 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.
from aspose.cells import Workbook | |
from aspose.cells.drawing import PlacementType | |
# 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(".") | |
# Open the existing file. | |
workbook = Workbook(dataDir + "chart.xls") | |
# Get the designer chart in the second sheet. | |
sheet = workbook.worksheets[1] | |
chart = sheet.charts[0] | |
# Add a new label to the chart. | |
label = chart.shapes.add_label_in_chart(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 = PlacementType.FREE_FLOATING | |
# 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 add_text_box_in_chart, 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.
from aspose.cells import Workbook | |
from aspose.cells.drawing import MsoLineDashStyle | |
from aspose.pydrawing import Color | |
# 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(".") | |
# Open the existing file. | |
workbook = Workbook(dataDir + "chart.xls") | |
# Get the designer chart in the second sheet. | |
sheet = workbook.worksheets[1] | |
chart = sheet.charts[0] | |
# Add a new textbox to the chart. | |
textbox0 = chart.shapes.add_text_box_in_chart(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.is_bold = True | |
# Set the font size. | |
textbox0.font.size = 14 | |
# Set font attribute to italic. | |
textbox0.font.is_italic = True | |
# Get the filformat of the textbox. | |
fillformat = textbox0.fill | |
# Get the lineformat type of the textbox. | |
lineformat = textbox0.line | |
# Set the line weight. | |
lineformat.weight = 2.0 | |
# Set the dash style to solid. | |
lineformat.dash_style = MsoLineDashStyle.SOLID | |
# Save the excel file. | |
workbook.save(dataDir + "chart.out.xls") |
Ajout d’une image au graphique
Aspose.Cells pour Python via .NET permet d’insérer des images dans un graphique. Par exemple, ajouter une image pour mettre en évidence ou donner plus de sens à un graphique ou à son contenu, ou insérer un fichier image de marque.
La classe aspose.cells.drawing.ShapeCollection fournit une méthode nommée add_picture_in_chart, 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.
from aspose.cells import Workbook | |
from aspose.cells.drawing import MsoLineDashStyle | |
# 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(".") | |
# Open the existing file. | |
workbook = Workbook(dataDir + "chart.xls") | |
# Get an image file to the stream. | |
stream = open(dataDir + "logo.jpg", "rb") | |
# Get the designer chart in the second sheet. | |
sheet = workbook.worksheets[0] | |
chart = sheet.charts[0] | |
# Add a new picture to the chart. | |
pic0 = chart.shapes.add_picture_in_chart(50, 50, stream, 40, 40) | |
# Get the lineformat type of the picture. | |
lineformat = pic0.line | |
# Set the dash style. | |
lineformat.dash_style = MsoLineDashStyle.SOLID | |
# Set the line weight. | |
lineformat.weight = 4.0 | |
# Save the excel file. | |
workbook.save(dataDir + "chart.out.xls") |
Ajout d’une case à cocher dans le graphique
Aspose.Cells pour Python via .NET 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.
Le fichier de sortie généré par le snippet de code suivant est joint à titre de référence.
from aspose.cells import SheetType, Workbook | |
from aspose.cells.charts import ChartType | |
from aspose.cells.drawing import MsoDrawingType, PlacementType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a chart to the worksheet | |
index = workbook.worksheets.add(SheetType.CHART) | |
sheet = workbook.worksheets[index] | |
sheet.charts.add_floating_chart(ChartType.COLUMN, 0, 0, 1024, 960) | |
sheet.charts[0].n_series.add("{1,2,3}", False) | |
# Add checkbox to the chart. | |
sheet.charts[0].shapes.add_shape_in_chart(MsoDrawingType.CHECK_BOX, PlacementType.MOVE, 400, 400, 1000, 600) | |
sheet.charts[0].shapes[0].text = "CheckBox 1" | |
# Save the excel file. | |
workbook.save(outputDir + "InsertCheckboxInChartSheet_out.xlsx") |