Formattazione del Grafico
Impostazione dell’aspetto del grafico
In Tipi di grafici, abbiamo dato una breve introduzione ai tipi di grafici e oggetti per la creazione di grafici offerti da Aspose.Cells.
In questo articolo, discutiamo come personalizzare l’aspetto dei grafici impostando diversi tipi di proprietà:
- Impostare l’area del grafico.
- Impostare le linee del grafico.
- Applicazione dei temi.
- Impostazione dei titoli per grafici e assi.
- Lavorare con le linee di griglia.
- Impostazione dei bordi per le pareti posteriori e laterali.
Impostazione dell’Area del Grafico
Ci sono diversi tipi di aree in un grafico e Aspose.Cells fornisce la flessibilità di modificare l’aspetto di ciascuna area. Gli sviluppatori possono applicare diverse impostazioni di formattazione su un’area cambiando il suo colore principale, colore di sfondo e formato di riempimento, ecc.
Nell’esempio di seguito, abbiamo applicato diverse impostazioni di formattazione su diversi tipi di aree di un grafico. Queste aree includono:
- Area del tracciato
- Area del grafico
- SeriesCollection area
- L’area di un singolo punto in un SeriesCollection
Dopo aver eseguito il codice di esempio, verrà aggiunto un grafico a colonne al foglio di lavoro come mostrato di seguito:
Un grafico a colonne con aree riempite
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingChartArea.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of the newly added worksheet by passing its | |
// sheet index | |
Worksheet worksheet = worksheets.get(0); | |
Cells cells = worksheet.getCells(); | |
// Adding a sample value to "A1" cell | |
cells.get("A1").setValue(50); | |
// Adding a sample value to "A2" cell | |
cells.get("A2").setValue(100); | |
// Adding a sample value to "A3" cell | |
cells.get("A3").setValue(150); | |
// Adding a sample value to "B1" cell | |
cells.get("B1").setValue(60); | |
// Adding a sample value to "B2" cell | |
cells.get("B2").setValue(32); | |
// Adding a sample value to "B3" cell | |
cells.get("B3").setValue(50); | |
// Adding a chart to the worksheet | |
ChartCollection charts = worksheet.getCharts(); | |
// Accessing the instance of the newly added chart | |
int chartIndex = charts.add(ChartType.COLUMN, 5, 0, 15, 7); | |
Chart chart = charts.get(chartIndex); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell | |
SeriesCollection nSeries = chart.getNSeries(); | |
nSeries.add("A1:B3", true); | |
// Setting the foreground color of the plot area | |
ChartFrame plotArea = chart.getPlotArea(); | |
Area area = plotArea.getArea(); | |
area.setForegroundColor(Color.getBlue()); | |
// Setting the foreground color of the chart area | |
ChartArea chartArea = chart.getChartArea(); | |
area = chartArea.getArea(); | |
area.setForegroundColor(Color.getYellow()); | |
// Setting the foreground color of the 1st NSeries area | |
Series aSeries = nSeries.get(0); | |
area = aSeries.getArea(); | |
area.setForegroundColor(Color.getRed()); | |
// Setting the foreground color of the area of the 1st NSeries point | |
ChartPointCollection chartPoints = aSeries.getPoints(); | |
ChartPoint point = chartPoints.get(0); | |
point.getArea().setForegroundColor(Color.getCyan()); | |
// Save the Excel file | |
workbook.save(dataDir + "SettingChartArea_out.xls"); | |
// Print message | |
System.out.println("ChartArea is settled successfully."); |
Impostazione delle Linee del Grafico
Gli sviluppatori possono anche applicare diversi tipi di stili alle linee o ai marker dei dati del SeriesCollection come mostrato di seguito nell’esempio. Eseguendo il codice di esempio verrà aggiunto un grafico a colonne al foglio di lavoro come mostrato di seguito:
Grafico a colonne dopo l’applicazione degli stili delle linee
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingChartLines.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of the newly added worksheet by passing its | |
// sheet index | |
Worksheet worksheet = worksheets.get(0); | |
Cells cells = worksheet.getCells(); | |
// Adding a chart to the worksheet | |
ChartCollection charts = worksheet.getCharts(); | |
Chart chart = charts.get(0); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell | |
SeriesCollection nSeries = chart.getNSeries(); | |
nSeries.add("A1:B3", true); | |
Series aSeries = nSeries.get(0); | |
Line line = aSeries.getSeriesLines(); | |
line.setStyle(LineType.DOT); | |
// Applying a triangular marker style on the data markers of an NSeries | |
aSeries.getMarker().setMarkerStyle(ChartMarkerType.TRIANGLE); | |
// Setting the weight of all lines in an NSeries to medium | |
aSeries = nSeries.get(1); | |
line = aSeries.getSeriesLines(); | |
line.setWeight(WeightType.MEDIUM_LINE); | |
// Save the Excel file | |
workbook.save(dataDir + "SettingChartLines_out.xls"); | |
// Print message | |
System.out.println("ChartArea is settled successfully."); |
Applicazione dei Temi Microsoft Excel 2007/2010 ai Grafici
Gli sviluppatori possono applicare diversi temi e colori di Microsoft Excel al SeriesCollection o ad altri oggetti del grafico come mostrato nell’esempio qui sotto.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ApplyingThemes.class) + "charts/"; | |
// Instantiate the workbook to open the file that contains a chart | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Get the first chart in the sheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Specify the FilFormat's type to Solid Fill of the first series | |
chart.getNSeries().get(0).getArea().getFillFormat().setFillType(FillType.SOLID); | |
// Get the CellsColor of SolidFill | |
CellsColor cc = chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().getCellsColor(); | |
// Create a theme in Accent style | |
cc.setThemeColor(new ThemeColor(ThemeColorType.ACCENT_6, 0.6)); | |
// Apply the them to the series | |
chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().setCellsColor(cc); | |
// Save the Excel file | |
workbook.save(dataDir + "AThemes_out.xlsx"); |
Impostare i Titoli dei Grafici o degli Assi
Puoi usare Microsoft Excel per impostare i titoli di un grafico e dei suoi assi in un ambiente WYSIWYG come mostrato di seguito.
Impostazione titoli di un grafico & dei suoi assi utilizzando Microsoft Excel
Aspose.Cells consente anche ai sviluppatori di impostare i titoli di un grafico e dei suoi assi durante l’esecuzione. Tutti i grafici e i loro assi contengono un metodo Title.setText che può essere utilizzato per impostare i propri titoli, come mostrato di seguito in un esempio. Dopo aver eseguito il codice di esempio, verrà aggiunto un grafico a colonne al foglio di lavoro come mostrato di seguito:
Grafico a colonne dopo l’impostazione dei titoli
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingTitlesAxes.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of the newly added worksheet by passing its | |
// sheet index | |
Worksheet worksheet = worksheets.get(0); | |
Cells cells = worksheet.getCells(); | |
// Adding a chart to the worksheet | |
ChartCollection charts = worksheet.getCharts(); | |
// Accessing the instance of the newly added chart | |
int chartIndex = charts.add(ChartType.COLUMN, 5, 0, 15, 7); | |
Chart chart = charts.get(chartIndex); | |
// Setting the title of a chart | |
Title title = chart.getTitle(); | |
title.setText("ASPOSE"); | |
// Setting the font color of the chart title to blue | |
Font font = title.getFont(); | |
font.setColor(Color.getBlue()); | |
// Setting the title of category axis of the chart | |
Axis categoryAxis = chart.getCategoryAxis(); | |
title = categoryAxis.getTitle(); | |
title.setText("Category"); | |
// Setting the title of value axis of the chart | |
Axis valueAxis = chart.getValueAxis(); | |
title = valueAxis.getTitle(); | |
title.setText("Value"); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell | |
SeriesCollection nSeries = chart.getNSeries(); | |
nSeries.add("A1:B3", true); | |
// Setting the foreground color of the plot area | |
ChartFrame plotArea = chart.getPlotArea(); | |
Area area = plotArea.getArea(); | |
area.setForegroundColor(Color.getBlue()); | |
// Setting the foreground color of the chart area | |
ChartArea chartArea = chart.getChartArea(); | |
area = chartArea.getArea(); | |
area.setForegroundColor(Color.getYellow()); | |
// Setting the foreground color of the 1st NSeries area | |
Series aSeries = nSeries.get(0); | |
area = aSeries.getArea(); | |
area.setForegroundColor(Color.getRed()); | |
// Setting the foreground color of the area of the 1st NSeries point | |
ChartPointCollection chartPoints = aSeries.getPoints(); | |
ChartPoint point = chartPoints.get(0); | |
point.getArea().setForegroundColor(Color.getCyan()); | |
// Save the Excel file | |
workbook.save(dataDir + "SettingTitlesAxes_out.xls"); | |
// Print message | |
System.out.println("Chart Title is changed successfully."); |
Impostazione delle linee principali della griglia
Nascondere le linee di griglia principali
I sviluppatori possono controllare la visibilità delle linee principali della griglia utilizzando il metodo setVisible dell’oggetto Line. Dopo aver nascosto le linee principali della griglia, un grafico a colonne aggiunto al foglio di lavoro avrà l’aspetto seguente:
Un grafico a colonne con linee principali della griglia nascoste
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Hiding the major gridlines of value axis | |
Axis valueAxis = chart.getValueAxis(); | |
Line majorGridLines = valueAxis.getMajorGridLines(); | |
majorGridLines.setVisible(false); |
Modifica delle impostazioni delle linee di griglia principali
I sviluppatori possono non solo controllare la visibilità delle linee principali della griglia ma anche altre proprietà, incluso il colore. Dopo aver impostato il colore delle linee principali della griglia, un grafico a colonne aggiunto al foglio di lavoro avrà l’aspetto seguente:
Grafico a colonne con linee principali della griglia colorate
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Setting the color of major gridlines of value axis to silver | |
Axis categoryAxis = chart.getCategoryAxis(); | |
categoryAxis.getMajorGridLines().setColor(Color.getSilver()); |
Impostazione dei bordi per il retro e i lati del grafico
Dal rilascio di Microsoft Excel 2007, le pareti di un grafico 3D sono state divise in due parti: parete laterale e parete posteriore, quindi dobbiamo usare due oggetti Walls per rappresentarli separatamente e è possibile accedervi utilizzando Chart.getBackWall() e Chart.getSideWall().
L’esempio riportato di seguito mostra come impostare il bordo della parete laterale utilizzando attributi diversi.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Get the side wall border line | |
Line sideLine = chart.getSideWall().getBorder(); | |
// Make it visible | |
sideLine.setVisible(true); | |
// Set the solid line | |
sideLine.setStyle(LineType.SOLID); | |
// Set the line width | |
sideLine.setWeight(10); | |
// Set the color | |
sideLine.setColor(Color.getBlack()); |
Cambia la posizione e le dimensioni del grafico
A volte, è necessario modificare la posizione o le dimensioni del grafico nuovo o esistente all’interno del foglio di lavoro. Aspose.Cells fornisce la proprietà Chart.getChartObject() per raggiungere questo obiettivo. È possibile utilizzare le sue sotto-proprietà per ridimensionare il grafico con nuove altezza e larghezza o riposizionarlo con nuove coordinate X e Y.
Modifica della posizione e delle dimensioni del grafico
Per cambiare la posizione (coordinate X, Y) e le dimensioni (altezza, larghezza) del grafico, utilizzare queste proprietà:
- Chart.getChartObject().get/setWidth()
- Chart.getChartObject().get/setHeight()
- Chart.getChartObject().get/setX()
- Chart.getChartObject().get/setY()
L’esempio seguente spiega l’uso delle proprietà sopra. Carica il foglio di lavoro esistente che contiene un grafico nel suo primo foglio. Poi ridimensiona, riposiziona il grafico e salva il foglio di lavoro.
Prima dell’esecuzione del codice di esempio, il file sorgente appare così:
Dimensioni e posizione del grafico prima dell’esecuzione del codice di esempio
Dopo l’esecuzione, il file di output appare così:
Dimensioni e posizione del grafico dopo l’esecuzione del codice di esempio
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ChangeChartPositionAndSize.class) + "charts/"; | |
String filePath = dataDir + "book1.xls"; | |
Workbook workbook = new Workbook(filePath); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Load the chart from source worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Resize the chart | |
chart.getChartObject().setWidth(400); | |
chart.getChartObject().setHeight(300); | |
// Reposition the chart | |
chart.getChartObject().setX(250); | |
chart.getChartObject().setY(150); | |
// Output the file | |
workbook.save(dataDir + "ChangeChartPositionAndSize_out.xls"); | |
// Print message | |
System.out.println("Position and Size of Chart is changed successfully."); |
Manipolazione dei Grafici del Designer
Ci sarà un momento in cui potresti avere bisogno di manipolare o modificare i grafici nei file di modello del tuo designer. Aspose.Cells supporta pienamente la manipolazione dei grafici di progettazione con i suoi contenuti ed elementi. I dati, i contenuti dei grafici, l’immagine di sfondo e la formattazione possono essere preservati con precisione.
Manipolazione dei grafici di progettazione nei file di modello
Per manipolare i grafici di progettazione in un file di modello, utilizzare tutte le chiamate API relative ai grafici. Ad esempio, utilizzare la proprietà Worksheet.getCharts per ottenere la raccolta di grafici esistenti nel file di modello.
Creazione di un Grafico
Nell’esempio seguente viene mostrato come creare un grafico a torta. Manipoleremo questo grafico in seguito. L’output seguente è generato dal codice.
Il grafico a torta di input
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(CreateChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(0); | |
// Adding some sample value to cells | |
Cells cells = sheet.getCells(); | |
Cell cell = cells.get("A1"); | |
cell.setValue(50); | |
cell = cells.get("A2"); | |
cell.setValue(100); | |
cell = cells.get("A3"); | |
cell.setValue(150); | |
cell = cells.get("B1"); | |
cell.setValue(4); | |
cell = cells.get("B2"); | |
cell.setValue(20); | |
cell = cells.get("B3"); | |
cell.setValue(50); | |
ChartCollection charts = sheet.getCharts(); | |
// Adding a chart to the worksheet | |
int chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5); | |
Chart chart = charts.get(chartIndex); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell to "B3" | |
SeriesCollection serieses = chart.getNSeries(); | |
serieses.add("A1:B3", true); | |
// Saving the Excel file | |
workbook.save(dataDir + "CreateChart_out.xls"); | |
// Print message | |
System.out.println("Workbook with chart is successfully created."); |
Manipolazione del Grafico
Nell’esempio seguente viene mostrato come manipolare il grafico esistente. In questo esempio modifichiamo il grafico creato in precedenza. L’output seguente è generato dal codice. Si noti che il colore del titolo del grafico è cambiato da blu a nero e ‘Inghilterra 30000’ è stato modificato in ‘Regno Unito, 30K’.
Il grafico a torta è stato modificato
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ModifyPieChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "ModifyCharts.xlsx"); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(1); | |
// Load the chart from source worksheet | |
Chart chart = sheet.getCharts().get(0); | |
DataLabels datalabels = chart.getNSeries().get(0).getPoints().get(0).getDataLabels(); | |
datalabels.setText("aspose"); | |
// Saving the Excel file | |
workbook.save(dataDir + "ModifyPieChart_out.xls"); | |
// Print message | |
System.out.println("Pie chart is successfully modified."); |
Manipolazione di un Grafico a Linee nel Modello del Designer
In questo esempio, manipoleremo un grafico a linee. Aggiungeremo alcune serie di dati al grafico esistente e ne cambieremo i colori delle linee.
Per prima cosa, dai un’occhiata al grafico a linee di progettazione.
Il grafico a linee di input
Ora manipoliamo il grafico a linee (contenuto nel file linechart.xls) utilizzando il seguente codice. Il seguente output è generato dal codice.
Il grafico a linee manipolato
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ModifyLineChart.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Obtaining the reference of the first worksheet | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet sheet = worksheets.get(0); | |
// Load the chart from source worksheet | |
Chart chart = sheet.getCharts().get(0); | |
// Adding NSeries (chart data source) to the chart ranging from "A1" | |
// cell to "B3" | |
SeriesCollection serieses = chart.getNSeries(); | |
serieses.add("{20,40,90}", true); | |
serieses.add("{110,70,220}", true); | |
// Saving the Excel file | |
workbook.save(dataDir + "ModifyLineChart_out.xls"); | |
// Print message | |
System.out.println("Line chart is successfully modified."); |
Utilizzo delle Sparklines
Microsoft Excel 2010 può analizzare le informazioni in modi più variati che mai. Consente agli utenti di monitorare ed evidenziare importanti tendenze dei dati con nuovi strumenti di analisi e visualizzazione dei dati. Le Sparklines sono mini-grafici che è possibile inserire all’interno delle celle, in modo da poter visualizzare dati e grafici sulla stessa tabella. Quando le sparklines vengono utilizzate correttamente, l’analisi dei dati è più rapida e diretta. Forniscono inoltre una visione semplice delle informazioni, evitando fogli di lavoro affollati con molti grafici elaborati.
Aspose.Cells fornisce un’API per manipolare le sparklines nei fogli di calcolo.
Le Sparklines in Microsoft Excel
Per inserire sparklines in Microsoft Excel 2010:
- Selezionare le celle in cui si desidera che compaiano le sparklines. Per renderle facili da visualizzare, selezionare le celle a lato dei dati.
- Fare clic su Inserisci nella barra multifunzione e quindi scegliere colonna nel gruppo Sparklines.
- Selezionare o inserire l’intervallo di celle nel foglio di lavoro che contiene i dati di origine. I grafici appaiono.
Le Sparklines ti aiutano a vedere le tendenze, ad esempio, o il record di vittorie o sconfitte per una lega di softball. Le Sparklines possono persino riassumere l’intera stagione di ciascuna squadra nella lega.
Sparklines utilizzando Aspose.Cells
I programmatori possono creare, eliminare o leggere sparklines (nel file modello) utilizzando l’API fornita da Aspose.Cells. Aggiungendo grafici personalizzati per un determinato intervallo di dati, i programmatori hanno la libertà di aggiungere diversi tipi di piccoli grafici alle aree di celle selezionate.
L’esempio di seguito mostra la funzione Sparklines. L’esempio mostra come:
- Aprire un semplice file modello.
- Leggere le informazioni delle sparklines per un foglio di lavoro.
- Aggiungere nuove sparklines per un dato intervallo di dati in un’area di celle.
- Salvare il file Excel su disco.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(UsingSparklines.class) + "charts/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = worksheets.get(0); | |
Cells cells = worksheet.getCells(); | |
System.out.println("Sparkline count: " + worksheet.getSparklineGroupCollection().getCount()); | |
for (int i = 0; i < worksheet.getSparklineGroupCollection().getCount(); i++) { | |
SparklineGroup g = worksheet.getSparklineGroupCollection().get(i); | |
System.out.println("sparkline group: type:" + g.getType()); | |
for (int j = 0; j < g.getSparklineCollection().getCount(); i++) { | |
Sparkline gg = g.getSparklineCollection().get(i); | |
System.out.println("sparkline: row:" + gg.getRow() + ", col:" + gg.getColumn() + ", dataRange:" | |
+ gg.getDataRange()); | |
} | |
} | |
// Add Sparklines | |
// Define the CellArea D2:D10 | |
CellArea ca = new CellArea(); | |
ca.StartColumn = 4; | |
ca.EndColumn = 4; | |
ca.StartRow = 1; | |
ca.EndRow = 7; | |
int idx = worksheet.getSparklineGroupCollection().add(SparklineType.COLUMN, "Sheet1!B2:D8", false, ca); | |
SparklineGroup group = worksheet.getSparklineGroupCollection().get(idx); | |
// Create CellsColor | |
CellsColor clr = workbook.createCellsColor(); | |
clr.setColor(Color.getChocolate()); | |
group.setSeriesColor(clr); | |
workbook.save(dataDir + "UsingSparklines_out.xls"); | |
// Print message | |
System.out.println("Workbook with chart is created successfully."); |
Applicazione del formato 3D al grafico
Potresti avere bisogno di stili di grafici 3D in modo da ottenere solo i risultati per il tuo scenario. Le API di Aspose.Cells forniscono l’API rilevante per applicare la formattazione 3D di Microsoft Excel 2007 come dimostrato in questo articolo.
Impostazione del formato 3D per il grafico
Di seguito è riportato un esempio completo per dimostrare come creare un grafico e applicare la formattazione 3D di Microsoft Excel 2007. Dopo l’esecuzione del codice di esempio sopra, verrà aggiunto un diagramma a colonne (con effetti 3D) al foglio di lavoro come mostrato di seguito.
Un grafico a colonne con formattazione 3D
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(Applying3DFormat.class) + "charts/"; | |
// Instantiate a new Workbook | |
Workbook book = new Workbook(); | |
// Add a Data Worksheet | |
Worksheet dataSheet = book.getWorksheets().add("DataSheet"); | |
// Add Chart Worksheet | |
Worksheet sheet = book.getWorksheets().add("MyChart"); | |
// Put some values into the cells in the data worksheet | |
dataSheet.getCells().get("B1").putValue(1); | |
dataSheet.getCells().get("B2").putValue(2); | |
dataSheet.getCells().get("B3").putValue(3); | |
dataSheet.getCells().get("A1").putValue("A"); | |
dataSheet.getCells().get("A2").putValue("B"); | |
dataSheet.getCells().get("A3").putValue("C"); | |
// Define the Chart Collection | |
ChartCollection charts = sheet.getCharts(); | |
// Add a Column chart to the Chart Worksheet | |
int chartSheetIdx = charts.add(ChartType.COLUMN, 5, 0, 25, 15); | |
// Get the newly added Chart | |
Chart chart = book.getWorksheets().get(2).getCharts().get(0); | |
// Set the background/foreground color for PlotArea/ChartArea | |
chart.getPlotArea().getArea().setBackgroundColor(Color.getWhite()); | |
chart.getChartArea().getArea().setBackgroundColor(Color.getWhite()); | |
chart.getPlotArea().getArea().setForegroundColor(Color.getWhite()); | |
chart.getChartArea().getArea().setForegroundColor(Color.getWhite()); | |
// Hide the Legend | |
chart.setShowLegend(false); | |
// Add Data Series for the Chart | |
chart.getNSeries().add("DataSheet!B1:B3", true); | |
// Specify the Category Data | |
chart.getNSeries().setCategoryData("DataSheet!A1:A3"); | |
// Get the Data Series | |
Series ser = chart.getNSeries().get(0); | |
// Apply the 3D formatting | |
ShapePropertyCollection spPr = ser.getShapeProperties(); | |
Format3D fmt3d = spPr.getFormat3D(); | |
// Specify Bevel with its height/width | |
Bevel bevel = fmt3d.getTopBevel(); | |
bevel.setType(BevelPresetType.CIRCLE); | |
bevel.setHeight(5); | |
bevel.setWidth(9); | |
// Specify Surface material type | |
fmt3d.setSurfaceMaterialType(PresetMaterialType.WARM_MATTE); | |
// Specify surface lighting type | |
fmt3d.setSurfaceLightingType(LightRigType.THREE_POINT); | |
// Specify lighting angle | |
fmt3d.setLightingAngle(20); | |
// Specify Series background/foreground and line color | |
ser.getArea().setBackgroundColor(Color.getMaroon()); | |
ser.getArea().setForegroundColor(Color.getMaroon()); | |
ser.getBorder().setColor(Color.getMaroon()); | |
// Save the Excel file | |
book.save(dataDir + "A3DFormat_out.xls"); | |
// Print message | |
System.out.println("3D format is applied successfully."); |