[4773] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12535] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4773] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 |
|
---|
[4776] | 27 | namespace HeuristicLab.Visualization {
|
---|
[4773] | 28 | public class Chart : IChart {
|
---|
| 29 | private PointD originalLowerLeft;
|
---|
| 30 | private PointD originalUpperRight;
|
---|
[13045] | 31 | protected List<Offset> zoomHistory;
|
---|
[4773] | 32 |
|
---|
[13045] | 33 | private bool enabled;
|
---|
| 34 | public bool Enabled {
|
---|
| 35 | get { return enabled; }
|
---|
[4773] | 36 | set {
|
---|
[13045] | 37 | if (enabled == value) return;
|
---|
| 38 | enabled = value;
|
---|
| 39 | OnRedrawRequired();
|
---|
[4773] | 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[13105] | 43 | private Color backgroundColor;
|
---|
| 44 | public Color BackgroundColor {
|
---|
| 45 | get { return backgroundColor; }
|
---|
| 46 | set {
|
---|
| 47 | if (backgroundColor == value) return;
|
---|
| 48 | backgroundColor = value;
|
---|
| 49 | OnRedrawRequired();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[13045] | 53 | protected bool SuppressRedraw { get; set; }
|
---|
| 54 |
|
---|
| 55 | public bool SuppressEvents { get; set; }
|
---|
| 56 |
|
---|
[4773] | 57 | private PointD myLowerLeft;
|
---|
| 58 | public virtual PointD LowerLeft {
|
---|
| 59 | get { return myLowerLeft; }
|
---|
| 60 | }
|
---|
| 61 | private PointD myUpperRight;
|
---|
| 62 | public virtual PointD UpperRight {
|
---|
| 63 | get { return myUpperRight; }
|
---|
| 64 | }
|
---|
| 65 | public virtual SizeD Size {
|
---|
| 66 | get { return new SizeD(UpperRight.X - LowerLeft.X, UpperRight.Y - LowerLeft.Y); }
|
---|
| 67 | }
|
---|
| 68 | private Size mySizeInPixels;
|
---|
[13045] | 69 |
|
---|
[4773] | 70 | public Size SizeInPixels {
|
---|
| 71 | get { return mySizeInPixels; }
|
---|
| 72 | }
|
---|
| 73 | public SizeD PixelToWorldRatio {
|
---|
| 74 | get { return new SizeD(Size.Width / SizeInPixels.Width, Size.Height / SizeInPixels.Height); }
|
---|
| 75 | }
|
---|
| 76 | public SizeD WorldToPixelRatio {
|
---|
| 77 | get { return new SizeD(SizeInPixels.Width / Size.Width, SizeInPixels.Height / Size.Height); }
|
---|
| 78 | }
|
---|
[13045] | 79 | public double Scale { get; set; }
|
---|
| 80 | public double MinimumZoomDistance { get; set; }
|
---|
| 81 | public IGroup Group { get; protected set; }
|
---|
[4773] | 82 |
|
---|
| 83 | public Chart(PointD lowerLeft, PointD upperRight) {
|
---|
| 84 | zoomHistory = new List<Offset>();
|
---|
| 85 | SetPosition(lowerLeft, upperRight);
|
---|
[13045] | 86 | mySizeInPixels = new Size((int)Size.Width, (int)Size.Height);
|
---|
[13105] | 87 | backgroundColor = Color.White;
|
---|
[13045] | 88 | Scale = 1.0;
|
---|
| 89 | Group = new Group(this);
|
---|
| 90 | Group.RedrawRequired += GroupOnRedrawRequired;
|
---|
| 91 | MinimumZoomDistance = 0.01;
|
---|
[4773] | 92 | }
|
---|
[13045] | 93 |
|
---|
[4773] | 94 | public Chart(double x1, double y1, double x2, double y2)
|
---|
[13045] | 95 | : this(new PointD(x1, y1), new PointD(x2, y2)) { }
|
---|
[4773] | 96 |
|
---|
| 97 | public PointD TransformPixelToWorld(Point point) {
|
---|
[13045] | 98 | var x = LowerLeft.X + point.X * PixelToWorldRatio.Width;
|
---|
| 99 | var y = LowerLeft.Y + (SizeInPixels.Height - point.Y) * PixelToWorldRatio.Height;
|
---|
[4773] | 100 | return new PointD(x, y);
|
---|
| 101 | }
|
---|
[13045] | 102 |
|
---|
[4773] | 103 | public SizeD TransformPixelToWorld(Size size) {
|
---|
[13045] | 104 | var width = size.Width * PixelToWorldRatio.Width;
|
---|
| 105 | var height = size.Height * PixelToWorldRatio.Height;
|
---|
[4773] | 106 | return new SizeD(width, height);
|
---|
| 107 | }
|
---|
[13045] | 108 |
|
---|
[4773] | 109 | public Point TransformWorldToPixel(PointD point) {
|
---|
[13045] | 110 | var x = (int)((point.X - LowerLeft.X) * WorldToPixelRatio.Width);
|
---|
| 111 | var y = (int)((UpperRight.Y - point.Y) * WorldToPixelRatio.Height);
|
---|
[4773] | 112 | return new Point(x, y);
|
---|
| 113 | }
|
---|
[13045] | 114 |
|
---|
[4773] | 115 | public Size TransformWorldToPixel(SizeD size) {
|
---|
[13045] | 116 | var width = (int)(size.Width * WorldToPixelRatio.Width);
|
---|
| 117 | var height = (int)(size.Height * WorldToPixelRatio.Height);
|
---|
[4773] | 118 | return new Size(width, height);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public virtual void SetPosition(PointD lowerLeft, PointD upperRight) {
|
---|
| 122 | if ((lowerLeft.X >= upperRight.X) || (lowerLeft.Y >= upperRight.Y))
|
---|
| 123 | throw new ArgumentException("Lower left point is greater or equal than upper right point");
|
---|
| 124 |
|
---|
| 125 | originalLowerLeft = lowerLeft;
|
---|
| 126 | originalUpperRight = upperRight;
|
---|
| 127 | if (zoomHistory != null) zoomHistory.Clear();
|
---|
| 128 | myLowerLeft = lowerLeft;
|
---|
| 129 | myUpperRight = upperRight;
|
---|
[13045] | 130 | OnRedrawRequired();
|
---|
[4773] | 131 | }
|
---|
[13045] | 132 |
|
---|
[4773] | 133 | public virtual void Move(Offset delta) {
|
---|
| 134 | myLowerLeft += delta;
|
---|
| 135 | myUpperRight += delta;
|
---|
[13045] | 136 | OnRedrawRequired();
|
---|
[4773] | 137 | }
|
---|
[13045] | 138 |
|
---|
| 139 | public virtual void Zoom(Point mousePosition, double delta) {
|
---|
| 140 | var cursor = TransformPixelToWorld(mousePosition);
|
---|
| 141 |
|
---|
| 142 | double zoomLevel = (100 + delta) / 100;
|
---|
| 143 | double width = this.UpperRight.X - this.LowerLeft.X;
|
---|
| 144 | double height = this.UpperRight.Y - this.LowerLeft.Y;
|
---|
| 145 | double newWidth = width * zoomLevel;
|
---|
| 146 | double newHeight = height * zoomLevel;
|
---|
| 147 | double xAdaption = (UpperRight.X - cursor.X) / width;
|
---|
| 148 | double yAdaption = (UpperRight.Y - cursor.Y) / height;
|
---|
| 149 |
|
---|
| 150 | ZoomIn(new PointD(cursor.X - newWidth * (1 - xAdaption), cursor.Y - newHeight * (1 - yAdaption)), new PointD(cursor.X + newWidth * xAdaption, cursor.Y + newHeight * yAdaption));
|
---|
| 151 | OnRedrawRequired();
|
---|
[4773] | 152 | }
|
---|
| 153 |
|
---|
[13045] | 154 | public virtual void ZoomIn(Point mousePosition) {
|
---|
| 155 | Zoom(mousePosition, -10);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[4773] | 158 | public virtual void ZoomIn(PointD lowerLeft, PointD upperRight) {
|
---|
[13045] | 159 | // maintain a 1:1 ratio between x and y axis
|
---|
| 160 | var pixels = SizeInPixels;
|
---|
| 161 | var diagonal = upperRight - lowerLeft;
|
---|
| 162 | var windowRatio = pixels.Height / (double)pixels.Width;
|
---|
| 163 | var viewRatio = diagonal.DY / diagonal.DX;
|
---|
| 164 | if (viewRatio < windowRatio) {
|
---|
| 165 | var neededHeight = windowRatio * diagonal.DX;
|
---|
| 166 | var diff = (neededHeight - diagonal.DY) / 2.0;
|
---|
| 167 | lowerLeft.Y -= diff;
|
---|
| 168 | upperRight.Y += diff;
|
---|
| 169 | } else {
|
---|
| 170 | var neededWidth = diagonal.DY / windowRatio;
|
---|
| 171 | var diff = (neededWidth - diagonal.DX) / 2.0;
|
---|
| 172 | lowerLeft.X -= diff;
|
---|
| 173 | upperRight.X += diff;
|
---|
| 174 | }
|
---|
| 175 | if ((lowerLeft - upperRight).Length < MinimumZoomDistance) return;
|
---|
[4773] | 176 | zoomHistory.Insert(0, LowerLeft - lowerLeft);
|
---|
| 177 | zoomHistory.Insert(0, UpperRight - upperRight);
|
---|
| 178 | myLowerLeft = lowerLeft;
|
---|
| 179 | myUpperRight = upperRight;
|
---|
[13045] | 180 | OnRedrawRequired();
|
---|
[4773] | 181 | }
|
---|
[13045] | 182 |
|
---|
[4773] | 183 | public virtual void ZoomIn(Point lowerLeft, Point upperRight) {
|
---|
| 184 | ZoomIn(TransformPixelToWorld(lowerLeft), TransformPixelToWorld(upperRight));
|
---|
| 185 | }
|
---|
[13045] | 186 |
|
---|
[4773] | 187 | public virtual void ZoomOut() {
|
---|
| 188 | if (zoomHistory.Count > 0) {
|
---|
| 189 | Offset upperRight = zoomHistory[0];
|
---|
| 190 | zoomHistory.RemoveAt(0);
|
---|
| 191 | Offset lowerLeft = zoomHistory[0];
|
---|
| 192 | zoomHistory.RemoveAt(0);
|
---|
| 193 | myLowerLeft = LowerLeft + lowerLeft;
|
---|
| 194 | myUpperRight = UpperRight + upperRight;
|
---|
[13045] | 195 | OnRedrawRequired();
|
---|
[4773] | 196 | } else {
|
---|
[13045] | 197 | var lowerLeft = new PointD(myLowerLeft.X - Size.Width / 4, myLowerLeft.Y - Size.Height / 4);
|
---|
| 198 | var upperRight = new PointD(myUpperRight.X + Size.Width / 4, myUpperRight.Y + Size.Height / 4);
|
---|
| 199 | ZoomIn(lowerLeft, upperRight);
|
---|
| 200 | if (zoomHistory.Count >= 2) {
|
---|
| 201 | zoomHistory.RemoveRange(0, 2);
|
---|
| 202 | }
|
---|
[4773] | 203 | }
|
---|
| 204 | }
|
---|
[13045] | 205 |
|
---|
[4773] | 206 | public virtual void Unzoom() {
|
---|
| 207 | SetPosition(originalLowerLeft, originalUpperRight);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[13045] | 210 | public virtual void IntoForeground(IPrimitive primitive) {
|
---|
| 211 | Group.IntoForeground(primitive);
|
---|
[4773] | 212 | }
|
---|
[13045] | 213 | public virtual void IntoBackground(IPrimitive primitive) {
|
---|
| 214 | Group.IntoBackground(primitive);
|
---|
[4773] | 215 | }
|
---|
[13045] | 216 | public virtual void OneLayerUp(IPrimitive primitive) {
|
---|
| 217 | Group.OneLayerUp(primitive);
|
---|
[4773] | 218 | }
|
---|
[13045] | 219 | public virtual void OneLayerDown(IPrimitive primitive) {
|
---|
| 220 | Group.OneLayerDown(primitive);
|
---|
[4773] | 221 | }
|
---|
| 222 |
|
---|
| 223 | public virtual Cursor GetCursor(Point point) {
|
---|
[13045] | 224 | return Group.GetCursor(TransformPixelToWorld(point)) ?? Cursors.Default;
|
---|
| 225 | }
|
---|
[4773] | 226 |
|
---|
| 227 | public virtual string GetToolTipText(Point point) {
|
---|
| 228 | return Group.GetToolTipText(TransformPixelToWorld(point));
|
---|
| 229 | }
|
---|
[13045] | 230 |
|
---|
| 231 | public virtual IPrimitive GetPrimitive(Point point) {
|
---|
| 232 | return Group.GetPrimitive(TransformPixelToWorld(point));
|
---|
[4773] | 233 | }
|
---|
| 234 |
|
---|
[13045] | 235 | public virtual IEnumerable<IPrimitive> GetAllPrimitives(Point point) {
|
---|
| 236 | return Group.GetAllPrimitives(TransformPixelToWorld(point));
|
---|
[4773] | 237 | }
|
---|
| 238 |
|
---|
| 239 | public virtual void Render(Graphics graphics, int width, int height) {
|
---|
| 240 | mySizeInPixels = new Size(width, height);
|
---|
[13105] | 241 | graphics.Clear(backgroundColor);
|
---|
[4773] | 242 | Group.PreDraw(graphics);
|
---|
| 243 | Group.Draw(graphics);
|
---|
| 244 | Group.PostDraw(graphics);
|
---|
[13045] | 245 | if (!enabled) {
|
---|
| 246 | var disabledColor = Color.FromArgb(150, SystemColors.Control);
|
---|
| 247 | var brush = new SolidBrush(disabledColor);
|
---|
| 248 | var rectangle = new System.Drawing.Rectangle(new Point(0, 0), mySizeInPixels);
|
---|
| 249 | graphics.FillRectangle(brush, rectangle);
|
---|
| 250 | }
|
---|
[4773] | 251 | }
|
---|
| 252 |
|
---|
[13045] | 253 | public event EventHandler RedrawRequired;
|
---|
| 254 | protected virtual void OnRedrawRequired() {
|
---|
| 255 | if (SuppressRedraw) return;
|
---|
| 256 | var handler = RedrawRequired;
|
---|
| 257 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[4773] | 258 | }
|
---|
[13045] | 259 |
|
---|
| 260 | public void RaiseRedrawRequired() {
|
---|
| 261 | var handler = RedrawRequired;
|
---|
| 262 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[4773] | 263 | }
|
---|
[13045] | 264 |
|
---|
| 265 | private void GroupOnRedrawRequired(object sender, EventArgs e) {
|
---|
| 266 | OnRedrawRequired();
|
---|
[4773] | 267 | }
|
---|
| 268 | }
|
---|
| 269 | }
|
---|