Control ZendGraph en C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 137

Line & Symbol Charts

Multi-Y Demo (C#, VB)

A Graph that demonstrates the use of multiple Y axes on a single graphpane.

Axis Cross Demo

A Graph that shows how to move the intersection of the axes to a location
other than the bottom left corner.

Point Label Demo

Demonstrates how to add a text label to each point on a line graph.

Polar Plot Demo

Shows how you can plot in polar coordinates, and simulate polar scale
decorations
Scatter Plot Demo

Shows a basic scatter plot (line graph with symbols only).

BaseTic Demo

Shows how to make the major tics begin at a point beyond the beginning
of the axis. For example, an axis that ranges from 0 to 100, but the major
tic labels are at 50, 150, 250, etc.

Dual-Y Demo

Shows how to display a Y-axis on both the left and right side of the graph.

Filled Curve Demo


Shows how to fill the area underneath a curve with a colored gradient.

Gradient-By-Value Demo (C#, VB)

Shows how to fill individual points with a color that varies according
to a data value.

Line Stack Demo

Shows how to stack curves so the values are summed, and to fill the area
between the curves with a color gradient.

Line With Band Demo (C#, VB)

Show a colored band on the graph to highlight a range on the scale.

Smooth Chart Demo


Demonstrates the use of line smoothing. Four curves are drawn which all
use the same data points, but different rendering options (smooth,
non-smooth, forward step, and rearward step).

Step Chart Demo

Shows a step chart, filled with a gradient.


Multi-Y Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl instance
public void CreateChart( ZedGraphControl zgc )
{
// Get a reference to the GraphPane
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Demonstration of Multi Y Graph";
myPane.XAxis.Title.Text = "Time, s";
myPane.YAxis.Title.Text = "Velocity, m/s";
myPane.Y2Axis.Title.Text = "Acceleration, m/s2";

// Make up some data points based on the Sine function


PointPairList vList = new PointPairList();
PointPairList aList = new PointPairList();
PointPairList dList = new PointPairList();
PointPairList eList = new PointPairList();

// Fabricate some data values


for ( int i=0; i<30; i++ )
{
double time = (double) i;
double acceleration = 2.0;
double velocity = acceleration * time;
double distance = acceleration * time * time / 2.0;
double energy = 100.0 * velocity * velocity / 2.0;
aList.Add( time, acceleration );
vList.Add( time, velocity );
eList.Add( time, energy );
dList.Add( time, distance );
}

// Generate a red curve with diamond symbols, and "Velocity" in the


legend
LineItem myCurve = myPane.AddCurve( "Velocity",
vList, Color.Red, SymbolType.Diamond );
// Fill the symbols with white
myCurve.Symbol.Fill = new Fill( Color.White );

// Generate a blue curve with circle symbols, and "Acceleration" in


the legend
myCurve = myPane.AddCurve( "Acceleration",
aList, Color.Blue, SymbolType.Circle );
// Fill the symbols with white
myCurve.Symbol.Fill = new Fill( Color.White );
// Associate this curve with the Y2 axis
myCurve.IsY2Axis = true;

// Generate a green curve with square symbols, and "Distance" in the


legend
myCurve = myPane.AddCurve( "Distance",
dList, Color.Green, SymbolType.Square );
// Fill the symbols with white
myCurve.Symbol.Fill = new Fill( Color.White );
// Associate this curve with the second Y axis
myCurve.YAxisIndex = 1;

// Generate a Black curve with triangle symbols, and "Energy" in the


legend
myCurve = myPane.AddCurve( "Energy",
eList, Color.Black, SymbolType.Triangle );
// Fill the symbols with white
myCurve.Symbol.Fill = new Fill( Color.White );
// Associate this curve with the Y2 axis
myCurve.IsY2Axis = true;
// Associate this curve with the second Y2 axis
myCurve.YAxisIndex = 1;

// Show the x axis grid


myPane.XAxis.MajorGrid.IsVisible = true;

// Make the Y axis scale red


myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
// turn off the opposite tics so the Y tics don't show up on the Y2
axis
myPane.YAxis.MajorTic.IsOpposite = false;
myPane.YAxis.MinorTic.IsOpposite = false;
// Don't display the Y zero line
myPane.YAxis.MajorGrid.IsZeroLine = false;
// Align the Y axis labels so they are flush to the axis
myPane.YAxis.Scale.Align = AlignP.Inside;
myPane.YAxis.Scale.Max = 100;

// Enable the Y2 axis display


myPane.Y2Axis.IsVisible = true;
// Make the Y2 axis scale blue
myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
// turn off the opposite tics so the Y2 tics don't show up on the Y
axis
myPane.Y2Axis.MajorTic.IsOpposite = false;
myPane.Y2Axis.MinorTic.IsOpposite = false;
// Display the Y2 axis grid lines
myPane.Y2Axis.MajorGrid.IsVisible = true;
// Align the Y2 axis labels so they are flush to the axis
myPane.Y2Axis.Scale.Align = AlignP.Inside;
myPane.Y2Axis.Scale.Min = 1.5;
myPane.Y2Axis.Scale.Max = 3;

// Create a second Y Axis, green


YAxis yAxis3 = new YAxis( "Distance, m" );
myPane.YAxisList.Add( yAxis3 );
yAxis3.Scale.FontSpec.FontColor = Color.Green;
yAxis3.Title.FontSpec.FontColor = Color.Green;
yAxis3.Color = Color.Green;
// turn off the opposite tics so the Y2 tics don't show up on the Y
axis
yAxis3.MajorTic.IsInside = false;
yAxis3.MinorTic.IsInside = false;
yAxis3.MajorTic.IsOpposite = false;
yAxis3.MinorTic.IsOpposite = false;
// Align the Y2 axis labels so they are flush to the axis
yAxis3.Scale.Align = AlignP.Inside;

Y2Axis yAxis4 = new Y2Axis( "Energy" );


yAxis4.IsVisible = true;
myPane.Y2AxisList.Add( yAxis4 );
// turn off the opposite tics so the Y2 tics don't show up on the Y
axis
yAxis4.MajorTic.IsInside = false;
yAxis4.MinorTic.IsInside = false;
yAxis4.MajorTic.IsOpposite = false;
yAxis4.MinorTic.IsOpposite = false;
// Align the Y2 axis labels so they are flush to the axis
yAxis4.Scale.Align = AlignP.Inside;
yAxis4.Type = AxisType.Log;
yAxis4.Scale.Min = 100;

// Fill the axis background with a gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0f );

zgc.AxisChange();
}
Axis Cross Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Axis Cross Demo";
myPane.XAxis.Title.Text = "My X Axis";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some data arrays based on the Sine function


double x, y;
PointPairList list = new PointPairList();
for ( int i=0; i<37; i++ )
{
x = ((double) i - 18.0 ) / 5.0;
y = x * x + 1.0;
list.Add( x, y );
}

// Generate a red curve with diamond


// symbols, and "Porsche" in the legend
LineItem myCurve = myPane.AddCurve( "Parabola",
list, Color.Green, SymbolType.Diamond );

// Set the Y axis intersect the X axis at an X value of 0.0


myPane.YAxis.Cross = 0.0;
// Turn off the axis frame and all the opposite side tics
myPane.Chart.Border.IsVisible = false;
myPane.XAxis.MajorTic.IsOpposite = false;
myPane.XAxis.MinorTic.IsOpposite = false;
myPane.YAxis.MajorTic.IsOpposite = false;
myPane.YAxis.MinorTic.IsOpposite = false;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Point Label Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Demo of Labeled Points";
myPane.XAxis.Title.Text = "Time, Seconds";
myPane.YAxis.Title.Text = "Pressure, Psia";

// Build a PointPairList with points based on Sine wave


PointPairList list = new PointPairList();
const int count = 15;
for ( int i = 0; i < count; i++ )
{
double x = i + 1;
double y = 21.1 * ( 1.0 + Math.Sin( (double)i * 0.15 ) );
list.Add( x, y );
}

// Hide the legend


myPane.Legend.IsVisible = false;
// Add a curve
LineItem curve = myPane.AddCurve( "label", list, Color.Red,
SymbolType.Circle );
curve.Line.Width = 2.0F;
curve.Line.IsAntiAlias = true;
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 7;

// Fill the axis background with a gradient


myPane.Chart.Fill = new Fill( Color.White, Color.FromArgb( 255,
Color.ForestGreen ), 45.0F );
// Offset Y space between point and label
// NOTE: This offset is in Y scale units, so it depends on your actual
data
const double offset = 1.0;

// Loop to add text labels to the points


for ( int i = 0; i < count; i++ )
{
// Get the pointpair
PointPair pt = curve.Points[i];
// Create a text label from the Y data value
TextObj text = new TextObj( pt.Y.ToString( "f2" ), pt.X, pt.Y +
offset,
CoordType.AxisXYScale, AlignH.Left, AlignV.Center );
text.ZOrder = ZOrder.A_InFront;
// Hide the border and the fill
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
//text.FontSpec.Fill = new Fill( Color.FromArgb( 100,
Color.White ) );
// Rotate the text to 90 degrees
text.FontSpec.Angle = 90;
myPane.GraphObjList.Add( text );
}

// Leave some extra space on top for the labels to fit within the chart
rect
myPane.YAxis.Scale.MaxGrace = 0.2;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Polar Plot Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
private void CreateGraph(ZedGraphControl zgc)
{
//parameter setup:
int noOfScaleCircles = 3;
double scaleDiam = 30;

// get a reference to the GraphPane


GraphPane myPane = zgc.GraphPane;

// Set the Titles and axis proprieties

myPane.Title.Text = "My Polar Graph";


myPane.XAxis.Title.Text = "";
myPane.YAxis.Title.Text = "";
myPane.XAxis.Cross = 0;
myPane.YAxis.Cross = 0;

myPane.XAxis.MajorTic.IsAllTics = true;
myPane.XAxis.MinorTic.IsAllTics = true;
myPane.YAxis.MajorTic.IsAllTics = true;
myPane.YAxis.MinorTic.IsAllTics = true;
myPane.XAxis.Scale.IsVisible = false;
myPane.YAxis.Scale.IsVisible = false;

myPane.XAxis.Scale.Min = -35;
myPane.XAxis.Scale.Max = 35;
myPane.YAxis.Scale.Max = 35;
myPane.YAxis.Scale.Min = -35;

// Render the simulated polar decorations:


RadarPointList[] scaleCircleList = new
RadarPointList[noOfScaleCircles];
LineItem[] scaleCircle = new LineItem[noOfScaleCircles];
double delta = (double) scaleDiam / noOfScaleCircles;

for (int j = 0; j < noOfScaleCircles; j++)


{
scaleCircleList[j] = new RadarPointList();
for (int i = 0; i < 20; i++)
{
scaleCircleList[j].Add(scaleDiam, 1);
}

scaleCircle[j] = myPane.AddCurve("",
scaleCircleList[j], Color.Black, SymbolType.None);
scaleCircle[j].Line.IsSmooth = true;
scaleCircle[j].Line.SmoothTension = 0.6f;
scaleCircle[j].Line.Style = DashStyle.Custom;
scaleCircle[j].Line.DashOff = 2;
scaleCircle[j].Line.DashOn = 4;

scaleDiam = scaleDiam - delta;

}
// Render the "rays" from the center
for (int j = 0; j < 20; j++)
{
LineObj line = new ArrowObj(Color.Black, 0, 0, 0,
(float)scaleCircleList[0][j].X, (float)scaleCircleList[0][j].Y);
line.Line.Style = DashStyle.Custom;
line.Line.DashOn = 1;
line.Line.DashOff = 4;
myPane.GraphObjList.Add(line);
}

// This is some random plot data


RadarPointList dataList = new RadarPointList();
Random rnd = new Random();
for (int i = 0; i < 16; i++)
{

double x =20+rnd.Next()%5;
dataList.Add(x, 1);
}
LineItem data = myPane.AddCurve("Data", dataList,
Color.Navy, SymbolType.Circle);
data.Line.IsSmooth = true;
data.Line.SmoothTension = 0.6f;
data.Line.Width = 2;

zgc.AxisChange();
}
Scatter Plot Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles


myPane.Title.Text = "Scatter Plot Demo";
myPane.XAxis.Title.Text = "Pressure, Atm";
myPane.YAxis.Title.Text = "Flow Rate, cc/hr";

// Get a random number generator


Random rand = new Random();

// Populate a PointPairList with a log-based function and some random


variability
PointPairList list = new PointPairList();
for ( int i = 0; i < 200; i++ )
{
double x = rand.NextDouble() * 20.0 + 1;
double y = Math.Log( 10.0 * ( x - 1.0 ) + 1.0 ) * ( rand.NextDouble()
* 0.2 + 0.9 );
list.Add( x, y );
}

// Add the curve


LineItem myCurve = myPane.AddCurve( "Performance", list, Color.Black,
SymbolType.Diamond );
// Don't display the line (This makes a scatter plot)
myCurve.Line.IsVisible = false;
// Hide the symbol outline
myCurve.Symbol.Border.IsVisible = false;
// Fill the symbol interior with color
myCurve.Symbol.Fill = new Fill( Color.Firebrick );

// Fill the background of the chart rect and pane


myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0f );
myPane.Fill = new Fill( Color.White, Color.SlateGray, 45.0f );

zgc.AxisChange();
}
BaseTic Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Demo of BaseTic property";
myPane.XAxis.Title.Text = "Time, Days";
myPane.YAxis.Title.Text = "Widget Production (units/hour)";

// Build a PointPairList with points based on Sine wave


PointPairList list = new PointPairList();
for ( double i=0; i<36; i++ )
{
double x = i * 10.0 + 50.0;
double y = Math.Sin( i * Math.PI / 15.0 ) * 16.0;
list.Add( x, y );
}

// Hide the legend


myPane.Legend.IsVisible = false;
// Add a curve
LineItem curve = myPane.AddCurve( "label", list, Color.Red,
SymbolType.Circle );
curve.Line.Width = 1.5F;
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 5;

// Make the XAxis start with the first label at 50


myPane.XAxis.Scale.BaseTic = 50;

// Fill the axis background with a gradient


myPane.Chart.Fill = new Fill( Color.White, Color.SteelBlue, 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();

// Refresh to paint the graph components


Refresh();
}
Dual-Y Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Demonstration of Dual Y Graph";
myPane.XAxis.Title.Text = "Time, Days";
myPane.YAxis.Title.Text = "Parameter A";
myPane.Y2Axis.Title.Text = "Parameter B";

// Make up some data points based on the Sine function


PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();
for ( int i=0; i<36; i++ )
{
double x = (double) i * 5.0;
double y = Math.Sin( (double) i * Math.PI / 15.0 ) * 16.0;
double y2 = y * 13.5;
list.Add( x, y );
list2.Add( x, y2 );
}

// Generate a red curve with diamond symbols, and "Alpha" in the legend
LineItem myCurve = myPane.AddCurve( "Alpha",
list, Color.Red, SymbolType.Diamond );
// Fill the symbols with white
myCurve.Symbol.Fill = new Fill( Color.White );

// Generate a blue curve with circle symbols, and "Beta" in the legend
myCurve = myPane.AddCurve( "Beta",
list2, Color.Blue, SymbolType.Circle );
// Fill the symbols with white
myCurve.Symbol.Fill = new Fill( Color.White );
// Associate this curve with the Y2 axis
myCurve.IsY2Axis = true;

// Show the x axis grid


myPane.XAxis.MajorGrid.IsVisible = true;

// Make the Y axis scale red


myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
// turn off the opposite tics so the Y tics don't show up on the Y2
axis
myPane.YAxis.MajorTic.IsOpposite = false;
myPane.YAxis.MinorTic.IsOpposite = false;
// Don't display the Y zero line
myPane.YAxis.MajorGrid.IsZeroLine = false;
// Align the Y axis labels so they are flush to the axis
myPane.YAxis.Scale.Align = AlignP.Inside;
// Manually set the axis range
myPane.YAxis.Scale.Min = -30;
myPane.YAxis.Scale.Max = 30;

// Enable the Y2 axis display


myPane.Y2Axis.IsVisible = true;
// Make the Y2 axis scale blue
myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
// turn off the opposite tics so the Y2 tics don't show up on the Y
axis
myPane.Y2Axis.MajorTic.IsOpposite = false;
myPane.Y2Axis.MinorTic.IsOpposite = false;
// Display the Y2 axis grid lines
myPane.Y2Axis.MajorGrid.IsVisible = true;
// Align the Y2 axis labels so they are flush to the axis
myPane.Y2Axis.Scale.Align = AlignP.Inside;

// Fill the axis background with a gradient


myPane.Chart.Fill = new Fill( Color.White, Color.LightGray, 45.0f );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Filled Curve Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "My Test Date Graph";
myPane.XAxis.Title.Text = "Date";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some data points from the Sine function


PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();
for ( int i=0; i<36; i++ )
{
double x = new XDate( 1995, i+1, 1 );
double y = Math.Sin( (double) i * Math.PI / 15.0 );
double y2 = 2 * y;

list.Add( x, y );
list2.Add( x, y2 );
}

// Generate a blue curve with circle symbols, and "My Curve 2" in the
legend
LineItem myCurve2 = myPane.AddCurve( "My Curve 2", list, Color.Blue,
SymbolType.Circle );
// Fill the area under the curve with a white-red gradient at 45 degrees
myCurve2.Line.Fill = new Fill( Color.White, Color.Red, 45F );
// Make the symbols opaque by filling them with white
myCurve2.Symbol.Fill = new Fill( Color.White );

// Generate a red curve with diamond symbols, and "My Curve" in the
legend
LineItem myCurve = myPane.AddCurve( "My Curve",
list2, Color.MediumVioletRed, SymbolType.Diamond );
// Fill the area under the curve with a white-green gradient
myCurve.Line.Fill = new Fill( Color.White, Color.Green );
// Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = new Fill( Color.White );

// Set the XAxis to date type


myPane.XAxis.Type = AxisType.Date;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Gradient-By-Value Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "PVT Properties";
myPane.XAxis.Title.Text = "Pressure (atm)";
myPane.YAxis.Title.Text = "Temperature (C)";

// Enter some calculated data constants


PointPairList list1 = new PointPairList();

list1.Add( 13.03, 59.26, 31.67 );


list1.Add( 18.73, 75.62, 23.34 );
list1.Add( 16.94, 11.09, 21.04 );
list1.Add( 11.66, 29.68, 32.19 );
list1.Add( 12.94, 27.73, 28.94 );
list1.Add( 18.04, 54.71, 22.76 );
list1.Add( 17.36, 66.92, 24.51 );
list1.Add( 13.19, 27.65, 28.39 );
list1.Add( 12.79, 41.93, 30.6 );
list1.Add( 19.66, 52.21, 20.79 );
list1.Add( 17.17, 41.82, 22.98 );
list1.Add( 12.57, 25.59, 29.53 );
list1.Add( 18.74, 45.86, 21.36 );
list1.Add( 12.66, 8.73, 27.71 );
list1.Add( 13.29, 26.49, 28.07 );
list1.Add( 12.47, 22.58, 29.48 );
list1.Add( 14.76, 38.87, 26.36 );
list1.Add( 22.54, 75.93, 19.5 );
list1.Add( 14.38, 72.0, 29.88 );
list1.Add( 12.01, 12.95, 29.58 );
list1.Add( 14.96, 8.99, 23.6 );
list1.Add( 17.58, 88.99, 25.76 );
list1.Add( 15.4, 34.66, 24.97 );
list1.Add( 15.39, 19.74, 23.79 );
list1.Add( 19.88, 43.63, 20.03 );
list1.Add( 12.42, 26.17, 29.93 );
list1.Add( 12.91, 21.17, 28.37 );
list1.Add( 16.48, 62.67, 25.47 );
list1.Add( 12.05, 21.33, 30.36 );
list1.Add( 17.4, 95.91, 26.52 );
list1.Add( 17.92, 63.01, 23.51 );
list1.Add( 15.74, 21.08, 23.4 );
list1.Add( 14.57, 20.99, 25.21 );
list1.Add( 16.08, 30.12, 23.58 );
list1.Add( 17.44, 31.23, 21.9 );
list1.Add( 15.79, 44.7, 25.19 );
list1.Add( 13.67, 42.4, 28.74 );
list1.Add( 15.98, 55.96, 25.74 );
list1.Add( 15.41, 33.06, 24.83 );
list1.Add( 14.37, 41.31, 27.28 );
list1.Add( 13.99, 59.27, 29.58 );
list1.Add( 11.43, 47.13, 34.65 );
list1.Add( 15.21, 46.2, 26.21 );
list1.Add( 20.08, 59.66, 20.82 );
list1.Add( 16.45, 42.42, 24 );
list1.Add( 11.76, 6.28, 29.51 );
list1.Add( 18.66, 62.34, 22.56 );
list1.Add( 14.91, 32.14, 25.58 );
list1.Add( 14.2, 50.85, 28.43 );
list1.Add( 14.99, 46.38, 26.6 );
list1.Add( 23.55, 80.77, 18.93 );
list1.Add( 13.4, 21.14, 27.34 );
list1.Add( 15.39, 33.16, 24.88 );
list1.Add( 20.4, 38.22, 19.21 );
list1.Add( 13.63, 58.78, 30.28 );
list1.Add( 13.23, 70.03, 32.2 );
list1.Add( 15.25, 39.34, 25.62 );
list1.Add( 14.92, 13.68, 24.04 );
list1.Add( 23.81, 85.41, 18.93 );
list1.Add( 18.5, 28.86, 20.48 );
list1.Add( 14.11, 98.51, 32.72 );
list1.Add( 17.22, 94.7, 26.68 );
list1.Add( 22.94, 87.21, 19.74 );
list1.Add( 12.38, 72.77, 34.61 );
list1.Add( 13.26, 71.69, 32.26 );
list1.Add( 15.07, 5.37, 23.11 );
list1.Add( 16.2, 70.1, 26.45 );
list1.Add( 12.81, 16.61, 28.14 );
list1.Add( 11.63, 10.06, 30.23 );
list1.Add( 13.84, 85.76, 32.23 );
list1.Add( 14.8, 6.93, 23.67 );
list1.Add( 13.6, 26.68, 27.5 );
list1.Add( 13.65, 63.22, 30.64 );
list1.Add( 24.85, 97.2, 18.77 );
list1.Add( 13.8, 44.99, 28.71 );
list1.Add( 20.02, 96.99, 23.19 );
list1.Add( 16.91, 50.47, 23.94 );
list1.Add( 11.94, 32.23, 31.72 );
list1.Add( 19.97, 77.09, 22 );
list1.Add( 14.21, 98.48, 32.49 );
list1.Add( 17.66, 38.76, 22.15 );
list1.Add( 17.23, 42.5, 22.96 );
list1.Add( 14.43, 8.97, 24.43 );
list1.Add( 14.94, 60.76, 27.87 );

// Generate a red curve with diamond symbols, and "Gas Data" in the
legend
LineItem myCurve = myPane.AddCurve( "Gas Data", list1, Color.Red,
SymbolType.Diamond );
myCurve.Symbol.Size = 12;
// Set up a red-blue color gradient to be used for the fill
myCurve.Symbol.Fill = new Fill( Color.Red, Color.Blue );
// Turn off the symbol borders
myCurve.Symbol.Border.IsVisible = false;
// Instruct ZedGraph to fill the symbols by selecting a color out of
the
// red-blue gradient based on the Z value. A value of 19 or less will
be red,
// a value of 34 or more will be blue, and values in between will be
a
// linearly apportioned color between red and blue.
myCurve.Symbol.Fill.Type = FillType.GradientByZ;
myCurve.Symbol.Fill.RangeMin = 19;
myCurve.Symbol.Fill.RangeMax = 34;
// Turn off the line, so the curve will by symbols only
myCurve.Line.IsVisible = false;

// Display a text item with "MW = 34" on the graph


TextObj text = new TextObj( "MW = 34", 12.9F, 110,
CoordType.AxisXYScale );
text.FontSpec.FontColor = Color.Blue;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
text.FontSpec.Size = 14;
myPane.GraphObjList.Add( text );

// Display a text item with "MW = 19" on the graph


text = new TextObj( "MW = 19", 25, 110, CoordType.AxisXYScale );
text.FontSpec.FontColor = Color.Red;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
text.FontSpec.Size = 14;
myPane.GraphObjList.Add( text );

// Show the X and Y grids


myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;

// Set the x and y scale and title font sizes to 14


myPane.XAxis.Scale.FontSpec.Size = 14;
myPane.XAxis.Title.FontSpec.Size = 14;
myPane.YAxis.Scale.FontSpec.Size = 14;
myPane.YAxis.Title.FontSpec.Size = 14;
// Set the GraphPane title font size to 16
myPane.Title.FontSpec.Size = 16;
// Turn off the legend
myPane.Legend.IsVisible = false;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White, Color.FromArgb( 255, 255,
166), 90F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Line Stack Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Wacky Widget Company\nProduction Report";
myPane.XAxis.Title.Text = "Time, Days\n(Since Plant Construction
Startup)";
myPane.YAxis.Title.Text = "Widget Production\n(units/hour)";

// enter some arbitrary data points


double[] x = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
double[] y = { 20, 10, 50, 25, 35, 75, 90, 40, 33, 50 };

// Add a green curve


LineItem curve;
curve = myPane.AddCurve( "Larry", x, y, Color.Green,
SymbolType.Circle );
curve.Line.Width = 1.5F;
// Make the curve smooth with cardinal splines
curve.Line.IsSmooth = true;
curve.Line.SmoothTension = 0.6F;
// Fill the symbols with white to make them opaque
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 10;

// Add a second curve


double[] y3 = { 5.2, 49.0, 33.8, 88.57, 99.9, 36.8, 22.1, 34.3, 10.4,
17.5 };
curve = myPane.AddCurve( "Moe", x, y3, Color.FromArgb( 200, 55, 135),
SymbolType.Triangle );
curve.Line.Width = 1.5F;
// Fill the symbols with white to make them opaque
curve.Symbol.Fill = new Fill( Color.White );
// Fill the area between the curves with color
curve.Line.Fill = new Fill( Color.White, Color.FromArgb( 160, 230, 145,
205), 90F );
curve.Symbol.Size = 10;

// Fill the pane background with a color gradient


myPane.Fill = new Fill( Color.WhiteSmoke, Color.Lavender, 0F );

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.FromArgb( 255, 255, 245),
Color.FromArgb( 255, 255, 190), 90F );

// Show the x and y axis gridlines


myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;

// Use the stacked curve type so the curve values sum up


// this also causes only the area between the curves to be filled,
rather than
// the area between each curve and the x axis
myPane.LineType = LineType.Stack;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Line With Band Demo

Sample Code in C#

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Line Graph with Band Demo";
myPane.XAxis.Title.Text = "Sequence";
myPane.YAxis.Title.Text = "Temperature, C";

// Enter some random data values


