Using ChartGlobalizationSettings Class to Set Different Language for Chart Component with Node.js via C++

Possible Usage Scenarios

Aspose.Cells APIs have exposed the ChartGlobalizationSettings class in order to deal with scenarios where the user wishes to set chart components to different languages and custom labels for subtotals in a spreadsheet.

Introduction to ChartGlobalizationSettings Class

The ChartGlobalizationSettings class currently offers the following eight methods, which can be overridden in a custom class to translate items such as AxisTitle name, AxisUnit name, ChartTitle name, and so on, to different languages.

  1. ChartGlobalizationSettings.getAxisTitleName(): Gets the name of the title for an axis.
  2. ChartGlobalizationSettings.getAxisUnitName(DisplayUnitType): Gets the name of the axis unit.
  3. ChartGlobalizationSettings.getChartTitleName(): Gets the name of the chart title.
  4. ChartGlobalizationSettings.getLegendDecreaseName(): Gets the name of “Decrease” for the legend.
  5. ChartGlobalizationSettings.getLegendIncreaseName(): Gets the name of “Increase” for the legend.
  6. ChartGlobalizationSettings.getLegendTotalName(): Gets the name of “Total” for the legend.
  7. ChartGlobalizationSettings.getOtherName(): Gets the name of “Other” labels for the chart.
  8. ChartGlobalizationSettings.getSeriesName(): Gets the name of a series in the chart.

Custom language translation

Here, we will create a waterfall chart based on the following data. The names of chart components will be displayed in English in the chart. We will use a Turkish language example to show how to display the chart title, legend increase/decrease names, total name, and axis title in Turkish.

todo:image_alt_text

Sample Code

The following sample code loads the sample Excel file.

try {
    const path = require("path");
    const AsposeCells = require("aspose.cells.node");

    class TurkeyChartGlobalizationSettings extends AsposeCells.ChartGlobalizationSettings {
        getChartTitleName() {
            return "Grafik Başlığı"; // Chart Title
        }
        getLegendIncreaseName() {
            return "Artış"; // Increase
        }
        getLegendDecreaseName() {
            return "Düşüş"; // Decrease
        }
        getLegendTotalName() {
            return "Toplam"; // Total
        }
        getAxisTitleName() {
            return "Eksen Başlığı"; // Axis Title
        }
    }

    async function chartGlobalizationSettingsTest() {
        // Create an instance of an existing workbook
        const dataDir = path.join(__dirname, "data");
        const pathName = path.join(dataDir, "input.xlsx");
        const workbook = new AsposeCells.Workbook(pathName);

        // Set custom chart globalization settings; here we use TurkeyChartGlobalizationSettings
        workbook.getSettings().getGlobalizationSettings().setChartSettings(new TurkeyChartGlobalizationSettings());

        // Get the worksheet 
        const worksheet = workbook.getWorksheets().get(0);
        const chartCollection = worksheet.getCharts();

        // Load the chart from the source worksheet
        const chart = chartCollection.get(0);

        // Calculate the chart
        chart.calculate();

        // Get the chart title
        const title = chart.getTitle();
        console.log("\nWorkbook chart title: " + title.getText());

        const legendEntriesLabels = chart.getLegend().getLegendLabels();

        // Output the names of the legend entries
        legendEntriesLabels.forEach(label => {
            console.log("\nWorkbook chart legend: " + label);
        });
    }
} catch (e) {
    console.error(e);
}

Output generated by the sample code

This is the console output of the above sample code.

  

Workbook chart title: Grafik Başlığı  

Workbook chart legend: Artış  

Workbook chart legend: Düşüş  

Workbook chart legend: Toplam  

Workbook category axis title: Eksen Başlığı