Skip to main content

Generate Custom Text for Series Labels

  • 2 minutes to read

This example shows how to configure the pie series so that its labels display point values as thousands, millions or billions of dollars in the following way:

  • 1234 -> $1.234K
  • 123456789 -> $123.457M
  • 12345678901 -> $12.346B

PieSeriesLabelValues

View Example

  1. Create a class (LabelTextProvider) that implements the ISeriesLabelTextProvider interface.
  2. Implement the GetText method that returns a string for each label instance. A PieSeriesLabelValues object is an argument of this method when it is called for a pie series. Use the PieSeriesLabelValues.Value property to access series point values.
  3. Assign a LabelTextProvider object to the PieSeriesLabel.TextProvider property.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dxc="http://schemas.devexpress.com/xamarin/2014/forms/charts"
             x:Class="SeriesLabelTextProviderExample.MainPage">
    <dxc:PieChartView x:Name="chart" />
</ContentPage>
using System;
using Xamarin.Forms;
using System.Globalization;
using System.Collections.Generic;
using DevExpress.XamarinForms.Charts;

namespace SeriesLabelTextProviderExample {
    public partial class MainPage : ContentPage {
        public MainPage() {
            DevExpress.XamarinForms.Charts.Initializer.Init();
            InitializeComponent();

            PieChartView pieChart = chart;

            PieSeries pieSeries = new PieSeries() {
                Data = new PieSeriesDataAdapter() {
                    LabelDataMember = "Label",
                    ValueDataMember = "Value",
                    DataSource = new List<DataItem>() {
                        new DataItem() { Label = "AAA", Value = 1230000 },
                        new DataItem() { Label = "BBB", Value = 3330000 },
                        new DataItem() { Label = "CCC", Value = 2100000 },
                    }
                },
                Label = new PieSeriesLabel() {
                    Position = PieSeriesLabelPosition.Inside,
                    TextProvider = new LabelTextProvider(),
                    Style = new PieSeriesLabelStyle(){
                        TextStyle = new TextStyle(){
                            Size = 18
                        }
                    }
                }
            };
            pieChart.Series.Add(pieSeries);
        }
    }

    public class LabelTextProvider : ISeriesLabelTextProvider {
        string ISeriesLabelTextProvider.GetText(SeriesLabelValuesBase values) {
            if (values is PieSeriesLabelValues seriesValues) {
                double v = seriesValues.Value;
                if (v >= 1000000000 || v <= -1000000000)
                    return (v / 1000000000.0).ToString("$#.###B", CultureInfo.InvariantCulture);
                else if (v >= 1000000 || v <= -1000000)
                    return (v / 1000000.0).ToString("$#.###M", CultureInfo.InvariantCulture);
                else if (v >= 1000 || v <= -1000)
                    return (v / 1000.0).ToString("$#.###K", CultureInfo.InvariantCulture);
                else
                    return v.ToString();
            }
            return String.Empty;
        }
    }

    public class DataItem {
        public string Label { get; set; }
        public double Value { get; set; }
    }
}