double[] y = { 100, 115, 75, 22, 98, 40, 10 };
double[] y2 = { 90, 100, 95, 35, 80, 35, 35 };
double[] y3 = { 80, 110, 65, 15, 54, 67, 18 };
double[] x = { 100, 200, 300, 400, 500, 600, 700 };

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.FromArgb( 255, 255, 245 ),
Color.FromArgb( 255, 255, 190 ), 90F );

// Generate a red curve with "Curve 1" in the legend


LineItem myCurve = myPane.AddCurve( "Curve 1", x, y, Color.Red );
// Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = new Fill( Color.White );

// Generate a blue curve with "Curve 2" in the legend


myCurve = myPane.AddCurve( "Curve 2", x, y2, Color.Blue );
// Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = new Fill( Color.White );

// Generate a green curve with "Curve 3" in the legend


myCurve = myPane.AddCurve( "Curve 3", x, y3, Color.Green );
// Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = new Fill( Color.White );

// Manually set the x axis range


myPane.XAxis.Scale.Min = 0;
myPane.XAxis.Scale.Max = 800;
// Display the Y axis grid lines
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MinorGrid.IsVisible = true;

// Draw a box item to highlight a value range


BoxObj box = new BoxObj( 0, 100, 1, 30, Color.Empty,
Color.FromArgb( 150, Color.LightGreen ) );
box.Fill = new Fill( Color.White, Color.FromArgb( 200,
Color.LightGreen ), 45.0F );
// Use the BehindAxis zorder to draw the highlight beneath the grid
lines
box.ZOrder = ZOrder.E_BehindCurves;
// Make sure that the boxObj does not extend outside the chart rect
if the chart is zoomed
box.IsClippedToChartRect = true;
// Use a hybrid coordinate system so the X axis always covers the full
x range
// from chart fraction 0.0 to 1.0
box.Location.CoordinateFrame = CoordType.XChartFractionYScale;
myPane.GraphObjList.Add( box );

// Add a text item to label the highlighted range


TextObj text = new TextObj( "Optimal\nRange", 0.95f, 85,
CoordType.AxisXYScale,
AlignH.Right, AlignV.Center );
text.FontSpec.Fill.IsVisible = false;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.IsBold = true;
text.FontSpec.IsItalic = true;
text.Location.CoordinateFrame = CoordType.XChartFractionYScale;
text.IsClippedToChartRect = true;
myPane.GraphObjList.Add( text );

// Fill the pane background with a gradient


myPane.Fill = new Fill( Color.WhiteSmoke, Color.Lavender, 0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Smooth Chart Demo v4

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// set the title and axis labels


myPane.Title = "Smooth Line Demo";
myPane.XAxis.Title = "Value";
myPane.YAxis.Title = "Time";

// Enter some arbitrary data


double[] x = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
double[] y = { 20, 10, 50, 25, 35, 75, 90, 40, 33, 50 };

// Add a smoothed curve


LineItem curve = myPane.AddCurve( "Smooth (Tension=0.5)", x, y,
Color.Red, SymbolType.Diamond );
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 5;
// activate the cardinal spline smoothing
curve.Line.IsSmooth = true;
curve.Line.SmoothTension = 0.5F;

// Add a forward step type curve


curve = myPane.AddCurve( "Forward Step", x, y, Color.Green,
SymbolType.Circle );
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 5;
curve.Line.StepType = StepType.ForwardStep;

// add a rearward step type curve


curve = myPane.AddCurve( "Rearward Step", x, y, Color.Gold,
SymbolType.Square );
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 5;
curve.Line.StepType = StepType.RearwardStep;

// add a regular non-step, non-smooth curve


curve = myPane.AddCurve( "Non-Step", x, y, Color.Blue,
SymbolType.Triangle );
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 5;

// Fill the axis background with a color gradient


myPane.AxisFill = new Fill( Color.White, Color.LightGray, 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Step Chart Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Demo for Step Charts";
myPane.XAxis.Title.Text = "Time, Days";
myPane.YAxis.Title.Text = "Widget Production (units/hour)";

// Generate some sine-based data values


PointPairList list = new PointPairList();
for ( double i=0; i<36; i++ )
{
double x = i * 5.0;
double y = Math.Sin( i * Math.PI / 15.0 ) * 16.0;
list.Add( x, y );
}

// Add a red curve with circle symbols


LineItem curve = myPane.AddCurve( "Step", list, Color.Red,
SymbolType.Circle );
curve.Line.Width = 1.5F;
// Fill the area under the curve
curve.Line.Fill = new Fill( Color.White, Color.FromArgb( 60, 190, 50),
90F );
// Fill the symbols with white to make them opaque
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 5;

// Set the curve type to forward steps


curve.Line.StepType = StepType.ForwardStep;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Bar Charts
Japanese Candlestick Demo (C#, VB)

Demonstrates the use of the Japanese Candlestick bar chart type for
displaying daily stock data with open, high, low, close data.

Open-High-Low-Close Demo

Demonstrates the use of the OHLC (Open-High-Low-Close) bar chart type for
displaying daily stock data with open, high, low, close data. Also shows
the use of "GradientByColorValue" to color the bars depending on up days
or down days.

Multi-Colored Bar Demo (C#, VB)

Demonstrates using the Gradient-By-Value Fill type to show multi-colored


bars with a single CurveItem.

Stacked Bar With Labels Demo (C#, VB)

Demonstrates a horizontal stacked bar graph with a label inside each bar
showing its value.
Vertical Bar With Labels Demo

Shows a clustered, vertical bar chart with a value label placed above each
bar.

Horizontal Bar With Labels Demo

Shows a clustered, horizontal bar chart with a value label placed to the
right of each bar.

ErrorBar Demo

Demonstrates the use of the ErrorBar graph type.

Filled Bar Demo


Shows how to fill the bars on a bar chart with an image, rather than the
standard gradient fill.

HiLowBar Demo

Demonstrates the use of the HiLowBar graph type.

Hi-Low-Close Demo

Combines a HiLowBar and a regular symbol graph to generate a


High-Low-Close stock chart.

Horizontal Bar Demo

Shows a clustered, horizontal bar chart with some negative bars.

Horizontal Stacked Bar Demo


Shows a stacked, horizontal bar chart.

Overlay Bar Demo

Demonstrates the overlay bar chart type, which is similar to a stacked


bar, but the values are not accumulated -- the bars are just drawn on top
of eachother.

Percent Stack Bar Demo

Demonstrates the percent stacked bar graph type.

Sine Bar Demo : A simple bar graph demo.

Sorted Overlay Bar Demo


Demonstrates the sorted overlay bar chart type, which is similar to a
stacked bar, but the values are not accumulated -- the bars are just drawn
on top of eachother with the shortest bar in front and the taller bars
behind in order of increasing bar height.
Japanese Candlestick Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Japanese Candlestick Chart Demo";
myPane.XAxis.Title.Text = "Trading Date";
myPane.YAxis.Title.Text = "Share Price, $US";

StockPointList spl = new StockPointList();


Random rand = new Random();

// First day is jan 1st


XDate xDate = new XDate( 2006, 1, 1 );
double open = 50.0;

for ( int i = 0; i < 50; i++ )


{
double x = xDate.XLDate;
double close = open + rand.NextDouble() * 10.0 - 5.0;
double hi = Math.Max( open, close ) + rand.NextDouble() * 5.0;
double low = Math.Min( open, close ) - rand.NextDouble() * 5.0;

StockPt pt = new StockPt( x, hi, low, open, close, 100000 );


spl.Add( pt );

open = close;
// Advance one day
xDate.AddDays( 1.0 );
// but skip the weekends
if ( XDate.XLDateToDayOfWeek( xDate.XLDate ) == 6 )
xDate.AddDays( 2.0 );
}

JapaneseCandleStickItem myCurve =
myPane.AddJapaneseCandleStick( "trades", spl );
myCurve.Stick.IsAutoSize = true;
myCurve.Stick.Color = Color.Blue;

// Use DateAsOrdinal to skip weekend gaps


myPane.XAxis.Type = AxisType.DateAsOrdinal;
myPane.XAxis.Scale.Min = new XDate( 2006, 1, 1 );

// pretty it up a little
myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0f );
myPane.Fill = new Fill( Color.White, Color.FromArgb( 220, 220, 255 ),
45.0f );

// Tell ZedGraph to calculate the axis ranges


zgc.AxisChange();
zgc.Invalidate();

}
Open-High-Low-Close Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "OHLC Chart Demo";
myPane.XAxis.Title.Text = "Date";
myPane.YAxis.Title.Text = "Price";

// Load a StockPointList with random data...


StockPointList spl = new StockPointList();
Random rand = new Random();

// First day is jan 1st


XDate xDate = new XDate( 2006, 1, 1 );
double open = 50.0;
double prevClose = 0;

// Loop to make 50 days of data


for ( int i = 0; i < 50; i++ )
{
double x = xDate.XLDate;
double close = open + rand.NextDouble() * 10.0 - 5.0;
double hi = Math.Max( open, close ) + rand.NextDouble() * 5.0;
double low = Math.Min( open, close ) - rand.NextDouble() * 5.0;

// Create a StockPt instead of a PointPair so we can carry 6


properties
StockPt pt = new StockPt( x, hi, low, open, close, 100000 );

//if price is increasing color=black, else color=red


pt.ColorValue = close > prevClose ? 2 : 1;
spl.Add( pt );

prevClose = close;
open = close;
// Advance one day
xDate.AddDays( 1.0 );
// but skip the weekends
if ( XDate.XLDateToDayOfWeek( xDate.XLDate ) == 6 )
xDate.AddDays( 2.0 );
}

// Setup the gradient fill...


// Use Red for negative days and black for positive days
Color[] colors = { Color.Red, Color.Black };
Fill myFill = new Fill( colors );
myFill.Type = FillType.GradientByColorValue;
myFill.SecondaryValueGradientColor = Color.Empty;
myFill.RangeMin = 1;
myFill.RangeMax = 2;

//Create the OHLC and assign it a Fill


OHLCBarItem myCurve = myPane.AddOHLCBar( "Price", spl,
Color.Empty );
myCurve.Bar.GradientFill = myFill;
myCurve.Bar.IsAutoSize = true;

// Use DateAsOrdinal to skip weekend gaps


myPane.XAxis.Type = AxisType.DateAsOrdinal;
//myPane.XAxis.Scale.Min = new XDate( 2006, 1, 1 );

// pretty it up a little
myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0f );
myPane.Fill = new Fill( Color.White, Color.FromArgb( 220, 220, 255 ),
45.0f );
myPane.Title.FontSpec.Size = 20.0f;
myPane.XAxis.Title.FontSpec.Size = 18.0f;
myPane.XAxis.Scale.FontSpec.Size = 16.0f;
myPane.YAxis.Title.FontSpec.Size = 18.0f;
myPane.YAxis.Scale.FontSpec.Size = 16.0f;
myPane.Legend.IsVisible = false;

// Tell ZedGraph to calculate the axis ranges


zgc.AxisChange();
}
Multi-Colored Bar Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
private void CreateGraph_GradientByZBars( ZedGraphControl z1 )
{
GraphPane myPane = z1.GraphPane;
myPane.Title.Text = "Demonstration of Multi-Colored Bars with a Single
BarItem";
myPane.XAxis.Title.Text = "Bar Number";
myPane.YAxis.Title.Text = "Value";

PointPairList list = new PointPairList();


Random rand = new Random();

for ( int i = 0; i < 16; i++ )


{
double x = (double)i+1;
double y = rand.NextDouble() * 1000;
double z = i / 4.0;
list.Add( x, y, z );
}

BarItem myCurve = myPane.AddBar( "Multi-Colored Bars", list,


Color.Blue );
Color[] colors = { Color.Red, Color.Yellow, Color.Green, Color.Blue,
Color.Purple };
myCurve.Bar.Fill = new Fill( colors );
myCurve.Bar.Fill.Type = FillType.GradientByZ;

myCurve.Bar.Fill.RangeMin = 0;
myCurve.Bar.Fill.RangeMax = 4;

myPane.Chart.Fill = new Fill( Color.White, Color.FromArgb( 220, 220,


255 ), 45 );
myPane.Fill = new Fill( Color.White, Color.FromArgb( 255, 255, 225 ),
45 );
// Tell ZedGraph to calculate the axis ranges
z1.AxisChange();
}
Stacked Bar With Labels Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Stacked Bars with Value Labels Inside Each Bar";
myPane.XAxis.Title.Text = "Position Number";
myPane.YAxis.Title.Text = "Some Random Thing";

// Create points for three BarItems


PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();

// Use random data values


Random rand = new Random();
for ( int i=1; i<5; i++ )
{
double y = (double) i;
double x1 = 100.0 + rand.NextDouble() * 100.0;
double x2 = 100.0 + rand.NextDouble() * 100.0;
double x3 = 100.0 + rand.NextDouble() * 100.0;

list1.Add( x1, y );
list2.Add( x2, y );
list3.Add( x3, y );
}

// Create the three BarItems, change the fill properties so the angle
is at 90
// degrees for horizontal bars
BarItem bar1 = myPane.AddBar( "Bar 1", list1, Color.Red );
bar1.Bar.Fill = new Fill( Color.Red, Color.White, Color.Red, 90 );
BarItem bar2 = myPane.AddBar( "Bar 2", list2, Color.Blue );
bar2.Bar.Fill = new Fill( Color.Blue, Color.White, Color.Blue, 90 );
BarItem bar3 = myPane.AddBar( "Bar 3", list3, Color.Green );
bar3.Bar.Fill = new Fill( Color.Green, Color.White, Color.Green, 90 );

// Set BarBase to the YAxis for horizontal bars


myPane.BarSettings.Base = BarBase.Y;
// Make the bars stack instead of cluster
myPane.BarSettings.Type = BarType.Stack;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166), 45.0F );

