问题描述
我有一个在 Visual Studio 2015 (C#) 中使用 MS Charts 创建的图,带有对数刻度(两个轴)(见图).
I have a plot created with MS Charts in Visual Studio 2015 (C#) with logarithmic scales (both axes) (see figure).
我需要在 x 轴上添加更多的网格线和相应的标签.我想在 1 (2, 3, 4 ...) 和 10 之间以及 10 和 100 (20, 30, 40 ...) 之间标记每个小刻度,而且,我想在例如之间添加网格线10 和 20.
我在图表的轴属性中使用 1
的间隔作为标签,但它不起作用.
I used the Interval of 1
for the label in the axes properties of the chart but it didn't work.
推荐答案
After(!) 在非零 x 值处添加一个点 或 设置 chart.SuppressExceptions = true
您可以将这些属性用于 Chartarea ca
:
After(!) adding a point at a non-zero x-value or setting chart.SuppressExceptions = true
you can use these properties for a 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
.
Since you don't want to have automatic labels (which are always aligned by value) you need to add CustomLabels
.
为此,您需要设置要显示标签的位置/值列表:
For this you need to set up a list of positions/values where you want a label to show:
// 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
.这总是有点棘手,但在这里甚至比平时更多..
Next we need to assign a FromPosition
and a ToPosition
to each CustomLabel
we create. This is always a little tricky but here even more than usual..
这两个值需要间隔足够远以允许标签适合......所以我们选择了一个间隔因子:
The two values need to be spaced far enough to allow a label to fit in.. So we pick a spacer-factor:
double spacer = 0.9d;
我们还关闭了自动拟合机制:
And we also turn off the auto-fitting mechanism:
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缩放并包含在函数中.
As you can see we need to calculate the values using the Log10
function that is applied to the Axis
and the spacing is achieved by multiplying/dividing the spacer, not by adding. The spacing value must also be scaled by Log10 and is included in the function.
我们还需要处理1
的值的情况,这相当于标签位置0
;但这在乘/除它时不会产生任何间距.所以我们手动设置了一个合适的ToPosition
.
We also need to take care of the case of a value of 1
, which amounts to a label position of 0
; but this won't result in any spacing when multiplying/dividing it. So we set a suitable ToPosition
manually.
我希望我知道一种更简单的方法来做到这一点,但由于标签位置列表确实是您的选择,我怀疑是否存在捷径..
I wish I knew of an easier way to do this, but as the list of label positions is really your choice, I doubt there is a short-cut..
我在 40 和 50 处添加了点以显示一个标签如何匹配.另外请注意标签位置是如何混合的.随意使用你的!
I have added points at 40 and 50 to show how one label matches.. Also note how the label positions are somewhat mixed. Feel free to use yours!
这篇关于以对数刻度 MS 图表(log-log)显示刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!