首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将GraphObj绘制到Zedgraph控件

将GraphObj绘制到Zedgraph控件
EN

Stack Overflow用户
提问于 2011-11-04 14:39:18
回答 1查看 5.6K关注 0票数 1

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

我正试图通过单击将BoxObj添加到曲线中(下面的代码),我看到该BoxObj已添加到GraphObjList中,但实际上并未绘制任何内容。会不会是我给对象的位置?

代码语言:javascript
复制
     //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();
        }

下面是整个图表的创建过程

代码语言:javascript
复制
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 Framework4.0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-04 15:30:15

你说得对,地点是错的。单击事件为您提供了显示坐标,但是BoxObj构造函数需要轴比例的单位或图表直方图的分数,具体取决于BoxObj的CoordType

因此,您必须决定哪个CoordType对您更方便,将事件位置转换为此类型,并将CoordType分配给您的BoxObj,例如:

代码语言:javascript
复制
box.Location.CoordinateFrame = CoordType.XScaleYChartFraction;

编辑:为了进行测试,您可以尝试以下操作,该框应该位于图表的中间:

代码语言:javascript
复制
BoxObj box = new BoxObj(0.5, 0.5, 40, 40, Color.Black,Color.Red);
box.Location.CoordinateFrame = CoordType.ChartFraction;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8005708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档