zgc.AxisChange();

// Create TextObj's to provide labels for each bar


BarItem.CreateBarLabels( myPane, true, "f0" );
}
Vertical Bar With Labels Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public 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" );

}
Horizontal Bar With Labels Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Horizontal Bars with Value Labels Above Each
Bar";
myPane.XAxis.Title.Text = "Some Random Thing";
myPane.YAxis.Title.Text = "Position Number";

// Create data points for three BarItems using Random data


PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();
Random rand = new Random();

for ( int i=0; i<4; i++ )


{
double y = (double) i+5;
double x = rand.NextDouble() * 1000;
double x2 = rand.NextDouble() * 1000;
double x3 = rand.NextDouble() * 1000;
list.Add( x, y );
list2.Add( x2, y );
list3.Add( x3, y );
}

// Create the three BarItems, change the fill properties so the angle
is at 90
// degrees for horizontal bars
BarItem myCurve = myPane.AddBar( "curve 1", list, Color.Blue );
myCurve.Bar.Fill = new Fill( Color.Blue, Color.White, Color.Blue,
90 );
BarItem myCurve2 = myPane.AddBar( "curve 2", list2, Color.Red );
myCurve2.Bar.Fill = new Fill( Color.Red, Color.White, Color.Red, 90 );
BarItem myCurve3 = myPane.AddBar( "curve 3", list3, Color.Green );
myCurve3.Bar.Fill = new Fill( Color.Green, Color.White, Color.Green,
90 );

// Set BarBase to the YAxis for horizontal bars


myPane.BarSettings.Base = BarBase.Y;

// Fill the chart background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166), 45.0F );

zgc.AxisChange();

// Create TextObj's to provide labels for each bar


BarItem.CreateBarLabels( myPane, false, "f0" );

}
ErrorBar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Error Bar Demo Chart";
myPane.XAxis.Title.Text = "Label";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some data points based on the Sine function


PointPairList list = new PointPairList();
for ( int i = 0; i < 44; i++ )
{
double x = i / 44.0;
double y = Math.Sin( (double) i * Math.PI / 15.0 );
double yBase = y - 0.4;
list.Add( x, y, yBase );
}
// Generate a red bar with "Curve 1" in the legend
ErrorBarItem myCurve = myPane.AddErrorBar( "Curve 1", list,
Color.Red );
// Make the X axis the base for this curve (this is the default)
myPane.BarSettings.Base = BarBase.X;
myCurve.Bar.PenWidth = 1f;
// Use the HDash symbol so that the error bars look like I-beams
myCurve.Bar.Symbol.Type = SymbolType.HDash;
myCurve.Bar.Symbol.Border.Width = .1f;
myCurve.Bar.Symbol.IsVisible = true;
myCurve.Bar.Symbol.Size = 4;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Filled Bar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Image Fill Example";
myPane.XAxis.Title.Text = "Region";
myPane.YAxis.Title.Text = "Astronomy Sector Sales";

// Make up some random data points


double[] y = { 80, 70, 65, 78, 40 };
double[] y2 = { 70, 50, 85, 54, 63 };
string[] str = { "North", "South", "West", "East", "Central" };

// Add a bar to the graph


BarItem myCurve = myPane.AddBar( "Curve 1", null, y, Color.White );
// Access an image from a file (use your own filename here)
Image image = Bitmap.FromFile( @"c:\temp\ngc4414.jpg" );
// create a brush with the image
TextureBrush brush = new TextureBrush( image );
// use the image for the bar fill
myCurve.Bar.Fill = new Fill( brush );
// turn off the bar border
myCurve.Bar.Border.IsVisible = false;

// Add a second bar to the graph


myCurve = myPane.AddBar( "Curve 2", null, y2, Color.White );
// Access an image from a file (use your own filename here)
Image image2 = Bitmap.FromFile( @"c:\temp\ngc4261.gif" );
// create a brush with the image
TextureBrush brush2 = new TextureBrush( image2 );
// use the image for the bar fill
myCurve.Bar.Fill = new Fill( brush2 );
// turn off the bar border
myCurve.Bar.Border.IsVisible = false;

// Draw the X tics between the labels instead of at the labels


myPane.XAxis.MajorTic.IsBetweenLabels = true;

// Set the XAxis labels


myPane.XAxis.Scale.TextLabels = str;

// Set the XAxis to Text type


myPane.XAxis.Type = AxisType.Text;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White, Color.SteelBlue, 45.0f );

// disable the legend


myPane.Legend.IsVisible = false;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
HiLowBar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Hi-Low Bar Graph Demo";
myPane.XAxis.Title.Text = "Event";
myPane.YAxis.Title.Text = "Range of Values";

// Make up some data points based on the Sine function


PointPairList list = new PointPairList();
for ( int i=1; i<45; i++ )
{
double y = Math.Sin( (double) i * Math.PI / 15.0 );
double yBase = y - 0.4;
list.Add( (double) i, y, yBase );
}

// Generate a red bar with "Curve 1" in the legend


HiLowBarItem myCurve = myPane.AddHiLowBar( "Curve 1", list,
Color.Red );
// Fill the bar with a red-white-red gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Red, Color.White, Color.Red, 0 );

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166), 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Hi-Low-Close Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "ZedgroSoft, International\nHi-Low-Close Daily
Stock Chart";
myPane.XAxis.Title.Text = "";
myPane.YAxis.Title.Text = "Trading Price, $US";

// Set the title font characteristics


myPane.Title.FontSpec.Family = "Arial";
myPane.Title.FontSpec.IsItalic = true;
myPane.Title.FontSpec.Size = 18;

// Generate some random stock price data


PointPairList hList = new PointPairList();
PointPairList cList = new PointPairList();
Random rand = new Random();
// initialize the starting close price
double close = 45;

for ( int i = 45; i < 65; i++ )


{
double x = (double)new XDate( 2004, 12, i - 30 );
close = close + 2.0 * rand.NextDouble() - 0.5;
double hi = close + 2.0 * rand.NextDouble();
double low = close - 2.0 * rand.NextDouble();
hList.Add( x, hi, low );
cList.Add( x, close );
}

// Make a new curve with a "Closing Price" label


LineItem curve = myPane.AddCurve( "Closing Price", cList,
Color.Black,
SymbolType.Diamond );
// Turn off the line display, symbols only
curve.Line.IsVisible = false;
// Fill the symbols with solid red color
curve.Symbol.Fill = new Fill( Color.Red );
curve.Symbol.Size = 7;

// Add a blue error bar to the graph


ErrorBarItem myCurve = myPane.AddErrorBar( "Price Range", hList,
Color.Blue );
myCurve.Bar.PenWidth = 3;
myCurve.Bar.Symbol.IsVisible = false;

// Set the XAxis to date type


myPane.XAxis.Type = AxisType.Date;
// X axis step size is 1 day
myPane.XAxis.Scale.MajorStep = 1;
myPane.XAxis.Scale.MajorUnit = DateUnit.Day;
// tilt the x axis labels to an angle of 65 degrees
myPane.XAxis.Scale.FontSpec.Angle = 65;
myPane.XAxis.Scale.FontSpec.IsBold = true;
myPane.XAxis.Scale.FontSpec.Size = 12;
myPane.XAxis.Scale.Format = "d MMM";
// make the x axis scale minimum 1 step less than the minimum data value
myPane.XAxis.Scale.Min = hList[0].X - 1;
// Display the Y axis grid
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.YAxis.Scale.MinorStep = 0.5;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166 ), 90F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Horizontal Bar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Horizontal Bar Graph";
myPane.XAxis.Title.Text = "Performance Factor";
myPane.YAxis.Title.Text = "Grouping";

// Enter some random data values


double[] y = { 100, 115, 75, -22, 98, 40, -10 };
double[] y2 = { 90, 100, 95, -35, 80, 35, 35 };
double[] y3 = { 80, 110, 65, -15, 54, 67, 18 };

// Generate a bar with "Curve 1" in the legend


BarItem myCurve = myPane.AddBar( "Curve 1", y, null, Color.Red );
// Fill the bar with a red-white-red gradient
myCurve.Bar.Fill = new Fill( Color.Red, Color.White, Color.Red, 90F );
// Generate a bar with "Curve 2" in the legend
myCurve = myPane.AddBar( "Curve 2", y2, null, Color.Blue );
// Fill the bar with a blue-white-blue gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Blue, Color.White, Color.Blue,
90F );

// Generate a bar with "Curve 3" in the legend


myCurve = myPane.AddBar( "Curve 3", y3, null, Color.Green );
// Fill the bar with a Green-white-Green gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.White, Color.Green, 90F );

// Draw the X tics between the labels instead of at the labels


myPane.YAxis.MajorTic.IsBetweenLabels = true;

// Set the XAxis to an ordinal type


myPane.YAxis.Type = AxisType.Ordinal;
// draw the X axis zero line
myPane.XAxis.MajorGrid.IsZeroLine = true;

//This is the part that makes the bars horizontal


myPane.BarSettings.Base = BarBase.Y;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166 ), 45.0F );

zgc.AxisChange();

BarItem.CreateBarLabels( myPane, false, "f0" );


}
Horizontal Stacked Bar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Cat Stats";
myPane.YAxis.Title.Text = "Big Cats";
myPane.XAxis.Title.Text = "Population";

// Make up some data points


string[] labels = { "Panther", "Lion", "Cheetah", "Cougar", "Tiger",
"Leopard" };
double[] x = { 100, 115, 75, 22, 98, 40 };
double[] x2 = { 120, 175, 95, 57, 113, 110 };
double[] x3 = { 204, 192, 119, 80, 134, 156 };

// Generate a red bar with "Curve 1" in the legend


BarItem myCurve = myPane.AddBar( "Here", x, null, Color.Red );
// Fill the bar with a red-white-red color gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Red, Color.White, Color.Red, 90f );

// Generate a blue bar with "Curve 2" in the legend


myCurve = myPane.AddBar( "There", x2, null, Color.Blue );
// Fill the bar with a Blue-white-Blue color gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Blue, Color.White, Color.Blue,
90f );

// Generate a green bar with "Curve 3" in the legend


myCurve = myPane.AddBar( "Elsewhere", x3, null, Color.Green );
// Fill the bar with a Green-white-Green color gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Green, Color.White, Color.Green,
90f );

// Draw the Y tics between the labels instead of at the labels


myPane.YAxis.MajorTic.IsBetweenLabels = true;

// Set the YAxis labels


myPane.YAxis.Scale.TextLabels = labels;
// Set the YAxis to Text type
myPane.YAxis.Type = AxisType.Text;

// Set the bar type to stack, which stacks the bars by automatically
accumulating the values
myPane.BarSettings.Type = BarType.Stack;

// Make the bars horizontal by setting the BarBase to "Y"


myPane.BarSettings.Base = BarBase.Y;

// Fill the chart background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166), 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Overlay Bar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis label


myPane.Title.Text = "Overlay Bar Graph Demo";
myPane.YAxis.Title.Text = "Value";

// Enter some data values


double[] y = { 100, 115, 75, -22, 98, 40, -10 };
double[] y2 = { 90, 100, 95, -35, 80, 35, 35 };
double[] y3 = { 80, 110, 65, -15, 54, 67, 18 };

// Manually sum up the curves


for ( int i = 0; i < y.GetLength( 0 ); i++ )
y2[i] += y[i];
for ( int i = 0; i < y2.GetLength( 0 ); i++ )
y3[i] += y2[i];
// Generate a red bar with "Curve 1" in the legend
CurveItem myCurve = myPane.AddBar( "Curve 1", null, y, Color.Red );

// Generate a blue bar with "Curve 2" in the legend


myCurve = myPane.AddBar( "Curve 2", null, y2, Color.Blue );

// Generate a green bar with "Curve 3" in the legend


myCurve = myPane.AddBar( "Curve 3", null, y3, Color.Green );

// Draw the X tics between the labels instead of at the labels


myPane.XAxis.MajorTic.IsBetweenLabels = true;

// Set the XAxis to the ordinal type


myPane.XAxis.Type = AxisType.Ordinal;

//Add Labels to the curves

// Shift the text items up by 5 user scale units above the bars
const float shift = 5;

for ( int i = 0; i < y.Length; i++ )


{
// format the label string to have 1 decimal place
string lab = y3[i].ToString( "F1" );
// create the text item (assumes the x axis is ordinal or text)
// for negative bars, the label appears just above the zero value
TextObj text = new TextObj( lab, (float)( i + 1 ), (float)( y3[i]
< 0 ? 0.0 : y3[i] ) + shift );
// tell Zedgraph to use user scale units for locating the TextItem
text.Location.CoordinateFrame = CoordType.AxisXYScale;
// AlignH the left-center of the text to the specified point
text.Location.AlignH = AlignH.Left;
text.Location.AlignV = AlignV.Center;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
// rotate the text 90 degrees
text.FontSpec.Angle = 90;
// add the TextItem to the list
myPane.GraphObjList.Add( text );
}

// Indicate that the bars are overlay type, which are drawn on top of
eachother
myPane.BarSettings.Type = BarType.Overlay;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();

// Add one step to the max scale value to leave room for the labels
myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;

}
Percent Stack Bar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Make up some data points


string[] quarters = { "Q1-'04", "Q2-'04", "Q3-'04", "Q4-'04" };
double[] y4 = { 20, 15, 90, 70 };
double[] y3 = { 0, 35, 40, 10 };
double[] y2 = { 60, 70, 20, 30 };
double[] y5 = new double[4];

// Set the pane title


