[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] | 22 | using System;
|
---|
[4621] | 23 | using System.ComponentModel;
|
---|
[4614] | 24 | using System.Drawing;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
[4637] | 27 |
|
---|
[4614] | 28 | namespace 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; }
|
---|
| 42 |
|
---|
[4636] | 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;
|
---|
[4637] | 48 | axis.ScrollBar.LineColor = Color.Gray;
|
---|
[4636] | 49 | axis.ScrollBar.ButtonColor = SystemColors.GradientInactiveCaption;
|
---|
[4637] | 50 | axis.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
|
---|
[4636] | 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;
|
---|
[4637] | 58 | chartArea.CursorX.SelectionColor = Color.Gray;
|
---|
| 59 | chartArea.CursorY.SelectionColor = Color.Gray;
|
---|
[4636] | 60 | }
|
---|
| 61 |
|
---|
| 62 | public void CustomizeAllChartAreas() {
|
---|
[4635] | 63 | foreach (ChartArea chartArea in ChartAreas) {
|
---|
[4636] | 64 | CustomizeChartArea(chartArea);
|
---|
[4635] | 65 | }
|
---|
[4621] | 66 | }
|
---|
| 67 |
|
---|
[4637] | 68 | #region Mouse Event Ehancements
|
---|
[4621] | 69 | protected override void OnMouseDoubleClick(MouseEventArgs e) {
|
---|
| 70 | if (EnableDoubleClickResetsZoom) {
|
---|
| 71 | HitTestResult result = HitTest(e.X, e.Y);
|
---|
[4637] | 72 | if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea || result.ChartElementType == ChartElementType.Gridlines)) {
|
---|
[4621] | 73 | foreach (var axis in result.ChartArea.Axes)
|
---|
[4630] | 74 | axis.ScaleView.ZoomReset(int.MaxValue);
|
---|
[4621] | 75 | }
|
---|
| 76 | }
|
---|
| 77 | base.OnMouseDoubleClick(e);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[4637] | 80 | #region Panning
|
---|
[4621] | 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 |
|
---|
[4638] | 95 | public double ChartX(double pixelX, int width) {
|
---|
[4654] | 96 | return ChartStartPosition.X - (pixelX - PixelStartPosition.X) *
|
---|
[4638] | 97 | (ChartArea.AxisX.ScaleView.ViewMaximum - ChartArea.AxisX.ScaleView.ViewMinimum) /
|
---|
| 98 | (width * ChartArea.Position.Width * ChartArea.InnerPlotPosition.Width / 100 / 100);
|
---|
[4621] | 99 | }
|
---|
[4638] | 100 | public double ChartY(double pixelY, int height) {
|
---|
[4654] | 101 | return ChartStartPosition.Y + (pixelY - PixelStartPosition.Y) *
|
---|
[4638] | 102 | (ChartArea.AxisY.ScaleView.ViewMaximum - ChartArea.AxisY.ScaleView.ViewMinimum) /
|
---|
| 103 | (height * ChartArea.Position.Height * ChartArea.InnerPlotPosition.Height / 100 / 100);
|
---|
[4621] | 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) {
|
---|
[4638] | 126 | double x = panning.ChartX(e.Location.X, Width);
|
---|
| 127 | double y = panning.ChartY(e.Location.Y, Height);
|
---|
[4636] | 128 | if (panning.ChartArea.CursorX.Interval > 0) {
|
---|
[4646] | 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;
|
---|
[4636] | 131 | }
|
---|
| 132 | panning.ChartArea.AxisX.ScaleView.Scroll(x);
|
---|
| 133 | panning.ChartArea.AxisY.ScaleView.Scroll(y);
|
---|
[4621] | 134 | }
|
---|
| 135 | base.OnMouseMove(e);
|
---|
| 136 | }
|
---|
| 137 | #endregion
|
---|
| 138 | #endregion
|
---|
| 139 |
|
---|
[4654] | 140 | private void exportChartToolStripMenuItem_Click(object sender, System.EventArgs e) {
|
---|
[4614] | 141 | // Set image file format
|
---|
| 142 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 143 | ChartImageFormat format = ChartImageFormat.Bmp;
|
---|
[4621] | 144 | string filename = saveFileDialog.FileName.ToLower();
|
---|
| 145 | if (filename.EndsWith("bmp")) {
|
---|
[4614] | 146 | format = ChartImageFormat.Bmp;
|
---|
[4621] | 147 | } else if (filename.EndsWith("jpg")) {
|
---|
[4614] | 148 | format = ChartImageFormat.Jpeg;
|
---|
[4621] | 149 | } else if (filename.EndsWith("emf")) {
|
---|
[4614] | 150 | format = ChartImageFormat.EmfDual;
|
---|
[4621] | 151 | } else if (filename.EndsWith("gif")) {
|
---|
[4614] | 152 | format = ChartImageFormat.Gif;
|
---|
[4621] | 153 | } else if (filename.EndsWith("png")) {
|
---|
[4614] | 154 | format = ChartImageFormat.Png;
|
---|
[4621] | 155 | } else if (filename.EndsWith("tif")) {
|
---|
[4614] | 156 | format = ChartImageFormat.Tiff;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | // Save image
|
---|
| 160 | SaveImage(saveFileDialog.FileName, format);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[4628] | 164 | private void copyImageToClipboardBitmapToolStripMenuItem_Click(object sender, System.EventArgs e) {
|
---|
[4614] | 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 | }
|
---|