How to put a logarithmic scale with rows represented in logarithm on chart in C # [duplicate]

情到浓时终转凉″ 提交于 2019-12-30 11:14:58

问题


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:


回答1:


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..



来源:https://stackoverflow.com/questions/43667315/how-to-put-a-logarithmic-scale-with-rows-represented-in-logarithm-on-chart-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!