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

我只是一个虾纸丫 提交于 2019-12-10 09:53:12

问题


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


回答1:


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,
        });



回答2:


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}";
        }


来源:https://stackoverflow.com/questions/30349725/oxyplot-how-to-change-format-of-values-next-to-the-axis-from-1000-to-1k

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!