在(!)在非零 x 值处添加一个点或设置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";
更新:
由于您不想使用自动标签(始终按值对齐),因此您需要添加 CustomLabels。
为此,您需要设置要显示标签的位置/值列表:
// pick a better name!
List<double> xs = new List<double>() { 1, 2, 3, 4, 5, 10, 20, 50, 100, 200, 500, 1000};
接下来,我们需要为我们创建的每个CustomLabel 分配一个FromPosition 和一个ToPosition。这总是有点棘手,但在这里甚至比平时更多..
这两个值需要间隔足够远以允许标签适合。所以我们选择一个间隔因子:
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);
}
如您所见,我们需要使用应用于Axis 的Log10 函数计算值,并且间距是通过乘/除分隔符而不是相加来实现的。间距值也必须按 Log10 进行缩放,并包含在函数中。
我们还需要处理1的值的情况,它相当于0的标签位置;但这在乘法/除法时不会产生任何间距。所以我们手动设置一个合适的ToPosition。
我希望我知道一种更简单的方法来做到这一点,但由于标签位置列表确实是您的选择,我怀疑是否有捷径..
我在 40 和 50 处添加了点以显示一个标签如何匹配。还要注意标签位置是如何混合的。随意使用你的!