Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3/EnhancedChart.cs @ 4628

Last change on this file since 4628 was 4628, checked in by mkommend, 14 years ago

Minor changes in EnhancedChart (ticket #1237).

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.ComponentModel;
23using System.Drawing;
24using System.Windows.Forms;
25using System.Windows.Forms.DataVisualization.Charting;
26namespace HeuristicLab.Visualization.ChartControlsExtensions {
27  public partial class EnhancedChart : Chart {
28    public EnhancedChart()
29      : base() {
30      InitializeComponent();
31      EnableDoubleClickResetsZoom = true;
32      EnableMiddleClickPanning = true;
33    }
34
35    [DefaultValue(true)]
36    public bool EnableDoubleClickResetsZoom { get; set; }
37
38    [DefaultValue(true)]
39    public bool EnableMiddleClickPanning { get; set; }
40
41    public void InitializeChartAreas() {
42      ChartAreas.Clear();
43      ChartAreas.Add(CreateDefaultChartArea("ChartArea"));
44    }
45
46    public static ChartArea CreateDefaultChartArea(string name) {
47      ChartArea chartArea = new ChartArea(name);
48      chartArea.AxisX.MajorGrid.LineColor = SystemColors.GradientInactiveCaption;
49      chartArea.AxisY.MajorGrid.LineColor = SystemColors.GradientInactiveCaption;
50      chartArea.AxisX.MajorTickMark.TickMarkStyle = TickMarkStyle.AcrossAxis;
51      chartArea.AxisY.MajorTickMark.TickMarkStyle = TickMarkStyle.AcrossAxis;
52      chartArea.AxisX.ScrollBar.BackColor = Color.Transparent;
53      chartArea.AxisY.ScrollBar.BackColor = Color.Transparent;
54      chartArea.AxisX.ScrollBar.LineColor = Color.Transparent;
55      chartArea.AxisY.ScrollBar.LineColor = Color.Transparent;
56      chartArea.AxisX.ScrollBar.ButtonColor = SystemColors.GradientInactiveCaption;
57      chartArea.AxisY.ScrollBar.ButtonColor = SystemColors.GradientInactiveCaption;
58      chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
59      chartArea.AxisY.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
60      chartArea.AxisX.ScrollBar.Size = 12;
61      chartArea.AxisY.ScrollBar.Size = 12;
62      chartArea.CursorX.Interval = 0;
63      chartArea.CursorY.Interval = 0;
64      chartArea.CursorX.IsUserSelectionEnabled = true;
65      chartArea.CursorY.IsUserSelectionEnabled = true;
66      chartArea.CursorX.SelectionColor = SystemColors.GradientActiveCaption;
67      chartArea.CursorY.SelectionColor = SystemColors.GradientActiveCaption;
68      return chartArea;
69    }
70
71    #region Mouse event ehancements
72
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) {
77          foreach (var axis in result.ChartArea.Axes)
78            axis.ScaleView.ZoomReset();
79        }
80      }
81      base.OnMouseDoubleClick(e);
82    }
83
84    #region panning
85
86    private class PanningSupport {
87      public ChartArea ChartArea { get; private set; }
88
89      private Point PixelStartPosition;
90      private PointF ChartStartPosition;
91      private SizeF Pixel2ChartScale;
92
93      public PanningSupport(Point pixelStartPos, ChartArea chartArea, Size size) {
94        PixelStartPosition = pixelStartPos;
95        ChartArea = chartArea;
96        ChartStartPosition = new PointF(
97          (float)chartArea.AxisX.ScaleView.Position,
98          (float)chartArea.AxisY.ScaleView.Position);
99        Pixel2ChartScale = new SizeF(
100          (float)chartArea.AxisX.ScaleView.Size /
101            (size.Width * chartArea.Position.Width * chartArea.InnerPlotPosition.Width / 100 / 100),
102          (float)chartArea.AxisY.ScaleView.Size /
103            (size.Height * chartArea.Position.Height * chartArea.InnerPlotPosition.Height / 100 / 100));
104      }
105
106      public double ChartX(double pixelX) {
107        return ChartStartPosition.X - (pixelX - PixelStartPosition.X) * Pixel2ChartScale.Width;
108      }
109
110      public double ChartY(double pixelY) {
111        return ChartStartPosition.Y + (pixelY - PixelStartPosition.Y) * Pixel2ChartScale.Height;
112      }
113    }
114
115    private PanningSupport panning = null;
116
117    protected override void OnMouseDown(MouseEventArgs e) {
118      if (EnableMiddleClickPanning && e.Button == MouseButtons.Middle) {
119        HitTestResult result = HitTest(e.X, e.Y);
120        if (result.ChartArea != null)
121          panning = new PanningSupport(e.Location, result.ChartArea, Size);
122      }
123      base.OnMouseDown(e);
124    }
125
126    protected override void OnMouseUp(MouseEventArgs e) {
127      if (e.Button == MouseButtons.Middle && panning != null)
128        panning = null;
129      base.OnMouseUp(e);
130    }
131
132    protected override void OnMouseMove(MouseEventArgs e) {
133      if (panning != null) {
134        panning.ChartArea.AxisX.ScaleView.Scroll(panning.ChartX(e.Location.X));
135        panning.ChartArea.AxisY.ScaleView.Scroll(panning.ChartY(e.Location.Y));
136      }
137      base.OnMouseMove(e);
138    }
139
140    #endregion
141
142    #endregion
143
144    private void saveImageToolStripMenuItem_Click(object sender, System.EventArgs e) {
145      SaveFileDialog saveFileDialog = new SaveFileDialog();
146
147      // Sets the current file name filter string, which determines
148      // the choices that appear in the "Save as file type" or
149      // "Files of type" box in the dialog box.
150      saveFileDialog.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif";
151      saveFileDialog.FilterIndex = 2;
152      saveFileDialog.RestoreDirectory = true;
153
154      // Set image file format
155      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
156        ChartImageFormat format = ChartImageFormat.Bmp;
157        string filename = saveFileDialog.FileName.ToLower();
158        if (filename.EndsWith("bmp")) {
159          format = ChartImageFormat.Bmp;
160        } else if (filename.EndsWith("jpg")) {
161          format = ChartImageFormat.Jpeg;
162        } else if (filename.EndsWith("emf")) {
163          format = ChartImageFormat.EmfDual;
164        } else if (filename.EndsWith("gif")) {
165          format = ChartImageFormat.Gif;
166        } else if (filename.EndsWith("png")) {
167          format = ChartImageFormat.Png;
168        } else if (filename.EndsWith("tif")) {
169          format = ChartImageFormat.Tiff;
170        }
171
172        // Save image
173        SaveImage(saveFileDialog.FileName, format);
174      }
175    }
176
177    private void copyImageToClipboardBitmapToolStripMenuItem_Click(object sender, System.EventArgs e) {
178      System.IO.MemoryStream stream = new System.IO.MemoryStream();
179      SaveImage(stream, System.Drawing.Imaging.ImageFormat.Bmp);
180      Bitmap bmp = new Bitmap(stream);
181      Clipboard.SetDataObject(bmp);
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.