forked from tstriker/hamster-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_treemap.py
More file actions
executable file
·150 lines (106 loc) · 4.1 KB
/
flat_treemap.py
File metadata and controls
executable file
·150 lines (106 loc) · 4.1 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2014 Toms Baugis <toms.baugis@gmail.com>
"""Base template"""
from gi.repository import Gtk as gtk
from lib import graphics
import random
class FlatMap(object):
def paint(self, g, w, h):
numbers = self.norm(0)
for depth, normalized in enumerate(numbers):
# we will split the dimension where we have more space
x, y = round(w * normalized), round(h * normalized)
y = 0 if x >= y else y
x = 0 if y > x else x
g.fill_area(0, 0, x or w, y or h, graphics.Colors.category20c[depth % 20])
g.translate(x, y)
w = w - x
h = h - y
class Fibonacci(FlatMap):
description = "Fibonacci"
def __init__(self):
self.numbers = [] # generate numbers
# don't drink and math
j, k = 0, 1
for i in range(1, 20):
res = j + k
self.numbers.append(res)
j, k = k, k + j
self.numbers = sorted(self.numbers, reverse=True)
def norm(self, depth):
numbers = self.numbers[depth:]
if len(numbers) < 2:
return []
top = numbers[0] * 1.0 / (numbers[1] + numbers[0])
return [top] + self.norm(depth+1)
class Sum2D(FlatMap):
description = "Current against the remaining sum"
def __init__(self):
self.numbers = [random.randint(1, 900) for i in range(10)]
#self.numbers = [i for i in range(10, 100, 30)]
self.numbers = sorted(self.numbers, reverse=True)
def norm(self, depth):
"""we will find out how much space we have and then start dividing
up the area accordingly"""
numbers = self.numbers[depth:]
if not numbers:
return []
total = sum(numbers) * 1.0
top = numbers[0] * 1.0 / total
return [top] + self.norm(depth+1)
class Max2D(FlatMap):
description = "Current against max value"
def __init__(self):
self.numbers = [random.randint(10, 100) for i in range(50)]
self.numbers = sorted(self.numbers, reverse=True)
def norm(self, depth):
"""we will find out how much space we have and then start dividing
up the area accordingly"""
numbers = self.numbers
if depth == len(self.numbers):
return []
total = max(numbers) * 1.0
top = numbers[0] * 1.0 / total * 0.5
return [top] + self.norm(depth+1)
class Scene(graphics.Scene):
def __init__(self):
graphics.Scene.__init__(self)
self.experiments = [
Fibonacci(),
Sum2D(),
Max2D(),
]
self.current_experiment = self.experiments[0]
self.connect("on-enter-frame", self.on_enter_frame)
def toggles(self):
idx = self.experiments.index(self.current_experiment)
self.current_experiment = self.experiments[(idx + 1) % len(self.experiments)]
self.redraw()
def on_enter_frame(self, scene, context):
# you could do all your drawing here, or you could add some sprites
g = graphics.Graphics(context)
self.current_experiment.paint(g, self.width, self.height)
class BasicWindow:
def __init__(self):
window = gtk.Window()
window.set_default_size(600, 500)
window.connect("delete_event", lambda *args: gtk.main_quit())
box = gtk.VBox(border_width=10, spacing=10)
window.add(box)
self.scene = Scene()
box.pack_start(self.scene, True, True, 0)
button = gtk.Button("Toggles")
box.pack_end(button, False, True, 0)
button.connect("clicked", self.on_button_clicked)
self.on_button_clicked(button)
window.show_all()
print "The morale of the story is that flat treemaps are boring with a simplified layout algo"
def on_button_clicked(self, button):
self.scene.toggles()
button.set_label(self.scene.current_experiment.description + ". Click for Next")
if __name__ == '__main__':
window = BasicWindow()
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL) # gtk3 screws up ctrl+c
gtk.main()