简体   繁体   English

MS Chart Control中的十年日志轴

[英]Decade log axis in MS Chart Control

I want to create decade logarithm y axis as shown in the first picture. 我想创建十进制对数y轴,如第一幅图所示。 The first picture has 78 as minimum value of logarithm y-axis. 第一张图片的对数y轴最小值为78。 Then it shows other axis values in the power of 10 like 100 and 1000. At the end, it shows maximum value of logarithm y-axis This first graph is not developed using MS Chart control. 然后,它显示其他以10的幂为单位的轴值(例如100和1000)。最后,它显示对数y轴的最大值。第一个图未使用MS Chart控件开发。

The graph in second picture is developed using MS Chart control. 第二张图中的图形是使用MS Chart控件开发的。 In the second graph, i am not able to show axis values in the power of 10 such as 100 and 1000 similar to the first graph. 在第二张图中,类似于第一张图,我无法以10的幂显示轴值,例如100和1000。 I am using below code to create this graph. 我正在使用下面的代码来创建此图。

    axis.MinorGrid.Interval = 1;
    axis.MinorTickMark.Interval = 1;
    axis.MajorGrid.Interval = 1;
    axis.MajorTickMark.Interval = 1;

I want to create second graph similar to the first graph 我想创建第二张图,类似于第一张图

所需的log y轴

使用MS图表的log y轴

After quite an effort, i found the solution to this problem. 经过一番努力,我找到了解决该问题的方法。 In order to show logarithmic y-axis values in power of 10, interval offset needs to be set for the labels. 为了以10的幂显示对数y轴值,需要为标签设置间隔偏移。 Major grid and minor grid can also be set accordingly. 也可以相应地设置主网格和次网格。 For example, in picture 2, the minimum is 78.53. 例如,在图片2中,最小值为78.53。 So if we want to show 100, we need to set an offset of 21.47. 因此,如果要显示100,则需要将偏移设置为21.47。 To calculate the offset, i have to perform the calculation 要计算偏移量,我必须执行计算

100 - 78.53 = 21.47. 100-78.53 = 21.47。

Now since we are dealing with logarithmic values of y-axis, we need to perform this calculation in logarithmic values 现在,由于我们正在处理y轴的对数值,因此我们需要以对数值进行此计算

Offset = log(100) - log(78.53) 偏移量= log(100)-log(78.53)

If we set the above offset in picture 2 to axis label style, major and minor grids, then the first label and grid will start from 100 followed by 1000, 100000 as we needed. 如果将图片2中的上述偏移量设置为轴标签样式,主网格和次网格,则第一个标签和网格将从100开始,然后根据需要从1000、100000开始。 Intervals for label style, major and minor grids should be set to 1, which indicates 1 decade. 标签样式,主要和次要网格的间隔应设置为1,表示1个十年。

Now the second problem is how to show the minimum and maximum values of the graph. 现在,第二个问题是如何显示图形的最小值和最大值。 By default, MS Chart will not show these values. 默认情况下,MS Chart将不显示这些值。 We need to work on custom labels to show minimum and maximum values as well as the values that occur in between ie 10, 100, 1000 etc because If we add custom labels, then we need to handle the labeling of the axis all by ourselves. 我们需要处理自定义标签以显示最小值和最大值以及介于10、100、1000等之间的值,因为如果添加自定义标签,则需要我们自己处理所有轴的标签。

Below is the code which creates the above desired graph. 下面是创建上述所需图形的代码。

            chart1.Series.Clear();
            chart1.ChartAreas[0].AxisY.CustomLabels.Clear();
            chart1.Series.Add("FirstSeries");

            // Set the type to line      
            chart1.Series["FirstSeries"].ChartType = SeriesChartType.Line;

            // Color the line of the graph light green and give it a thickness of 3
            chart1.Series[0].Color = Color.Red;
            chart1.Series[0].BorderWidth = 1;

            int minimum = 7, maximum = 300;
            chart1.ChartAreas[0].AxisY.Minimum = minimum;
            chart1.ChartAreas[0].AxisY.Maximum = maximum;
            double logMin = Math.Log(chart1.ChartAreas[0].AxisY.Minimum, 10);

            //If minimum starts from log(7) = 0.845 then take 1 which is equivalent to 100 in normal mode.
            double ceilMin = Math.Ceiling(logMin);
            double logMax = Math.Log(chart1.ChartAreas[0].AxisY.Maximum, 10);

            chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
            chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = true;
            chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = true;
            chart1.ChartAreas[0].AxisY.MinorTickMark.Enabled = true;

            double intervalOffset = ceilMin - logMin;
            chart1.ChartAreas[0].AxisY.LabelStyle.IntervalOffset = intervalOffset;
            chart1.ChartAreas[0].AxisY.MajorGrid.IntervalOffset = intervalOffset;
            chart1.ChartAreas[0].AxisY.MinorGrid.IntervalOffset = intervalOffset;
            chart1.ChartAreas[0].AxisY.MajorTickMark.IntervalOffset = intervalOffset;
            chart1.ChartAreas[0].AxisY.MinorTickMark.IntervalOffset = intervalOffset;

            //Setting intervals
            chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 1;
            chart1.ChartAreas[0].AxisY.MajorTickMark.Interval = 1;
            chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 1;
            chart1.ChartAreas[0].AxisY.MinorTickMark.Interval = 1;
            chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 1;

            for (int x = minimum; x <= maximum; x += 1)
            {
                chart1.Series[0].Points.Add(x, x);
            }

            //Adding custom labels for powers of 10 for example 1,10,100 
            for (double x = minimum; x <= maximum; x+=1)
            {
                double logX = Math.Log10(x);
                double floorX = Math.Floor(logX);
                if (logX - floorX == 0)
                    chart1.ChartAreas[0].AxisY.CustomLabels.Add(logX-0.05, logX + 0.05, "" + Math.Pow(10, logX), 0, LabelMarkStyle.None);
            }

            //Adding minimum and maximum label. 0.05 factor is added by experiment.
            chart1.ChartAreas[0].AxisY.CustomLabels.Add(logMin-0.05, logMin + 0.05, "" + chart1.ChartAreas[0].AxisY.Minimum, 0, LabelMarkStyle.None);
            chart1.ChartAreas[0].AxisY.CustomLabels.Add(logMax-0.05, logMax+0.05, "" + chart1.ChartAreas[0].AxisY.Maximum, 0, LabelMarkStyle.None);

            chart1.ChartAreas[0].AxisY.IsLogarithmic = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM