Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OxyPlot. How to change Format of values next to the axis from 1000 to 1k

Im trying to change the format of the value next to the axis from for example 1000 to 1k or 1000000 to 1M.

Is this possible in LinearAxis?

This is my Code:

            m.Axes.Add(new LinearAxis
        {
            Position = AxisPosition.Right,
            IsZoomEnabled = false,
            IsPanEnabled = false,
            Minimum = -(_maxPointValue2*0.1),
            Maximum = _maxPointValue2 + (_maxPointValue2*0.1),
            FontSize = 15,
            Key = "right",
            TickStyle = TickStyle.Outside,

        });

Is this perhaps possible with StringFormat?

Also is it possible to change the TickStyle, so that the dashes going trough the whole plot?

Thanks in advance

Michael

like image 429
midi Avatar asked Dec 05 '25 04:12

midi


2 Answers

You can use the LabelFormatter property of the Axis class to change from 1000 to 1K etc.

Create your formatting function to take a double and return a string:

private static string _formatter(double d)
    {
        if (d < 1E3)
        {
            return String.Format("{0}", d);
        }
        else if (d >= 1E3 && d < 1E6)
        {
            return String.Format("{0}K", d / 1E3);
        }
        else if (d >= 1E6 && d < 1E9)
        {
            return String.Format("{0}M", d / 1E6);
        }
        else if (d >= 1E9)
        {
            return String.Format("{0}B", d / 1E9);
        }
        else
        {
            return String.Format("{0}", d);
        }
    }

Then add it to the Axis class:

plotmodel.Axes.Add(new LinearAxis
        {
            //Other properties here
            LabelFormatter = _formatter,
        });
like image 58
Eric Wilson Avatar answered Dec 07 '25 17:12

Eric Wilson


I use this approach. Based on Metric prefixes. Works for values in interval (-Inf, 0.001> u <1000, +Inf) ie 0.001 convert to 1m, 1000 to 1k etc

// Axis
PlotModel.Axes.Add(new LinearAxis
{
    Title = "Value",
    LabelFormatter = ValueAxisLabelFormatter,
});

// ValueAxisLabelFormatter method
private string ValueAxisLabelFormatter(double input)
        {
            double res = double.NaN;
            string suffix = string.Empty;

            // Prevod malych hodnot
            if (Math.Abs(input) <= 0.001)
            {
                Dictionary<int, string> siLow = new Dictionary<int, string>
                {
                    [-12] = "p",
                    [-9] = "n",
                    [-6] = "μ",
                    [-3] = "m",
                    //[-2] = "c",
                    //[-1] = "d",
                };

                foreach (var v in siLow.Keys)
                {
                    if (input != 0 && Math.Abs(input) <= Math.Pow(10, v))
                    {
                        res = input * Math.Pow(10, Math.Abs(v));
                        suffix = siLow[v];
                        break;
                    }
                }
            }

            // Prevod velkych hodnot
            if (Math.Abs(input) >= 1000)
            {
                Dictionary<int, string> siHigh = new Dictionary<int, string>
                {
                    [12] = "T",
                    [9] = "G",
                    [6] = "M",
                    [3] = "k",
                    //[2] = "h",
                    //[1] = "da",
                };

                foreach (var v in siHigh.Keys)
                {
                    if (input != 0 && Math.Abs(input) >= Math.Pow(10, v))
                    {
                        res = input / Math.Pow(10, Math.Abs(v));
                        suffix = siHigh[v];
                        break;
                    }
                }
            }

            return double.IsNaN(res) ? input.ToString() : $"{res}{suffix}";
        }
like image 34
honzakuzel1989 Avatar answered Dec 07 '25 16:12

honzakuzel1989



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!