Free cookie consent management tool by TermsFeed Policy Generator

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

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

Minor changes to EnhancedChart and the adapation of the RunCollectionBubbleChart (ticket #1237).

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