如何在 C# 中的图表上放置以对数表示的行的对数刻度 [英] How to put a logarithmic scale with rows represented in logarithm on chart in C #
问题描述
我正在用 C# 开发一个图表,我需要用对数表示图形线,比例已经可以放置,只是根据下图缺少对数比例中的垂直线:
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:
推荐答案
更新:
要在您需要为图表区域的 x 轴打开 MinorGrid 的时间段之间获得这些额外的线:
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 :-)
使用所有者绘制的 GridLine 的旧解决方案:
以下是如何所有者绘制对数轴的网格线.
Here is how you can owner-draw the gridlines for a logarithmic axis.
为了定义区间规则,我首先收集一个双值列表作为停止值.沿 x 轴.
To define the interval rules I first collect a List of double values for the stop values. along the x-axis.
这是一个在每个时期使用 10 条网格线的示例..
Here is an example using 10 gridlines in each period..
图表的 x 轴设置为 Minimum = 0.1d
和 Maximum = 1000
.
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);
}
}
您可以缓存 x 值列表,但必须在数据或轴视图更改时重新计算..
You could cache the x-values list but would have to re-calculate whenever the data or the axis view changes..
请注意,在收集网格停止值时,我们从 1 开始计数,因为我们不想将一个周期的起点加倍:1..9 + 10..90
等.所以我们实际上只添加了 count-1
个网格停止点..
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屋!