Search code examples
c#mauilivecharts

Formatting Datalabels in LiveCharts 2 - C#


I use LiveCharts 2 for my charts in Maui : https://lvcharts.com/docs/maui/2.0.0-beta.710/gallery

I can't find a way to format the data labels of my graphs... I can change the size and the color, but I can't find any way to format the value itself. There is a DataLabelsFormatter property which seems promising, but I can't find anywhere how to use it.

// Serie #1 : Author 1
ColumnSeries<double?> seriesAuthor1 = new()
{
    Values = new List<double?> { groups.ElementAt(0).MessagesCnt },
    Name = groups.ElementAt(0).Sender,
    Stroke = new SolidColorPaint(SKColor.Parse(App.thePrefs.Sender1Color)) { StrokeThickness = 2 },
    Fill = new SolidColorPaint(SKColor.Parse(App.thePrefs.Sender1Color)),
    DataLabelsSize = 20,
    DataLabelsPaint = new SolidColorPaint(SKColor.Parse(App.thePrefs.Sender1Color)),
    DataLabelsFormatter = ??? // <--
};

My data is double and I want to display thousand separators.

Any ideas ?


Solution

  • from their repo

    // The DataLabelsFormatter is a function 
    // that takes the current point as parameter
    // and returns a string.
    // in this case we returned the PrimaryValue property as currency
    DataLabelsFormatter = (point) => point.PrimaryValue.ToString("C2"),