- Home
- Forum
- Xamarin.Forms
- Number abbreviations on axis
Number abbreviations on axis
Is there any way to abbreviate numbers on an axis label? For example turn $1,000,000 into $1m and turn $1,000 into $1k. In addition I need it to work dynamically so the abbreviation changes on demand as the user shows and hides data series in a chart and the range on the axis changes accordingly.
Not a huge deal, just trying to save some screen space on the y axis labels on smaller screens when the numbers get really big.
Thanks
SIGN IN To post a reply.
3 Replies
BK
Bharathiraja K
Syncfusion Team
June 17, 2019 09:12 AM UTC
Hi Peter,
Greetings from Syncfusion. You can abbreviate the axis label, by modifying the label content in LabelCreated event of NumericalAxis class. This event will be triggered for every label created and passes the values using the event arguments such as Position, LabelContent and LabelStyle. We can provide customization to the individual labels by using these arguments.
[XAML]:
|
<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis Minimum="1000" Maximum="1000000" LabelCreated="NumericalAxis_LabelCreated">
</chart:NumericalAxis>
</chart:SfChart.SecondaryAxis> |
[C#]:
|
private void NumericalAxis_LabelCreated(object sender, ChartAxisLabelEventArgs e)
{
var position = e.Position;
var label = position.ToString("#,#", CultureInfo.InvariantCulture);
if (position >= 1000 && position < 1000000)
{
label = position.ToString("#,##0,K", CultureInfo.InvariantCulture);
}
else if (position >= 1000000 && position < 1000000000)
{
label = position.ToString("#,##0,,M", CultureInfo.InvariantCulture);
}
else if (position >= 1000000000)
{
label = position.ToString("#,##0,,,B", CultureInfo.InvariantCulture);
}
e.LabelContent = label;
}
} |
We have prepared the sample based on your requirement and it can be downloaded from the below link.
Output:
Regards,
Bharathi.
PY
Peter Yates
June 17, 2019 09:01 PM UTC
Bharathi,
Thanks for the quick response. That works great.
BK
Bharathiraja K
Syncfusion Team
June 18, 2019 05:33 AM UTC
Hi Peter,
Thanks for your update.
-Bharathi.
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
PY Peter Yates
- Jun 14, 2019 08:47 PM UTC
- Jun 18, 2019 05:33 AM UTC