Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4636 was 4636, checked in by epitzer, 14 years ago

More improvements to EnhancedChart (#1237)

  • Fixed panning (now snaps to selected cursor interval)
  • Changed chart type in DataTableView to EnhancedChart
  • Added static method CustomizeChartArea()
  • Changed CustomizeChartAreas() to CustomizeAllChartAreas()
  • Disabled cursor cross hair
  • Smaller title font
File size: 7.2 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;
23using System.ComponentModel;
24using System.Drawing;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27namespace HeuristicLab.Visualization.ChartControlsExtensions {
28  public partial class EnhancedChart : Chart {
29    public EnhancedChart()
30      : base() {
31      InitializeComponent();
32      EnableDoubleClickResetsZoom = true;
33      EnableMiddleClickPanning = true;
34      CustomizeAllChartAreas();
35    }
36
37    [DefaultValue(true)]
38    public bool EnableDoubleClickResetsZoom { get; set; }
39    [DefaultValue(true)]
40    public bool EnableMiddleClickPanning { get; set; }
41
42    public static void CustomizeChartArea(ChartArea chartArea) {
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, 10);
52      }
53      chartArea.CursorX.Interval = 0;
54      chartArea.CursorY.Interval = 0;
55      chartArea.CursorX.IsUserSelectionEnabled = true;
56      chartArea.CursorY.IsUserSelectionEnabled = true;
57      chartArea.CursorX.IsUserEnabled = false;
58      chartArea.CursorY.IsUserEnabled = false;
59      chartArea.CursorX.SelectionColor = SystemColors.GradientActiveCaption;
60      chartArea.CursorY.SelectionColor = SystemColors.GradientActiveCaption;
61    }
62
63    public void CustomizeAllChartAreas() {
64      foreach (ChartArea chartArea in ChartAreas) {
65        CustomizeChartArea(chartArea);
66      }
67    }
68
69    #region Mouse event ehancements
70
71    protected override void OnMouseDoubleClick(MouseEventArgs e) {
72      if (EnableDoubleClickResetsZoom) {
73        HitTestResult result = HitTest(e.X, e.Y);
74        if (result.ChartArea != null && result.ChartElementType == ChartElementType.PlottingArea) {
75          foreach (var axis in result.ChartArea.Axes)
76            axis.ScaleView.ZoomReset(int.MaxValue);
77        }
78      }
79      base.OnMouseDoubleClick(e);
80    }
81
82    #region panning
83    private class PanningSupport {
84      public ChartArea ChartArea { get; private set; }
85
86      private Point PixelStartPosition;
87      private PointF ChartStartPosition;
88      private SizeF Pixel2ChartScale;
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        Pixel2ChartScale = new SizeF(
97          (float)chartArea.AxisX.ScaleView.Size /
98            (size.Width * chartArea.Position.Width * chartArea.InnerPlotPosition.Width / 100 / 100),
99          (float)chartArea.AxisY.ScaleView.Size /
100            (size.Height * chartArea.Position.Height * chartArea.InnerPlotPosition.Height / 100 / 100));
101      }
102
103      public double ChartX(double pixelX) {
104        return ChartStartPosition.X - (pixelX - PixelStartPosition.X) * Pixel2ChartScale.Width;
105      }
106      public double ChartY(double pixelY) {
107        return ChartStartPosition.Y + (pixelY - PixelStartPosition.Y) * Pixel2ChartScale.Height;
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);
131        double y = panning.ChartY(e.Location.Y);
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
143    #endregion
144
145    private void saveImageToolStripMenuItem_Click(object sender, System.EventArgs e) {
146      SaveFileDialog saveFileDialog = new SaveFileDialog();
147
148      // Sets the current file name filter string, which determines
149      // the choices that appear in the "Save as file type" or
150      // "Files of type" box in the dialog box.
151      saveFileDialog.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif";
152      saveFileDialog.FilterIndex = 2;
153      saveFileDialog.RestoreDirectory = true;
154
155      // Set image file format
156      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
157        ChartImageFormat format = ChartImageFormat.Bmp;
158        string filename = saveFileDialog.FileName.ToLower();
159        if (filename.EndsWith("bmp")) {
160          format = ChartImageFormat.Bmp;
161        } else if (filename.EndsWith("jpg")) {
162          format = ChartImageFormat.Jpeg;
163        } else if (filename.EndsWith("emf")) {
164          format = ChartImageFormat.EmfDual;
165        } else if (filename.EndsWith("gif")) {
166          format = ChartImageFormat.Gif;
167        } else if (filename.EndsWith("png")) {
168          format = ChartImageFormat.Png;
169        } else if (filename.EndsWith("tif")) {
170          format = ChartImageFormat.Tiff;
171        }
172
173        // Save image
174        SaveImage(saveFileDialog.FileName, format);
175      }
176    }
177
178    private void copyImageToClipboardBitmapToolStripMenuItem_Click(object sender, System.EventArgs e) {
179      System.IO.MemoryStream stream = new System.IO.MemoryStream();
180      SaveImage(stream, System.Drawing.Imaging.ImageFormat.Bmp);
181      Bitmap bmp = new Bitmap(stream);
182      Clipboard.SetDataObject(bmp);
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.