ZedGraph:如何使用TextObj直接在图表中标记LineItem?

时间:2012-12-07 12:48:13

标签: c# label zedgraph

我在我的项目中使用ZedGraph而且非常棒!但仍有一件事我无法弄清楚。我正在寻找直接在图表中绘制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();

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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/