myPane.Title.Text = "% Product Sales by Quarter by Type";
// Position the legend and fill the background
myPane.Legend.Position = LegendPos.TopCenter;
myPane.Legend.Fill.Color = Color.LightCyan;
// Fill the pane background with a solid color
myPane.Fill.Color = Color.Cornsilk;
// Fill the axis background with a solid color
myPane.Chart.Fill.Type = FillType.Solid;
myPane.Chart.Fill.Color = Color.LightCyan;
// Set the bar type to percent stack, which makes the bars sum up to
100%
myPane.BarSettings.Type = BarType.PercentStack;

// Set the X axis title


myPane.XAxis.Title.Text = "Quarter";
myPane.XAxis.Type = AxisType.Text;
myPane.XAxis.Scale.TextLabels = quarters;

// Set the Y2 axis properties


myPane.Y2Axis.Title.Text = "2004 Total Sales ($M)";
myPane.Y2Axis.IsVisible = true;
myPane.Y2Axis.MinorTic.IsOpposite = false;
myPane.Y2Axis.MajorTic.IsOpposite = false;
myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Red;

// Set the Y axis properties


myPane.YAxis.Title.IsVisible = false;
myPane.YAxis.Scale.Max = 120;
myPane.YAxis.MinorTic.IsOpposite = false;
myPane.YAxis.MajorTic.IsOpposite = false;
myPane.YAxis.Scale.Format = "0'%'";

// get total values into array for Line


for ( int x = 0; x < 4; x++ )
y5[x] = y4[x] + y3[x] + y2[x];

// Add a curve to the graph


LineItem curve;
curve = myPane.AddCurve( "Total Sales", null, y5, Color.Black,
SymbolType.Circle );
// Associate the curve with the Y2 axis
curve.IsY2Axis = true;
curve.Line.Width = 1.5F;
// Make the symbols solid red
curve.Line.Color = Color.Red;
curve.Symbol.Fill = new Fill( Color.Red );
myPane.Y2Axis.Title.FontSpec.FontColor = Color.Red;
curve.Symbol.Size = 8;

// Add a gradient blue bar


BarItem bar = myPane.AddBar( "Components", null, y4,
Color.RoyalBlue );
bar.Bar.Fill = new Fill( Color.RoyalBlue, Color.White,
Color.RoyalBlue );

// Add a gradient green bar


bar = myPane.AddBar( "Misc", null, y3, Color.LimeGreen );
bar.Bar.Fill = new Fill( Color.LimeGreen, Color.White,
Color.LimeGreen );

// Add a gradient yellow bar


bar = myPane.AddBar( "Assemblies", null, y2, Color.Yellow );
bar.Bar.Fill = new Fill( Color.Yellow, Color.White, Color.Yellow );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Sine Bar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "A Sine wave displayed by a bar graph.";
myPane.XAxis.Title.Text = "Label";
myPane.YAxis.Title.Text = "My Y Axis";

// Generate some curve data from the Sine function


const int size = 41;
PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();

for ( int i=0; i<size; i++ )


{
double x = i + 1;
double y = Math.Sin( (double) i / 30.0 * 2.0 * Math.PI ) * 50.0 +
5.0;
double y2 = Math.Sin( (double) i / 30.0 * 2.0 * Math.PI ) * 50.0
+ 25.0;
list.Add( x, y );
list2.Add( x, y2 );
}

// Generate a red bar with "Curve 1" in the legend


BarItem myBar = myPane.AddBar( "Curve 1", list, Color.Red );

// Generate a blue bar with "Curve 2" in the legend


myBar = myPane.AddBar( "Curve 2", list2, Color.Blue );

// Set the XAxis to ordinal type


myPane.XAxis.Type = AxisType.Ordinal;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166), 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Sorted Overlay Bar Demo

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "My Test Sorted Overlay Bar Graph";
myPane.XAxis.Title.Text = "Label";
myPane.YAxis.Title.Text = "My Y Axis";

// Enter some data values


string[] labels = { "Panther", "Lion", "Cheetah", "Cougar", "Tiger",
"Leopard", "Kitty" };
double[] y = { 100, 115, 75, -22, 98, 40, -10 };
double[] y2 = { 90, 100, 95, -35, 80, 35, 35 };
double[] y3 = { 80, 110, 65, -15, 104, 67, 18 };

// Generate a red bar with "Curve 1" in the legend


CurveItem myCurve = myPane.AddBar( "Curve 1", null, y, Color.Red );
// Generate a blue bar with "Curve 2" in the legend
myCurve = myPane.AddBar( "Curve 2", null, y2, Color.Blue );

// Generate a green bar with "Curve 3" in the legend


myCurve = myPane.AddBar( "Curve 3", null, y3, Color.Green );

// Draw the X tics between the labels instead of at the labels


myPane.XAxis.MajorTic.IsBetweenLabels = true;

// Set the XAxis to Text type


myPane.XAxis.Type = AxisType.Text;
// Set the XAxis labels
myPane.XAxis.Scale.TextLabels = labels;
myPane.XAxis.Scale.FontSpec.Size = 10.0F ;

// Make the bars a sorted overlay type so that they are drawn on top
of eachother
// (without summing), and each stack is sorted so the shorter bars are
in front
// of the taller bars
myPane.BarSettings.Type = BarType.SortedOverlay;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Pie Charts
Demonstrates basic pie chart functionality including offset

slices.

Pie Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the GraphPane title


myPane.Title.Text = "2004 ZedGraph Sales by Region\n($M)";
myPane.Title.FontSpec.IsItalic = true;
myPane.Title.FontSpec.Size = 24f;
myPane.Title.FontSpec.Family = "Times New Roman";
// Fill the pane background with a color gradient
myPane.Fill = new Fill( Color.White, Color.Goldenrod, 45.0f );
// No fill for the chart background
myPane.Chart.Fill.Type = FillType.None;

// Set the legend to an arbitrary location


myPane.Legend.Position = LegendPos.Float;
myPane.Legend.Location = new Location( 0.95f, 0.15f,
CoordType.PaneFraction,
AlignH.Right, AlignV.Top );
myPane.Legend.FontSpec.Size = 10f;
myPane.Legend.IsHStack = false;

// Add some pie slices


PieItem segment1 = myPane.AddPieSlice( 20, Color.Navy, Color.White,
45f, 0, "North" );
PieItem segment3 = myPane.AddPieSlice( 30, Color.Purple, Color.White,
45f, .0, "East" );
PieItem segment4 = myPane.AddPieSlice( 10.21, Color.LimeGreen,
Color.White, 45f, 0, "West" );
PieItem segment2 = myPane.AddPieSlice( 40, Color.SandyBrown,
Color.White, 45f, 0.2, "South" );
PieItem segment6 = myPane.AddPieSlice( 250, Color.Red, Color.White,
45f, 0, "Europe" );
PieItem segment7 = myPane.AddPieSlice( 1500, Color.Blue, Color.White,
45f, 0.2, "Pac Rim" );
PieItem segment8 = myPane.AddPieSlice( 400, Color.Green, Color.White,
45f, 0, "South America" );
PieItem segment9 = myPane.AddPieSlice( 50, Color.Yellow, Color.White,
45f, 0.2, "Africa" );

segment2.LabelDetail.FontSpec.FontColor = Color.Red;

// Sum up the pie values


CurveList curves = myPane.CurveList;
double total = 0;
for ( int x = 0; x < curves.Count; x++ )
total += ( (PieItem)curves[x] ).Value;

// Make a text label to highlight the total value


TextObj text = new TextObj( "Total 2004 Sales\n" + "$" +
total.ToString() + "M",
0.18F, 0.40F, CoordType.PaneFraction );
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Bottom;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill = new Fill( Color.White, Color.FromArgb( 255, 100,
100 ), 45F );
text.FontSpec.StringAlignment = StringAlignment.Center;
myPane.GraphObjList.Add( text );

// Create a drop shadow for the total value text item


TextObj text2 = new TextObj( text );
text2.FontSpec.Fill = new Fill( Color.Black );
text2.Location.X += 0.008f;
text2.Location.Y += 0.01f;
myPane.GraphObjList.Add( text2 );

// Calculate the Axis Scale Ranges


zgc.AxisChange();

}
Special Charts
General demonstration graph with bars, lines, semi-transparent fill,
labels, and banded ranges.

Gas Gauge Demo

Demonstration of the gas gauge type.

Contour Chart Demo

Shows how to make a 2D contour plot using regular LineItems with fills.

SampleMultiPointList Demo

Demonstrates a custom data storage class that implements the IPointList


interface, allowing it to be used directly by ZedGraph.
MasterPane Demo

Demonstrates the use of the MasterPane, which handles the layout of


multiple GraphPanes.

Multi-Pie Chart Demo

Shows multiple pie graphs in a MasterPane.

Spider Demo

Shows spider graph in a MasterPane.

spider_graph_150.gif

Transparent Background Demo

Shows how to make the plot area transparent so the chart can be overlaid
on a background image.
Combo Chart Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "Wacky Widget Company\nProduction Report";
myPane.XAxis.Title.Text = "Time, Days\n(Since Plant Construction
Startup)";
myPane.YAxis.Title.Text = "Widget Production\n(units/hour)";

LineItem curve;

// Set up curve "Larry"


double[] x = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
double[] y = { 20, 10, 50, 25, 35, 75, 90, 40, 33, 50 };
// Use green, with circle symbols
curve = myPane.AddCurve( "Larry", x, y, Color.Green,
SymbolType.Circle );
curve.Line.Width = 1.5F;
// Fill the area under the curve with a white-green gradient
curve.Line.Fill = new Fill( Color.White, Color.FromArgb( 60, 190, 50),
90F );
// Make it a smooth line
curve.Line.IsSmooth = true;
curve.Line.SmoothTension = 0.6F;
// Fill the symbols with white
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 10;

// Second curve is "moe"


double[] x3 = { 150, 250, 400, 520, 780, 940 };
double[] y3 = { 5.2, 49.0, 33.8, 88.57, 99.9, 36.8 };
// Use a red color with triangle symbols
curve = myPane.AddCurve( "Moe", x3, y3, Color.FromArgb( 200, 55, 135),
SymbolType.Triangle );
curve.Line.Width = 1.5F;
// Fill the area under the curve with semi-transparent pink using the
alpha value
curve.Line.Fill = new Fill( Color.White, Color.FromArgb( 160, 230, 145,
205), 90F );
// Fill the symbols with white
curve.Symbol.Fill = new Fill( Color.White );
curve.Symbol.Size = 10;

// Third Curve is a bar, called "Wheezy"


double[] x4 = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
double[] y4 = { 30, 45, 53, 60, 75, 83, 84, 79, 71, 57 };
BarItem bar = myPane.AddBar( "Wheezy", x4, y4, Color.SteelBlue );
// Fill the bars with a RosyBrown-White-RosyBrown gradient
bar.Bar.Fill = new Fill( Color.RosyBrown, Color.White,
Color.RosyBrown );

// Fourth curve is a bar


double[] x2 = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
double[] y2 = { 10, 15, 17, 20, 25, 27, 29, 26, 24, 18 };
bar = myPane.AddBar( "Curly", x2, y2, Color.RoyalBlue );
// Fill the bars with a RoyalBlue-White-RoyalBlue gradient
bar.Bar.Fill = new Fill( Color.RoyalBlue, Color.White,
Color.RoyalBlue );
// Fill the pane background with a gradient
myPane.Fill = new Fill( Color.WhiteSmoke, Color.Lavender, 0F );
// Fill the axis background with a gradient
myPane.Chart.Fill = new Fill( Color.FromArgb( 255, 255, 245),
Color.FromArgb( 255, 255, 190), 90F );

// Make each cluster 100 user scale units wide. This is needed because
the X Axis
// type is Linear rather than Text or Ordinal
myPane.BarSettings.ClusterScaleWidth = 100;
// Bars are stacked
myPane.BarSettings.Type = BarType.Stack;

// Enable the X and Y axis grids


myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible= true;

// Manually set the scale maximums according to user preference


myPane.XAxis.Scale.Max = 1200;
myPane.YAxis.Scale.Max = 120;

// Add a text item to decorate the graph


TextObj text = new TextObj("First Prod\n21-Oct-93", 175F, 80.0F );
// Align the text such that the Bottom-Center is at (175, 80) in user
scale coordinates
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Bottom;
text.FontSpec.Fill = new Fill( Color.White, Color.PowderBlue, 45F );
text.FontSpec.StringAlignment = StringAlignment.Near;
myPane.GraphObjList.Add( text );

// Add an arrow pointer for the above text item


ArrowObj arrow = new ArrowObj( Color.Black, 12F, 175F, 77F, 100F,
45F );
arrow.Location.CoordinateFrame = CoordType.AxisXYScale;
myPane.GraphObjList.Add( arrow );

// Add a another text item to to point out a graph feature


text = new TextObj("Upgrade", 700F, 50.0F );
// rotate the text 90 degrees
text.FontSpec.Angle = 90;
// Align the text such that the Right-Center is at (700, 50) in user
scale coordinates
text.Location.AlignH = AlignH.Right;
text.Location.AlignV = AlignV.Center;
// Disable the border and background fill options for the text
text.FontSpec.Fill.IsVisible = false;
text.FontSpec.Border.IsVisible = false;
myPane.GraphObjList.Add( text );

// Add an arrow pointer for the above text item


arrow = new ArrowObj( Color.Black, 15, 700, 53, 700, 80 );
arrow.Location.CoordinateFrame = CoordType.AxisXYScale;
arrow.PenWidth = 2.0F;
myPane.GraphObjList.Add( arrow );

// Add a text "Confidential" stamp to the graph


