Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1265: worked on visualization

  • added BackgroundColor to IChart
  • added RulerChartMode
  • renamed MoveChartMode to PanChartMode
File size: 4.1 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 HandleOnMouseMove(object sender, MouseEventArgs e) {
46      base.HandleOnMouseMove(sender, e);
47
48      if (startPoint.HasValue) {
49        try {
50          chartControl.PictureBox.Refresh();
51          DrawCross(startPoint.Value);
52          DrawCross(e.Location);
53          DrawLineBetweenLocations(startPoint.Value, e.Location);
54        } finally {
55          chartControl.PictureBox.Update();
56        }
57      }
58    }
59
60    public override void HandleOnMouseUp(object sender, MouseEventArgs e) {
61      base.HandleOnMouseUp(sender, e);
62
63      switch (e.Button) {
64        case MouseButtons.Left:
65          if (!startPoint.HasValue)
66            startPoint = e.Location;
67          else {
68            if (e.Location != startPoint.Value)
69              DrawInfoBox(e.Location);
70            else {
71              chartControl.PictureBox.Refresh();
72              chartControl.PictureBox.Update();
73            }
74
75            startPoint = null;
76          }
77          break;
78      }
79    }
80
81    protected virtual void DrawCross(Point point) {
82      using (var graphics = chartControl.PictureBox.CreateGraphics())
83      using (var pen = new Pen(Color.Red)) {
84        graphics.DrawLine(pen, point.X - 3, point.Y - 3, point.X + 3, point.Y + 3);
85        graphics.DrawLine(pen, point.X - 3, point.Y + 3, point.X + 3, point.Y - 3);
86      }
87    }
88
89    protected virtual void DrawLineBetweenLocations(Point startLocation, Point endLocation) {
90      using (var graphics = chartControl.PictureBox.CreateGraphics())
91      using (var pen = new Pen(Color.Red))
92        graphics.DrawLine(pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y);
93    }
94
95    protected virtual void DrawInfoBox(Point location) {
96      var offsetP = new Size(location.X - startPoint.Value.X, location.Y - startPoint.Value.Y);
97      var offsetW = chartControl.Chart.TransformPixelToWorld(offsetP);
98
99      double lengthP = Math.Sqrt(Math.Pow(offsetP.Width, 2) + Math.Pow(offsetP.Height, 2));
100      double lengthW = Math.Sqrt(Math.Pow(offsetW.Width, 2) + Math.Pow(offsetW.Height, 2));
101      string text = string.Format("Pixel: {0}{1}World: {2:##.###}", Math.Round(lengthP), Environment.NewLine, Math.Round(lengthW, 3));
102
103      using (var graphics = chartControl.PictureBox.CreateGraphics()) {
104        var textSize = graphics.MeasureString(text, SystemFonts.DefaultFont);
105        int width = (int)Math.Ceiling(textSize.Width);
106        int height = (int)Math.Ceiling(textSize.Height);
107
108        graphics.FillRectangle(Brushes.White, location.X, location.Y, width, height);
109        graphics.DrawRectangle(Pens.Red, location.X, location.Y, width, height);
110        graphics.DrawString(text, SystemFonts.DefaultFont, Brushes.Black, location.X, location.Y + 2);
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.