2012-12-07 53 views
1

我在我的項目中使用了ZedGraph,它真棒!但還有一件事我不明白。即時尋找直接在圖表繪製的LineItem的描述中,類似上圖:ZedGraph:如何使用TextObj直接在圖表中標記LineItem?

http://www.imagesup.net/?di=113548312290

我試圖用TextObj的一些可能性,但我仍然有正確的問題計算角度,它不對應於線的斜率。任何人都可以告訴我什麼是錯的? PS:也許這可能是由於X軸和Y軸的不同範圍或屏幕上這些軸的長度不同造成的?

 PointPair ptA = new PointPair(0, 100); 
     PointPair ptB = new PointPair(100, 0); 

     PointPairList ppl = new PointPairList(); 
     ppl.Add(ptA); 
     ppl.Add(ptB); 

     LineItem myCurve = zedGraphControl1.GraphPane.AddCurve(string.Empty, ppl, Color.Red, SymbolType.Circle); 

     // centre of line 
     PointPair pt = new PointPair(0.5 * (ptA.X + ptB.X), 0.5 * (ptA.Y + ptB.Y)); 

     TextObj text = new TextObj("desc", pt.X, pt.Y, CoordType.AxisXYScale, AlignH.Center, AlignV.Center); 
     text.ZOrder = ZOrder.A_InFront; 

     double dX = ptB.X - ptA.X; 
     double dY = ptB.Y - ptA.Y; 

     float alfa = (float)(Math.Atan2(dY, dX) * (180.0/Math.PI)); 

     text.FontSpec.Angle = alfa; 

     zedGraphControl1.GraphPane.GraphObjList.Add(text); 

     zedGraphControl1.AxisChange(); 
     zedGraphControl1.Invalidate(); 
     zedGraphControl1.Refresh(); 

回答

0

我有同樣的問題。這是我在VB中處理的一個部分解決方案;應該是很容易轉換成C#:

' Calc deltas for x and y 
Dim dX As Double = ptB.X - ptA.X 
Dim dY As Double = ptB.Y - ptA.Y 

' compensate delta x for graph resizing, which affects line slopes 
Dim resizeCompensation As Double = 1.0/(myPane.XAxis.Scale.Max - myPane.XAxis.Scale.Min) 
dX = dX * resizeCompensation 

' now calculate angle 
Dim alfa As Double = Math.Atan2(dY, dX) * (180.0/Math.PI) 
text.FontSpec.Angle = alfa 

雖然上面已經足夠好了,我的目的,更全面的解決方案可能是在這裏度過的: http://sourceforge.net/p/zedgraph/discussion/392232/thread/0d261bc7/

1
// Call this method from the Form_Load method, passing your ZedGraphControl 

public void CreateChart(ZedGraphControl zgc) 
{ 
    GraphPane myPane = zgc.GraphPane; 

    // Set the titles and axis labels 
    myPane.Title.Text = "Demo of Labeled Points"; 
    myPane.XAxis.Title.Text = "Time, Seconds"; 
    myPane.YAxis.Title.Text = "Pressure, Psia"; 

    // Build a PointPairList with points based on Sine wave 
    PointPairList list = new PointPairList(); 
    const int count = 15; 
    for (int i = 0; i < count; i++) 
    { 
     double x = i + 1; 

     double y = 21.1 * (1.0 + Math.Sin((double)i * 0.15)); 

     list.Add(x, y); 
    } 

    // Hide the legend 
    myPane.Legend.IsVisible = false; 

    // Add a curve 
    LineItem curve = myPane.AddCurve("label", list, Color.Red, SymbolType.Circle); 
    curve.Line.Width = 2.0F; 
    curve.Line.IsAntiAlias = true; 
    curve.Symbol.Fill = new Fill(Color.White); 
    curve.Symbol.Size = 7; 

    // Fill the axis background with a gradient 
    myPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, Color.ForestGreen), 45.0F); 

    // Offset Y space between point and label 
    // NOTE: This offset is in Y scale units, so it depends on your actual data 
    const double offset = 1.0; 

    // Loop to add text labels to the points 
    for (int i = 0; i < count; i++) 
    { 
     // Get the pointpair 
     PointPair pt = curve.Points[i]; 

     // Create a text label from the Y data value 
     TextObj text = new TextObj(pt.Y.ToString("f2"), pt.X, pt.Y + offset, 
      CoordType.AxisXYScale, AlignH.Left, AlignV.Center); 
     text.ZOrder = ZOrder.A_InFront; 
     // Hide the border and the fill 
     text.FontSpec.Border.IsVisible = false; 
     text.FontSpec.Fill.IsVisible = false; 
     //text.FontSpec.Fill = new Fill(Color.FromArgb(100, Color.White)); 
     // Rotate the text to 90 degrees 
     text.FontSpec.Angle = 90; 

     myPane.GraphObjList.Add(text); 
    } 

    // Leave some extra space on top for the labels to fit within the chart rect 
    myPane.YAxis.Scale.MaxGrace = 0.2; 

    // Calculate the Axis Scale Ranges 
    zgc.AxisChange(); 
} 

來源:http://zedgraph.dariowiz.com/index9769.html?title=Point_Label_Demo

相關問題