ピーグラフのカスタムサブトータルラベルおよびその他のラベル用のGlobalizationSettingsクラスの使用
可能な使用シナリオ
Aspose.CellsのAPIは、スプレッドシートでサブトータルのカスタムラベルを使用したい場合にGlobalizationSettingsクラスを公開しています。さらに、GlobalizationSettingsクラスを使用して、ワークシートやグラフでOtherラベルを変更することもできます。
GlobalizationSettingsクラスの紹介
GlobalizationSettingsクラスは現在、以下の3つのメソッドを提供しており、これらはカスタムクラスでオーバーライドし、サブトータルのために望ましいラベルを取得するために使用することができます。また、円グラフのOtherラベルに対してカスタムテキストをレンダリングすることもできます。
- GlobalizationSettings.getTotalName: 関数の合計名を取得します。
- GlobalizationSettings.getGrandTotalName: 関数の総計名を取得します。
- GlobalizationSettings.getOtherName: パイチャートの「その他」ラベルの名前を取得します。
サブトータルのカスタムラベル
GlobalizationSettingsクラスは、GlobalizationSettings.getTotalNameとGlobalizationSettings.getGrandTotalNameメソッドをオーバーライドして、小計ラベルをカスタマイズするために使用できます。前述の例も参考にしてください。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public String getTotalName(int functionType) { | |
switch (functionType) { | |
case ConsolidationFunction.AVERAGE: | |
return "AVG"; | |
// Handle other cases | |
default: | |
return super.getTotalName(functionType); | |
} | |
} | |
public String getGrandTotalName(int functionType) { | |
switch (functionType) { | |
case ConsolidationFunction.AVERAGE: | |
return "GRAND AVG"; | |
// Handle other cases | |
default: | |
return super.getGrandTotalName(functionType); | |
} | |
} | |
public String getOtherName() | |
{ | |
String language = Locale.getDefault().getLanguage(); | |
System.out.println(language); | |
switch (language) | |
{ | |
case "en": | |
return "Other"; | |
case "fr": | |
return "Autre"; | |
case "de": | |
return "Andere"; | |
//Handle other cases as per requirement | |
default: | |
return super.getOtherName(); | |
} | |
} |
カスタムラベルを追加するには、小計をワークシートに追加する前に、WorkbookSettings.GlobalizationSettings プロパティに先ほど定義した CustomSettings クラスのインスタンスを割り当てる必要があります。
// 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(CustomLabelsforSubtotals.class) + "articles/"; | |
// Loads an existing spreadsheet containing some data | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
// Assigns the GlobalizationSettings property of the WorkbookSettings | |
// class | |
// to the class created in first step | |
book.getSettings().setGlobalizationSettings(new CustomSettings()); | |
// Accesses the 1st worksheet from the collection which contains data | |
// Data resides in the cell range A2:B9 | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Adds SubTotal of type Average to the worksheet | |
sheet.getCells().subtotal(CellArea.createCellArea("A2", "B9"), 0, ConsolidationFunction.AVERAGE, new int[] { 1 }); | |
// Calculates Formulas | |
book.calculateFormula(); | |
// Auto fits all columns | |
sheet.autoFitColumns(); | |
// Saves the workbook on disc | |
book.save(dataDir + "CustomLabelsforSubtotals_out.xlsx"); |
円グラフの他のラベルのカスタムテキスト
GlobalizationSettingsクラスは、getOtherNameメソッドを提供しており、ピーチャートの「Other」ラベルにカスタム値を設定できます。以下のスニペットは、カスタムクラスを定義し、getOtherNameメソッドをオーバーライドして、JVMに設定されたデフォルト言語に基づくカスタムラベルを取得する例です。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public String getTotalName(int functionType) { | |
switch (functionType) { | |
case ConsolidationFunction.AVERAGE: | |
return "AVG"; | |
// Handle other cases | |
default: | |
return super.getTotalName(functionType); | |
} | |
} | |
public String getGrandTotalName(int functionType) { | |
switch (functionType) { | |
case ConsolidationFunction.AVERAGE: | |
return "GRAND AVG"; | |
// Handle other cases | |
default: | |
return super.getGrandTotalName(functionType); | |
} | |
} | |
public String getOtherName() | |
{ | |
String language = Locale.getDefault().getLanguage(); | |
System.out.println(language); | |
switch (language) | |
{ | |
case "en": | |
return "Other"; | |
case "fr": | |
return "Autre"; | |
case "de": | |
return "Andere"; | |
//Handle other cases as per requirement | |
default: | |
return super.getOtherName(); | |
} | |
} |
既存のスプレッドシートを読み込み、CustomSettings クラスを利用してワークブックの WorkbookSettings.GlobalizationSettings プロパティに割り当て、円グラフを画像としてレンダリングする以下のスニペットです。
// 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(CustomTextforOtherLabelofPieChart.class) + "articles/"; | |
//Loads an existing spreadsheet containing a pie chart | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
//Assigns the GlobalizationSettings property of the WorkbookSettings class | |
//to the class created in first step | |
book.getSettings().setGlobalizationSettings(new CustomSettings()); | |
//Accesses the 1st worksheet from the collection which contains pie chart | |
Worksheet sheet = book.getWorksheets().get(0); | |
//Accesses the 1st chart from the collection | |
Chart chart = sheet.getCharts().get(0); | |
//Refreshes the chart | |
chart.calculate(); | |
//Renders the chart to image | |
chart.toImage(dataDir + "CustomTextforOtherLabelofPieChart_out.png", new ImageOrPrintOptions()); |
マシンのロケールがフランスに設定されている場合の結果画像は以下の通りです。CustomSettings クラスで定義したように、「その他」ラベルは「Autre」に翻訳されていることがわかります。