Couleurs de tranche ou de secteur personnalisées dans le graphique à secteurs
Contents
[
Hide
]
Cet article explique comment ajouter des couleurs personnalisées aux tranches/secteurs du graphique à secteurs. Par défaut, les graphiques à secteurs utilisent le modèle Excel par défaut Microsoft. Pour utiliser d’autres couleurs, il est possible de redéfinir les couleurs dans le nuancier.
Pour définir la couleur personnalisée des tranches ou secteurs individuels d’un graphique à secteurs :
- Accéder auSérie objetsPoint de graphique.
- Attribuez une couleur de votre choix à l’aide desChartPoint.getArea().setForegroundColor()méthode.
Cet article explique également comment définir :
- Données de catégorie d’un graphique.
- Un titre de graphique lié à une cellule.
- Les paramètres de police du titre du graphique.
- La position de la légende.
ChartPoint.getArea().setForegroundColor() n’est pas spécifique aux camemberts mais peut être utilisé pour tous les types de graphiques.
Couleurs de tranche personnalisées dans le graphique à secteurs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.getDataDir(CustomSliceOrSectorColorsPieChart.class); | |
// Create a workbook object from the template file | |
Workbook workbook = new Workbook(); | |
// Access the first worksheet. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Put the sample values used in a pie chart | |
worksheet.getCells().get("C3").putValue("India"); | |
worksheet.getCells().get("C4").putValue("China"); | |
worksheet.getCells().get("C5").putValue("United States"); | |
worksheet.getCells().get("C6").putValue("Russia"); | |
worksheet.getCells().get("C7").putValue("United Kingdom"); | |
worksheet.getCells().get("C8").putValue("Others"); | |
// Put the sample values used in a pie chart | |
worksheet.getCells().get("D2").putValue("% of world population"); | |
worksheet.getCells().get("D3").putValue(25); | |
worksheet.getCells().get("D4").putValue(30); | |
worksheet.getCells().get("D5").putValue(10); | |
worksheet.getCells().get("D6").putValue(13); | |
worksheet.getCells().get("D7").putValue(9); | |
worksheet.getCells().get("D8").putValue(13); | |
// Create a pie chart with desired length and width | |
int pieIdx = worksheet.getCharts().add(ChartType.PIE, 1, 6, 15, 14); | |
// Access the pie chart | |
Chart pie = worksheet.getCharts().get(pieIdx); | |
// Set the pie chart series | |
pie.getNSeries().add("D3:D8", true); | |
// Set the category data | |
pie.getNSeries().setCategoryData("=Sheet1!$C$3:$C$8"); | |
// Set the chart title that is linked to cell D2 | |
pie.getTitle().setLinkedSource("D2"); | |
// Set the legend position at the bottom. | |
pie.getLegend().setPosition(LegendPositionType.BOTTOM); | |
// Set the chart title's font name and color | |
pie.getTitle().getFont().setName("Calibri"); | |
pie.getTitle().getFont().setSize(18); | |
// Access the chart series | |
Series srs = pie.getNSeries().get(0); | |
// Color the indvidual points with custom colors | |
srs.getPoints().get(0).getArea().setForegroundColor(com.aspose.cells.Color.fromArgb(0, 246, 22, 219)); | |
srs.getPoints().get(1).getArea().setForegroundColor(com.aspose.cells.Color.fromArgb(0, 51, 34, 84)); | |
srs.getPoints().get(2).getArea().setForegroundColor(com.aspose.cells.Color.fromArgb(0, 46, 74, 44)); | |
srs.getPoints().get(3).getArea().setForegroundColor(com.aspose.cells.Color.fromArgb(0, 19, 99, 44)); | |
srs.getPoints().get(4).getArea().setForegroundColor(com.aspose.cells.Color.fromArgb(0, 208, 223, 7)); | |
srs.getPoints().get(5).getArea().setForegroundColor(com.aspose.cells.Color.fromArgb(0, 222, 69, 8)); | |
// Autofit all columns | |
worksheet.autoFitColumns(); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx", SaveFormat.XLSX); |