text = new TextObj("Confidential", 0.85F, -0.03F );
// use ChartFraction coordinates so the text is placed relative to the
Chart.Rect
text.Location.CoordinateFrame = CoordType.ChartFraction;
// rotate the text 15 degrees
text.FontSpec.Angle = 15.0F;
// Text will be red, bold, and 16 point
text.FontSpec.FontColor = Color.Red;
text.FontSpec.IsBold = true;
text.FontSpec.Size = 16;
// Disable the border and background fill options for the text
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
// Align the text such the the Left-Bottom corner is at the specified
coordinates
text.Location.AlignH = AlignH.Left;
text.Location.AlignV = AlignV.Bottom;
myPane.GraphObjList.Add( text );

// Add a BoxObj to show a colored band behind the graph data


BoxObj box = new BoxObj( 0, 110, 1200, 10,
Color.Empty, Color.FromArgb( 225, 245, 225) );
box.Location.CoordinateFrame = CoordType.AxisXYScale;
// Align the left-top of the box to (0, 110)
box.Location.AlignH = AlignH.Left;
box.Location.AlignV = AlignV.Top;
// place the box behind the axis items, so the grid is drawn on top
of it
box.ZOrder = ZOrder.E_BehindAxis;
myPane.GraphObjList.Add( box );
// Add some text inside the above box to indicate "Peak Range"
TextObj myText = new TextObj( "Peak Range", 1170, 105 );
myText.Location.CoordinateFrame = CoordType.AxisXYScale;
myText.Location.AlignH = AlignH.Right;
myText.Location.AlignV = AlignV.Center;
myText.FontSpec.IsItalic = true;
myText.FontSpec.IsBold = false;
myText.FontSpec.Fill.IsVisible = false;
myText.FontSpec.Border.IsVisible = false;
myPane.GraphObjList.Add( myText );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Gas Gauge Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Define the title


myPane.Title.Text = "Gas Gauge Demo";

// Fill the pane with gray


myPane.Fill = new Fill( Color.LightGray, Color.White, 45.0f );
// Fill the chart rect with blue
myPane.Chart.Fill = new Fill( Color.White, Color.SkyBlue, 45.0f );

// Don't show any axes for the gas gauge


myPane.XAxis.IsVisible = false;
myPane.Y2Axis.IsVisible = false;
myPane.YAxis.IsVisible = false;
//Define needles; can add more than one
GasGaugeNeedle gg1 = new GasGaugeNeedle( "Cereal", 30.0f,
Color.Black );
GasGaugeNeedle gg2 = new GasGaugeNeedle( "Milk", 80.0f,
Color.DarkGreen );
myPane.CurveList.Add( gg1 );
myPane.CurveList.Add( gg2 );

//Define all regions


GasGaugeRegion ggr1 = new GasGaugeRegion( "Red", 0.0f, 33.0f,
Color.Red );
GasGaugeRegion ggr2 = new GasGaugeRegion( "Yellow", 33.0f, 66.0f,
Color.Yellow );
GasGaugeRegion ggr3 = new GasGaugeRegion( "Green", 66.0f, 100.0f,
Color.Green );

// Add the curves


myPane.CurveList.Add( ggr1 );
myPane.CurveList.Add( ggr2 );
myPane.CurveList.Add( ggr3 );

zgc.AxisChange();
}
Contour Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateGraph_Contour( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

myPane.Title.Text = "Sample Contour Plot";


myPane.XAxis.Title.Text = "X Coordinate (m)";
myPane.YAxis.Title.Text = "Y Coordinate (m)";

Random rand = new Random();

// Generate four contours


for ( int i = 0; i < 4; i )
{
PointPairList list = new PointPairList();
// each contour gets a point every 10 degrees
for ( int j = 0; j < 36; j )
{
// the angle, theta, in radians
double theta = 2.0 * Math.PI * j / 36.0;
// the radius, with some random variability
double r = i * 2 2 0.5 * rand.NextDouble();
// Convert (r,p) to (x,y)
double x = r * Math.Cos( theta ) * 10.0 250.0;
double y = r * Math.Sin( theta ) * 10.0 250.0;
list.Add( x, y );
}

// duplicate the first point at the end to complete the circle


list.Add( list[0] );

// Add a curve with a suitable level, no symbols


LineItem myCurve = myPane.AddCurve( "Level=" ( i
1 ).ToString(), list,
Color.Black, SymbolType.None );

// Smooth out the contours a little


myCurve.Line.IsSmooth = true;
myCurve.Line.SmoothTension = 0.7f;
}

// Fill the curves with a different color for each curve


( myPane.CurveList[0] as LineItem ).Line.Fill =
new Fill( Color.White, Color.FromArgb( 255, 150, 255 ), 45.0f );
( myPane.CurveList[1] as LineItem ).Line.Fill =
new Fill( Color.White, Color.FromArgb( 255, 255, 150), 45.0f );
( myPane.CurveList[2] as LineItem ).Line.Fill =
new Fill( Color.White, Color.FromArgb( 150, 255, 150 ), 45.0f );
( myPane.CurveList[3] as LineItem ).Line.Fill =
new Fill( Color.White, Color.FromArgb( 150, 255, 255 ), 45.0f );

myPane.Legend.IsVisible = false;
myPane.Chart.Fill = new Fill( Color.White, Color.LightGray, 45.0f );
myPane.Fill = new Fill( Color.White, Color.LightGoldenrodYellow,
45.0f );

// Manually set the axis ranges


myPane.XAxis.Scale.Min = 150;
myPane.XAxis.Scale.Max = 350;
myPane.YAxis.Scale.Min = 150;
myPane.YAxis.Scale.Max = 350;

zgc.AxisChange();
}
SampleMultiPointList Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "SampleMultiPointList (IPointList) Demo";
myPane.XAxis.Title.Text = "Time, seconds";
myPane.YAxis.Title.Text = "Distance (m), or Velocity (m/s)";

// Create a new SampleMultiPointList (see SampleMultiPointList.cs for


details)
SampleMultiPointList myList = new SampleMultiPointList();
// For the first list, specify that the Y data to be plotted will be
the distance
myList.YData = PerfDataType.Distance;
// note how it does not matter that we created the second list before
actually
// adding the data -- this is because the cloned list shares data with
the
// original
SampleMultiPointList myList2 = new SampleMultiPointList( myList );
// For the second list, specify that the Y data to be plotted will be
the velocity
myList2.YData = PerfDataType.Velocity;

// Populate the dataset using some calculated values


for ( int i=0; i<20; i++ )
{
double time = (double) i;
double acceleration = 1.0;
double velocity = acceleration * time;
double distance = acceleration * time * time / 2.0;
PerformanceData perfData = new PerformanceData( time, distance,
velocity, acceleration );
myList.Add( perfData );
}

// Add two curves to the graph


myPane.AddCurve( "Distance", myList, Color.Blue );
myPane.AddCurve( "Velocity", myList2, Color.Red );

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.LightGoldenrodYellow, 45.0F );

zgc.AxisChange();
}
MasterPane Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
MasterPane myMaster = zgc.MasterPane;

myMaster.PaneList.Clear();

// Set the master pane title


myMaster.Title.Text = "MasterPane Test";
myMaster.Title.IsVisible = true;

// Fill the pane background with a color gradient


myMaster.Fill = new Fill( Color.White, Color.MediumSlateBlue,
45.0F );

// Set the margins and the space between panes to 10 points


myMaster.Margin.All = 10;
myMaster.InnerPaneGap = 10;

// Enable the master pane legend


myMaster.Legend.IsVisible = true;
myMaster.Legend.Position = LegendPos.TopCenter;

// Add a confidential stamp


TextObj text = new TextObj( "Confidential", 0.80F, 0.12F );
text.Location.CoordinateFrame = CoordType.PaneFraction;
// angle the text at 15 degrees
text.FontSpec.Angle = 15.0F;
text.FontSpec.FontColor = Color.Red;
text.FontSpec.IsBold = true;
text.FontSpec.Size = 16;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Border.Color = Color.Red;
text.FontSpec.Fill.IsVisible = false;
text.Location.AlignH = AlignH.Left;
text.Location.AlignV = AlignV.Bottom;
myMaster.GraphObjList.Add( text );

// Add a draft watermark


text = new TextObj("DRAFT", 0.5F, 0.5F );
text.Location.CoordinateFrame = CoordType.PaneFraction;
// tilt the text at 30 degrees
text.FontSpec.Angle = 30.0F;
// Use the alpha value (70) to make the text semi-transparent (a
watermark)
text.FontSpec.FontColor = Color.FromArgb( 70, 255, 100, 100 );
text.FontSpec.IsBold = true;
text.FontSpec.Size = 100;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Center;
text.ZOrder = ZOrder.A_InFront;
myMaster.GraphObjList.Add( text );

for ( int j=0; j<6; j++ )


{
// Create a new GraphPane
GraphPane myPane = new GraphPane();
myPane.Title.Text = "My Test Graph #" + (j+1).ToString();
myPane.XAxis.Title.Text = "X Axis";
myPane.YAxis.Title.Text = "Y Axis";

// Fill the pane background with a color gradient


myPane.Fill = new Fill( Color.White, Color.LightYellow, 45.0F );
// Reduce the base dimension to 6 inches, since these panes tend
to be smaller
myPane.BaseDimension = 6.0F;

// Make up some data arrays based on the Sine function


PointPairList list = new PointPairList();
for ( int i=0; i<36; i++ )
{
double x = (double) i + 5;
double y = 3.0 * ( 1.5 + Math.Sin( (double) i * 0.2 + (double)
j ) );
list.Add( x, y );
}

// Generate a red curve with diamond symbols


LineItem myCurve = myPane.AddCurve( "label" + j.ToString(),
list, Color.Red, SymbolType.Diamond );

// Add the new GraphPane to the MasterPane


myMaster.Add( myPane );
}

// Tell ZedGraph to auto layout all the panes


using ( Graphics g = CreateGraphics() )
{
myMaster.SetLayout( g, PaneLayout.SquareColPreferred );
zgc.AxisChange();
}
}
Multi-Pie Chart Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
MasterPane myMaster = zgc.MasterPane;

myMaster.PaneList.Clear();

// Set the master pane title


myMaster.Title.Text = "Multiple Pie Charts on a MasterPane";
myMaster.Title.IsVisible = true;

// Fill the masterpane background with a color gradient


myMaster.Fill = new Fill( Color.White, Color.MediumSlateBlue,
45.0F );

// Set the margins and the space between panes to 10 points


myMaster.Margin.All = 10;
myMaster.InnerPaneGap = 10;

// Enable the masterpane legend


myMaster.Legend.IsVisible = true;
myMaster.Legend.Position = LegendPos.TopCenter;
myMaster.IsUniformLegendEntries = true;

// Enter some data values


double[] values = { 15, 15, 40, 20 };
double[] values2 = { 250, 50, 400, 50 };
Color[] colors = { Color.Red, Color.Blue, Color.Green, Color.Yellow };
double[] displacement = { .0, .0, .0, .0 };
string[] labels = { "East", "West", "Central", "Canada" };

// Create some GraphPanes


for ( int x = 0; x < 3; x++ )
{
// Create the GraphPane
GraphPane myPane = new GraphPane();
myPane.Title.Text = "2003 Regional Sales";

// Fill the pane background with a solid color


myPane.Fill = new Fill( Color.Cornsilk );
// Fill the chart background with a solid color
myPane.Chart.Fill = new Fill( Color.Cornsilk );

// Hide the GraphPane legend


myPane.Legend.IsVisible = false;

// Add some pie slices


PieItem segment1 = myPane.AddPieSlice( 20, Color.Blue, .10,
"North" );
PieItem segment2 = myPane.AddPieSlice( 40, Color.Red, 0, "South" );
PieItem segment3 = myPane.AddPieSlice( 30, Color.Yellow, .0,
"East" );
PieItem segment4 = myPane.AddPieSlice( 10.21, Color.Green, .20,
"West" );
PieItem segment5 = myPane.AddPieSlice( 10.5, Color.Aquamarine, .0,
"Canada" );
segment1.LabelType = PieLabelType.Name_Value;
segment2.LabelType = PieLabelType.Name_Value;
segment3.LabelType = PieLabelType.Name_Value;
segment4.LabelType = PieLabelType.Name_Value;
segment5.LabelType = PieLabelType.Name_Value;
// Add the graphpane to the masterpane
myMaster.Add( myPane );
}

// Tell ZedGraph to auto layout the graphpanes


using ( Graphics g = CreateGraphics() )
{
myMaster.SetLayout( g, PaneLayout.ExplicitRow12 );
zgc.AxisChange();
}

}
Spider Demo
Image:Spider.png

C# Code Sample

