Xuni Controls > FlexChart > Features > Axis Label Formatter |
The FlexChart's axis label formatter allows users to customize the axis label format beyond the capabilities of standard format strings. With the LabelLoading event, a user can display customized annotations on the axis, such as images, icons, text, or symbols. This optimizes the use of screen space, enabling users visualize the chart data clearly.
This following sections demonstrate how to customize the X and Y axis of the FlexChart using axis label formatter:
For axes labels with long numeric strings, you can use axis label formatting to abbreviate long numeric representations. To illustrate the feature, this example uses a custom format, that is writing K in place of '000 to represent a thousand. This lets users plot label values with 6 or more figures easily on the mobile screen without taking much of the screen space.
Let us take another scenario, where we have sales and expenses data for six countries. The user wants to display the flags of these countries instead of their names to make it more interactive and easy to understand. You can customize the axis using FlexChart's axis label formatting.
The country icons displayed on the X-axis of the FlexChart below are .png image files added under Images folder of the Visual Studio project. These country flags are available in Xuni | Samples folder. You can use images/icons of your choice as well. |
This example uses the sample created in the Quick Start section but with some changes in the data source, and an additional FlagConverter class added to convert the items appearing on the X-axis into images.
The image given below shows customized labels on X- and Y-axis using the axis label formatting in the FlexChart control.
C# |
Copy Code |
---|---|
public class FlexChartDataSource { private List<Country> appData; public List<Country> Data { get { return appData; } } public FlexChartDataSource() { // appData appData = new List<Country>(); var countryName = "US,Japan,UK,Germany,Italy,Greece".Split(','); var salesData = new[] { 5000, 8500, 7000, 6500, 12000, 14800, 18500, 7500, 6500, 13000, 20000, 9000 }; var downloadsData = new[] { 6000, 7500, 12000, 5800, 11000, 7000, 16000, 17500, 19500, 13250, 13800, 19000 }; var expensesData = new[] { 15000, 18000, 15500, 18500, 11000, 16000, 8000, 7500, 6500, 6000, 13500, 5000 }; for (int i = 0; i < 6; i++) { Country tempCountry = new Country(); tempCountry.Name = countryName[i]; tempCountry.Sales = salesData[i]; tempCountry.Downloads = downloadsData[i]; tempCountry.Expenses = expensesData[i]; appData.Add(tempCountry); } } } public class Country { string _name; long _sales, _downloads, _expenses; public string Name { get { return _name; } set { _name = value; } } public long Sales { get { return _sales; } set { _sales = value; } } public long Downloads { get { return _downloads; } set { _downloads = value; } } public long Expenses { get { return _expenses; } set { _expenses = value; } } } |
C# |
Copy Code |
---|---|
public class FlagConverter : IValueConverter { private static ImageSource us = ImageSource.FromResource("FlexChartApp.Images.US.png"); private static ImageSource germany = ImageSource.FromResource("FlexChartApp.Images.Germany.png"); private static ImageSource uk = ImageSource.FromResource("FlexChartApp.Images.UK.png"); private static ImageSource japan = ImageSource.FromResource("FlexChartApp.Images.Japan.png"); private static ImageSource italy = ImageSource.FromResource("FlexChartApp.Images.Italy.png"); private static ImageSource greece = ImageSource.FromResource("FlexChartApp.Images.Greece.png"); public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { String flag = value.ToString().ToUpper(); switch (flag) { case "US": return us; case "UK": return uk; case "GERMANY": return germany; case "JAPAN": return japan; case "ITALY": return italy; case "GREECE": return greece; } } return us; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return us; } } |
C# |
Copy Code |
---|---|
//add LabelLoading event handler for X and Y-axis
chart.AxisX.LabelLoading += AxisX_LabelLoading;
chart.AxisY.LabelLoading += AxisY_LabelLoading; |
C# |
Copy Code |
---|---|
//formatting the labels on X and Y-axis static void AxisX_LabelLoading(object sender, LabelLoadingEventArgs e) { FlagConverter flagConverter = new FlagConverter(); Image img = new Image(); ImageSource src = flagConverter.Convert(e.Text, typeof(ImageSource), null, CultureInfo.CurrentUICulture) as ImageSource; img.Source = src; e.Label = img; } static void AxisY_LabelLoading(object sender, LabelLoadingEventArgs e) { Label label = new Label(); label.Text = string.Format("{0}K", e.Value / 1000); Device.OnPlatform(Android: () => label.FontSize = 7); e.Label = label; } |