Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Visualization.ChartControlsExtensions/3.3/EnhancedChart.cs @ 14670

Last change on this file since 14670 was 14670, checked in by abeham, 7 years ago

#2728: merged r14651 to stable

File size: 6.9 KB
RevLine 
[4614]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4614]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
[4636]22using System;
[4621]23using System.ComponentModel;
[4614]24using System.Drawing;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
[4637]27
[4614]28namespace HeuristicLab.Visualization.ChartControlsExtensions {
29  public partial class EnhancedChart : Chart {
[6640]30    private ImageExportDialog exportDialog;
31
[4614]32    public EnhancedChart()
33      : base() {
[4628]34      InitializeComponent();
[6640]35      exportDialog = new ImageExportDialog(this);
[4621]36      EnableDoubleClickResetsZoom = true;
37      EnableMiddleClickPanning = true;
[4636]38      CustomizeAllChartAreas();
[4614]39    }
40
[4621]41    [DefaultValue(true)]
42    public bool EnableDoubleClickResetsZoom { get; set; }
43    [DefaultValue(true)]
44    public bool EnableMiddleClickPanning { get; set; }
45
[4636]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;
[4637]51        axis.ScrollBar.LineColor = Color.Gray;
[4636]52        axis.ScrollBar.ButtonColor = SystemColors.GradientInactiveCaption;
[4637]53        axis.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
[4636]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;
[4637]61      chartArea.CursorX.SelectionColor = Color.Gray;
62      chartArea.CursorY.SelectionColor = Color.Gray;
[4636]63    }
64
65    public void CustomizeAllChartAreas() {
[4635]66      foreach (ChartArea chartArea in ChartAreas) {
[4636]67        CustomizeChartArea(chartArea);
[4635]68      }
[4621]69    }
70
[4637]71    #region Mouse Event Ehancements
[4621]72    protected override void OnMouseDoubleClick(MouseEventArgs e) {
73      if (EnableDoubleClickResetsZoom) {
74        HitTestResult result = HitTest(e.X, e.Y);
[4637]75        if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea || result.ChartElementType == ChartElementType.Gridlines)) {
[4621]76          foreach (var axis in result.ChartArea.Axes)
[14670]77            axis.ScaleView.ZoomReset(0);
[4621]78        }
79      }
80      base.OnMouseDoubleClick(e);
81    }
82
[4637]83    #region Panning
[4621]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
[4638]98      public double ChartX(double pixelX, int width) {
[4654]99        return ChartStartPosition.X - (pixelX - PixelStartPosition.X) *
[4638]100          (ChartArea.AxisX.ScaleView.ViewMaximum - ChartArea.AxisX.ScaleView.ViewMinimum) /
101            (width * ChartArea.Position.Width * ChartArea.InnerPlotPosition.Width / 100 / 100);
[4621]102      }
[4638]103      public double ChartY(double pixelY, int height) {
[4654]104        return ChartStartPosition.Y + (pixelY - PixelStartPosition.Y) *
[4638]105          (ChartArea.AxisY.ScaleView.ViewMaximum - ChartArea.AxisY.ScaleView.ViewMinimum) /
106            (height * ChartArea.Position.Height * ChartArea.InnerPlotPosition.Height / 100 / 100);
[4621]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) {
[4638]129        double x = panning.ChartX(e.Location.X, Width);
130        double y = panning.ChartY(e.Location.Y, Height);
[4636]131        if (panning.ChartArea.CursorX.Interval > 0) {
[4646]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;
[4636]134        }
135        panning.ChartArea.AxisX.ScaleView.Scroll(x);
136        panning.ChartArea.AxisY.ScaleView.Scroll(y);
[4621]137      }
138      base.OnMouseMove(e);
139    }
140    #endregion
141    #endregion
142
[6640]143    private void exportChartToolStripMenuItem_Click(object sender, EventArgs e) {
[4614]144      // Set image file format
145      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
146        ChartImageFormat format = ChartImageFormat.Bmp;
[4621]147        string filename = saveFileDialog.FileName.ToLower();
148        if (filename.EndsWith("bmp")) {
[4614]149          format = ChartImageFormat.Bmp;
[4621]150        } else if (filename.EndsWith("jpg")) {
[4614]151          format = ChartImageFormat.Jpeg;
[4621]152        } else if (filename.EndsWith("emf")) {
[4614]153          format = ChartImageFormat.EmfDual;
[4621]154        } else if (filename.EndsWith("gif")) {
[4614]155          format = ChartImageFormat.Gif;
[4621]156        } else if (filename.EndsWith("png")) {
[4614]157          format = ChartImageFormat.Png;
[4621]158        } else if (filename.EndsWith("tif")) {
[4614]159          format = ChartImageFormat.Tiff;
160        }
161
162        // Save image
163        SaveImage(saveFileDialog.FileName, format);
164      }
165    }
166
[6640]167    private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
168      exportDialog.ShowDialog();
169    }
170
171    private void copyImageToClipboardBitmapToolStripMenuItem_Click(object sender, EventArgs e) {
[4614]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.