Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Visualization.ChartControlsExtensions/3.3/EnhancedChart.cs @ 6307

Last change on this file since 6307 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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