1+ # ---
2+ # title: Polygon
3+ # id: demo_polygon_plot1
4+ # description: Using the `Polygon` mark to draw simple polygons
5+ # cover: assets/polygon1.svg
6+ # ---
7+
8+ using InMemoryDatasets, StatisticalGraphics, Chain
9+
10+ ds= Dataset (x= [0 ,0 ,2 ,2 ,4 ,4 ,6 ,6 ,4 ,4 ,2 ,2 ,7 ,7 ,9 ,9 ], y= [0 ,10 ,10 ,6 ,6 ,10 ,10 ,0 ,0 ,4 ,4 ,0 ,0 ,10 ,10 ,0 ], id= [fill (1 ,12 );fill (2 ,4 )]) # hide #md
11+
12+ svg (" assets/polygon1.svg" , sgplot (ds, Polygon (x= :x , y= :y , id= :id , opacity= 0.5 ), width= 100 , height= 100 , xaxis= Axis (offset= 0 , padding= 10 , grid= true ), yaxis= Axis (offset= 0 , padding= 10 , grid= true ))) # hide #md
13+
14+
15+ # `Polygon` can be used to draw polygons. Each polygon is represented by xy-coordinates and its id. `sgplot` produces a seperate polygon for each id.
16+
17+ ds= Dataset (
18+ x= [0 ,0 ,2 ,2 ,4 ,4 ,6 ,6 ,4 ,4 ,2 ,2 ,7 ,7 ,9 ,9 ],
19+ y= [0 ,10 ,10 ,6 ,6 ,10 ,10 ,0 ,0 ,4 ,4 ,0 ,0 ,10 ,10 ,0 ],
20+ id= [fill (1 ,12 );fill (2 ,4 )]
21+ )
22+
23+ sgplot (ds,
24+ Polygon (x= :x , y= :y , id= :id , opacity= 0.5 ),
25+ xaxis= Axis (padding= 10 ),
26+ yaxis= Axis (padding= 10 )
27+ )
28+
29+ # Users can pass `colorresponse` or `group` for colorful drawings
30+
31+ ds = Dataset (id = repeat (1 : 10 , 3 ), x = rand (30 ), y= rand (30 ))
32+
33+ sgplot (ds, Polygon (x= :x , y= :y , id= :id , colorresponse= :id , opacity= 0.7 ))
34+
35+ # `Polygon` can be customised by other keywords,
36+
37+ triangle (a, mul= [1 ,1 ,1 ]) = [(0.0 , 0.0 ) .*
38+ mul[1 ], (sqrt (2 * a^ 2 - 2 * a^ 2 * cos (a)), 0.0 ) .* mul[2 ],
39+ ((a^ 2 - a^ 2 * cos (a)) / sqrt (
40+ 2 * a^ 2 - 2 * a^ 2 * cos (a)),
41+ (a^ 2 * sin (a)) / sqrt (2 * a^ 2 - 2 * a^ 2 * cos (a))) .* mul[3 ]
42+ ]
43+
44+ ds = Dataset (x= range (0.01 , 3 , step= 0.091 ))
45+
46+ @chain ds begin
47+ modify! (
48+ :x => byrow (x-> x/ 10 ) => :opacity ,
49+ :x => byrow (triangle) => :t1 ,
50+ :x => byrow (x-> triangle (x, [(1 ,- 1 ), (1 ,- 1 ), (3.1 ,- 1 )])) => :t2
51+ )
52+
53+ flatten! (r" ^t" )
54+
55+ modify! (
56+ :t1 => splitter => [:x1 , :y1 ],
57+ :t2 => splitter => [:x2 , :y2 ]
58+ )
59+ sgplot (
60+ [
61+ Polygon (x= " x$i " , y= " y$i " ,
62+ id= :x ,
63+ opacityresponse= :opacity ,
64+ color= :darkgreen ,
65+ outline= false )
66+ for i in 1 : 2
67+ ],
68+ height= 200 ,
69+ width= 800 ,
70+ xaxis= Axis (show= false ),
71+ yaxis= Axis (show= false )
72+ )
73+ end
0 commit comments