将 GraphObj 绘制到 Zedgraph 控件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说得对,只是位置错了。 click-Event 为您提供显示坐标,但 BoxObj 构造函数需要轴刻度的单位或图表矩形的分数,具体取决于 BoxObj 的 CoordType。
因此,您必须决定哪种 CoordType 对您来说更方便,将事件位置转换为这种类型,并将 CoordType 分配给您的 BoxObj,例如:
编辑:为了测试,您可以尝试以下操作,并且该框应该位于图表的中间:
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:
EDIT: For testing you could try the following, and the box should be in the middle of your chart: