Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Visualization.ChartControlsExtensions/3.3/EnhancedChart.cs @ 15632

Last change on this file since 15632 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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