我有一个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 Framework4.0
发布于 2011-11-04 15:30:15
你说得对,地点是错的。单击事件为您提供了显示坐标,但是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;https://stackoverflow.com/questions/8005708
复制相似问题