Uso de la clase ChartGlobalizationSettings para establecer diferentes idiomas para el componente del gráfico con JavaScript vía C++

Escenarios de uso posibles

Las API de Aspose.Cells han expuesto la clase ChartGlobalizationSettings para tratar escenarios donde el usuario desea establecer componentes del gráfico en diferentes idiomas y etiquetas personalizadas para los subtotales en una hoja de cálculo.

Introducción a la clase ChartGlobalizationSettings

La clase ChartGlobalizationSettings actualmente ofrece los siguientes 8 métodos que pueden ser anulados en una clase personalizada para traducir, como el nombre del Título del Eje, la Unidad del Eje, el Título del Gráfico, etc., a diferentes idiomas.

  1. ChartGlobalizationSettings.axisTitleName: Obtiene el nombre del Título para el Eje.
  2. ChartGlobalizationSettings.axisUnitName(DisplayUnitType): Obtiene el nombre de la Unidad del Eje.
  3. ChartGlobalizationSettings.chartTitleName: Obtiene el nombre del Título del Gráfico.
  4. ChartGlobalizationSettings.legendDecreaseName: Obtiene el nombre de Disminución para la Leyenda.
  5. ChartGlobalizationSettings.legendIncreaseName: Obtiene el nombre de Incremento para la leyenda.
  6. ChartGlobalizationSettings.legendTotalName: Obtiene el nombre de Total para la Leyenda.
  7. ChartGlobalizationSettings.otherName: Obtiene el nombre de las etiquetas “Otro” para el Gráfico.
  8. ChartGlobalizationSettings.seriesName: Obtiene el nombre de la Serie en el Gráfico.

Traducción personalizada de idioma

Aquí, crearemos un gráfico de cascada basado en los siguientes datos. Los nombres de los componentes del gráfico se mostrarán en inglés en el gráfico. Utilizaremos un ejemplo en idioma turco para mostrar cómo mostrar el Título del Gráfico, los nombres de Aumento/Disminución de la Leyenda, el nombre de Total y el Título del Eje en turco.

todo:image_alt_text

Código de muestra

El siguiente código de ejemplo carga el archivo de Excel de muestra.

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Chart Globalization Settings Example</title>
    </head>
    <body>
        <h1>Chart Globalization Settings Example (Turkey)</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <button id="runExample">Run Example</button>
        <a id="downloadLink" style="display: none;">Download Result</a>
        <div id="result"></div>
    </body>

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, SaveFormat } = AsposeCells;

        AsposeCells.onReady({
            license: "/lic/aspose.cells.enc",
            fontPath: "/fonts/",
            fontList: [
                "arial.ttf",
                "NotoSansSC-Regular.ttf"
            ]
        }).then(() => {
            console.log("Aspose.Cells initialized");
        });

        // Define TurkeyChartGlobalizationSettings by converting getXxx methods to properties
        class TurkeyChartGlobalizationSettings extends AsposeCells.ChartGlobalizationSettings {
            constructor() {
                super();
                this.chartTitleName = "Grafik Başlığı"; // Chart Title
                this.legendIncreaseName = "Artış"; // Increase
                this.legendDecreaseName = "Düşüş"; // Decrease
                this.legendTotalName = "Toplam"; // Total
                this.axisTitleName = "Eksen Başlığı"; // Axis Title
            }
        }

        document.getElementById('runExample').addEventListener('click', async () => {
            const fileInput = document.getElementById('fileInput');
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = '';

            if (!fileInput.files.length) {
                resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
                return;
            }

            // No try-catch: let errors propagate
            const file = fileInput.files[0];
            const arrayBuffer = await file.arrayBuffer();

            // Instantiate Workbook from uploaded file
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Set custom chartGlobalizationSettings (Turkey)
            workbook.settings.globalizationSettings.chartSettings = new TurkeyChartGlobalizationSettings();

            // Access the first worksheet and its charts
            const worksheet = workbook.worksheets.get(0);
            const chartCollection = worksheet.charts;
            const chart = chartCollection.get(0);

            // Calculate the chart
            chart.calculate();

            // Get the chart title text
            const title = chart.title;
            const titleText = title ? title.text : "(No Title)";

            // Prepare output messages
            const messages = [];
            messages.push('<p style="color: green;">Operation completed successfully!</p>');
            messages.push(`<p>Workbook chart title: ${titleText}</p>`);

            // Get legend labels and output them
            const legendEntriesLabels = chart.legend.legendLabels;
            if (legendEntriesLabels && legendEntriesLabels.forEach) {
                const legendItems = [];
                legendEntriesLabels.forEach(label => {
                    legendItems.push(`<li>${label}</li>`);
                });
                if (legendItems.length) {
                    messages.push('<p>Workbook chart legend:</p>');
                    messages.push(`<ul>${legendItems.join('')}</ul>`);
                } else {
                    messages.push('<p>(No legend labels found)</p>');
                }
            } else {
                messages.push('<p>(No legend or legend labels available)</p>');
            }

            // Save modified workbook to allow download (optional)
            const outputData = workbook.save(SaveFormat.Xlsx);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'output.xlsx';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Modified Excel File';

            resultDiv.innerHTML = messages.join('');
        });
    </script>
</html>

Resultado generado por el código de ejemplo

Este es el resultado de consola del código de ejemplo anterior.

  

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ığı