在(!)在非零 x 值处添加一个点之后or环境chart.SuppressExceptions = true
您可以将这些属性用于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";
Update:
由于您不想拥有自动标签(始终按值对齐),因此您需要添加CustomLabels
.
为此,您需要设置要显示标签的位置/值列表:
// pick a better name!
List<double> xs = new List<double>() { 1, 2, 3, 4, 5, 10, 20, 50, 100, 200, 500, 1000};
接下来我们需要分配一个FromPosition
and a ToPosition
每一个CustomLabel
我们创造。这总是有点棘手,但这里比平常更棘手。
这两个值需要间隔足够远以允许标签适合。所以我们选择一个间隔因子:
double spacer = 0.9d;
我们还关闭自动调整机制:
ca.AxisX.IsLabelAutoFit = false;
现在我们可以添加CustomLabels
:
for (int i = 0; i < xs.Count; i++)
{
CustomLabel cl = new CustomLabel();
if (xs[i] == 1 || xs[i] <= 0)
{
cl.FromPosition = 0f;
cl.ToPosition = 0.01f;
}
else
{
cl.FromPosition = Math.Log10(xs[i] * spacer);
cl.ToPosition = Math.Log10(xs[i] / spacer);
}
cl.Text = xs[i] + "";
ca.AxisX.CustomLabels.Add(cl);
}
正如您所看到的,我们需要使用以下方法计算值Log10
函数被应用到Axis
并且间距是通过乘以/除以间隔物来实现的,而不是通过相加来实现。间距值还必须按 Log10 缩放并包含在函数中。
我们还需要处理值为1
,相当于标签位置0
;但这在乘法/除法时不会产生任何间距。所以我们设置一个合适的ToPosition
手动。
我希望我知道有一种更简单的方法来做到这一点,但由于标签位置列表实际上是您的选择,我怀疑是否有捷径。
我在 40 和 50 处添加了点来显示一个标签如何匹配。还要注意标签位置如何有些混合。请随意使用您的!