当锚点关闭时,C#图线注释消失轴查看
问题描述:
我确定这个问题有一个简单的解决方案,我不知道,但我会问无论如何。我有这样的代码:当锚点关闭时,C#图线注释消失轴查看
HorizontalLineAnnotation h = new HorizontalLineAnnotation();
h.AnchorX = startOfNewGraph;
h.Width = newGraphWidth;
h.AxisX = resultGraph.ChartAreas[0].AxisX;
h.AxisY = resultGraph.ChartAreas[0].AxisY;
h.IsSizeAlwaysRelative = false;
h.ClipToChartArea = resultGraph.ChartAreas[0].Name;
originalHLAAnchors.Add(h.AnchorX);
originalHLAWidths.Add(h.Width);
resultGraph.Annotations.Add(h);
我的问题是,当我的图形视图缩放到其AnchorX是视图外的注释消失。我希望保持这种状态,以便即使注释的末尾都不在视图中,我仍然可以看到它们之间的界限。这里就是我试图做补救:
private void resultGraph_AxisViewChanged(object sender, ViewEventArgs e)
{
if (e.Axis == resultGraph.ChartAreas[0].AxisX)
{
ResizeHorizontalAnnotations();
}
}
private void ResizeHorizontalAnnotations()
{
int count = 0;
for (int i = 0; i < resultGraph.Annotations.Count; i++)
{
if (resultGraph.Annotations[i] is HorizontalLineAnnotation)
{
if (originalHLAAnchors[count] < resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum)
{
resultGraph.Annotations[i].AnchorX = resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum + 0.0005;
if ((originalHLAAnchors[count] + originalHLAWidths[count]) >= resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMaximum)
{
resultGraph.Annotations[i].Width = resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMaximum - resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
}
else
{
resultGraph.Annotations[i].Width -= resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum - originalHLAAnchors[count];
}
}
else
{
resultGraph.Annotations[i].AnchorX = originalHLAAnchors[count];
resultGraph.Annotations[i].Width = originalHLAWidths[count];
}
resultGraph.Annotations[i].Visible = true;
count++;
}
}
resultGraph.UpdateAnnotations();
}
然而,这段代码似乎并没有擦出火花。注释没有显示,当我第一次尝试时,所以我加了0.005到AnchorX,看它是否必须稍微超过视图。这也没有用。当我检查注释的本地值时,数字是正确的。注释只是无法正确显示或完全显示。 UpdateAnnotations()缩小视图并禁用我的注释。
任何想法?
答
看起来像Annotations
,它们直接与AnchorX
属性关联,或者通过使用AnchorDataPoint
一旦锚点不再位于视图中,就会消失。
不确定这是一个错误还是一个功能,但您可以通过直接设置位置来解决问题。
所以,你应该使用带有h.X
属性,大小,沿着h.Width
和位置Annotation
:
h.X = startOfNewGraph;
h.Width = newGraphWidth;
你没告诉我们你是如何定位的Annoation
垂直;可能会锚定到DataPoint
?或者通过设置Y
属性?两者现在都适用于放大x轴。
请注意,Annotation.X
和Annotation.AnchorX
的默认值都是double.NaN
,在图表语言中通常表示“自动”。在这种情况下,我想这意味着'不适用'。
如果你设置了两个属性,我发现Annotation.X
获胜。因此将其设置回double.NaN
将使Annotation.AnchorX
重新生效。
答
使用一系列替代和你鸵鸟政策需要操心的锚和调整的问题与.NET图表注释:
private void Line(Point start, Point end)
{
chart1.Series.Add("line");
chart1.Series["line"].ChartType = SeriesChartType.Line;
chart1.Series["line"].Color = System.Drawing.Color.Red;
chart1.Series["line"].Points.AddXY(start.X, start.Y);
chart1.Series["line"].Points.AddXY(end.X, end.Y);
}
我终于回到现在,你的第一句话就是答案。我所需要做的就是将值设置为X而不是AnchorX,即使在X不可见的情况下它也能正常工作。知道这是简单的事情! – cl12