pdthekd
pdthekd

Reputation: 87

How to add a ZedGraph programmatically?

I want to add a ZedGraph when I click a button, but the ZedGraph is not coming up when I click my button. Here is my button click handler:

 ZedGraphControl zg1 = new ZedGraphControl(); 
 zg1.Dock = DockStyle.Fill;
 GraphPane myPane = new GraphPane();
 BarItem myBar = new BarItem("Bar1");
 myBar.AddPoint(1, 10);
 myBar.AddPoint(2, 20);
 myBar.Bar.Fill = new Fill(Color.AliceBlue, Color.White, Color.AliceBlue);
 zg1.AxisChange();
 zg1.Invalidate();

 zg1.Show();

Upvotes: 0

Views: 2548

Answers (1)

Mark Hall
Mark Hall

Reputation: 54562

The main thing that jumps out at me is that I don't see you adding your new Control to your Container Object wether it be a Form or a Panel. Also you are not associating your Pane or your Bar to your ZedGraphControl

Try Something like this

ZedGraphControl zg1 = new ZedGraphControl();
zg1.Dock = DockStyle.Fill;
zg1.GraphPane = new GraphPane();

BarItem myBar = new BarItem("Bar1");
myBar.AddPoint(1, 10);
myBar.AddPoint(2, 20);
myBar.Bar.Fill = new Fill(Color.AliceBlue, Color.White, Color.AliceBlue);

zg1.GraphPane.CurveList.Add(myBar);


zg1.AxisChange();
zg1.Invalidate();

zg1.Show();
this.Controls.Add(zg1);

Upvotes: 2

Related Questions