Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Visualization.ChartControlsExtensions/3.3/EnhancedChart.cs @ 6010

Last change on this file since 6010 was 6010, checked in by abeham, 13 years ago

#1465

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