チャートのシリーズのデータポイントにカスタムラベルを追加
Contents
[
Hide
]
Aspose.Cellsを使用して、チャートのシリーズのデータポイントにカスタムラベルを追加することができます。Aspose.Cellsは、ChartPoint.getDataLabels().setText()プロパティを提供して、これらのカスタムラベルを追加する方法を説明します。この記事では、このプロパティを使用してチャートのシリーズのデータポイントにカスタムラベルを追加する方法について説明します。
チャートシリーズのデータポイントにカスタムラベルを追加する
次のコードは、データマーカーを持つ線で接続された散布図を作成し、その後、チャートのシリーズのデータポイントにカスタムラベルを追加します。各カスタムラベルには、シリーズ名とポイント名が表示されます。それに代わる任意のテキストを使用できます。コードの実行後、次のExcelファイルが作成されます。チャート内を見ると、各データポイントにカスタムラベルが設定されています。
This file contains hidden or 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(AddCustomLabelsToDataPoints.class); | |
Workbook workbook = new Workbook(FileFormatType.XLSX); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
// Put data | |
sheet.getCells().get(0, 0).putValue(1); | |
sheet.getCells().get(0, 1).putValue(2); | |
sheet.getCells().get(0, 2).putValue(3); | |
sheet.getCells().get(1, 0).putValue(4); | |
sheet.getCells().get(1, 1).putValue(5); | |
sheet.getCells().get(1, 2).putValue(6); | |
sheet.getCells().get(2, 0).putValue(7); | |
sheet.getCells().get(2, 1).putValue(8); | |
sheet.getCells().get(2, 2).putValue(9); | |
// Generate the chart | |
int chartIndex = sheet.getCharts().add(ChartType.SCATTER_CONNECTED_BY_LINES_WITH_DATA_MARKER, 5, 1, 24, 10); | |
Chart chart = sheet.getCharts().get(chartIndex); | |
chart.getTitle().setText("Test"); | |
chart.getCategoryAxis().getTitle().setText("X-Axis"); | |
chart.getValueAxis().getTitle().setText("Y-Axis"); | |
chart.getNSeries().setCategoryData("A1:C1"); | |
// Insert series | |
chart.getNSeries().add("A2:C2", false); | |
Series series = chart.getNSeries().get(0); | |
int pointCount = series.getPoints().getCount(); | |
for (int i = 0; i < pointCount; i++) { | |
ChartPoint pointIndex = series.getPoints().get(i); | |
pointIndex.getDataLabels().setText("Series 1" + "\n" + "Point " + i); | |
} | |
// Insert series | |
chart.getNSeries().add("A3:C3", false); | |
series = chart.getNSeries().get(1); | |
pointCount = series.getPoints().getCount(); | |
for (int i = 0; i < pointCount; i++) { | |
ChartPoint pointIndex = series.getPoints().get(i); | |
pointIndex.getDataLabels().setText("Series 2" + "\n" + "Point " + i); | |
} | |
workbook.save(dataDir + "Test.xlsx", SaveFormat.XLSX); |