MasterPane: Handling Multiple Graphs

From ZedGraphWiki

Jump to: navigation, search

NOTE: The code on this page is for ZedGraph version 5. You can view the code for version 4 here.


The MasterPane class is designed to handle multiple GraphPane instances that are grouped together in an organized way. MasterPane is essentially a collection class for GraphPanes, with some handy utility methods to layout, draw, and resize the GraphPanes as a group. MasterPane also includes the ability to present a title, legend, and GraphItem's that are separate from any of the individual GraphPane's.

Using a MasterPane is very easy -- you just create how ever many GraphPanes you like, then add them to a MasterPane instance. For example, if you have five GraphPane's, with names of pane1, pane2, pane3, pane4, and pane5, you can put them in a MasterPane as shown below.

Contents

ZedGraphControl Usage

Using a MasterPane in the ZedGraphControl is trivial because the control is actually already using the MasterPane. By default, ZedGraphControl creates a MasterPane, and adds one GraphPane to the MasterPane. It also sets the MasterPane margins to zero so you don't even notice it's there unless you need it. The ZedGraphControl.GraphPane property is really just an alias for the first GraphPane in the MasterPane collection.

To add several GraphPane's, you should first delete the GraphPane that was automatically created by the ZedGraphControl, then add in your own GraphPanes. Painting and Resizing of the MasterPane are handled automatically by the ZedGraphControl. Here's an example (assume that the name of your ZedGraphControl is 'zg1'):

// First, clear out any old GraphPane's from the MasterPane collection
MasterPane master = zg1.MasterPane;
master.PaneList.Clear();

// Display the MasterPane Title, and set the outer margin to 10 points
master.Title.IsVisible = true;
master.Title.Text = "My MasterPane Title";
master.Margin.All = 10;

// Create some GraphPane's (normally you would add some curves too
GraphPane pane1 = new GraphPane();
GraphPane pane2 = new GraphPane();
GraphPane pane3 = new GraphPane();
GraphPane pane4 = new GraphPane();
GraphPane pane5 = new GraphPane();

// Add all the GraphPanes to the MasterPane
master.Add( pane1 );
master.Add( pane2 );
master.Add( pane3 );
master.Add( pane4 );
master.Add( pane5 );

// Refigure the axis ranges for the GraphPanes
zg1.AxisChange();

// Layout the GraphPanes using a default Pane Layout
using ( Graphics g = this.CreateGraphics() )
{
   master.SetLayout( g, PaneLayout.SquareColPreferred );
}

Class Library Usage

Here's how to create a MasterPane with the class library:

// Create an instance of a MasterPane class
// The rectangle is the total PaneRect for the MasterPane that will contain all the GraphPanes
MasterPane master = new MasterPane( "My MasterPane Title", new RectangleF( 10, 10, 640, 480 ) );

// Add all the GraphPanes to the MasterPane
master.Add( pane1 );
master.Add( pane2 );
master.Add( pane3 );
master.Add( pane4 );
master.Add( pane5 );

// Get a graphics instance to work with
using ( Graphics g = this.CreateGraphics() )
{
   // Refigure the axis ranges for the GraphPanes
   master.AxisChange( g );

   // Layout the GraphPanes using a default Pane Layout
   master.SetLayout( g, PaneLayout.SquareColPreferred );
}

In your Paint method, instead of calling GraphPane.Draw( g ), you would call:

MasterPane.Draw( g );

Whenever the graph needs to be resized, call:

MasterPane.Resize( g, newPaneRect )

Pane Layout Options

The MasterPane provides a wide range of pane layout options via the MasterPane.SetLayout method. The various options are described in detail on the MasterPane Layout Options page.

Utility Methods

For convenence, the MasterPane class includes the following utility methods:

Draw() 
Renders the MasterPane according to the current layout and including all GraphPane's that are a part of the MasterPane collection.
Resize() 
Resizes the MasterPane to the specified new PaneRect, re-arranges the pane layout to whatever layout was last setup via a MasterPane.AutoPaneLayout method call, and resizes all the PaneRect's of the GraphPanes that belong to the collection.
AxisChange() 
Calls GraphPane.AxisChange() for each of the GraphPanes in the collection.
SetLayout() 
Provides several overloads to allow a variety of pane layout options. This method resizes all the GraphPane's in the collection to fit within the PaneRect of the MasterPane according to the specified layout.
FindPane() 
Returns that GraphPane instance that contains the specified mouse click location.
FindChartRect() 
Returns the GraphPane instance that contains the specified mouse click location, but only if the mouse click lies inside the Chart.Rect.
FindNearestPaneObject() 
Finds the GraphPane, and the specific object within that GraphPane, at the specified mouse click location (see GraphPane.FindNearestObject).
Master[index] 
Gets an instance to the GraphPane that lies at the specified index position in the collection.