-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSquarifiedTreeMapsPanel.cs
More file actions
99 lines (86 loc) · 3.08 KB
/
Copy pathSquarifiedTreeMapsPanel.cs
File metadata and controls
99 lines (86 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
namespace TreeMaps.Controls
{
public class SquarifiedTreeMapsPanel : TreeMapsPanel
{
#region protected methods
protected override Rect GetRectangle(RowOrientation orientation, TreeNodeData item, double x, double y, double width, double height)
{
if (orientation == RowOrientation.Horizontal)
{
return new Rect(x, y, width, item.RealArea / width);
}
else
{
return new Rect(x, y, item.RealArea / height, height);
}
}
protected override void ComputeNextPosition(RowOrientation orientation, ref double xPos, ref double yPos, double width, double height)
{
if (orientation == RowOrientation.Horizontal)
yPos += height;
else
xPos += width;
}
protected override void ComputeBounds()
{
if (this.Root != null && this.Root.Children != null)
{
this.Squarify(this.Root.Children, new List<TreeNodeData>(), this.GetShortestSide());
}
}
#endregion
#region private methods
private void Squarify(List<TreeNodeData> items, List<TreeNodeData> row, double sideLength)
{
if (items.Count == 0)
{
this.AddRowToLayout(row);
return;
}
TreeNodeData item = items[0];
List<TreeNodeData> row2 = new List<TreeNodeData>(row);
row2.Add(item);
List<TreeNodeData> items2 = new List<TreeNodeData>(items);
items2.RemoveAt(0);
double worst1 = this.Worst(row, sideLength);
double worst2 = this.Worst(row2, sideLength);
if (row.Count == 0 || worst1 > worst2)
{
this.Squarify(items2, row2, sideLength);
}
else
{
this.AddRowToLayout(row);
this.Squarify(items, new List<TreeNodeData>(), this.GetShortestSide());
}
}
private void AddRowToLayout(List<TreeNodeData> row)
{
base.ComputeTreeMaps(row);
}
private double Worst(List<TreeNodeData> row, double w)
{
if (row.Count == 0) return 0;
double maxArea = 0;
double minArea = double.MaxValue;
double s = 0;
foreach (TreeNodeData item in row)
{
maxArea = Math.Max(maxArea, item.RealArea);
minArea = Math.Min(minArea, item.RealArea);
s += item.RealArea;
}
if (minArea == double.MaxValue) minArea = 0;
double sSqr = s * s;
double wSqr = w * w;
double val1 = (wSqr * maxArea) / sSqr;
double val2 = sSqr / (wSqr * minArea);
return Math.Max(val1, val2);
}
#endregion
}
}