我確定這個問題有一個簡單的解決方案,我不知道,但我會問無論如何。我有這樣的代碼:當錨點關閉時,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()縮小視圖並禁用我的註釋。
任何想法?
我終於回到現在,你的第一句話就是答案。我所需要做的就是將值設置爲X而不是AnchorX,即使在X不可見的情況下它也能正常工作。知道這是簡單的事情! – cl12