绘图和绘图到Zedgraph控件

本文关键字:绘图 控件 Zedgraph | 更新日期: 2023-09-27 18:13:34

我有一个zedgraph控件,在那里我得到了正弦和余弦波的基本折线图(来自教程)

我试图通过点击它(下面的代码)添加一个BoxObj到曲线,我看到BoxObj被添加到GraphObjList,但实际上没有绘制。可以是我给对象的位置吗?

     //This works
     void zedGraphControl1_Click(object sender, EventArgs e)
        {
            Point p = (e as MouseEventArgs).Location;
            CurveItem nearestCurve;
            int index;
            this.zedGraphControl1.GraphPane.FindNearestPoint(new PointF(p.X, p.Y), out nearestCurve, out index);
            //Check for null when no curve clicked
            if (nearestCurve == null)
                return;
            BoxObj box = new BoxObj(nearestCurve[index].X, nearestCurve[index].Y, 1, 0.1, Color.Black,Color.Red);
            box.IsVisible = true;
            box.Location.CoordinateFrame = CoordType.AxisXYScale;
            box.ZOrder = ZOrder.A_InFront;
            zedGraphControl1.GraphPane.GraphObjList.Add(box);

            zedGraphControl1.Invalidate();
        }

这是整个图形的创建

public void CreateGraph(zedGraph ZedGraphControl)
{
    // Lets generate sine and cosine wave
    double[] x = new double[100];
    double[] y = new double[100];
    double[] z = new double[100];
    for (int i = 0; i < x.Length; i++)
    {
        x[i] = i;
        y[i] = Math.Sin(0.3 * x[i]);
        z[i] = Math.Cos(0.3 * x[i]);
    }
    // This is to remove all plots
    zedGraph.GraphPane.CurveList.Clear();
    // GraphPane object holds one or more Curve objects (or plots)
    GraphPane myPane = zedGraph.GraphPane;
    // PointPairList holds the data for plotting, X and Y arrays 
    PointPairList spl1 = new PointPairList(x, y);
    PointPairList spl2 = new PointPairList(x, z);
    // Add cruves to myPane object
    LineItem myCurve1 = myPane.AddCurve("Sine Wave", spl1, Color.Blue, SymbolType.None);
    LineItem myCurve2 = myPane.AddCurve("Cosine Wave", spl2, Color.Red, SymbolType.None);
    myCurve1.Line.Width = 3.0F;
    myCurve2.Line.Width = 3.0F;
    myPane.Title.Text = "My First Plot";
    // I add all three functions just to be sure it refeshes the plot.   
    zedGraph.AxisChange();
    zedGraph.Invalidate();
    zedGraph.Refresh();
}

环境:Windows XP上的MS Visual Studio 2010和。net Framework 4.0

绘图和绘图到Zedgraph控件

你说得对,位置不对。click-Event为您提供了显示坐标,但是BoxObj构造函数需要轴尺度的单位或图表矩形的部分,这取决于BoxObj的CoordType。
因此,您必须决定哪种CoordType对您更方便,将事件位置转换为这种类型,并将CoordType分配给BoxObj,例如:

box.Location.CoordinateFrame = CoordType.XScaleYChartFraction;
编辑:为了测试,您可以尝试以下操作,并且该框应该位于图表的中间:
BoxObj box = new BoxObj(0.5, 0.5, 40, 40, Color.Black,Color.Red);
box.Location.CoordinateFrame = CoordType.ChartFraction;