ピーグラフのカスタムサブトータルラベルおよびその他のラベル用のGlobalizationSettingsクラスの使用

可能な使用シナリオ

Aspose.CellsのAPIは、スプレッドシートでサブトータルのカスタムラベルを使用したい場合にGlobalizationSettingsクラスを公開しています。さらに、GlobalizationSettingsクラスを使用して、ワークシートやグラフでOtherラベルを変更することもできます。

GlobalizationSettingsクラスの紹介

GlobalizationSettingsクラスは現在、以下の3つのメソッドを提供しており、これらはカスタムクラスでオーバーライドし、サブトータルのために望ましいラベルを取得するために使用することができます。また、円グラフのOtherラベルに対してカスタムテキストをレンダリングすることもできます。

  1. GlobalizationSettings.getTotalName: 関数の合計名を取得します。
  2. GlobalizationSettings.getGrandTotalName: 関数の総合計名を取得します。
  3. 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 メソッドを提供します。以下のスニペットは、デフォルトの言語設定に基づいてカスタムラベルを取得するために、カスタムクラスを定義し getOtherName メソッドを上書きしています。

// 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」に翻訳されていることがわかります。

todo:image_alt_text