Skip to content

Instantly share code, notes, and snippets.

@Larry57
Last active December 28, 2015 15:49
Show Gist options
  • Save Larry57/7524673 to your computer and use it in GitHub Desktop.
Save Larry57/7524673 to your computer and use it in GitHub Desktop.
ZedGraph extensions. - Independant Pan and Zoom Y axes - YAxis order change in GraphPane - GraphPane order change in MasterPane
public static class ZedGraphExtensions
{
/// <summary>
/// Change the Y axes order
/// </summary>
/// <param name="list">YAxisList</param>
/// <param name="pane">GraphPane</param>
/// <param name="index">index</param>
/// <param name="relativePos">relative position</param>
/// <returns>new index</returns>
public static int Move(this YAxisList list, GraphPane pane, int index, int relativePos)
{
if (index < 0 || index >= list.Count)
return -1;
var axis = list[index];
list.RemoveAt(index);
var newIndex = index + relativePos;
if (newIndex < 0)
newIndex = 0;
if (newIndex > list.Count)
newIndex = list.Count;
list.Insert(newIndex, axis);
foreach (var curve in pane.CurveList)
if (curve.YAxisIndex == newIndex)
curve.YAxisIndex = index;
else if (curve.YAxisIndex == index)
curve.YAxisIndex = newIndex;
return newIndex;
}
/// <summary>
/// Change the panes order
/// </summary>
/// <param name="list">PaneList</param>
/// <param name="index">index</param>
/// <param name="relativePos">relative position</param>
/// <returns>new index</returns>
public static int Move(this PaneList list, int index, int relativePos)
{
if (index < 0 || index >= list.Count)
return -1;
var pane = list[index];
list.RemoveAt(index);
var newIndex = index + relativePos;
if (newIndex < 0)
newIndex = 0;
if (newIndex > list.Count)
newIndex = list.Count;
list.Insert(newIndex, pane);
return newIndex;
}
public static void EnableYAxesZoomPan(this ZedGraphControl z, bool enable)
{
if (enable)
{
dic.Add(z, new zBag());
z.MouseDownEvent += z_MouseDownEvent;
z.MouseMoveEvent += z_MouseMoveEvent;
z.MouseUpEvent += z_MouseUpEvent;
z.MouseWheel += z_MouseWheel;
}
else
{
dic.Remove(z);
z.MouseDownEvent -= z_MouseDownEvent;
z.MouseMoveEvent -= z_MouseMoveEvent;
z.MouseUpEvent -= z_MouseUpEvent;
z.MouseWheel -= z_MouseWheel;
}
}
static bool z_MouseDownEvent(ZedGraphControl z, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Left)
return false;
var bag = dic[z];
if (bag.hoveredYAxis == null)
return false;
bag.movedYAxisStartY = e.Location.Y;
bag.movedYAxisMin = bag.hoveredYAxis.Scale.Min;
bag.movedYAxisMax = bag.hoveredYAxis.Scale.Max;
return true;
}
static bool z_MouseMoveEvent(ZedGraphControl z, System.Windows.Forms.MouseEventArgs e)
{
var pt = e.Location;
var bag = dic[z];
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (bag.hoveredYAxis == null)
return false;
var yOffset = bag.hoveredYAxis.Scale.ReverseTransform(pt.Y) - bag.hoveredYAxis.Scale.ReverseTransform(bag.movedYAxisStartY);
bag.hoveredYAxis.Scale.Min = bag.movedYAxisMin - yOffset;
bag.hoveredYAxis.Scale.Max = bag.movedYAxisMax - yOffset;
z.Invalidate();
return true;
}
var foundObject = findZedGraphObject(z);
bag.hoveredYAxis = foundObject as YAxis;
if (bag.hoveredYAxis != null)
{
z.Cursor = Cursors.SizeNS;
return true;
}
if (z.IsShowPointValues)
{
z.Cursor = Cursors.Cross;
return false;
}
z.Cursor = Cursors.Default;
return true;
}
static void z_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
var z = sender as ZedGraphControl;
var bag = dic[z];
if (bag.hoveredYAxis == null)
return;
var direction = e.Delta < 1 ? -.05f : .05f;
var increment = direction * (bag.hoveredYAxis.Scale.Max - bag.hoveredYAxis.Scale.Min);
var newMin = bag.hoveredYAxis.Scale.Min + increment;
var newMax = bag.hoveredYAxis.Scale.Max - increment;
bag.hoveredYAxis.Scale.Min = newMin;
bag.hoveredYAxis.Scale.Max = newMax;
z.AxisChange();
z.Invalidate();
}
static bool z_MouseUpEvent(ZedGraphControl z, System.Windows.Forms.MouseEventArgs e)
{
var bag = dic[z];
bag.hoveredYAxis = null;
return false;
}
static object findZedGraphObject(ZedGraphControl z)
{
var pt = z.PointToClient(Control.MousePosition);
var foundPane = z.MasterPane.FindPane(pt);
if (foundPane == null)
return null;
var bag = dic[z];
bag.foundPane = foundPane;
object foundObject;
int forget;
using (var g = z.CreateGraphics())
if (bag.foundPane.FindNearestObject(pt, g, out foundObject, out forget))
return foundObject;
return null;
}
static Dictionary<ZedGraphControl, zBag> dic = new Dictionary<ZedGraphControl, zBag>();
class zBag
{
internal GraphPane foundPane;
internal YAxis hoveredYAxis;
internal float movedYAxisStartY;
internal double movedYAxisMin;
internal double movedYAxisMax;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment