【发布时间】:2016-07-05 18:08:46
【问题描述】:
我确定这个问题有一个我不知道的简单解决方案,但无论如何我都会问。我有这个代码:
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();
}
但是,这段代码似乎也不起作用。我第一次尝试时没有显示注释,所以我在 AnchorX 中添加了 0.005 以查看它是否必须超过视图最小值。这也不起作用。当我检查注释的本地值时,数字是正确的。注释要么没有正确显示,要么根本没有显示。 UpdateAnnotations() 会缩小视图并禁用我的注释。
有什么想法吗?
【问题讨论】:
标签: c# .net visual-studio charts data-visualization