2016-07-05 94 views
1

我確定這個問題有一個簡單的解決方案,我不知道,但我會問無論如何。我有這樣的代碼:當錨點關閉時,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()縮小視圖並禁用我的註釋。

任何想法?

回答

0

看起來像Annotations,它們直接與AnchorX屬性關聯,或者通過使用AnchorDataPoint一旦錨點不再位於視圖中,就會消失。

不確定這是一個錯誤還是一個功能,但您可以通過直接設置位置來解決問題。

所以,你應該使用帶有h.X屬性,大小,沿着h.Width和位置Annotation

h.X = startOfNewGraph; 
    h.Width = newGraphWidth; 

你沒告訴我們你是如何定位的Annoation垂直;可能會錨定到DataPoint?或者通過設置Y屬性?兩者現在都適用於放大x軸。

請注意,Annotation.XAnnotation.AnchorX的默認值都是double.NaN,在圖表語言中通常表示「自動」。在這種情況下,我想這意味着'不適用'。

如果你設置了兩個屬性,我發現Annotation.X獲勝。因此將其設置回double.NaN將使Annotation.AnchorX重新生效。

+0

我終於回到現在,你的第一句話就是答案。我所需要做的就是將值設置爲X而不是AnchorX,即使在X不可見的情況下它也能正常工作。知道這是簡單的事情! – cl12

0

使用一系列替代和你鴕鳥政策需要操心的錨和調整的問題與.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); 
}