Pan Zedgraph Like A Pro: Easy Steps To Success

how to pan zedgraph

ZedGraph is a class library, Windows Forms UserControl, and ASP web-accessible control for creating 2D line, bar, and pie graphs of arbitrary datasets. It is maintained as an open-source development project on SourceForge. The default behaviour is to zoom with the left mouse button and to pan with the middle mouse button. However, the mouse controls can be remapped. The context menu allows users to un-pan to prior states, and the graph attributes can be modified in a wide variety of ways.

Characteristics Values
Panning Middle mouse button
Zooming Left mouse button
Graph Types 2D line, bar, and pie graphs
Graph Flexibility Almost every aspect of the graph can be user-modified
Graph Compatibility .NET 1.1, .NET 2.0, VS .NET 2005
Graph Customization Axis labels, titles, legends, text labels, arrows, images

cycookery

Remapping mouse controls

To remap mouse controls in ZedGraph, you can modify the Pan and Zoom behaviours. By default, ZedGraph allows panning with the middle mouse button and zooming with the left mouse button.

To enable panning with the left mouse button, you can use the following code:

Zg1.PanButtons = MouseButtons.Left;

Zg1.PanModifierKeys = Keys.None;

This will allow you to pan by clicking and dragging with the left mouse button. However, as mentioned in the Stack Overflow thread, you may need to specify the "Control" for the Modifier Keys in the code, such as in the Form's Load event handler.

Additionally, you can control the step size fraction for panning with the mouse wheel using the "PanStep" property:

Public int PanStep { get; set; } = 4;

You can also disable the zoom-on-mousewheel behaviour by setting the "IsEnableWheelZoom" property to false:

ZedGraphControl1.IsEnableWheelZoom = false;

Alternatively, you can disable the tab page from listening to the mouse wheel scroll event to prevent unintended zooming on the tab page.

cycookery

Panning with the left mouse button

To enable panning with the left mouse button, you can use the following code:

Zg1.PanButtons = MouseButtons.Left;

Zg1.PanModifierKeys = Keys.None;

This code snippet sets the left mouse button as the button for panning and specifies that no modifier key needs to be pressed along with it. You can add this code to your ZedGraphControl to customize the mouse behavior.

It is worth noting that the designer properties window might not allow you to set the modifier key to "Control" directly. In such cases, you can put the desired behavior in code, such as in the Form's Load event handler.

By implementing these adjustments, you can effectively enable panning with just the left mouse button in your ZedGraph charts, providing a more intuitive or convenient interaction for your users or yourself.

cycookery

Zooming capabilities

ZedGraph offers a range of zooming capabilities that can be customized to fit your specific needs. The default behavior for zooming is to use the left mouse button, while panning is typically achieved with the middle mouse button. However, you can easily modify these settings to suit your preferences.

To enable zooming with a specific mouse button configuration, you can utilize the ZedGraphControl properties. For example, to zoom by holding down the Ctrl key and dragging with the left mouse button, you can make the necessary adjustments. This customization provides a more tailored experience, allowing you to choose the mouse button setup that works best for your workflow.

Additionally, the context menu in ZedGraph offers further flexibility. This menu appears when you right-click on the ZedGraphControl, providing a range of options. With this menu, you can un-zoom to prior states, restoring the scale to full auto mode. This feature ensures that you can easily navigate back to previous zoom levels and quickly adjust the graph's scale as needed.

The context menu also offers other valuable tools, such as the ability to un-pan, copy the graph to the clipboard, and access point value tooltips. These features enhance your interaction with the graph, providing a comprehensive set of tools for analysis and presentation.

By combining the default zooming capabilities with the customizable options and the convenient context menu, ZedGraph empowers users with a versatile set of tools for exploring and manipulating graphs to meet their specific requirements.

cycookery

Modifying graph attributes

ZedGraph allows users to modify graph attributes in a wide variety of ways. The graph is modified by accessing properties in each of the classes provided in ZedGraph. Each part of the graph is encapsulated in a class structure, which has modifiable properties to control the output.

The ZedGraphControl class provides interactive functionality for zooming and panning. To zoom, left-click inside the AxisRect area and drag out a new rectangle to indicate the scale region. To pan, either click with the middle mouse button or hold down the shift key and left-click to drag the graph around. The zoom/pan key combinations are also user-modifiable. Scrollbars can be added to the control with IsShowHScrollBar and IsShowVScrollBar, and the mouse wheel can be used for zooming.

The Fill class is an important addition to ZedGraph, allowing for seemingly scattered data to gain visual coherency using the color attribute. The GraphPane.CurveList and GraphPane.GraphObjList contain the graphic items in the display. CurveList contains all the curves, including bars and lines, while GraphObjList contains text items, images, shapes, and arrows. The Z-Order is controlled by the order of the objects in the list, with the first objects appearing in front of the later objects. The order of any object can be modified relative to other objects in the same collection (list) with the Move() method in the collection class.

The GraphObj class has a ZOrder property that controls the depth of each individual GraphObj relative to other, non-GraphObj objects. The ZOrder is an enumeration type with the following values: FillType.GradientByZ.

Other modifiable attributes include the ability to add axis labels and titles, a legend, text labels, and arrows. ZedGraph also handles 2D line/scatter graphs, horizontal/vertical bar graphs, stacked bar charts, stacked percent bar charts, error bar charts, open-high-low-close charts, Japanese candlestick charts, and pie charts.

cycookery

Using the context menu

The context menu in ZedGraph allows for various customisations and modifications. To edit the context menu, you must first subscribe to the ContextMenuBuilder event. This can be done in the Forms designer in Visual Studio by right-clicking on the ZedGraphControl in the form, selecting "properties", clicking the yellow lightning bolt, and then clicking in the empty box to the right of ContextMenuBuilder.

Once you have subscribed to the ContextMenuBuilder event, you will be able to edit the context menu before it is displayed. The context menu is built "on-the-fly" each time the user right-clicks in the GraphPane area. After the menu is constructed, but before it is displayed, you can edit the menu items. For example, you can remove or disable menu items, such as the "Set Scale to Default" option.

You can also add new menu items with custom text and functionality. This is done by creating a new ToolStripMenuItem, assigning it a name and tag, and then defining the text that will appear in the menu. You can also add a handler to respond when the menu item is selected.

Additionally, there have been feature requests and discussions around localising the context menu for different languages, and issues with the ContextMenuBuilder routine being called twice, resulting in added context menu items appearing twice.

Frequently asked questions

ZedGraph is a class library, Windows Forms UserControl, and ASP web-accessible control for creating 2D line, bar, and pie graphs of arbitrary datasets.

To pan ZedGraph, you can use the middle mouse button or hold down Shift and drag the left mouse button.

To change the panning settings, you can modify the PanButtons and PanModifierKeys properties of the ZedGraphControl. For example, to enable panning with just the left mouse button, you can set zg1.PanButtons = MouseButtons.Left and zg1.PanModifierKeys = Keys.None.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment