以对数刻度显示刻度标签MS图(对数对数) [英] Display tick labels in logarithmic scale MS Chart (log-log)

查看:901
本文介绍了以对数刻度显示刻度标签MS图(对数对数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MS Chart在Visual Studio 2015(C#)中以对数刻度(两个轴)创建的绘图(见图)。

I have a plot created with MS Charts in Visual Studio 2015 (C#) with logarithmic scales (both axes) (see figure).

以在x轴中添加更多网格线和相应的标签。我想标记每个小的刻度线之间1(2,3,4 ...)和10和10和100(20,30,40 ...)之间,并且,我想添加网格线之间的例如。

我使用 1 图表,但它没有工作。

I used the Interval of 1 for the label in the axes properties of the chart but it didn't work.

推荐答案

在(x)非零x值设置 chart.SuppressExceptions = true 下添加点后,可以使用这些属性a Chartarea ca

After(!) adding a point at a non-zero x-value or setting chart.SuppressExceptions = true you can use these properties for a Chartarea ca:

ca.AxisX.IsLogarithmic = true;
ca.AxisX.LogarithmBase = 10;

// with 10 as the base it will go to 1, 10, 100, 1000..
ca.AxisX.Interval = 1;

// this adds 4 tickmarks into each interval:
ca.AxisX.MajorTickMark.Interval = 0.25;

// this add 8 gridlines into each interval:
ca.AxisX.MajorGrid.Interval = 0.125;

// this sets two i.e. adds one extra label per interval
ca.AxisX.LabelStyle.Interval = 0.5;
ca.AxisX.LabelStyle.Format = "#0.0";

更新:

由于您不希望自动标签总是按值对齐,因此您需要添加 CustomLabels

Since you don't want to have automatic labels that are always aligned by value you need to add CustomLabels.

为此,您需要设置一个位置/值列表, :

For this you need to set up a list of positions/values where you want a label to show:

    // pick a better name!
    List<double> xs = new List<double>() { 1, 2, 3, 4, 5, 10, 20, 50, 100, 200, 500, 1000};

接下来我们需要分配一个 FromPosition 我们创建的每个 CustomLabel ToPosition 这总是有点棘手,但在这里甚至比平常更多。

Next we need to assign a FromPosition and a ToPosition to each CustomLabel we create. This always a little tricky but here even more than usual..

这两个值需要足够的间隔,以允许一个标签适合..所以我们选择spacer-factor:

The two values need to be spaced far enough to allow a label to fit in.. So we pick a spacer-factor:

    double spacer = 0.9d;

我们也关闭自动装配机制:

And we also turn off the auto-fitting mechanism:

    ca.AxisX.IsLabelAutoFit = false;

现在我们可以添加 CustomLabels

    for (int i = 0; i < xs.Count; i++)
    {
        CustomLabel cl = new CustomLabel();
        cl.FromPosition = Math.Log10(xs[i]) * spacer;
        cl.ToPosition = Math.Log10(xs[i]) / spacer ;
        if (xs[i] <= 1)
        {
            cl.FromPosition = 0f;
            cl.ToPosition = 0.01f;
        }
        cl.Text = xs[i] + "";
        ca.AxisX.CustomLabels.Add(cl);

    }

正如你所看到的,我们需要使用应用于 Axis Log10 函数,并且间距通过乘以/除以间隔而不是通过添加来实现。 。

As you can see we need to calculate the values using the Log10 function that is applied to the Axis and the spacing is achieved by multiplying/dividing the spacer, not by adding..

我们还需要处理值 1 的情况,这相当于标签位置 0 ;但是在乘法/除法时不会产生任何间隔。所以我们手动设置一个合适的 ToPosition

We also need to take care of the case of a value of 1, which amounts to a label position of 0; but this won't result in any spacing when multiplying/dividing it. So we set a suitable ToPosition manually.

我希望我知道一个更简单的方法,标签位置列表真的是你的选择,我怀疑有一个捷径..

I wish I knew of an easier way to do this, but as the list of label positions is really your choice, I doubt there is a short-cut..

我已经在40和50添加了点,以显示一个标签如何匹配。还要注意标签位置是如何混合。随时使用您的!

I have added points at 40 and 50 to show how one label matches.. Also note how the label positions are somewhat mixed. Feel free to use yours!

这篇关于以对数刻度显示刻度标签MS图(对数对数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