将 GraphObj 绘制到 Zedgraph 控件

发布于 2024-12-13 21:35:48 字数 2261 浏览 1 评论 0原文

我有一个 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

I have a zedgraph control where I got at the the basic line chart of sine and cosine wave (from tutorial)

I am trying to add a BoxObj to the curve by clicking on it (code below) and I see that the BoxObj is added to GraphObjList, but nothing is actually drawn. Can it be the location that I give the object?

     //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();
        }

Here is the whole graph creation

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();
}

Environment:
MS Visual Studio 2010 and .NET Framework 4.0 on Windows XP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风流物 2024-12-20 21:35:48

你说得对,只是位置错了。 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;

You're right, the location is wrong. The click-Event gives you the display-coordinates, but the BoxObj Constructor needs either units of your axis-scales or fraction of the chart-rect, depending on the CoordType of your BoxObj.
So you have to decide, which CoordType is more handy to you, convert the event-location to this type and also assign the CoordType to your BoxObj, for instance:

box.Location.CoordinateFrame = CoordType.XScaleYChartFraction;

EDIT: For testing you could try the following, and the box should be in the middle of your chart:

BoxObj box = new BoxObj(0.5, 0.5, 40, 40, Color.Black,Color.Red);
box.Location.CoordinateFrame = CoordType.ChartFraction;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文