source: branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Chart.cs @ 13105

Last change on this file since 13105 was 13105, checked in by jkarder, 7 years ago

#1265: worked on visualization

  • added BackgroundColor to IChart
  • added RulerChartMode
  • renamed MoveChartMode to PanChartMode
File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
24using System.Drawing;
25using System.Windows.Forms;
26
27namespace HeuristicLab.Visualization {
28  public class Chart : IChart {
29    private PointD originalLowerLeft;
30    private PointD originalUpperRight;
31    protected List<Offset> zoomHistory;
32
33    private bool enabled;
34    public bool Enabled {
35      get { return enabled; }
36      set {
37        if (enabled == value) return;
38        enabled = value;
39        OnRedrawRequired();
40      }
41    }
42
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
53    protected bool SuppressRedraw { get; set; }
54
55    public bool SuppressEvents { get; set; }
56
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;
69
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    }
79    public double Scale { get; set; }
80    public double MinimumZoomDistance { get; set; }
81    public IGroup Group { get; protected set; }
82
83    public Chart(PointD lowerLeft, PointD upperRight) {
84      zoomHistory = new List<Offset>();
85      SetPosition(lowerLeft, upperRight);
86      mySizeInPixels = new Size((int)Size.Width, (int)Size.Height);
87      backgroundColor = Color.White;
88      Scale = 1.0;
89      Group = new Group(this);
90      Group.RedrawRequired += GroupOnRedrawRequired;
91      MinimumZoomDistance = 0.01;
92    }
93
94    public Chart(double x1, double y1, double x2, double y2)
95      : this(new PointD(x1, y1), new PointD(x2, y2)) { }
96
97    public PointD TransformPixelToWorld(Point point) {
98      var x = LowerLeft.X + point.X * PixelToWorldRatio.Width;
99      var y = LowerLeft.Y + (SizeInPixels.Height - point.Y) * PixelToWorldRatio.Height;
100      return new PointD(x, y);
101    }
102
103    public SizeD TransformPixelToWorld(Size size) {
104      var width = size.Width * PixelToWorldRatio.Width;
105      var height = size.Height * PixelToWorldRatio.Height;
106      return new SizeD(width, height);
107    }
108
109    public Point TransformWorldToPixel(PointD point) {
110      var x = (int)((point.X - LowerLeft.X) * WorldToPixelRatio.Width);
111      var y = (int)((UpperRight.Y - point.Y) * WorldToPixelRatio.Height);
112      return new Point(x, y);
113    }
114
115    public Size TransformWorldToPixel(SizeD size) {
116      var width = (int)(size.Width * WorldToPixelRatio.Width);
117      var height = (int)(size.Height * WorldToPixelRatio.Height);
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;
130      OnRedrawRequired();
131    }
132
133    public virtual void Move(Offset delta) {
134      myLowerLeft += delta;
135      myUpperRight += delta;
136      OnRedrawRequired();
137    }
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();
152    }
153
154    public virtual void ZoomIn(Point mousePosition) {
155      Zoom(mousePosition, -10);
156    }
157
158    public virtual void ZoomIn(PointD lowerLeft, PointD upperRight) {
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;
176      zoomHistory.Insert(0, LowerLeft - lowerLeft);
177      zoomHistory.Insert(0, UpperRight - upperRight);
178      myLowerLeft = lowerLeft;
179      myUpperRight = upperRight;
180      OnRedrawRequired();
181    }
182
183    public virtual void ZoomIn(Point lowerLeft, Point upperRight) {
184      ZoomIn(TransformPixelToWorld(lowerLeft), TransformPixelToWorld(upperRight));
185    }
186
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;
195        OnRedrawRequired();
196      } else {
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        }
203      }
204    }
205
206    public virtual void Unzoom() {
207      SetPosition(originalLowerLeft, originalUpperRight);
208    }
209
210    public virtual void IntoForeground(IPrimitive primitive) {
211      Group.IntoForeground(primitive);
212    }
213    public virtual void IntoBackground(IPrimitive primitive) {
214      Group.IntoBackground(primitive);
215    }
216    public virtual void OneLayerUp(IPrimitive primitive) {
217      Group.OneLayerUp(primitive);
218    }
219    public virtual void OneLayerDown(IPrimitive primitive) {
220      Group.OneLayerDown(primitive);
221    }
222
223    public virtual Cursor GetCursor(Point point) {
224      return Group.GetCursor(TransformPixelToWorld(point)) ?? Cursors.Default;
225    }
226
227    public virtual string GetToolTipText(Point point) {
228      return Group.GetToolTipText(TransformPixelToWorld(point));
229    }
230
231    public virtual IPrimitive GetPrimitive(Point point) {
232      return Group.GetPrimitive(TransformPixelToWorld(point));
233    }
234
235    public virtual IEnumerable<IPrimitive> GetAllPrimitives(Point point) {
236      return Group.GetAllPrimitives(TransformPixelToWorld(point));
237    }
238
239    public virtual void Render(Graphics graphics, int width, int height) {
240      mySizeInPixels = new Size(width, height);
241      graphics.Clear(backgroundColor);
242      Group.PreDraw(graphics);
243      Group.Draw(graphics);
244      Group.PostDraw(graphics);
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      }
251    }
252
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);
258    }
259
260    public void RaiseRedrawRequired() {
261      var handler = RedrawRequired;
262      if (handler != null) handler(this, EventArgs.Empty);
263    }
264
265    private void GroupOnRedrawRequired(object sender, EventArgs e) {
266      OnRedrawRequired();
267    }
268  }
269}
Note: See TracBrowser for help on using the repository browser.