]> gitweb.factorcode.org Git - factor.git/commitdiff
ui.gadgets.charts.[axes|demos]: add simple axis drawing
authorAlexander Iljin <ajsoft@yandex.ru>
Sat, 4 Feb 2017 01:21:20 +0000 (04:21 +0300)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 17 Apr 2017 20:54:51 +0000 (13:54 -0700)
extra/ui/gadgets/charts/axes/axes.factor
extra/ui/gadgets/charts/demos/demos.factor

index 34248b95e985f8c64f09bbe9a511f310148c8b85..24c9d5f24d47c605db414609a5bb9aa599c5ee6a 100644 (file)
@@ -1,11 +1,39 @@
 ! Copyright (C) 2017 Alexander Ilin.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors kernel locals ui.gadgets ui.gadgets.charts
+USING: accessors arrays colors.constants kernel locals math
+math.order opengl sequences ui.gadgets ui.gadgets.charts
+ui.gadgets.charts.lines ui.gadgets.charts.lines.private
 ui.render ;
 IN: ui.gadgets.charts.axes
 
-TUPLE: axis < gadget vertical? ;
+TUPLE: axis < gadget vertical? color ;
+
+<PRIVATE
+
+ALIAS: x first
+ALIAS: y second
+
+: axis-pos ( min,max -- value ) 0 swap first2 clamp ;
+
+: default-color ( default obj -- )
+    color>> dup [ swap ] unless gl-color drop ;
+
+:: x-0y-chunk ( x y -- chunk ) x 0 2array x y 2array 2array ;
+:: 0x-y-chunk ( x y -- chunk ) 0 y 2array x y 2array 2array ;
+: flip-y ( axis-y xmax ymax -- xmax axis-y' ) rot - ;
+
+: ?[x/y] ( ? -- quot )
+    [ x ] [ y ] ? [ call( a -- b ) ] curry ; inline
+
+PRIVATE>
 
 M: axis draw-gadget*
     dup parent>> dup chart? [| axis chart |
+        axis vertical?>> :> vert?
+        chart dim>> :> dim
+        COLOR: black axis default-color
+        dim chart chart-axes vert? ?[x/y] bi@
+        [ axis-pos ] keep first2 swap scale
+        dim first2 vert? [ nip x-0y-chunk ] [ flip-y 0x-y-chunk ] if
+        draw-line
     ] [ 2drop ] if ;
index d893100cc1219ebc22437592705ed6ffba418829..43a629dfa66545af31366cd70ab1722c568b3f0a 100644 (file)
@@ -2,7 +2,8 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors arrays colors.constants kernel literals locals
 math math.constants math.functions sequences ui ui.gadgets
-ui.gadgets.charts ui.gadgets.charts.lines ;
+ui.gadgets.charts ui.gadgets.charts.axes ui.gadgets.charts.lines
+;
 IN: ui.gadgets.charts.demos
 
 CONSTANT: -pi $[ pi neg ]
@@ -23,6 +24,8 @@ CONSTANT: -pi $[ pi neg ]
     chart new ${ ${ -pi pi } { -1 1 } } >>axes
     line new COLOR: blue >>color n sine-wave >>data add-gadget
     line new COLOR: red >>color n cosine-wave >>data add-gadget
+    axis new add-gadget
+    axis new t >>vertical? add-gadget
     "Chart" open-window ;
 
 PRIVATE>