private void OnRenderGraph1(System.Drawing.Graphics g,


ZedGraph.MasterPane masterPane)
{
/*test for making SpiderDiagrams
* Initial from Line Stack Demo due to the coloring of
the space in between two lines
* Assume 8 dimensions (4 pairs - generalize later to n
pairs);
* i.e. global angle-factor, f, is given from the angle,
v=360/8=45 => f=cos(v)=sin(v)=1/sqrt(2)
* Assume pairing as (score1,score2), (score3,score4),
(score5, score6), ...
* where odd numbered scores are shown above the
horizontal origo and even ones below
* This gives the two lines the coordinates (f# meaning
f*score#)
* {{-f1,0},{-f3,f3},{0,f5}
*
*/
// Get the GraphPane so we can work with it
GraphPane myPane = masterPane[0];
// Fill the axis background with a color gradient
myPane.Chart.Fill = new Fill(Color.FromArgb(255, 255,
245), Color.FromArgb(255, 255, 190), 90F);
myPane.XAxis.Title.Text = "";
Random rand = new Random();

object[] myColors = {Color.Red, Color.Blue, Color.Green,


Color.Yellow,
Color.YellowGreen, Color.Wheat, Color.WhiteSmoke,
Color.AntiqueWhite,
Color.DarkOliveGreen, Color.Beige};
// Generate generel spiderweb
string[] labels = { "Domineering", "Nonassertive",
"Vindictive", "Accomodating", "Cold", "SelfSacrificing", "Inhibited",
"Intrusive" };
//first the spider creates the basic lines
for (int j = 0; j < 8; j++)
{
PointPairList list = new PointPairList();
// the angle, theta, in radians
double theta = 2.0 * Math.PI * j / 8.0;
// the radius, with some random variability
double r2 = 4.0;
// Convert (r,p) to (x,y)
double x = r2 * Math.Cos(theta);
double y = r2 * Math.Sin(theta);
list.Add((double)0.0, (double)0.0);
list.Add(x, y);
LineItem li = myPane.AddCurve("", list, Color.Black,
SymbolType.None);

// Create a text label with offset depending on the


Y value and the text length
TextObj text = new TextObj(labels[j],
(float)(x + .11 * Math.Cos(theta)),
(float)y + (float)(.4 * Math.Sin(theta)),
CoordType.AxisXYScale, Math.Cos(theta) < 0.1 ?
AlignH.Right : AlignH.Left,
Math.Abs(Math.Sin(theta)) < .1 ?
AlignV.Bottom : AlignV.Center);
text.ZOrder = ZOrder.A_InFront;
// Hide the border and the fill
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
//text.FontSpec.Fill = new
Fill( Color.FromArgb( 100, Color.White ) );
// Set the XAxis to date type
myPane.GraphObjList.Add(text);
}
//then it makes one web-ring per unit
for (int i = 0; i < 4; i++)
{
PointPairList list = new PointPairList();
for (int j = 0; j < 8; j++)
{
// the angle, theta, in radians
double theta = 2.0 * Math.PI * j / 8.0;
// the radius, with some random variability
double r2 = 1.0 + i;
// Convert (r,p) to (x,y)
double x = r2 * Math.Cos(theta);
double y = r2 * Math.Sin(theta);
list.Add(x, y);
}

// duplicate the first point at the end to complete


the circle
list.Add(list[0]);
myPane.AddCurve("", list, Color.Black,
SymbolType.None);

//here it displays a ruler at the top


TextObj text = new TextObj((i+1).ToString(),
(float).1, (float)(i + 1.1),
CoordType.AxisXYScale, AlignH.Left, AlignV.Center);
text.ZOrder = ZOrder.A_InFront;
// Hide the border and the fill
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
myPane.GraphObjList.Add(text);
}

// Generate three random contours


for (int i = 0; i < 3; i++)
{
PointPairList list = new PointPairList();
// each contour gets a point every 10 degrees
for (int j = 0; j < 8; j++)
{
// the angle, theta, in radians
double theta = 2.0 * Math.PI * j / 8.0;
// the radius, with some random variability
double r2 = 1.0 + (1.0 + i) * 1.0 *
rand.NextDouble();
// Convert (r,p) to (x,y)
double x = r2 * Math.Cos(theta);
double y = r2 * Math.Sin(theta);
list.Add(x, y);
}

// duplicate the first point at the end to complete


the circle
list.Add(list[0]);
// Add a curve with a suitable level, no symbols
LineItem myCurve = myPane.AddCurve("Level=" +
(i).ToString(), list,
Color.Black, SymbolType.None);

// Fill the curves with a different color for each


curve
myCurve.Line.Fill = new Fill((Color)myColors[i],
Color.White);
}
// Generate std.deviation; i.e. drawing eight areas
PointPair firstPair = new PointPair(0.0, 0.0);
PointPair prevPair = new PointPair(0.0, 0.0);
PointPairList normList = new PointPairList();
for (int j = 0; j < 8 + 1; j++)
{
list = new PointPairList();

if (j != 8)
{
// the angle, theta, in radians
double theta = 2.0 * Math.PI * j / 8.0;
// the radius, with some random variability
double r2 = 1.0 + rand.NextDouble();
// Convert (r,p) to (x,y)
list.Add(new PointPair(r2 * Math.Cos(theta), r2
* Math.Sin(theta)));
//generate + 2*sdev
list.Add(list[0].X * 1.5, list[0].Y * 1.5);
}
else //last step connects to first step
{
list.Add(firstPair);
//generate + 2*sdev
list.Add(firstPair.X * 1.5, firstPair.Y * 1.5);
}
if (j == 0) //first pair are saved for later use
firstPair = list[0];
else //otw draw combined with previous pair of
PointPairs
{
//now add previous
list.Add(prevPair.X * 1.5, prevPair.Y * 1.5);
list.Add(prevPair);
// duplicate the first point at the end to
complete the circle
list.Add(list[0]);
// Add a curve with a suitable level, no symbols
myCurve = myPane.AddCurve(j == 1 ? "SD" : "",
list,
Color.LightGreen, SymbolType.None);
myCurve.Line.Fill = new
Fill(Color.LightGreen);
}
//finally step
prevPair = list[0];
}
myPane.Legend.IsVisible = true;
myPane.Fill = new Fill(Color.White,
Color.LightGoldenrodYellow, 45.0f);
myPane.XAxis.MajorGrid.IsVisible = false;
myPane.XAxis.MinorGrid.IsVisible = false;
// Display the Y axis grid lines
myPane.YAxis.MajorGrid.IsVisible = false;
myPane.YAxis.MinorGrid.IsVisible = false;
myPane.XAxis.Scale.IsVisible = false;
myPane.YAxis.Scale.IsVisible = false;
myPane.YAxis.Scale.Min = -5;
myPane.YAxis.Scale.Max = 5;
myPane.XAxis.Scale.Min = -7;
myPane.XAxis.Scale.Max = 7;

// Calculate the Axis Scale Ranges


masterPane.AxisChange(g);
Transparent Background Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.XAxis.Title.Text = "Time, Years";
myPane.YAxis.Title.Text = "Rainfall, mm/yr";

// Enter some data points


double[] x4 = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
double[] y4 = { 30, 45, 53, 60, 75, 83, 84, 79, 71, 57 };

// Make a bar
BarItem bar = myPane.AddBar( "Wheezy", x4, y4, Color.SteelBlue );
// Fill the bar with a color gradient
bar.Bar.Fill = new Fill( Color.FromArgb( 100, 130, 255, 130 ),
Color.FromArgb( 100, 255, 255, 255 ),
Color.FromArgb( 100, 130, 255, 130 ) );

// Make a second bar


double[] x2 = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
double[] y2 = { 10, 15, 17, 20, 25, 27, 29, 26, 24, 18 };
bar = myPane.AddBar( "Curly", x2, y2, Color.RoyalBlue );
// Fill the bar with a color gradient
bar.Bar.Fill = new Fill( Color.RoyalBlue, Color.White,
Color.RoyalBlue );

// Get an image for the background (use your own filename here)
Image image = Bitmap.FromFile( @"c:\temp\ngc4414.jpg" );

// Fill the pane background with the image


TextureBrush texBrush = new TextureBrush( image );
myPane.Fill = new Fill( texBrush );

// Turn off the axis background fill


myPane.Chart.Fill.IsVisible = false;
// Hide the legend
myPane.Legend.IsVisible = false;

// Add a text label


TextObj text = new TextObj( "Desert Rainfall", 0.5F, -0.05F,
CoordType.ChartFraction,
AlignH.Center, AlignV.Bottom );
text.FontSpec.FontColor = Color.Black;
text.FontSpec.Size = 20F;
text.FontSpec.IsBold = true;
text.FontSpec.IsItalic = true;
text.FontSpec.Fill.IsVisible = false;
text.FontSpec.Border.IsVisible = false;
myPane.GraphObjList.Add( text );

// Hide the title


myPane.Title.IsVisible = false;

// Set the colors to white show it shows up on a dark background


myPane.XAxis.Color = Color.White;
myPane.YAxis.Color = Color.White;
myPane.XAxis.Scale.FontSpec.FontColor = Color.White;
myPane.XAxis.Title.FontSpec.FontColor = Color.White;
myPane.YAxis.Scale.FontSpec.FontColor = Color.White;
myPane.YAxis.Title.FontSpec.FontColor = Color.White;
myPane.Chart.Border.Color = Color.White;
myPane.XAxis.MajorGrid.Color = Color.White;
myPane.YAxis.MajorGrid.Color = Color.White;

// Show the grid lines


myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.YAxis.Scale.Max = 120;

// Set the cluster with to 100 user units


// this is necessary since the scale is not an ordinal type
myPane.BarSettings.ClusterScaleWidth = 100;

// Make it a stacked bar


myPane.BarSettings.Type = BarType.Stack;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Charts from CodeProject Tutorial
Tutorial:Initial Chart Demo

The very basic, first chart.

Tutorial:Modified Initial Chart Demo

The basic chart after modifying some properties.

Tutorial:Date Axis Chart Demo

A simple chart that employs a date axis.

Tutorial:Text Axis Chart Demo

A simple chart the employs a text axis.


Tutorial:Bar Chart Demo

Clustered bar chart demo with a filled .

Tutorial:Stacked Bar Chart Demo

Basic stacked bar chart.

Tutorial:Horizontal Bar Chart Demo

Basic horizontal bar chart.

Tutorial:Pie Chart Demo

Pie chart demonstration.

Tutorial:Masterpane Chart Demo


Demonstration of a MasterPane layout.
Tutorial:Initial Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "My Test Graph\n(For CodeProject Sample)";
myPane.XAxis.Title.Text = "My X Axis";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some data arrays based on the Sine function


PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
for ( int i=0; i<36; i++ )
{
double x = (double) i + 5;
double y1 = 1.5 + Math.Sin( (double) i * 0.2 );
double y2 = 3.0 * ( 1.5 + Math.Sin( (double) i * 0.2 ) );
list1.Add( x, y1 );
list2.Add( x, y2 );
}

// Generate a red curve with diamond


// symbols, and "Porsche" in the legend
LineItem myCurve = myPane.AddCurve( "Porsche",
list1, Color.Red, SymbolType.Diamond );

// Generate a blue curve with circle


// symbols, and "Piper" in the legend
LineItem myCurve2 = myPane.AddCurve( "Piper",
list2, Color.Blue, SymbolType.Circle );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Tutorial:Modified Initial Chart
Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set up the title and axis labels


myPane.Title.Text = "My Test Graph\n(For CodeProject Sample)";
myPane.XAxis.Title.Text = "My X Axis";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some data arrays based on the Sine function


PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
for ( int i = 0; i < 36; i++ )
{
double x = (double)i + 5;
double y1 = 1.5 + Math.Sin( (double)i * 0.2 );
double y2 = 3.0 * ( 1.5 + Math.Sin( (double)i * 0.2 ) );
list1.Add( x, y1 );
list2.Add( x, y2 );
}

// Generate a red curve with diamond


// symbols, and "Porsche" in the legend
LineItem myCurve = myPane.AddCurve( "Porsche",
list1, Color.Red, SymbolType.Diamond );

// Generate a blue curve with circle


// symbols, and "Piper" in the legend
LineItem myCurve2 = myPane.AddCurve( "Piper",
list2, Color.Blue, SymbolType.Circle );

// Change the color of the title


myPane.Title.FontSpec.FontColor = Color.Green;

// Add gridlines to the plot, and make them gray


myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.MajorGrid.Color = Color.LightGray;
myPane.YAxis.MajorGrid.Color = Color.LightGray;

// Move the legend location


myPane.Legend.Position = ZedGraph.LegendPos.Bottom;

// Make both curves thicker


myCurve.Line.Width = 2.0F;
myCurve2.Line.Width = 2.0F;

// Fill the area under the curves


myCurve.Line.Fill = new Fill( Color.White, Color.Red, 45F );
myCurve2.Line.Fill = new Fill( Color.White, Color.Blue, 45F );

// Increase the symbol sizes, and fill them with solid white
myCurve.Symbol.Size = 8.0F;
myCurve2.Symbol.Size = 8.0F;
myCurve.Symbol.Fill = new Fill( Color.White );
myCurve2.Symbol.Fill = new Fill( Color.White );

// Add a background gradient fill to the axis frame


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 210 ), -45F );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Tutorial:Date Axis Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the titles and axis labels


myPane.Title.Text = "My Test Date Graph";
myPane.XAxis.Title.Text = "Date";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some data points based on the Sine function


PointPairList list = new PointPairList();
for ( int i=0; i<36; i++ )
{
double x = (double) new XDate( 1995, 5, i+11 );
double y = Math.Sin( (double) i * Math.PI / 15.0 );
list.Add( x, y );
}

// Generate a red curve with diamond


// symbols, and "My Curve" in the legend
LineItem myCurve = myPane.AddCurve( "My Curve",
list, Color.Red, SymbolType.Diamond );

// Set the XAxis to date type


myPane.XAxis.Type = AxisType.Date;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Tutorial:Text Axis Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "My Test Date Graph";
myPane.XAxis.Title.Text = "Label";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some data points


string[] labels = { "USA", "Spain\nMadrid", "Qatar", "Morocco", "UK",
"Uganda",
"Cambodia", "Malaysia", "Australia", "Ecuador" };
double[] y = new double[10];
for ( int i=0; i<10; i++ )
y[i] = Math.Sin( (double) i * Math.PI / 2.0 );
// Generate a red curve with diamond
// symbols, and "My Curve" in the legend
LineItem myCurve = myPane.AddCurve( "My Curve",
null, y, Color.Red, SymbolType.Diamond );

