Forme nei grafici

Aggiunta del controllo etichetta al grafico

Le etichette forniscono un mezzo per fornire informazioni agli utenti sul contenuto di un foglio di calcolo. Aspose.Cells consente di aggiungere e manipolare etichette anche nei grafici.

IlAspose.Cells.Drawing.ShapeCollection class fornisce un metodo denominatoAddLabelInChart, utilizzato per aggiungere un controllo etichetta a un grafico. Di seguito è riportato un elenco dei parametri utilizzati per il metodo:

  • superiore – l’offset verticale dell’etichetta dall’angolo superiore sinistro in unità di 1/4000 dell’area del grafico.
  • sinistra – l’offset verticale dell’etichetta dall’angolo superiore sinistro in unità di 1/4000 dell’area del grafico.
  • altezza – l’altezza dell’etichetta, in unità di 1/4000 dell’area del grafico.
  • larghezza – la larghezza dell’etichetta, in unità di 1/4000 dell’area del grafico.

Il metodo ritornaAspose.Cells.Drawing.Labeloggetto. IlEtichetta class rappresenta un’etichetta nel grafico. Ha alcuni membri importanti:

  • Testo(proprietà) – specifica la stringa di didascalia di un’etichetta.
  • Riempire (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 contiene un grafico. Utilizziamo questo file per inserire un’etichetta nel grafico. Di seguito è riportato il codice originale per l’aggiunta di un’etichetta al grafico. Il seguente output 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 consiste nell’utilizzare una casella di testo. Ad esempio, inserisci il testo per evidenziare il nome dell’azienda o per indicare l’area geografica con le vendite più elevate. IlAspose.Cells.Drawing.ShapeCollection class fornisce un metodo denominatoAddTextBoxInChartutilizzato per aggiungere un controllo casella di testo a un grafico. Di seguito è riportato l’elenco dei parametri utilizzati per il metodo:

  • superiore – l’offset verticale della casella di testo dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
  • sinistra – l’offset verticale della casella di testo dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
  • altezza– l’altezza della casella di testo, in unità di 1/4000 dell’area del grafico.
  • larghezza – la larghezza della casella di testo, in unità di 1/4000 dell’area del grafico.

Il metodo ritornaAspose.Cells.Drawing.TextBox oggetto. IlCasella di testoclass 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 l’aggiunta di 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 immagini 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 al suo contenuto, oppure inserisci un file di immagine del marchio.

IlAspose.Cells.Drawing.ShapeCollection class fornisce un metodo denominatoAggiungi immagine nel grafico, utilizzato per aggiungere un oggetto immagine al grafico. Di seguito è riportato l’elenco dei parametri utilizzati per il metodo:

  • superiore– l’offset verticale dell’immagine dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
  • sinistra– l’offset verticale dell’immagine dall’angolo in alto a sinistra in unità di 1/4000 dell’area del grafico.
  • flusso – un oggetto stream che contiene i dati dell’immagine.
  • widthScale – la scala della larghezza dell’immagine, un valore percentuale.
  • heightScala – la scala dell’altezza dell’immagine, un valore percentuale.

Il metodo restituisce unAspose.Cells.Drawing.Picture oggetto. IlImmagineclass 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. Usiamo questo file per inserire un’immagine nel grafico. Di seguito è riportato il codice originale per l’aggiunta di immagini 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 utilizzandoMsoDrawingType enumerazione. L’esempio seguente mostra l’aggiunta di una casella di controllo a un foglio grafico.

L’immagine seguente mostra il foglio grafico con la casella di controllo nel file di output.

cose da fare:immagine_alt_testo

Ilfile di uscitagenerato dal seguente frammento di codice è allegato come 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");

Argomenti avanzati