【问题标题】:OxyPlot. How to change Format of values next to the axis from 1000 to 1k氧图。如何将轴旁边的值格式从 1000 更改为 1k
【发布时间】:2015-08-01 16:53:25
【问题描述】:

我正在尝试将轴旁边的值的格式从例如 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,使破折号贯穿整个情节?

提前致谢

迈克尔

【问题讨论】:

    标签: string-formatting oxyplot


    【解决方案1】:

    您可以使用 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,
            });
    

    【讨论】:

    • 有没有办法改变参数增加的间隔?我正在处理类似的问题,但似乎无法正确调整间隔。
    【解决方案2】:

    我使用这种方法。基于Metric prefixes。适用于区间(-Inf, 0.001&gt; u &lt;1000, +Inf)0.001 中的值转换为1m10001k

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

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多