Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/ChartModes/RulerChartMode.cs @ 13114

Last change on this file since 13114 was 13114, checked in by jkarder, 8 years ago

#1265: worked on visualization

  • removed BackgroundColor and PictureBox from ChartControl
  • updated chart modes
File size: 4.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.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Common.Resources;
26
27namespace HeuristicLab.Visualization.ChartModes {
28  public class RulerChartMode : ChartMode {
29    protected Point? startPoint;
30
31    public override Image Image {
32      get { return VSImageLibrary.TrackBar; }
33    }
34
35    public override string ToolTip {
36      get { return "Ruler"; }
37    }
38
39    public override Cursor Cursor {
40      get { return Cursors.Cross; }
41    }
42
43    public RulerChartMode(ChartControl control) : base(control) { }
44
45    public override void HandleOnMouseDown(object sender, MouseEventArgs e) {
46      try {
47        if (startPoint.HasValue) {
48          if (startPoint.Value == e.Location) {
49            startPoint = null;
50            chartControl.RefreshPicture();
51          }
52        } else
53          startPoint = e.Location;
54      } finally {
55        base.HandleOnMouseDown(sender, e);
56      }
57    }
58
59    public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
60      try {
61        if (startPoint.HasValue) {
62          chartControl.RefreshPicture();
63          using (var graphics = chartControl.CreatePictureGraphics()) {
64            DrawCross(graphics, startPoint.Value);
65            DrawCross(graphics, e.Location);
66            DrawLineBetweenLocations(graphics, startPoint.Value, e.Location);
67          }
68        }
69      } finally {
70        base.HandleOnMouseMove(sender, e);
71      }
72    }
73
74    public override void HandleOnMouseUp(object sender, MouseEventArgs e) {
75      try {
76        switch (e.Button) {
77          case MouseButtons.Left:
78            if (startPoint.HasValue && startPoint.Value != e.Location) {
79              using (var graphics = chartControl.CreatePictureGraphics())
80                DrawInfoBox(graphics, e.Location);
81              startPoint = null;
82            }
83            break;
84        }
85      } finally {
86        base.HandleOnMouseUp(sender, e);
87      }
88    }
89
90    protected virtual void DrawCross(Graphics graphics, Point point) {
91      using (var pen = new Pen(Color.Red)) {
92        graphics.DrawLine(pen, point.X - 3, point.Y - 3, point.X + 3, point.Y + 3);
93        graphics.DrawLine(pen, point.X - 3, point.Y + 3, point.X + 3, point.Y - 3);
94      }
95    }
96
97    protected virtual void DrawLineBetweenLocations(Graphics graphics, Point startLocation, Point endLocation) {
98      using (var pen = new Pen(Color.Red))
99        graphics.DrawLine(pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y);
100    }
101
102    protected virtual void DrawInfoBox(Graphics graphics, Point location) {
103      var offsetP = new Size(location.X - startPoint.Value.X, location.Y - startPoint.Value.Y);
104      var offsetW = chartControl.Chart.TransformPixelToWorld(offsetP);
105
106      double lengthP = Math.Sqrt(Math.Pow(offsetP.Width, 2) + Math.Pow(offsetP.Height, 2));
107      double lengthW = Math.Sqrt(Math.Pow(offsetW.Width, 2) + Math.Pow(offsetW.Height, 2));
108      string text = string.Format("Pixel: {0} " + Environment.NewLine + "World: {1:##.###}", Math.Round(lengthP), Math.Round(lengthW, 3));
109
110      var textSize = graphics.MeasureString(text, SystemFonts.DefaultFont);
111      int width = (int)Math.Ceiling(textSize.Width);
112      int height = (int)Math.Ceiling(textSize.Height);
113
114      graphics.FillRectangle(Brushes.White, location.X, location.Y, width, height);
115      graphics.DrawRectangle(Pens.Red, location.X, location.Y, width, height);
116      graphics.DrawString(text, SystemFonts.DefaultFont, Brushes.Black, location.X, location.Y + 2);
117
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.