カテゴリ軸の設定方法
Contents
[
Hide
]
可能な使用シナリオ
ワークシートにチャートを作成した後、それにカテゴリ軸を設定できます。この記事では、Aspose.Cellsを使用してExcelチャートのカテゴリ軸を設定する方法を示します。
サンプルコードの手順
-
新しいブックを作成します。
-
最初のワークシートで新しいチャートを作成します。
-
最初のワークシートのセルにいくつかの値を追加します。
-
ここでカテゴリ軸を設定できます。セルデータを使用する方法と、直接文字列を使用する方法の両方がサンプルコードで示されています。
-
値軸を設定し、結果を表示するためにブックを保存します。
次のサンプルコードは、出力Excelファイルを生成します。
サンプルコード
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
//How to Set Category Axis | |
//Your local test path | |
String path = ""; | |
//Create a new workbook | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
worksheet.setName("CHART"); | |
// Add a chart to the worksheet | |
int chartIndex = worksheet.getCharts().add(ChartType.COLUMN, 8, 0, 20, 10); | |
Chart chart = worksheet.getCharts().get(chartIndex); | |
//Add some values to cells | |
worksheet.getCells().get("A1").putValue("Sales"); | |
worksheet.getCells().get("A2").putValue(100); | |
worksheet.getCells().get("A3").putValue(150); | |
worksheet.getCells().get("A4").putValue(130); | |
worksheet.getCells().get("A5").putValue(160); | |
worksheet.getCells().get("A6").putValue(150); | |
worksheet.getCells().get("B1").putValue("Days"); | |
worksheet.getCells().get("B2").putValue(1); | |
worksheet.getCells().get("B3").putValue(2); | |
worksheet.getCells().get("B4").putValue(3); | |
worksheet.getCells().get("B5").putValue(4); | |
worksheet.getCells().get("B6").putValue(5); | |
//Some values in string | |
String Sales = "100,150,130,160,150"; | |
String Days = "1,2,3,4,5"; | |
//Set Category Axis Data with string | |
chart.getNSeries().setCategoryData("{" + Days + "}"); | |
//Or you can set Category Axis Data with data in cells, try it! | |
//chart.getNSeries().setCategoryData("B2:B6"); | |
//Add Series to the chart | |
chart.getNSeries().add("Demand1", true); | |
//Set value axis with string | |
chart.getNSeries().get(0).setValues("{" + Sales + "}"); | |
chart.getNSeries().add("Demand2", true); | |
//Set value axis with data in cells | |
chart.getNSeries().get(1).setValues("A2:A6"); | |
//Set some Category Axis properties | |
chart.getCategoryAxis().getTickLabels().setRotationAngle(45); | |
chart.getCategoryAxis().getTickLabels().getFont().setSize(8); | |
chart.getLegend().setPosition(LegendPositionType.BOTTOM); | |
//Save the workbook to view the result file | |
workbook.save(path + "Output.xlsx"); | |