OxyPlot.如何改变值的格式旁边的轴从1000到1k

本文关键字:1000 1k 格式 OxyPlot 何改变 改变 | 更新日期: 2023-09-27 17:49:53

我试图改变轴旁边的值的格式,例如从1000到1k或1000000到1M。

这在LinearAxis中是可能的吗?

这是我的代码

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

这可能与StringFormat?

也可以改变TickStyle,使破折号贯穿整个情节吗?

Thanks in advance

迈克尔

OxyPlot.如何改变值的格式旁边的轴从1000到1k

您可以使用Axis类的LabelFormatter属性将1000更改为1K等

创建格式化函数,接受双精度类型并返回字符串:

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

然后将其添加到Axis类:

plotmodel.Axes.Add(new LinearAxis
        {
            //Other properties here
            LabelFormatter = _formatter,
        });

我使用这种方法。基于Metric前缀。适用于区间(-Inf, 0.001> u <1000, +Inf),即0.001转换为1m, 1000转换为1k

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