Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Visualization.ChartControlsExtensions/3.3/EnhancedChart.cs @ 6014

Last change on this file since 6014 was 6014, checked in by abeham, 13 years ago

#1465

  • added option to set fixed minimum and maximum for all four possible axis
  • added an icon to the properties context menu entry
    • added dependency from ChartControlsExtensions to Common.Resources
File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.ComponentModel;
24using System.Drawing;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Common.Resources;
28
29namespace HeuristicLab.Visualization.ChartControlsExtensions {
30  public partial class EnhancedChart : Chart {
31    public EnhancedChart()
32      : base() {
33      InitializeComponent();
34      propertiesToolStripMenuItem.Image = VSImageLibrary.Properties;
35      EnableDoubleClickResetsZoom = true;
36      EnableMiddleClickPanning = true;
37      CustomizeAllChartAreas();
38    }
39
40    [DefaultValue(true)]
41    public bool EnableDoubleClickResetsZoom { get; set; }
42    [DefaultValue(true)]
43    public bool EnableMiddleClickPanning { get; set; }
44    [DefaultValue(false)]
45    public bool ShowPropertiesContextMenuItem { get; set; }
46
47    public static void CustomizeChartArea(ChartArea chartArea) {
48      foreach (Axis axis in chartArea.Axes) {
49        axis.MajorGrid.LineColor = SystemColors.GradientInactiveCaption;
50        axis.MajorTickMark.TickMarkStyle = TickMarkStyle.AcrossAxis;
51        axis.ScrollBar.BackColor = Color.Transparent;
52        axis.ScrollBar.LineColor = Color.Gray;
53        axis.ScrollBar.ButtonColor = SystemColors.GradientInactiveCaption;
54        axis.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
55        axis.ScrollBar.Size = 12;
56        axis.TitleFont = new Font(axis.TitleFont.FontFamily, 10);
57      }
58      chartArea.CursorX.IsUserSelectionEnabled = true;
59      chartArea.CursorY.IsUserSelectionEnabled = true;
60      chartArea.CursorX.IsUserEnabled = false;
61      chartArea.CursorY.IsUserEnabled = false;
62      chartArea.CursorX.SelectionColor = Color.Gray;
63      chartArea.CursorY.SelectionColor = Color.Gray;
64    }
65
66    public void CustomizeAllChartAreas() {
67      foreach (ChartArea chartArea in ChartAreas) {
68        CustomizeChartArea(chartArea);
69      }
70    }
71
72    #region Mouse Event Ehancements
73    protected override void OnMouseDoubleClick(MouseEventArgs e) {
74      if (EnableDoubleClickResetsZoom) {
75        HitTestResult result = HitTest(e.X, e.Y);
76        if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea || result.ChartElementType == ChartElementType.Gridlines)) {
77          foreach (var axis in result.ChartArea.Axes)
78            axis.ScaleView.ZoomReset(int.MaxValue);
79        }
80      }
81      base.OnMouseDoubleClick(e);
82    }
83
84    #region Panning
85    private class PanningSupport {
86      public ChartArea ChartArea { get; private set; }
87
88      private Point PixelStartPosition;
89      private PointF ChartStartPosition;
90
91      public PanningSupport(Point pixelStartPos, ChartArea chartArea, Size size) {
92        PixelStartPosition = pixelStartPos;
93        ChartArea = chartArea;
94        ChartStartPosition = new PointF(
95          (float)chartArea.AxisX.ScaleView.Position,
96          (float)chartArea.AxisY.ScaleView.Position);
97      }
98
99      public double ChartX(double pixelX, int width) {
100        return ChartStartPosition.X - (pixelX - PixelStartPosition.X) *
101          (ChartArea.AxisX.ScaleView.ViewMaximum - ChartArea.AxisX.ScaleView.ViewMinimum) /
102            (width * ChartArea.Position.Width * ChartArea.InnerPlotPosition.Width / 100 / 100);
103      }
104      public double ChartY(double pixelY, int height) {
105        return ChartStartPosition.Y + (pixelY - PixelStartPosition.Y) *
106          (ChartArea.AxisY.ScaleView.ViewMaximum - ChartArea.AxisY.ScaleView.ViewMinimum) /
107            (height * ChartArea.Position.Height * ChartArea.InnerPlotPosition.Height / 100 / 100);
108      }
109    }
110
111    private PanningSupport panning = null;
112
113    protected override void OnMouseDown(MouseEventArgs e) {
114      if (EnableMiddleClickPanning && e.Button == MouseButtons.Middle) {
115        HitTestResult result = HitTest(e.X, e.Y);
116        if (result.ChartArea != null)
117          panning = new PanningSupport(e.Location, result.ChartArea, Size);
118      }
119      base.OnMouseDown(e);
120    }
121
122    protected override void OnMouseUp(MouseEventArgs e) {
123      if (e.Button == MouseButtons.Middle && panning != null)
124        panning = null;
125      base.OnMouseUp(e);
126    }
127
128    protected override void OnMouseMove(MouseEventArgs e) {
129      if (panning != null) {
130        double x = panning.ChartX(e.Location.X, Width);
131        double y = panning.ChartY(e.Location.Y, Height);
132        if (panning.ChartArea.CursorX.Interval > 0) {
133          x = Math.Round(x / panning.ChartArea.CursorX.Interval) * panning.ChartArea.CursorX.Interval;
134          y = Math.Round(y / panning.ChartArea.CursorY.Interval) * panning.ChartArea.CursorY.Interval;
135        }
136        panning.ChartArea.AxisX.ScaleView.Scroll(x);
137        panning.ChartArea.AxisY.ScaleView.Scroll(y);
138      }
139      base.OnMouseMove(e);
140    }
141    #endregion
142    #endregion
143
144    private void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
145      propertiesToolStripSeparator.Visible = ShowPropertiesContextMenuItem;
146      propertiesToolStripMenuItem.Visible = ShowPropertiesContextMenuItem;
147    }
148
149    private void exportChartToolStripMenuItem_Click(object sender, System.EventArgs e) {
150      // Set image file format
151      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
152        ChartImageFormat format = ChartImageFormat.Bmp;
153        string filename = saveFileDialog.FileName.ToLower();
154        if (filename.EndsWith("bmp")) {
155          format = ChartImageFormat.Bmp;
156        } else if (filename.EndsWith("jpg")) {
157          format = ChartImageFormat.Jpeg;
158        } else if (filename.EndsWith("emf")) {
159          format = ChartImageFormat.EmfDual;
160        } else if (filename.EndsWith("gif")) {
161          format = ChartImageFormat.Gif;
162        } else if (filename.EndsWith("png")) {
163          format = ChartImageFormat.Png;
164        } else if (filename.EndsWith("tif")) {
165          format = ChartImageFormat.Tiff;
166        }
167
168        // Save image
169        SaveImage(saveFileDialog.FileName, format);
170      }
171    }
172
173    private void copyImageToClipboardBitmapToolStripMenuItem_Click(object sender, System.EventArgs e) {
174      System.IO.MemoryStream stream = new System.IO.MemoryStream();
175      SaveImage(stream, System.Drawing.Imaging.ImageFormat.Bmp);
176      Bitmap bmp = new Bitmap(stream);
177      Clipboard.SetDataObject(bmp);
178    }
179
180    private void propertiesToolStripMenuItem_Click(object sender, EventArgs e) {
181      OnPropertiesClicked();
182    }
183
184    public event EventHandler PropertiesClicked;
185    private void OnPropertiesClicked() {
186      var handler = PropertiesClicked;
187      if (handler != null) handler(this, EventArgs.Empty);
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.