MSChart Extension V2

Introduction

MSChart Extension is an extension class for Microsoft Chart (MSChart) control in Visual Studio for WinForms applications. The tool was first published on July 2012 (See: MSChart Extension - Zoom and Pan Control) with the intention to overcome some of the limitation from the original MSChart. If you are new to MSChart Extension, we recommend you to read the previous article first.

Version 2 is created based on Version 1 code base with new features and improvement introduced.
The source code is also hosted on GitHub with the compiled package is released as NuGet Package.

WARNING: The Version 2 released is not 100% backward compatible with Version 1, details as below.

Known Issue

  • MSChart Extensions is designed for chart type with X and Y Axis, the extension method will not work with some of the chart type such as Radar and Pie.
  • It is known that zoom does not works properly with Log Axis. We decided to disable extensions functions for chart with LOG Axis.
  • Date Time Axis - Zoom may not behave correctly for chart where XAxis values in DateTime format.

What's New


Support Multiple Chart Area

The initial implementation of MSChart Extension was created based on assumption that one chart for every single chart object. Since it is possible to create more than one chart within a chart object, we decided to review the original implementation to support multiple chart areas. Unfortunately, this had break some portion of the code which make it not 100% backward compatible with Version 1. New argument class ChartOption is added to accommodate more configuration options as well as further expansion in future.

    public static void EnableZoomAndPanControls(this Chart sender,
        CursorPositionChanged selectionChanged,
        CursorPositionChanged cursorMoved,
        ZoomChanged zoomChanged = null, ChartOption option = null)

Method signature for CursorPositionChanged and ZoomChanged had been updated as follow:

Version 2 (Breaking Change)

    public delegate void ZoomChanged(Chart sender);
    public delegate void CursorPositionChanged(Chart sender, ChartCursor cursor);

Version 1 (Previous Implementation)

    private delegate void CursorPositionChanged(double x, double y);
    private delegate void ZoomChanged(ChartExtents extents);

In order to support multiple chart area, we need to know which chart area is the mouse pointer currently located. A private method named ChartAreaHitTest is implemented to identify the mouse pointer positions. Besides, we also created a list of supported Chart Type in order to disable the controls for non-supported chart type, such as Radar and Pie. We also disable the extensions functions when mouse pointer is not located in any of the chart area.

Dual Cursor

Another additional cursor is added to Version 2 which allow user to calculate different between 2 points. New data class ChartCursor is introduced to hold the related information for both cursors: Cursor1 and Cursor2.

    public class ChartCursor : ICloneable
    {
        public double X { get; set; }
        public double Y { get; set; }
        public ChartArea ChartArea { get; set; } = null;
        public object Clone()
        {
            return new ChartCursor() { X = this.X, Y = this.Y,
               ChartArea = this.ChartArea };
        }
    }

Object for both cursors are accessible from the following extensions methods:

    public static ChartCursor Cursor1(this Chart sender);
    public static ChartCursor Cursor2(this Chart sender);
    public static PointF CursorsDiff(this Chart sender);

Appearance of each cursor: Color, Width and DashStyle for each are configurable in ChartOption, a variable which passed to extension class in EnableZoomAndPanControls as stated above.

In order to support dual cursor, the previous implementation for chart cursor which realize using the internal user interface fro MSChart is dropped, replaced with cursors which implemented with Annotations. Thus, clearing the annotations associated with chart will remove both cursors as well.

Mouse Wheel Actions

Mouse Wheel action is added from version 2.0.0 onward as stated below:
  • Wheel Scroll: Scroll zoomed chart horizontally, where wheel up = scroll right and wheel down = scroll left.
  • SHIFT + Wheel Scroll: Scroll zoomed chart vertically, where wheel up = scroll up and wheel down = scroll down.
  • CTRL + Wheel Scroll: Zoom In / Out Chart.

The MouseWheel event is handle in the following method in MSChartExtension class:

    private static void ChartControl_MouseWheel(object sender, MouseEventArgs e);

Zoom Functions

Besides "Zoom Window", "Zoom XAxis" and "Zoom Out" which is carry over from Version 1, "Zoom YAxis", "Zoom Dialog" are introduced in Version 2.
  • Zoom Out: Reset zoom and display the entire chart.
  • Zoom Window: Zoom into a chart by drawing a rectangle on the chart with mouse.
  • Zoom XAxis: Zoom along XAxis without changing the YAxis
  • Zoom YAxis (Added in V2.0): Zoom along YAxis without changing the XAxis.
  • Zoom Dialog (Added in V2.0): Let user define the MIN and MAX value for each X and Y Axis manually input from a dialog box.

Enable / Disable Series

With multiple chart area support installed, context menu (right click on mouse button) in any chart area will only show a series which is assigned to the respective chart area. This option can enable / disable in ChartOption.ContextMenuAllowToHideSeries. Setting this flag to false will disable user to show / hide series interactively. 

Refactored Chart Extents

Methods to return the entire chart area GetBoundariesOfData and visible chart area GetBoundariesOfVisibleData is replaced with new methods named GetChartAreaBoundary and GetChartVisibleAreaBoundary with refactored implementations.

    public static RectangleF GetChartAreaBoundary(this ChartArea sender, 
        bool primaryAxis = true);
    public static RectangleF GetChartVisibleAreaBoundary(this ChartArea sender, 
        bool primaryAxis = true);

These two methods return 4 points coordinate based on X and Y Axis values which represent the chart region.

Using the Code - Programmer's Reference

  • Install the latest package from NuGet Package Manage in Visual Studio. In such, you will get notified with the latest package once it's released.
  • Configure chart area and series.
  • Execute EnableZoomAndPanControls to enable extensions features.
  • It is recommended to DisableZoomAndPanControls prior to add / remove ChartArea or Series.

Using the Tool - User Manual

For end users, most of the functions are accessible from chart context menu (on right mouse click) with mouse pointer hover over the desired chart area. A quick reference for functions which associated with mouse wheel control is described in About dialog as shown below.

Other configurations / settings such as the appearance of the chart cursor might be accessible depends if it is make accessible by the developers.

Reference

Acknowledgement

Popular posts from this blog

(AGauge) WinForms Gauge Control - Discontinued

C# DSP Simulation - DSP Lab

WinForms Controls