Value formatting of the scale label depending on the value.


https://www.nevron.com/Forum/Topic10706.aspx
Print Topic | Close Window

By Fabio Olcese - 8 Years Ago
Hi,

I was wondering if there was a way to format values depending on the value itself. For example in a NumerixAxis using a NLinearScaleConfigurator, if the label is a number greater or equal 1 000 000 use scientific notation otherwise just display the number.

Thanks in advance!
By Nevron Support - 8 Years Ago
Hi Fabio,
Yes it is possible to have formatting dependent on the value - the following code shows how to create a custom value formatter which formats values greater than 1000 to 1K, 2K etc:

        class CustomValueFormatter : NValueFormatter
        {
            public override string FormatValue(double value)
            {
                if (value >= 1000)
                {
                    return ((int)value / 1000).ToString() + "K";
                }

                return value.ToString();
            }

            public override string FormatValue(object value)
            {
                return FormatValue((double)value);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            NChart chart = nChartControl1.Charts[0];

            NBarSeries bar = new NBarSeries();

            bar.Values.Add(10);
            bar.Values.Add(100);
            bar.Values.Add(1000);

            chart.Series.Add(bar);

            NLinearScaleConfigurator linearScale = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator;

            linearScale.LabelValueFormatter = new CustomValueFormatter();
        }

Hope this helps - let us know if you meet any problems or have any questions.