I need to draw a graph in C# by ZedGraph in VS2013. I have created a windowns application project and added Form1.cs as win forms file. But, when I ran the code, only an empty form was created but no graph. In debug mode, I found that Form1_Load() is not executed. Why ?
This is the C# code.
using System.Windows.Forms;
using ZedGraph;
namespace zedgraph_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// it is not executed.
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Form1_Load is called");
ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
CreateChart(zg1);
}
// definition of CreateChart
...
}
Any help would be appreciated.
UPDATE
I have tried the solution but I only got an empty form and no charts displayed in the form.
This is code of From1.cs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
namespace zedgraph_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Form1_Load is called");
ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
CreateChart(zg1);
SetSize();
}
private void Form1_Resize(object sender, EventArgs e)
{
SetSize();
}
private void SetSize()
{
zg1.Location = new Point(10, 10);
// Leave a small margin around the outside of the control
zg1.Size = new Size(this.ClientRectangle.Width - 20, this.ClientRectangle.Height - 20);
}
// Call this method from the Form_Load method, passing your ZedGraphControl
public static void CreateChart(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
// Set the title and axis labels
myPane.Title.Text = "Vertical Bars with Value Labels Above Each Bar";
myPane.XAxis.Title.Text = "Position Number";
myPane.YAxis.Title.Text = "Some Random Thing";
PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();
Random rand = new Random();
// Generate random data for three curves
for (int i = 0; i < 5; i++)
{
double x = (double)i;
double y = rand.NextDouble() * 1000;
double y2 = rand.NextDouble() * 1000;
double y3 = rand.NextDouble() * 1000;
list.Add(x, y);
list2.Add(x, y2);
list3.Add(x, y3);
}
// create the curves
BarItem myCurve = myPane.AddBar("curve 1", list, Color.Blue);
BarItem myCurve2 = myPane.AddBar("curve 2", list2, Color.Red);
BarItem myCurve3 = myPane.AddBar("curve 3", list3, Color.Green);
// Fill the axis background with a color gradient
myPane.Chart.Fill = new Fill(Color.White,
Color.FromArgb(255, 255, 166), 45.0F);
zgc.AxisChange();
// expand the range of the Y axis slightly to accommodate the labels
myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;
// Create TextObj's to provide labels for each bar
BarItem.CreateBarLabels(myPane, false, "f0");
//Console.ReadLine();
}
private void zg1_Load(object sender, EventArgs e)
{
}
}
}
More update I have added zedgraphControl by adding zedgraph.dll under object relational design in toolbox of VS2013.
But, all components in my toolbox are all grayed out in VS2013.
When I ran the code, I still got an empty form.
I tried the project at http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET
I can get the chart even though all components in my toolbox are also all grayed out in VS2013.