Créer des graphiques à l aide d Apache POI et Aspose.Cells
Contents
[
Hide
]
Aspose.Cells - Créer des graphiques
Il est possible d’ajouter une variété de graphiques aux feuilles de calcul avec Aspose.Cells. Aspose.Cells fournit de nombreux objets de diagrammes flexibles.
Java
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet
Worksheet worksheet = workbook.getWorksheets().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 "A4" cell
cells.get("A4").setValue(200);
//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 sample value to "B4" cell
cells.get("B4").setValue(40);
//Adding a chart to the worksheet and
//accessing the instance of the newly added chart
int chartIndex = worksheet.getCharts().add(ChartType.COLUMN,5,0,15,5);
Chart chart = worksheet.getCharts().get(chartIndex);
//Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B4"
SeriesCollection nSeries = chart.getNSeries();
nSeries.add("A1:B4",true);
//Setting the chart type of 2nd NSeries to display as line chart
Series series = nSeries.get(1);
series.setType(ChartType.LINE);
Apache POI SS (HSSF + XSSF) - Créer des graphiques
Java
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("linechart");
final int NUM_OF_ROWS = 3;
final int NUM_OF_COLUMNS = 10;
// Create a row and put some cells in it. Rows are 0 based.
Row row;
Cell cell;
for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex);
cell.setCellValue(colIndex * (rowIndex + 1));
}
}
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
LineChartData data = chart.getChartDataFactory().createLineChartData();
// Use a category axis for the bottom axis.
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
data.addSeries(xs, ys1);
data.addSeries(xs, ys2);
chart.plot(data, bottomAxis, leftAxis);
Télécharger le code en cours d’exécution
Télécharger des exemples en cours d’exécution pour Créer des graphiques en utilisant Apache POI et Aspose.Cells à partir de l’un des sites de codage social mentionnés ci-dessous:
Télécharger le code source
Télécharger le code source pour Créer des graphiques en utilisant Apache POI et Aspose.Cells à partir de l’un des sites de codage social mentionnés ci-dessous:
Pour plus de détails, visitez Créer des graphiques personnalisés.