在MS图表中对齐和同步X轴不起作用 [英] Aligning and Synchronising X Axes in MS Chart Doesn't Work
问题描述
我有一个带有两个图表区域的图表控件(来自System.Windows.Forms.DataVisualization). ChartArea2与ChartArea1对齐,如下所示:
I have a chart control (from System.Windows.Forms.DataVisualization) with two chart areas. ChartArea2 is aligned to ChartArea1 as follows:
ChartArea2.AlignWithChartArea = "ChartArea1";
ChartArea2.AlignmentOrientation = AreaAlignmentOrientations.Vertical;
ChartArea2.AlignmentStyle = AreaAlignmentStyles.All;
这很好用,除非X轴未对齐(尽管已包含在AlignmentStyle中).相反,它们的最小值,最大值,间隔等保持独立,并根据数据点进行设置.
This works well except the X Axes are not aligned, despite being included in the AlignmentStyle. Instead, their minimum, maximum, interval, etc remain independent and are set according to the datapoints.
我需要X轴相同,即最小,最大,间隔等.我可以在代码中设置这些属性以强制它们相同.但是,一旦我放大ChartArea1,X轴就会再次失准.
I need the X Axes to be identical, i.e. min, max, interval, etc. I can set these properties in code to force them to be identical. However, as soon as I zoom into ChartArea1, then the X Axes become misaligned again.
是否有一种简单的方法可以使X轴相互镜像而不管缩放级别如何?
Is there a simple way for the X Axes to mirror each other regardless of the zoom level?
推荐答案
好吧,它们实际上是 aligned ,也就是说,不管标签如何,它们都位于相同的位置,但是它们没有相同的范围.
Well, they actually are aligned , i.e. sit at the same position regardless of their labels but they don't have the same range.
垂直放置时看不到X轴的对齐方式,而是看Y轴:它们具有不同的字体大小,但位于相同的水平位置!
You don't see the alignment of the X-Axes when the sit vertically but look at the Y-Axes: They have different Font sizes but sit at the same horizontal position!
If you want to show the same range you need to set the range, as you wrote, by setting the Minimum
& Maximum
from the default NaN
(which here means Automatic
) to some values.
当您选择 zoom 时,只要选择AreaAlignmentStyles.AxesView
或AreaAlignmentStyles.All
,其他人就会自动并行缩放.
And when you zoom one the other will zoom in parallel automatically as long as AreaAlignmentStyles.AxesView
or AreaAlignmentStyles.All
are selected.
因此,您需要的是非自动的显式范围(针对未缩放状态)和合适的AreaAlignmentStyle
(针对缩放状态)的组合.
So what you need is the combination of a non-automatic, explicit range (for the unzoomed state) and and a suitable AreaAlignmentStyle
(for the zoomed state.)
注意,只需为两个ChartAreas
中的一个创建AlignmentStyle
.但是需要为两者设置Minimum/Maximum
值:
Note that the AlignmentStyle
needs to be made only for one of the two ChartAreas
. But the Minimum/Maximum
values need to be set for both:
ChartArea CA1 = chart1.ChartAreas[0];
ChartArea CA2 = chart1.ChartAreas.Add("ChartArea2");
// 2nd CA aligns to the 1st one:
CA2.AlignWithChartArea = "ChartArea1";
CA2.AlignmentOrientation = AreaAlignmentOrientations.Vertical;
CA2.AlignmentStyle = AreaAlignmentStyles.All;
// both have the same range:
CA1.AxisX.Maximum = 30;
CA2.AxisX.Maximum = 30;
CA1.AxisX.Minimum = 0;
CA2.AxisX.Minimum = 0;
// both are interactively zoomable:
CA1.AxisX.ScaleView.Zoomable = true;
CA1.AxisX.ScrollBar.Enabled = true;
CA1.CursorX.IsUserSelectionEnabled = true;
CA2.AxisX.ScaleView.Zoomable = true;
CA2.AxisX.ScrollBar.Enabled = true;
CA2.CursorX.IsUserSelectionEnabled = true;
在缩放状态下,两个ChartAreas
仍显示相同的范围,并且具有相同的ScaleView.ViewMinimum / ScaleView.ViewMaximum
:
In a zoomed state both ChartAreas
still show the same range and have the same ScaleView.ViewMinimum / ScaleView.ViewMaximum
:
用于测试ScaleView
值的代码:
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
AxisScaleView ASV1X = chart1.ChartAreas[0].AxisX.ScaleView;
AxisScaleView ASV2X = chart1.ChartAreas[1].AxisX.ScaleView;
label1.Text = "ScaleViews Min/Max: " + ASV1X.ViewMinimum + " - " + ASV1X.ViewMaximum +
" | " + ASV2X.ViewMinimum + " - " + ASV2X.ViewMaximum ;
}
请注意,不仅要保持值范围对齐,还要保持视觉显示轴,您需要使用AlignmentStyle = AreaAlignmentStyles.All;
,而不仅仅是AxisView
或否则,值的格式设置结果或要显示的点数之间的巨大差异会导致Y轴移动并使X轴看起来未对齐!
Note that to keep not only the value ranges aligned but also the visual display of the Axes you need to use the AlignmentStyle = AreaAlignmentStyles.All;
, not just AxisView
or else great differences in the values' formatting results or in the number of points to display can move the Y-Axis and make the X-Axes look misaligned!
这篇关于在MS图表中对齐和同步X轴不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!