Formatting Number and Currency Labels in Waterfall Charts by Culture in C#

Hello Team,

We are using Aspose.Slides in our .Net Core application and trying to create ChartType.WaterFall by adding chart into slide using slide.Shapes.AddChart() method.

We are facing issue in formatting the numbers and currency values based on current culture information and need your help.

Is this possible to apply the current culture number and currency format in WaterFall chart dynamically based on selected culture?

Please find the below screenshot1(Currency) and screenshot2(Number) for desire output, screenshot3(Currency) and screenshot4(Number) for current output** and code snippet for your reference and any help in this issue is really appreciated.

screenshot1
image.png (48.8 KB)
screenshot2
image.png (39.2 KB)
screenshot3
image.png (7.3 KB)
screenshot4
image.png (6.8 KB)

Code Snippet

public void AddChart(ISlide slide)
{
var categories = new List<string>();
var values = new List<double>();
var costValues = new List<double>();
var showCost = values.Count() == 0 ? true : false;

var chart = slide.Shapes.AddChart(Aspose.Slides.Charts.ChartType.Waterfall, 350, 280, 1400, 650);

chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();

var workbook = chart.ChartData.ChartDataWorkbook;

for (int i = 0; i < categories.Count; i++)
{
    chart.ChartData.Categories.Add(workbook.GetCell(0, i + 1, 0, categories[i]));
}

var seriesTitle = "Title Series";
var series = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 1, seriesTitle), chart.Type);

// Set bars width
series.ParentSeriesGroup.GapWidth = Convert.ToUInt16(20);

var data = showCost ? costValues : values;

for (int i = 0; i < data.Count; i++)
{
    var dp = series.DataPoints.AddDataPointForWaterfallSeries(workbook.GetCell(0, i + 1, 1, Math.Round(data[i], 2)));
 dp.Label.DataLabelFormat.ShowValue = true;
 dp.Label.DataLabelFormat.TextFormat.PortionFormat.FillFormat.FillType = FillType.Solid;
 dp.Label.DataLabelFormat.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = chartConfig.DataLabelFontColor;
 dp.Label.DataLabelFormat.Position = Color.White;
}

chart.HasTitle = false;
chart.HasLegend = false;
chart.Axes.HorizontalAxis.HasTitle = false;
chart.Axes.VerticalAxis.HasTitle = false;
}

@shashi.2526

Cause

To format numbers and currency values in a Waterfall chart dynamically based on the current culture in your .NET Core application using Aspose.Slides, you need to set the appropriate number format for each data point in the chart. The default formatting may not align with your desired culture-specific formatting.

Solution

You can achieve this by using the PresetNumberFormat property of the IChartDataCell class. This allows you to set predefined number formats that correspond to various cultures. You can loop through your data points and apply the desired format based on the selected culture.

Code

Here’s a code snippet that demonstrates how to set the number format for a Waterfall chart:

public void AddChart(ISlide slide)
{
    var categories = new List<string> { "Start", "Increase", "Decrease", "End" };
    var values = new List<double> { 100, 50, -30, 120 };

    var chart = slide.Shapes.AddChart(Aspose.Slides.Charts.ChartType.Waterfall, 350, 280, 1400, 650);
    chart.ChartData.Categories.Clear();
    chart.ChartData.Series.Clear();

    var workbook = chart.ChartData.ChartDataWorkbook;

    for (int i = 0; i < categories.Count; i++)
    {
        chart.ChartData.Categories.Add(workbook.GetCell(0, i + 1, 0, categories[i]));
    }

    var series = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 1, "Waterfall Series"), chart.Type);

    for (int i = 0; i < values.Count; i++)
    {
        var dp = series.DataPoints.AddDataPointForWaterfallSeries(workbook.GetCell(0, i + 1, 1, Math.Round(values[i], 2)));
        dp.Label.DataLabelFormat.ShowValue = true;

        // Set the number format based on culture
        dp.Value.AsCell.PresetNumberFormat = 7; // Example for currency format
    }

    chart.HasTitle = false;
    chart.HasLegend = false;
    chart.Axes.HorizontalAxis.HasTitle = false;
    chart.Axes.VerticalAxis.HasTitle = false;
}

In this example, PresetNumberFormat is set to 7, which corresponds to a currency format. You can adjust this value based on the specific formatting you need for different cultures.

Next Steps

  1. Determine the specific culture formats you need and map them to the appropriate PresetNumberFormat values.
  2. Test the chart with different cultures to ensure the formatting is applied correctly.
  3. If you need custom formats, consider using the CustomNumberFormat property instead.

Please implement these steps, and let me know if you need further assistance!

@shashi.2526,

See the following sample code to accomplish your task for your reference. You may try to use CustomNumberFormat or PresetNumberFormat attribute to format the source chart data cells. To get reference on possible preset number formats along with their preset index and that can be used, see the bottom table in the document: Chart Formatting|Aspose.Slides Documentation.

// Creates an instance of the Presentation class
Presentation presentation = new Presentation();

ISlide slide = presentation.Slides[0];

var categories = new List<string> { "Start", "Increase", "Decrease", "End" };
var values = new List<double> { 10001.344, 5002.357, -30.353, 12024.232 };

var chart = slide.Shapes.AddChart(Aspose.Slides.Charts.ChartType.Waterfall, 50, 28, 500, 350);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();

var workbook = chart.ChartData.ChartDataWorkbook;

for (int i = 0; i < categories.Count; i++)
{
    chart.ChartData.Categories.Add(workbook.GetCell(0, i + 1, 0, categories[i]));
}

var series = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 1, "Waterfall Series"), chart.Type);

for (int i = 0; i < values.Count; i++)
{
    var dp = series.DataPoints.AddDataPointForWaterfallSeries(workbook.GetCell(0, i + 1, 1, Math.Round(values[i], 2)));
    dp.Label.DataLabelFormat.ShowValue = true;

    // Set the custom number format
    dp.Value.AsCell.CustomNumberFormat = "$#,##0.00";
}

chart.HasTitle = false;
chart.HasLegend = false;
chart.Axes.HorizontalAxis.HasTitle = false;
chart.Axes.VerticalAxis.HasTitle = false;

presentation.Save("e:\\test2\\output.pptx", SaveFormat.Pptx); 

Please find attached the output PPTX file which is generated by the above code for your reference.
output.zip (34.4 KB)

Hope, this helps a bit.

Thanks @amjad.sahi and @Professionalize.Discourse for your response on this. I have another question which is given below and need help on this.

How to show small values in waterfall chart as due to high interval gaps in YAxis values, small values in chart is not displaying properly. Also few datapoints values are set to SetAsTotal = true.

Please find the below screensot for your reference.

image.png (20.9 KB)

@shashi.2526,

Please provide a PPTX file with your desired chart intact. You can manually create the chart in MS PowerPoint. Additionally, share the sample (runnable) code you are using to accomplish the task. We will check it soon.