//Make the curve smooth


myCurve.Line.IsSmooth = true;

// Set the XAxis to Text type


myPane.XAxis.Type = AxisType.Text;
// Set the XAxis labels
myPane.XAxis.Scale.TextLabels = labels;
// Set the labels at an angle so they don't overlap
myPane.XAxis.Scale.FontSpec.Angle = 40;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Tutorial:Bar Chart Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
// get a reference to the GraphPane
GraphPane myPane = zgc.GraphPane;

// Set the Titles


myPane.Title.Text = "My Test Bar Graph";
myPane.XAxis.Title.Text = "Label";
myPane.YAxis.Title.Text = "My Y Axis";

// Make up some random data points


string[] labels = { "Panther", "Lion", "Cheetah",
"Cougar", "Tiger", "Leopard" };
double[] y = { 100, 115, 75, 22, 98, 40 };
double[] y2 = { 90, 100, 95, 35, 80, 35 };
double[] y3 = { 80, 110, 65, 15, 54, 67 };
double[] y4 = { 120, 125, 100, 40, 105, 75 };

// Generate a red bar with "Curve 1" in the legend


BarItem myBar = myPane.AddBar( "Curve 1", null, y,
Color.Red );
myBar.Bar.Fill = new Fill( Color.Red, Color.White,
Color.Red );

// Generate a blue bar with "Curve 2" in the legend


myBar = myPane.AddBar( "Curve 2", null, y2, Color.Blue );
myBar.Bar.Fill = new Fill( Color.Blue, Color.White,
Color.Blue );

// Generate a green bar with "Curve 3" in the legend


myBar = myPane.AddBar( "Curve 3", null, y3, Color.Green );
myBar.Bar.Fill = new Fill( Color.Green, Color.White,
Color.Green );

// Generate a black line with "Curve 4" in the legend


LineItem myCurve = myPane.AddCurve( "Curve 4",
null, y4, Color.Black, SymbolType.Circle );
myCurve.Line.Fill = new Fill( Color.White,
Color.LightSkyBlue, -45F );

// Fix up the curve attributes a little


myCurve.Symbol.Size = 8.0F;
myCurve.Symbol.Fill = new Fill( Color.White );
myCurve.Line.Width = 2.0F;

// Draw the X tics between the labels instead of


// at the labels
myPane.XAxis.MajorTic.IsBetweenLabels = true;

// Set the XAxis labels


myPane.XAxis.Scale.TextLabels = labels;
// Set the XAxis to Text type
myPane.XAxis.Type = AxisType.Text;

// Fill the Axis and Pane backgrounds


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166 ), 90F );
myPane.Fill = new Fill( Color.FromArgb( 250, 250, 255 ) );

// Tell ZedGraph to refigure the


// axes since the data have changed
zgc.AxisChange();
}
Tutorial:Stacked Bar Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "Cat Stats";
myPane.XAxis.Title.Text = "Big Cats";
myPane.YAxis.Title.Text = "Population";

// Make up some data points


string[] labels = { "Panther", "Lion", "Cheetah", "Cougar", "Tiger",
"Leopard" };
double[] y = { 100, 115, 75, 22, 98, 40 };
double[] y2 = { 120, 175, 95, 57, 113, 110 };
double[] y3 = { 204, 192, 119, 80, 134, 156 };
// Generate a red bar with "Curve 1" in the legend
BarItem myCurve = myPane.AddBar( "Here", null, y, Color.Red );
// Fill the bar with a red-white-red color gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Red, Color.White, Color.Red );

// Generate a blue bar with "Curve 2" in the legend


myCurve = myPane.AddBar( "There", null, y2, Color.Blue );
// Fill the bar with a Blue-white-Blue color gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Blue, Color.White, Color.Blue );

// Generate a green bar with "Curve 3" in the legend


myCurve = myPane.AddBar( "Elsewhere", null, y3, Color.Green );
// Fill the bar with a Green-white-Green color gradient for a 3d look
myCurve.Bar.Fill = new Fill( Color.Green, Color.White, Color.Green );

// Draw the X tics between the labels instead of at the labels


myPane.XAxis.MajorTic.IsBetweenLabels = true;

// Set the XAxis labels


myPane.XAxis.Scale.TextLabels = labels;
// Set the XAxis to Text type
myPane.XAxis.Type = AxisType.Text;

// Set the bar type to stack, which stacks the bars by automatically
accumulating the values
myPane.BarSettings.Type = BarType.Stack;

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Tutorial:Horizontal Bar Chart Demo

C# Code Sample

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the title and axis labels


myPane.Title.Text = "A Horizontal Percent Stack Graph";
myPane.XAxis.Title.Text = "Stuff";
myPane.YAxis.Title.Text = "";

// Enter some random data values


double[] y = { 100, 115, 15, 22, 98 };
double[] y2 = { 90, 60, 95, 35, 30 };
double[] y3 = { 20, 40, 105, 15, 30 };

// Generate a red bar with "Nina" in the legend


BarItem myCurve = myPane.AddBar( "Nina", y, null, Color.Red );
myCurve.Bar.Fill = new Fill( Color.Red, Color.White, Color.Red, 90F );

// Generate a blue bar with "Pinta" in the legend


myCurve = myPane.AddBar( "Pinta", y2, null, Color.Blue );
myCurve.Bar.Fill = new Fill( Color.Blue, Color.White, Color.Blue,
90F );

// Generate a green bar with "Santa Maria" in the legend


myCurve = myPane.AddBar( "Santa Maria", y3, null, Color.Green );
myCurve.Bar.Fill = new Fill( Color.Green, Color.White, Color.Green,
90F );

// Draw the Y tics between the labels instead of at the labels


myPane.YAxis.MajorTic.IsBetweenLabels = true;

// Set the YAxis to text type


myPane.YAxis.Type = AxisType.Text;
string[] labels = { "Australia", "Africa", "America", "Asia",
"Antartica" };
myPane.YAxis.Scale.TextLabels = labels;
myPane.XAxis.Scale.Max = 110;

// Make the bars horizontal by setting bar base axis to Y


myPane.BarSettings.Base = BarBase.Y;
// Make the bars percent stack type
myPane.BarSettings.Type = BarType.PercentStack;

// Fill the axis background with a color gradient


myPane.Chart.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 166), 90F );
// Fill the legend background with a color gradient
myPane.Legend.Fill = new Fill( Color.White,
Color.FromArgb( 255, 255, 250), 90F );
// Fill the pane background with a solid color
myPane.Fill = new Fill( Color.FromArgb( 250, 250, 255) );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Tutorial:Pie Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// Set the GraphPane title


myPane.Title.Text = "2004 ZedGraph Sales by Region\n($M)";
myPane.Title.FontSpec.IsItalic = true;
myPane.Title.FontSpec.Size = 24f;
myPane.Title.FontSpec.Family = "Verdana";

// Fill the pane background with a color gradient


myPane.Fill = new Fill( Color.White, Color.Goldenrod, 45.0f );
// No fill for the axis background
myPane.Chart.Fill.IsVisible = false;

// Set the legend to an arbitrary location


myPane.Legend.Position = LegendPos.Float ;
myPane.Legend.Location = new Location( 0.95f, 0.15f,
CoordType.PaneFraction,
AlignH.Right, AlignV.Top );
myPane.Legend.FontSpec.Size = 10f;
myPane.Legend.IsHStack = false;

// Add some pie slices


PieItem segment1 = myPane.AddPieSlice( 20, Color.Navy, Color.White,
45f, 0, "North" );
PieItem segment3 = myPane.AddPieSlice( 30, Color.Purple, Color.White,
45f, .0, "East" );
PieItem segment4 = myPane.AddPieSlice( 10.21, Color.LimeGreen,
Color.White, 45f, 0, "West" );
PieItem segment2 = myPane.AddPieSlice( 40, Color.SandyBrown,
Color.White, 45f, 0.2, "South" );
PieItem segment6 = myPane.AddPieSlice( 250, Color.Red, Color.White,
45f, 0, "Europe" );
PieItem segment7 = myPane.AddPieSlice( 50, Color.Blue, Color.White,
45f, 0.2, "Pac Rim" );
PieItem segment8 = myPane.AddPieSlice( 400, Color.Green, Color.White,
45f, 0, "South America" );
PieItem segment9 = myPane.AddPieSlice( 50, Color.Yellow, Color.White,
45f, 0.2, "Africa" );

segment2.LabelDetail.FontSpec.FontColor = Color.Red;

// Sum up the pie values


CurveList curves = myPane.CurveList ;
double total = 0 ;
for ( int x = 0 ; x < curves.Count ; x++ )
total += ((PieItem)curves[x]).Value ;

// Make a text label to highlight the total value


TextObj text = new TextObj( "Total 2004 Sales\n" + "$" + total.ToString
() + "M",
0.18F, 0.40F, CoordType.PaneFraction );
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Bottom;
text.FontSpec.Border.IsVisible = false ;
text.FontSpec.Fill = new Fill( Color.White, Color.FromArgb( 255, 100,
100 ), 45F );
text.FontSpec.StringAlignment = StringAlignment.Center ;
myPane.GraphObjList.Add( text );
// Create a drop shadow for the total value text item
TextObj text2 = new TextObj( text );
text2.FontSpec.Fill = new Fill( Color.Black );
text2.Location.X += 0.008f;
text2.Location.Y += 0.01f;
myPane.GraphObjList.Add( text2 );

// Calculate the Axis Scale Ranges


zgc.AxisChange();
}
Tutorial:Masterpane Chart Demo

C# Sample Code

// Call this method from the Form_Load method, passing your


ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
MasterPane myMaster = zgc.MasterPane;

myMaster.PaneList.Clear();

// Set the masterpane title


myMaster.Title.Text = "ZedGraph MasterPane Example";
myMaster.Title.IsVisible = true;

// Fill the masterpane background with a color gradient


myMaster.Fill = new Fill( Color.White, Color.MediumSlateBlue,
45.0F );

// Set the margins to 10 points


myMaster.Margin.All = 10;
// Enable the masterpane legend
myMaster.Legend.IsVisible = true;
myMaster.Legend.Position = LegendPos.TopCenter;

// Add a priority stamp


TextObj text = new TextObj( "Priority", 0.88F, 0.12F );
text.Location.CoordinateFrame = CoordType.PaneFraction;
text.FontSpec.Angle = 15.0F;
text.FontSpec.FontColor = Color.Red;
text.FontSpec.IsBold = true;
text.FontSpec.Size = 16;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Border.Color = Color.Red;
text.FontSpec.Fill.IsVisible = false;
text.Location.AlignH = AlignH.Left;
text.Location.AlignV = AlignV.Bottom;
myMaster.GraphObjList.Add( text );

// Add a draft watermark


text = new TextObj( "DRAFT", 0.5F, 0.5F );
text.Location.CoordinateFrame = CoordType.PaneFraction;
text.FontSpec.Angle = 30.0F;
text.FontSpec.FontColor = Color.FromArgb( 70, 255, 100, 100 );
text.FontSpec.IsBold = true;
text.FontSpec.Size = 100;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Center;
text.ZOrder = ZOrder.A_InFront;
myMaster.GraphObjList.Add( text );

// Initialize a color and symbol type rotator


ColorSymbolRotator rotator = new ColorSymbolRotator();

// Create some new GraphPanes


for ( int j = 0; j < 5; j++ )
{
// Create a new graph - rect dimensions do not matter here, since
it
// will be resized by MasterPane.AutoPaneLayout()
GraphPane myPane = new GraphPane( new Rectangle( 10, 10, 10, 10 ),
"Case #" + ( j + 1 ).ToString(),
"Time, Days",
"Rate, m/s" );

// Fill the GraphPane background with a color gradient


myPane.Fill = new Fill( Color.White, Color.LightYellow, 45.0F );
myPane.BaseDimension = 6.0F;

// Make up some data arrays based on the Sine function


PointPairList list = new PointPairList();
for ( int i = 0; i < 36; i++ )
{
double x = (double)i + 5;
double y = 3.0 * ( 1.5 + Math.Sin( (double)i * 0.2 + (double)j ) );
list.Add( x, y );
}

// Add a curve to the Graph, use the next sequential color and symbol
LineItem myCurve = myPane.AddCurve( "Type " + j.ToString(),
list, rotator.NextColor, rotator.NextSymbol );
// Fill the symbols with white to make them opaque
myCurve.Symbol.Fill = new Fill( Color.White );

// Add the GraphPane to the MasterPane


myMaster.Add( myPane );
}

using ( Graphics g = this.CreateGraphics() )


{
// Tell ZedGraph to auto layout the new GraphPanes
myMaster.SetLayout( g, PaneLayout.ExplicitRow32 );
}

zgc.AxisChange();
}
Using the samples without the ZedGraphControl
(Class Library Only)
These samples are setup to work specifically with the ZedGraphControl.
However, they can be easily adapted to work with the class library if you
make a few simple changes. First, instead of passing the ZedGraphControl,
just pass the GraphPane (or MasterPane, as appropriate). The top of the
CreateGraph() method should look like this:

public void CreateGraph( GraphPane myPane )

or for Visual Basic:

Private Sub CreateGraph(ByVal myPane As GraphPane)

At the end of the CreateGraph() method, there is a call to AxisChange()


using the ZedGraphControl. Change that call to this:

Graphics g = CreateGraphics();
myPane.AxisChange( g );
g.Dispose();

or for Visual Basic:

Dim g as Graphics = CreateGraphics()


myPane.AxisChange( g )
g.Dispose()

You might also like