Forme nei grafici
Aggiunta del Controllo Etichetta al Grafico
Le etichette forniscono un modo per fornire informazioni agli utenti sul contenuto di un foglio di calcolo. Aspose.Cells consente di aggiungere e manipolare le etichette anche nei grafici.
La classe Aspose.Cells.Drawing.ShapeCollection fornisce un metodo chiamato AddLabelInChart, utilizzato per aggiungere un controllo etichetta a un grafico. Di seguito è riportato un elenco dei parametri utilizzati per il metodo:
- top – lo spostamento verticale dell’etichetta dall’angolo in alto a sinistra in unità dello 1/4000 dell’area del grafico.
- sinistra – lo spostamento verticale dell’etichetta dall’angolo in alto a sinistra in unità dello 1/4000 dell’area del grafico.
- altezza – l’altezza dell’etichetta, in unità dello 1/4000 dell’area del grafico.
- larghezza – la larghezza dell’etichetta, in unità dello 1/4000 dell’area del grafico.
Il metodo restituisce l’oggetto Aspose.Cells.Drawing.Label. La classe Label rappresenta un’etichetta nel grafico. Ha alcuni membri importanti:
- Text (proprietà) – specifica una stringa di sottotitolo dell’etichetta.
- Fill (proprietà) – specifica gli attributi del colore di riempimento.
L’esempio seguente mostra come aggiungere un’etichetta al grafico. L’esempio utilizza un file di progettazione (exp_piechart.xls) che ha un grafico al suo interno. Usiamo questo file per inserire un’etichetta nel grafico. Di seguito è riportato il codice originale per aggiungere un’etichetta al grafico. L’output seguente viene generato durante l’esecuzione del codice.
// 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"); |
Aggiunta del controllo TextBox al grafico
Un modo per evidenziare informazioni importanti in un report è utilizzare una casella di testo. Ad esempio, inserire del testo per evidenziare il nome dell’azienda o per indicare la regione geografica con le vendite più alte. La classe Aspose.Cells.Drawing.ShapeCollection fornisce un metodo chiamato AddTextBoxInChart, che viene utilizzato per aggiungere un controllo casella di testo a un grafico. Di seguito è riportato l’elenco dei parametri utilizzati per il metodo:
- top – lo spostamento verticale della casella di testo dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
- left – lo spostamento verticale della casella di testo dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
- height – l’altezza della casella di testo, in unità di 1/4000 dell’area del grafico.
- width – la larghezza della casella di testo, in unità di 1/4000 dell’area del grafico.
Il metodo restituisce un oggetto Aspose.Cells.Drawing.TextBox. La classe TextBox rappresenta una casella di testo nel grafico.
L’esempio seguente mostra come aggiungere una casella di testo a un grafico. L’esempio utilizza il file di progettazione precedente (exp_piechart.xls) che contiene un grafico. Utilizziamo questo file per inserire una casella di testo nel grafico per mostrare il titolo del grafico. Di seguito è riportato il codice originale per aggiungere una casella di testo al grafico.
// 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"); |
Aggiunta di un’immagine al grafico
Aspose.Cells consente di inserire immagini in un grafico. Ad esempio, aggiungi un’immagine per enfatizzare o dare più significato a un grafico o ai suoi contenuti, o inserisci un file immagine del marchio.
La classe Aspose.Cells.Drawing.ShapeCollection fornisce un metodo chiamato AddPictureInChart, che viene utilizzato per aggiungere un oggetto immagine al grafico. Di seguito è riportato l’elenco dei parametri utilizzati per il metodo:
- top – lo spostamento verticale dell’immagine dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
- left – lo spostamento verticale dell’immagine dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
- stream – un oggetto stream che contiene i dati dell’immagine.
- widthScale – la scala della larghezza dell’immagine, un valore percentuale.
- heightScale – la scala dell’altezza dell’immagine, un valore percentuale.
Il metodo restituisce un oggetto Aspose.Cells.Drawing.Picture. La classe Picture rappresenta un oggetto immagine nel grafico.
L’esempio seguente mostra come aggiungere un’immagine al grafico. L’esempio utilizza il file di progettazione precedente (exp_piechart.xls) che contiene un grafico. Utilizziamo questo file per inserire un’immagine nel grafico. Di seguito è riportato il codice originale per aggiungere un’immagine al grafico.
// 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"); |
Aggiunta di una casella di controllo nel grafico
Aspose.Cells consente di inserire caselle di controllo in un foglio grafico utilizzando l’enumerazione MsoDrawingType. L’esempio seguente illustra come aggiungere una casella di controllo a un foglio grafico.
L’immagine seguente mostra il foglio di lavoro del grafico con la casella di controllo nel file di output.
Il file di output generato dal frammento di codice seguente è allegato per il tuo riferimento.
// 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"); |