Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking.Views/3.4/GenealogyGraphChart.cs @ 12388

Last change on this file since 12388 was 12208, checked in by bburlacu, 10 years ago

#1772: Merged trunk and worked on the graph view.

File size: 13.7 KB
RevLine 
[10650]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
[10264]23using System.Collections.Generic;
24using System.Drawing;
25using System.Drawing.Drawing2D;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Visualization;
30
[10271]31namespace HeuristicLab.EvolutionTracking.Views {
[10300]32  public partial class GenealogyGraphChart : ChartControl {
33    private IGenealogyGraph genealogyGraph;
[10264]34
[10269]35    private const double XIncrement = 30;
36    private const double YIncrement = 30;
37    private const double Diameter = 20;
[10264]38
[10827]39    private readonly Brush defaultBrush;
40    private readonly Pen defaultPen;
41
[11852]42    private Dictionary<IGenealogyGraphNode, VisualGenealogyGraphNode> nodeMap;
43    private Dictionary<Tuple<VisualGenealogyGraphNode, VisualGenealogyGraphNode>, VisualGenealogyGraphArc> arcMap;
44
[11881]45    #region chart modes
46    public bool SimpleLineages { get; set; }
47    public bool LockGenealogy { get; set; }
48    public bool TraceFragments { get; set; }
49    #endregion
50
51    private bool DrawInProgress { get; set; } // do not try to update the chart while the drawing is not finished
52
[10300]53    public IGenealogyGraph GenealogyGraph {
[10269]54      get { return genealogyGraph; }
55      set {
56        if (value == null) return;
57        genealogyGraph = value;
58        Clear();
59        DrawGraph(XIncrement, YIncrement, Diameter);
60      }
61    }
[10264]62
[11506]63    public IGenealogyGraphNode SelectedGraphNode {
64      get {
65        return SelectedVisualNode == null ? null : SelectedVisualNode.Data;
66      }
[11864]67      set {
[12208]68        if (value == null || value == SelectedGraphNode)
69          return;
[11864]70        SelectedVisualNode = GetMappedNode(value);
71        UpdateSelectedVisualNode();
72      }
[11506]73    }
[10650]74
[10269]75    private void Clear() {
[10830]76      nodeMap = new Dictionary<IGenealogyGraphNode, VisualGenealogyGraphNode>();
77      arcMap = new Dictionary<Tuple<VisualGenealogyGraphNode, VisualGenealogyGraphNode>, VisualGenealogyGraphArc>();
[10264]78
[10269]79      Chart.Group.Clear();
[10264]80    }
81
[11852]82    public bool UpdateEnabled {
83      get { return Chart.UpdateEnabled; }
84      set { Chart.UpdateEnabled = value; }
85    }
[10269]86
[11852]87    public void EnforceUpdate() {
88      Chart.EnforceUpdate();
89    }
90
[10269]91    private Visualization.Rectangle TargetRectangle { get; set; }
[11506]92    protected VisualGenealogyGraphNode SelectedVisualNode { get; set; }
[10269]93
[12208]94    public VisualGenealogyGraphNode GetMappedNode(IGenealogyGraphNode node) {
[10269]95      VisualGenealogyGraphNode v;
96      nodeMap.TryGetValue(node, out v);
97      return v;
[10264]98    }
99
[12208]100    public VisualGenealogyGraphArc GetMappedArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
[10269]101      VisualGenealogyGraphNode visualSource, visualTarget;
102      nodeMap.TryGetValue(source, out visualSource);
103      nodeMap.TryGetValue(target, out visualTarget);
104
105      if (visualSource == null || visualTarget == null) return null;
106
107      VisualGenealogyGraphArc arc;
108      arcMap.TryGetValue(new Tuple<VisualGenealogyGraphNode, VisualGenealogyGraphNode>(visualSource, visualTarget), out arc);
109      return arc;
[10264]110    }
[10269]111
[11817]112    public GenealogyGraphChart() {
[10269]113      InitializeComponent();
[10827]114
115      defaultBrush = new SolidBrush(Color.Transparent);
116      defaultPen = new Pen(Color.DarkGray);
[10264]117    }
[10650]118
[10269]119    protected virtual void DrawGraph(double xIncrement, double yIncrement, double diameter) {
[10264]120      Chart.UpdateEnabled = false;
[10269]121      DrawInProgress = true;
[10264]122
[10269]123      var ranks = GenealogyGraph.Ranks.Select(t => new { Rank = t.Key, Nodes = t.Value }).OrderBy(p => p.Rank).ToList();
124      double x = 0;
125      double y = PreferredSize.Height + yIncrement + 2 * diameter;
[10264]126
[10269]127      foreach (var rank in ranks) {
128        var nodes = rank.Nodes.ToList();
129        nodes.Sort((a, b) => b.CompareTo(a)); // sort descending by quality
130        var nl = Environment.NewLine;
[10264]131
[10269]132        foreach (var node in nodes) {
[10732]133          var brush = new SolidBrush(node.GetColor());
[10827]134          var visualNode = new VisualGenealogyGraphNode(Chart, x, y, x + diameter, y + diameter, defaultPen, brush) {
[10264]135            Data = node,
136            ToolTipText = "Rank: " + node.Rank + nl +
137                          "Quality: " + String.Format("{0:0.0000}", node.Quality) + nl +
[11262]138                          "IsElite: " + node.IsElite + nl +
139                          "In/Out Degree: " + node.InDegree + " " + node.OutDegree
[10264]140          };
141          Chart.Group.Add(visualNode);
[10269]142          nodeMap.Add(node, visualNode);
[10264]143
[10269]144          x += xIncrement;
[10264]145        }
[10269]146        y -= yIncrement;
[10285]147        x = 0;
[10264]148      }
[10285]149
[10269]150      // add arcs
[10903]151      foreach (var node in GenealogyGraph.Vertices) {
[10732]152        if (!node.InArcs.Any()) continue;
153        var visualNode = GetMappedNode(node);
[10833]154
[10269]155        if (visualNode == null) continue;
156        foreach (var arc in node.InArcs) {
157          var parent = arc.Source;
158          var visualParent = GetMappedNode(parent);
159          if (visualParent == null) continue;
[10827]160          var pen = Pens.Transparent;
[10269]161          var visualArc = AddArc(Chart, visualParent, visualNode, pen);
[10833]162          visualArc.OneLayerDown(); // send it behind the visual nodes
[10830]163          if (!arcMap.ContainsKey(Tuple.Create(visualParent, visualNode))) {
[10285]164            arcMap.Add(Tuple.Create(visualParent, visualNode), visualArc);
[10830]165          }
[10264]166        }
167      }
168
169      Chart.UpdateEnabled = true;
170      Chart.EnforceUpdate();
171
[10269]172      DrawInProgress = false;
[10264]173    }
[10650]174
175    public event MouseEventHandler GenealogyGraphNodeClicked;
[11506]176    protected void OnGenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
[10650]177      var clicked = GenealogyGraphNodeClicked;
178      if (clicked != null) clicked(sender, e);
179    }
180
[11506]181    public event MouseEventHandler GenealogyGraphNodeDoubleClicked;
182    protected void OnGenealogyGraphNodeDoubleClicked(object sender, MouseEventArgs e) {
183      var doubleClicked = GenealogyGraphNodeDoubleClicked;
184      if (doubleClicked != null)
185        doubleClicked(sender, e);
186    }
[10650]187    #region event handlers
[11506]188
[10285]189    protected override void pictureBox_MouseMove(object sender, MouseEventArgs e) {
[10269]190      if (!DrawInProgress) {
[10264]191        switch (e.Button) {
192          case MouseButtons.Left:
193            Chart.Mode = ChartMode.Select;
194            Cursor = Cursors.Default;
195            break;
196          case MouseButtons.Middle:
197            Chart.Mode = ChartMode.Move;
198            Cursor = Cursors.Hand;
199            break;
200        }
201      }
[10269]202      base.pictureBox_MouseMove(sender, e);
[10264]203    }
[11881]204
[10264]205    protected override void pictureBox_MouseUp(object sender, MouseEventArgs e) {
206      Cursor = Cursors.Default;
207      if (Chart.Mode == ChartMode.Move) {
208        Chart.Mode = ChartMode.Select;
209        return;
210      }
211      if (Chart.Mode != ChartMode.Select) {
212        base.pictureBox_MouseUp(sender, e);
213        return;
214      }
[11817]215      var primitive = Chart.GetAllPrimitives(e.Location).FirstOrDefault(p => p is VisualGenealogyGraphNode);
216      if (primitive == null) {
[10269]217        SelectedVisualNode = null;
[10264]218        return;
219      }
[11817]220      if (SelectedVisualNode == primitive) return;
221      SelectedVisualNode = primitive as VisualGenealogyGraphNode;
[10269]222      if (SelectedVisualNode == null) return;
[10264]223
[11864]224      UpdateSelectedVisualNode(); // redraw ancestries, mark node etc.
225
226      base.pictureBox_MouseUp(sender, e);
227    }
228
229    private void UpdateSelectedVisualNode() {
[10264]230      if (!LockGenealogy) {
231        // new node has been selected, clean up
232        Chart.UpdateEnabled = false;
[10730]233        if (ModifierKeys != Keys.Shift) {
[10264]234          // clear colors
[10269]235          ClearPrimitives();
[10730]236        }
[10264]237        // use a rectangle to highlight the currently selected genealogy graph node
[10732]238        var graphNode = SelectedVisualNode.Data;
239        var visualNode = GetMappedNode(graphNode);
[10264]240
241        DrawLineage(visualNode, n => SimpleLineages ? n.IncomingArcs.Take(1) : n.IncomingArcs, a => a.Source);
[10888]242        ((SolidBrush)visualNode.Brush).Color = Color.Transparent;
[10264]243        DrawLineage(visualNode, n => n.OutgoingArcs, a => a.Target);
244      }
245
246      MarkSelectedNode();
247
248      // update
[11852]249      UpdateEnabled = true;
250      EnforceUpdate();
[10264]251
[10269]252      if (SelectedVisualNode != null)
[11864]253        OnGenealogyGraphNodeClicked(SelectedVisualNode, null); // emit clicked event
[10264]254    }
[10650]255    #endregion
256
[11881]257    #region drawing routines
[10730]258    private static void DrawLineage(VisualGenealogyGraphNode node, Func<VisualGenealogyGraphNode, IEnumerable<VisualGenealogyGraphArc>> arcSelector, Func<VisualGenealogyGraphArc, VisualGenealogyGraphNode> nodeSelector) {
[10732]259      var brush = (SolidBrush)node.Brush;
[10746]260      if (brush.Color != Color.Transparent) return; // this lineage was already drawn (avoid redrawing common ancestors)
[10732]261      brush.Color = node.Data.GetColor();
[10746]262      var arcs = arcSelector(node);
[10830]263      var pen = new Pen(Color.Transparent);
[10264]264      foreach (var arc in arcs) {
265        var source = arc.Source.Data;
266        var target = arc.Target.Data;
267        var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
268        var end = new Point((int)arc.End.X, (int)arc.End.Y);
[10830]269        arc.Pen = pen;
[10264]270        arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
271        DrawLineage(nodeSelector(arc), arcSelector, nodeSelector);
272      }
273    }
[10650]274
[10264]275    void MarkSelectedNode() {
[10269]276      var center = SelectedVisualNode.Center;
277      var size = SelectedVisualNode.Size;
[10264]278      double x1 = center.X - size.Width / 2;
279      double x2 = x1 + size.Width;
280      double y1 = center.Y - size.Height / 2;
281      double y2 = y1 + size.Height;
[10269]282      if (TargetRectangle == null) {
283        TargetRectangle = new Visualization.Rectangle(Chart, x1, y1, x2, y2, new Pen(Color.Black), null);
284        Chart.Group.Add(TargetRectangle);
[10264]285      } else {
[10269]286        TargetRectangle.SetPosition(x1, y1, x2, y2);
[10264]287      }
288    }
[10650]289
[10269]290    private static VisualGenealogyGraphArc AddArc(IChart chart, VisualGenealogyGraphNode source, VisualGenealogyGraphNode target, Pen pen, Brush brush = null) {
291      var arc = new VisualGenealogyGraphArc(chart, source, target, pen) { Brush = brush };
292      arc.UpdatePosition();
293      source.OutgoingArcs.Add(arc);
294      target.IncomingArcs.Add(arc);
295      chart.Group.Add(arc);
296      return arc;
297    }
[10650]298
[11852]299    public void ClearArcs() {
300      foreach (var primitive in Chart.Group.Primitives.OfType<VisualGenealogyGraphArc>()) {
301        primitive.Pen = Pens.Transparent;
302      }
303    }
304
305    public void ClearNodes() {
306      foreach (var primitive in Chart.Group.Primitives.OfType<VisualGenealogyGraphNode>()) {
307        primitive.Brush = new SolidBrush(Color.Transparent);
308        primitive.Pen = new Pen(Color.LightGray);
309      }
310    }
311
[10269]312    public virtual void ClearPrimitives() {
[10264]313      foreach (var primitive in Chart.Group.Primitives) {
314        if (primitive is VisualGenealogyGraphArc) {
[10827]315          primitive.Pen = Pens.Transparent;
[10732]316        } else if (primitive is VisualGenealogyGraphNode) {
[10830]317          primitive.Brush = new SolidBrush(Color.Transparent);
318          primitive.Pen = new Pen(Color.DarkGray);
[10264]319        }
320      }
321    }
[10650]322
[11852]323    public void HighlightNodes(IEnumerable<IGenealogyGraphNode> nodes, bool clearPrimitives = true) {
324      if (clearPrimitives)
325        ClearPrimitives();
326
[10264]327      foreach (var node in nodes) {
[10650]328        var graphNode = GetMappedNode(node);
329        graphNode.Brush = new SolidBrush(node.GetColor());
[10264]330      }
331    }
[10650]332
[11852]333    public void HighlightNodes(IEnumerable<IGenealogyGraphNode> nodes, Color color, bool clearPrimitives = true) {
334      if (clearPrimitives)
335        ClearPrimitives();
336
337      foreach (var node in nodes.Select(GetMappedNode)) {
338        node.Brush = new SolidBrush(color);
339      }
340    }
341
[10264]342    public void HighlightAll() {
[10269]343      foreach (var visualNode in nodeMap.Values) {
[10264]344        visualNode.Brush = new SolidBrush(visualNode.Data.GetColor());
345      }
[10269]346      foreach (var arc in arcMap.Values) {
[10264]347        var source = arc.Source.Data;
348        var target = arc.Target.Data;
349        var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
350        var end = new Point((int)arc.End.X, (int)arc.End.Y);
351        arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
352      }
353    }
[11506]354
355    public void HighlightArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
356      var arc = GetMappedArc(source, target) ??
357                AddArc(Chart, GetMappedNode(source), GetMappedNode(target), new Pen(Color.Transparent));
358      var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
359      var end = new Point((int)arc.End.X, (int)arc.End.Y);
360      arc.Pen = new Pen(Color.Transparent);
361      arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
362    }
[11881]363    #endregion
[10264]364  }
[10650]365
[10264]366  internal static class Util {
[10269]367    public static Color GetColor(this IGenealogyGraphNode node) {
[11817]368      if (double.IsNaN(node.Quality))
369        return ColorGradient.Colors[0];
[10732]370      var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
371      if (colorIndex >= ColorGradient.Colors.Count) return ColorGradient.Colors.Last();
372      return ColorGradient.Colors[colorIndex];
[10264]373    }
374  }
[10269]375}
Note: See TracBrowser for help on using the repository browser.