/// <summary> /// Get current control state. /// </summary> /// <param name="sender"></param> /// <returns></returns> public static MSChartExtensionToolState GetChartToolState(this Chart sender) { if (!ChartTool.ContainsKey(sender)) { return(MSChartExtensionToolState.Unknown); } else { return(ChartTool[sender].ToolState); } }
private static void ChartContext_Opening(object sender, CancelEventArgs e) { ContextMenuStrip menuStrip = (ContextMenuStrip)sender; Chart senderChart = (Chart)menuStrip.SourceControl; ChartData ptrData = ChartTool[senderChart]; //Check Zoomed state if (senderChart.ChartAreas[0].AxisX.ScaleView.IsZoomed || senderChart.ChartAreas[0].AxisY.ScaleView.IsZoomed || senderChart.ChartAreas[0].AxisY2.ScaleView.IsZoomed) { ptrData.ChartToolZoomOut.Visible = true; ptrData.ChartToolZoomOutSeparator.Visible = true; } else { ptrData.ChartToolZoomOut.Visible = false; ptrData.ChartToolZoomOutSeparator.Visible = false; } //Get Chart Control State if (!ChartTool.ContainsKey(senderChart)) { //Initialize Chart Tool SetChartControlState(senderChart, MSChartExtensionToolState.Select); } //Update menu (uncheck all, check current) based on current state. ptrData.UpdateState(); //Update series for (int x = 0; x < menuStrip.Items.Count; x++) { if (menuStrip.Items[x].Tag != null) { if (menuStrip.Items[x].Tag.ToString() == "Series") { menuStrip.Items.RemoveAt(x); x--; } } } SeriesCollection chartSeries = ((Chart)menuStrip.SourceControl).Series; foreach (Series ptrSeries in chartSeries) { ToolStripItem ptrItem = menuStrip.Items.Add(ptrSeries.Name); ToolStripMenuItem ptrMenuItem = (ToolStripMenuItem)ptrItem; ptrMenuItem.Checked = ptrSeries.Enabled; ptrItem.Tag = "Series"; } }
/// <summary> /// Disable Zoom and Pan Controls /// </summary> /// <param name="sender"></param> public static void DisableZoomAndPanControls(this Chart sender) { Chart ptrChart = sender; ptrChart.ContextMenuStrip = null; if (ChartTool.ContainsKey(ptrChart)) { ptrChart.MouseDown -= ChartControl_MouseDown; ptrChart.MouseMove -= ChartControl_MouseMove; ptrChart.MouseUp -= ChartControl_MouseUp; ChartTool[ptrChart].Restore(); ChartTool.Remove(ptrChart); } }
/// <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); } }