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