如何在C#中的图表上放置以对数表示的行的对数刻度 [英] How to put a logarithmic scale with rows represented in logarithm on chart in C #

查看:513
本文介绍了如何在C#中的图表上放置以对数表示的行的对数刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#开发图表,我需要图形线以对数表示,刻度已经可以放了,只是根据下图缺少对数刻度的垂直线:





我的图表当前如下所示:



解决方案

更新:



要获得两个周期之间的额外线条,您需要为图表区域的x轴打开MinorGrid:

  chart.ChartAreas [0] .AxisX.MinorGrid.Enabled = true; 
chart.ChartAreas [0] .AxisX.MinorGrid.Interval = 1;

这显然比我自己画画容易得多,因为我认为这很必要。 >

不过,我将所有者自定义的解决方案留在下面,因为它在做额外的特殊操作(例如使用不常用的笔或自定义间隔)时可能很有用。



(我很高兴看到结果相同:-)



  private void chart_PrePaint(对象发送方, e)
{
ChartArea ca = chart.ChartAreas [0];
Graphics g = e.ChartGraphics.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

//首先获取y的最高值和最低值:
int y0 =(int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Minimum);
int y1 =(int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Maximum);

int iCount = 10; //每个周期的网格线数
var xes = new List< double>(); //网格位置值
double xx = ca.AxisX.Minimum;
do {
xx * = ca.AxisX.LogarithmBase; //下一个周期
double delta = 1d * xx / iCount; //此时间段内的距离
for(int i = 1; i< icount; i ++)xes.Add(i * delta); //收集值
} while(xx< ca.AxisX.Maximum);

//现在我们可以绘制..
使用(钢笔=​​新的Pen(Color.FromArgb(64,Color.Blue))
{DashStyle = System.Drawing。 Drawing2D.DashStyle.Dot})
foreach(xes中的var xv)
{
float x =(float)ca.AxisX.ValueToPixelPosition(xv);
g.DrawLine(pen,x,y0,x,y1);
}
}

您可以缓存x值列表,但可以



请注意,当收集网格停止值时,我们从1开始计数,因为我们不想加倍一个时期的起点: 1..9 + 10..90 等。因此,我们实际上只添加了 count-1 网格停止。.


I'm developing a chart in C # and I need the graph lines to be represented in logarithm, the scale can already put, just missing the vertical lines in logarithmic scale according to the image below:

My chart currently looks like this:

解决方案

Update:

To get those additional lines between the periods you need to turn on the MinorGrid for the x-axis of your chartArea:

chart.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
chart.ChartAreas[0].AxisX.MinorGrid.Interval = 1;

This is obviosly a lot easier than drawing them yourself, as I thought was necessary..

I leave the owner-drawn solution below, though, as it may be useful when doing extra special stuff, like using unsusual pens or customizing the intervals..

(And I was glad to see that the results are identical :-)

Old solution with owner-drawn GridLines:

Here is how you can owner-draw the gridlines for a logarithmic axis.

To define the interval rules I first collect a List of double values for the stop values. along the x-axis.

Here is an example using 10 gridlines in each period..

The chart's x-axis was set up with Minimum = 0.1d and Maximum = 1000.

private void chart_PrePaint(object sender, ChartPaintEventArgs e)
{
    ChartArea ca = chart.ChartAreas[0];
    Graphics g = e.ChartGraphics.Graphics;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    // first get y top and bottom values:
    int y0 = (int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Minimum);
    int y1 = (int)ca.AxisY.ValueToPixelPosition(ca.AxisY.Maximum);

    int iCount = 10;                         // number of gridlines per period
    var xes = new List<double>();            // the grid position values
    double xx = ca.AxisX.Minimum;
    do {
       xx *= ca.AxisX.LogarithmBase;         // next period
       double delta = 1d * xx / iCount ;     // distance in this period
       for (int i = 1; i < icount; i++) xes.Add(i * delta);  // collect the values
    }  while (xx < ca.AxisX.Maximum);

    // now we can draw..
    using (Pen pen = new Pen(Color.FromArgb(64, Color.Blue))
          {DashStyle = System.Drawing.Drawing2D.DashStyle.Dot} )
       foreach (var xv in xes)
       {
           float x = (float)ca.AxisX.ValueToPixelPosition(xv);
           g.DrawLine(pen, x, y0, x, y1);
       }
}

You could cache the x-values list but would have to re-calculate whenever the data or the axis view changes..

Note that, when collection the grid stop values, we count from 1 because we don't want to double the starting points of a period: 1..9 + 10..90 etc. So we actually add only count-1 grid stops..

这篇关于如何在C#中的图表上放置以对数表示的行的对数刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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