This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SliceAndDiceLayoutAlgorithm.cs
72 lines (61 loc) · 2.84 KB
/
SliceAndDiceLayoutAlgorithm.cs
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
#region #CustomLayoutAlgorithmImpl
using DevExpress.Xpf.TreeMap;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System;
namespace CustomLayoutAlgorithmSample {
class CustomSliceAndDiceLayoutAlgorithm : TreeMapLayoutAlgorithmBase, IComparer<ITreeMapLayoutItem> {
// Cut the slice depending on the non-filled space width/height ratio.
public override void Calculate(IList<ITreeMapLayoutItem> items, Size size, int groupLevel) {
double unlayoutedItemsWeight = 0;
foreach (var item in items)
unlayoutedItemsWeight += item.Weight;
var sortedItems = items.ToList();
sortedItems.Sort(this);
Rect emptySpace = new Rect(0, 0, size.Width, size.Height);
foreach (var item in sortedItems) {
double itemWidth;
double itemHeight;
double newEmptySpaceX;
double newEmptySpaceY;
double newEmptySpaceWidth;
double newEmptySpaceHeight;
if (emptySpace.Width / emptySpace.Height > 1) {
itemWidth = emptySpace.Width * item.Weight / unlayoutedItemsWeight;
itemHeight = emptySpace.Height;
newEmptySpaceX = emptySpace.X + itemWidth;
newEmptySpaceY = emptySpace.Y;
newEmptySpaceHeight = emptySpace.Height;
newEmptySpaceWidth = emptySpace.Width - itemWidth;
newEmptySpaceWidth = newEmptySpaceWidth < 0 ? 0 : newEmptySpaceWidth;
}
else {
itemWidth = emptySpace.Width;
itemHeight = emptySpace.Height * item.Weight / unlayoutedItemsWeight;
newEmptySpaceX = emptySpace.X;
newEmptySpaceY = emptySpace.Y + itemHeight;
newEmptySpaceWidth = emptySpace.Width;
newEmptySpaceHeight = emptySpace.Height - itemHeight;
newEmptySpaceHeight = newEmptySpaceHeight < 0 ? 0 : newEmptySpaceHeight;
}
item.Layout = new Rect(emptySpace.X, emptySpace.Y, itemWidth, itemHeight);
emptySpace = new Rect(
newEmptySpaceX,
newEmptySpaceY,
newEmptySpaceWidth,
newEmptySpaceHeight);
unlayoutedItemsWeight -= item.Weight;
}
}
public int Compare(ITreeMapLayoutItem x, ITreeMapLayoutItem y) {
if (x.Weight > y.Weight) return -1;
else if (x.Weight < y.Weight) return 1;
else return 0;
}
protected override TreeMapDependencyObject CreateObject() {
return new CustomSliceAndDiceLayoutAlgorithm();
}
}
}
#endregion #CustomLayoutAlgorithmImpl