Datenformatierung in Diagrammen
Contents
[
Hide
]
In unseren vorherigen Themen haben wir bereits viele Beispiele bereitgestellt, um zu demonstrieren, wie Sie eine Datenquelle für Ihr Diagramm festlegen können, aber in diesem Thema werden wir weitere Details zu den Datentypen bereitstellen, die für ein Diagramm festgelegt werden können.
Diagrammdaten einstellen
Bei der Arbeit an Diagrammen mit Aspose.Cells müssen zwei Arten von Daten wie folgt behandelt werden:
Diagrammdaten
Diagrammdaten sind die Daten, die wir als Datenquelle zum Erstellen unserer Diagramme verwenden. Wir können einen Bereich der Zellen (mit Diagrammdaten) hinzufügen, indem wir die aufrufenSerieSammlung ObjektAddieren Methode.
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.getSharedDataDir(SettingChartsData.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(); | |
// 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 sample value to "C1" cell as category data | |
cells.get("C1").setValue("Q1"); | |
// Adding a sample value to "C2" cell as category data | |
cells.get("C2").setValue("Q2"); | |
// Adding a sample value to "C3" cell as category data | |
cells.get("C3").setValue("Y1"); | |
// Adding a sample value to "C4" cell as category data | |
cells.get("C4").setValue("Y2"); | |
// 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, 5); | |
Chart chart = charts.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 data source for the category data of NSeries | |
nSeries.setCategoryData("C1:C4"); | |
workbook.save(dataDir + "SettingChartsData_out.xls"); | |
// Print message | |
System.out.println("Workbook with chart is created successfully."); |
Kategoriedaten
Kategoriedaten dienen der Beschriftung von Diagrammdaten und können ergänzt werdenSerieSammlung durch die Verwendung seinersetCategoryDataMethode.
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.getSharedDataDir(SettingCategoryData.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(); | |
// 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 sample value to "C1" cell as category data | |
cells.get("C1").setValue("Q1"); | |
// Adding a sample value to "C2" cell as category data | |
cells.get("C2").setValue("Q2"); | |
// Adding a sample value to "C3" cell as category data | |
cells.get("C3").setValue("Y1"); | |
// Adding a sample value to "C4" cell as category data | |
cells.get("C4").setValue("Y2"); | |
// 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, 5); | |
Chart chart = charts.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 data source for the category data of NSeries | |
nSeries.setCategoryData("C1:C4"); | |
workbook.save(dataDir + "SettingCategoryData_out.xls"); | |
// Print message | |
System.out.println("Workbook with chart is created successfully."); |
Säulendiagramm mit Diagramm- und Kategoriedaten