/// <summary> /// Enable Zoom and Pan Controls. /// </summary> /// <param name="sender">The sender.</param> /// <param name="selectionChanged">Selection changed callabck. Triggered when user select a point with selec tool.</param> /// <param name="cursorMoved">Cursor moved callabck. Triggered when user move the mouse in chart area.</param> /// <remarks>Callback are optional.</remarks> public static void EnableZoomAndPanControls(this Chart sender, CursorPositionChanged selectionChanged, CursorPositionChanged cursorMoved) { if (!ChartTool.ContainsKey(sender)) { ChartTool[sender] = new ChartData(sender); ChartData ptrChartData = ChartTool[sender]; ptrChartData.Backup(); ptrChartData.SelectionChangedCallback = selectionChanged; ptrChartData.CursorMovedCallback = cursorMoved; //Populate Context menu Chart ptrChart = sender; if (ptrChart.ContextMenuStrip == null) { //Context menu is empty, use ChartContextMenuStrip directly ptrChart.ContextMenuStrip = new ContextMenuStrip(); ptrChart.ContextMenuStrip.Items.AddRange(ChartTool[ptrChart].MenuItems.ToArray()); } else { //User assigned context menu to chart. Merge current menu with ChartContextMenuStrip. ContextMenuStrip newMenu = new ContextMenuStrip(); newMenu.Items.AddRange(ChartTool[sender].MenuItems.ToArray()); foreach (object ptrItem in ChartTool[sender].ContextMenuStrip.Items) { if (ptrItem is ToolStripMenuItem) { newMenu.Items.Add(((ToolStripMenuItem)ptrItem).Clone()); } else if (ptrItem is ToolStripSeparator) { newMenu.Items.Add(new ToolStripSeparator()); } } newMenu.Items.Add(new ToolStripSeparator()); ptrChart.ContextMenuStrip = newMenu; ptrChart.ContextMenuStrip.AddHandlers(ChartTool[sender].ContextMenuStrip); } ptrChart.ContextMenuStrip.Opening += ChartContext_Opening; ptrChart.ContextMenuStrip.ItemClicked += ChartContext_ItemClicked; ptrChart.MouseDown += ChartControl_MouseDown; ptrChart.MouseMove += ChartControl_MouseMove; ptrChart.MouseUp += ChartControl_MouseUp; ptrChart.KeyDown += ChartControl_KeyDown; //Override settings. ChartArea ptrChartArea = ptrChart.ChartAreas[0]; ptrChartArea.CursorX.AutoScroll = false; ptrChartArea.CursorX.Interval = 1e-06; ptrChartArea.CursorY.AutoScroll = false; ptrChartArea.CursorY.Interval = 1e-06; ptrChartArea.AxisX.ScrollBar.Enabled = false; ptrChartArea.AxisX2.ScrollBar.Enabled = false; ptrChartArea.AxisY.ScrollBar.Enabled = false; ptrChartArea.AxisY2.ScrollBar.Enabled = false; SetChartControlState(sender, MSChartExtensionToolState.Select); } }
/// <summary> /// Enable Zoom and Pan Controls. /// </summary> /// <param name="sender">The sender.</param> /// <param name="selectionChanged">Selection changed callabck. Triggered when user select a point with selec tool.</param> /// <param name="cursorMoved">Cursor moved callabck. Triggered when user move the mouse in chart area.</param> /// <param name="zoomChanged">Callback triggered when chart has /// zoomed in or out (and on first painting of the chart).</param> /// <remarks>Callback are optional (pass in null to ignore).</remarks> public static void EnableZoomAndPanControls(this Chart sender, CursorPositionChanged selectionChanged, CursorPositionChanged cursorMoved, ZoomChanged zoomChanged = null) { if (!ChartTool.ContainsKey(sender)) { ChartTool[sender] = new ChartData(sender); ChartData ptrChartData = ChartTool[sender]; ptrChartData.Backup(); ptrChartData.SelectionChangedCallback = selectionChanged; ptrChartData.CursorMovedCallback = cursorMoved; ptrChartData.ZoomChangedCallback = zoomChanged; //Populate Context menu Chart ptrChart = sender; if (ptrChart.ContextMenuStrip == null) { //Context menu is empty, use ChartContextMenuStrip directly ptrChart.ContextMenuStrip = new ContextMenuStrip(); ptrChart.ContextMenuStrip.Items.AddRange(ChartTool[ptrChart].MenuItems.ToArray()); } else { //User assigned context menu to chart. Merge current menu with ChartContextMenuStrip. ContextMenuStrip newMenu = new ContextMenuStrip(); newMenu.Items.AddRange(ChartTool[sender].MenuItems.ToArray()); foreach (object ptrItem in ChartTool[sender].ContextMenuStrip.Items) { if (ptrItem is ToolStripMenuItem) { newMenu.Items.Add(((ToolStripMenuItem)ptrItem).Clone()); } else if (ptrItem is ToolStripSeparator) { newMenu.Items.Add(new ToolStripSeparator()); } } newMenu.Items.Add(new ToolStripSeparator()); ptrChart.ContextMenuStrip = newMenu; ptrChart.ContextMenuStrip.AddHandlers(ChartTool[sender].ContextMenuStrip); } ptrChart.ContextMenuStrip.Opening += ChartContext_Opening; ptrChart.ContextMenuStrip.ItemClicked += ChartContext_ItemClicked; ptrChart.MouseDown += ChartControl_MouseDown; ptrChart.MouseMove += ChartControl_MouseMove; ptrChart.MouseUp += ChartControl_MouseUp; ptrChart.PostPaint += ChartOnPostPaint; // Necessary to kickstart ZoomChanged callback // The following is for testing out the built-in events. // They don't seem to be as reliable as just handling mouse up/move/down //ptrChart.CursorPositionChanging += (sender1, e) => // { // // Changed event isn't triggered with any zoom or select operations! From looking at the Cursor.cs code, it seems to be a bug. // // Changing event is raised twice, once for each cursor (X, Y) // var axis = e.Axis; // }; //ptrChart.SelectionRangeChanging += (o, args) => // { // // Changed event isn't triggered with any zoom or select operations! // // Neither is changed event... odd // Console.WriteLine("SelectionRangeChanging raised " + args.ToString()); // var axis = args.Axis; // var chartArea = args.ChartArea; // }; //Override settings. ChartArea ptrChartArea = ptrChart.ChartAreas[0]; ptrChartArea.CursorX.AutoScroll = false; ptrChartArea.CursorX.Interval = 1e-06; ptrChartArea.CursorY.AutoScroll = false; ptrChartArea.CursorY.Interval = 1e-06; ptrChartArea.AxisX.ScrollBar.Enabled = false; ptrChartArea.AxisX2.ScrollBar.Enabled = false; ptrChartArea.AxisY.ScrollBar.Enabled = false; ptrChartArea.AxisY2.ScrollBar.Enabled = false; SetChartControlState(sender, MSChartExtensionToolState.Select); } }