How to change axis text

i wonder if i'm possible to set numeric values to custom text.

When x axis is year, and y axis is price..
raw values of y axis are [1,1000,1000000..] 

If it is 1 , the text is 1.

But I want to use 1 thousand instead of 1000 when axis value is 1000.
(so, 1,000,000  -> 1 million)

How can I put those unit String?

I tried to solve it, using labelformatter.. labelformat.. but I can't solve this problem at last.

Thank you for helping!


3 Replies 1 reply marked as answer

HK Hariharasudhan Kanagaraj Syncfusion Team October 26, 2023 03:30 PM UTC

Hi,


You can achieve the mentioned requirement by formatting the text using either the axisLabelFormatter callback or the numberFormat property. As the mentioned range is very high, we have used the LogarithmicAxis as the primaryYAxis. Similarly, you can achieve the expected output by using the NumericAxis or LogarithmicAxis as the primaryYAxis, based on your requirement.


If you need to format the text for only the 1000 and 100000 values specifically or you need to format the text of the y-axis based on some conditions, then you can use the axisLabelFormatter callback of the primaryYAxis as shown in the code snippet below.


primaryYAxis: LogarithmicAxis(

  axisLabelFormatter: (AxisLabelRenderDetails axisLabelRenderArgs) {

    if (axisLabelRenderArgs.text == '1000') {

      return ChartAxisLabel(

        '1 thousand',

        axisLabelRenderArgs.textStyle,

      );

    } else if (axisLabelRenderArgs.text == '1000000') {

      return ChartAxisLabel(

        '1 million',

        axisLabelRenderArgs.textStyle,

      );

    } else {

      return ChartAxisLabel(

        axisLabelRenderArgs.text,

        axisLabelRenderArgs.textStyle,

      );

    }

  },

),


Snapshot :


Formatting the text which has 1000 and 1000000 using axisLabelFormatter callback.


If you need to format the text of all the y-axis, then you can achieve it by setting the NumberFormat.compactLong to the numberFormat property of the primaryYAxis as shown in the code snippet below.


primaryYAxis: LogarithmicAxis(

  numberFormat: NumberFormat.compactLong(),

),


Snapshot :


Formatting all the text of the y axis using numberFormat property.


Note : You need to import intl package for formatting the labels using the NumberFormat class.


Shared the User Guide Documentation links below regarding the axisLabelFormatter callback and numberFormat property for your reference.


Axis Label Formatter – https://help.syncfusion.com/flutter/cartesian-charts/callbacks#axislabelformatter.

Number Format - https://help.syncfusion.com/flutter/cartesian-charts/axis-types#formatting-the-labels.


Also attached the sample below for your reference and you can modify the sample according to your needs. If you have further queries, please get back to us.


Regards,

Hari Hara Sudhan. K.


Attachment: 185130_fc6e5fdf.zip

Marked as answer

MB Melvin Baker October 30, 2023 07:18 AM UTC


From: snake game

Can you sumup your sharing? It looks so difficult to understand!



HK Hariharasudhan Kanagaraj Syncfusion Team October 31, 2023 01:16 PM UTC

Hi Melvin Baker,


You can achieve the mentioned requirement by formatting the text using either the axisLabelFormatter callback or the numberFormat property. If you need to format the text based on some conditions, then you can use the axisLabelFormatter callback of the primaryYAxis. Otherwise, If you need to format the text of all the y-axis, then you can achieve it by setting the NumberFormat.compactLong to the numberFormat property of the primaryYAxis.


Note : You need to import intl package for formatting the labels using the NumberFormat class.


Shared the User Guide Documentation links below regarding the axisLabelFormatter callback and numberFormat property for your reference.


Axis Label Formatter – https://help.syncfusion.com/flutter/cartesian-charts/callbacks#axislabelformatter.

Number Format - https://help.syncfusion.com/flutter/cartesian-charts/axis-types#formatting-the-labels.


Kindly refer the code snippet below :

primaryYAxis: LogarithmicAxis(

// numberFormat: NumberFormat.compactLong(),

  axisLabelFormatter: (AxisLabelRenderDetails axisLabelRenderArgs) {

    if (axisLabelRenderArgs.text == '1000') {

      return ChartAxisLabel(

        '1 thousand',

        axisLabelRenderArgs.textStyle,

      );

    } else if (axisLabelRenderArgs.text == '1000000') {

      return ChartAxisLabel(

        '1 million',

        axisLabelRenderArgs.textStyle,

      );

    } else {

      return ChartAxisLabel(

        axisLabelRenderArgs.text,

        axisLabelRenderArgs.textStyle,

      );

    }

  },

),


Snapshot :


Using axisLabelFormatter callback based on some conditions.


Formatting all the text of the y axis using numberFormat property.


If you have further queries, please get back to us.


Regards,

Hari Hara Sudhan. K.


Loader.
Up arrow icon