1
+ package app .logorrr .views
2
+
3
+ import javafx .application .Application
4
+ import javafx .scene .Scene
5
+ import javafx .scene .layout .Pane
6
+ import javafx .scene .paint .Color
7
+ import javafx .scene .shape .Rectangle
8
+ import javafx .stage .Stage
9
+
10
+ class HorizontalVerticalStackedTreemapApp extends Application {
11
+ override def start (primaryStage : Stage ): Unit = {
12
+ // Numbers to visualize
13
+ val numbers = List (1 , 3 , 5 , 7 , 11 , 17 , 50 , 100 , 400 , 1000 , 5000 , 10000 )
14
+
15
+ // Calculate the total sum
16
+ val total = numbers.sum.toDouble
17
+
18
+ // Define the dimensions of the rectangle (painting area)
19
+ val width = 1000.0
20
+ val height = 100.0
21
+
22
+ // Create a root pane to hold rectangles
23
+ val root = new Pane ()
24
+
25
+ // Recursive function to partition and draw rectangles
26
+ def drawStackedRectangles (data : List [Double ], startX : Double , startY : Double , w : Double , h : Double ): Unit = {
27
+ if (data.isEmpty) return
28
+
29
+ if (data.size == 1 ) {
30
+ // Base case: Draw a single rectangle
31
+ val rect = new Rectangle (startX, startY, w, h)
32
+ rect.setFill(Color .hsb((data.head / total) * 360 , 0.7 , 0.9 ))
33
+ rect.setStroke(Color .BLACK )
34
+ root.getChildren.add(rect)
35
+ return
36
+ }
37
+
38
+ // Determine whether to split horizontally or vertically based on aspect ratio
39
+ // val isHorizontalSplit = Math.random() > 0.5
40
+ val isHorizontalSplit = w > h
41
+
42
+ // Calculate total for proportions
43
+ val subsetTotal = data.sum
44
+ var accumulated = 0.0
45
+ val splitIndex = data.indexWhere { value =>
46
+ accumulated += value
47
+ accumulated >= subsetTotal / 2
48
+ }
49
+ // Partition data into two groups
50
+ val (firstHalf, secondHalf) = data.splitAt(splitIndex)
51
+
52
+ if (isHorizontalSplit) {
53
+ // Split horizontally
54
+ val splitWidth = w * firstHalf.sum / subsetTotal
55
+ drawStackedRectangles(firstHalf, startX, startY, splitWidth, h)
56
+ drawStackedRectangles(secondHalf, startX + splitWidth, startY, w - splitWidth, h)
57
+ } else {
58
+ // Split vertically
59
+ val splitHeight = h * firstHalf.sum / subsetTotal
60
+ drawStackedRectangles(firstHalf, startX, startY, w, splitHeight)
61
+ drawStackedRectangles(secondHalf, startX, startY + splitHeight, w, h - splitHeight)
62
+ }
63
+ }
64
+
65
+ // Start drawing the treemap
66
+ drawStackedRectangles(numbers.map(_.toDouble), 0.0 , 0.0 , width, height)
67
+
68
+ // Create the scene and show the stage
69
+ val scene = new Scene (root, width, height)
70
+ primaryStage.setTitle(" Horizontal and Vertical Stacked Treemap" )
71
+ primaryStage.setScene(scene)
72
+ primaryStage.show()
73
+ }
74
+ }
75
+
76
+ object HorizontalVerticalStackedTreemapApp {
77
+ def main (args : Array [String ]): Unit = {
78
+ Application .launch(classOf [HorizontalVerticalStackedTreemapApp ], args : _* )
79
+ }
80
+ }
0 commit comments