Control ZendGraph en C
Control ZendGraph en C
Control ZendGraph en C
A Graph that shows how to move the intersection of the axes to a location
other than the bottom left corner.
Shows how you can plot in polar coordinates, and simulate polar scale
decorations
Scatter Plot Demo
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.
Shows how to fill individual points with a color that varies according
to a data value.
Shows how to stack curves so the values are summed, and to fill the area
between the curves with a color gradient.
zgc.AxisChange();
}
Axis Cross Demo
// Leave some extra space on top for the labels to fit within the chart
rect
myPane.YAxis.Scale.MaxGrace = 0.2;
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;
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;
}
// 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);
}
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
zgc.AxisChange();
}
BaseTic Demo
// 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;
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 );
C# Sample Code
// 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;
Sample Code in C#
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.
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.
Shows a clustered, horizontal bar chart with a value label placed to the
right of each bar.
ErrorBar Demo
HiLowBar Demo
Hi-Low-Close Demo
C# Code Sample
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;
// 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 );
}
Open-High-Low-Close Demo
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 );
}
// 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;
C# Code Sample
myCurve.Bar.Fill.RangeMin = 0;
myCurve.Bar.Fill.RangeMax = 4;
C# Code Sample
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 );
zgc.AxisChange();
zgc.AxisChange();
}
Horizontal Bar With Labels Demo
// 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 );
zgc.AxisChange();
}
ErrorBar Demo
zgc.AxisChange();
// Set the bar type to stack, which stacks the bars by automatically
accumulating the values
myPane.BarSettings.Type = BarType.Stack;
// Shift the text items up by 5 user scale units above the bars
const float shift = 5;
// Indicate that the bars are overlay type, which are drawn on top of
eachother
myPane.BarSettings.Type = BarType.Overlay;
// 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
// 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;
slices.
C# Sample Code
segment2.LabelDetail.FontSpec.FontColor = Color.Red;
}
Special Charts
General demonstration graph with bars, lines, semi-transparent fill,
labels, and banded ranges.
Shows how to make a 2D contour plot using regular LineItems with fills.
SampleMultiPointList Demo
Spider Demo
spider_graph_150.gif
Shows how to make the plot area transparent so the chart can be overlaid
on a background image.
Combo Chart Demo
C# Code Sample
LineItem curve;
// 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;
C# Code Sample
zgc.AxisChange();
}
Contour Chart Demo
C# Sample Code
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 );
zgc.AxisChange();
}
SampleMultiPointList Demo
C# Code Sample
zgc.AxisChange();
}
MasterPane Demo
C# Code Sample
myMaster.PaneList.Clear();
C# Code Sample
myMaster.PaneList.Clear();
}
Spider Demo
Image:Spider.png
C# Code Sample
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;
C# Code Sample
// 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 ) );
// Get an image for the background (use your own filename here)
Image image = Bitmap.FromFile( @"c:\temp\ngc4414.jpg" );
C# Sample Code
C# Sample Code
// 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 );
C# Sample Code
C# Sample Code
C# Code Sample
C# Sample Code
// Set the bar type to stack, which stacks the bars by automatically
accumulating the values
myPane.BarSettings.Type = BarType.Stack;
C# Code Sample
C# Sample Code
segment2.LabelDetail.FontSpec.FontColor = Color.Red;
C# Sample Code
myMaster.PaneList.Clear();
// 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 );
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:
Graphics g = CreateGraphics();
myPane.AxisChange( g );
g.Dispose();