]> gitweb.factorcode.org Git - factor.git/blob - extra/google/charts/charts.factor
28d40b82404641776879236c9bc8eed77575bd16
[factor.git] / extra / google / charts / charts.factor
1 ! Copyright (C) 2011 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors arrays assocs colors.hex combinators formatting
5 http.client images.http images.loader images.loader.private
6 images.viewer kernel math math.order present sequences splitting
7 urls ;
8
9 IN: google.charts
10
11 TUPLE: chart type width height title data data-scale labels
12 background foreground margin bar-width ;
13
14 : <chart> ( type -- chart )
15     chart new
16         swap >>type
17         320 >>width
18         240 >>height ;
19
20 <PRIVATE
21
22 : x,y ( seq -- str ) [ present ] map "," join ;
23
24 : x|y ( seq -- str ) [ present ] map "|" join ;
25
26 : chd ( chart seq -- chart )
27     [ x,y >>data ] [
28         [ infimum 0 min ] [ supremum 0 max ] bi 2array
29         x,y >>data-scale
30     ] bi ;
31
32 : chl ( chart seq -- chart ) x|y >>labels ;
33
34 : chd/chl ( chart assoc -- chart )
35     [ values chd ] [ keys chl ] bi ;
36
37 PRIVATE>
38
39 : <pie> ( assoc -- chart )
40     [ "p" <chart> ] dip chd/chl ;
41
42 : <pie-3d> ( assoc -- chart )
43     [ "p3" <chart> ] dip chd/chl ;
44
45 : <bar> ( assoc -- chart )
46     [ "bvs" <chart> ] dip chd/chl ;
47
48 : <line> ( seq -- chart )
49     [ "lc" <chart> ] dip chd ;
50
51 : <line-xy> ( seq -- chart )
52     [ "lxy" <chart> ] dip [ keys ] [ values ] bi
53     [ x,y ] bi@ "|" glue >>data ;
54
55 : <scatter> ( seq -- chart )
56     [ "s" <chart> ] dip [ keys ] [ values ] bi
57     [ x,y ] bi@ "|" glue >>data ;
58
59 : <sparkline> ( seq -- chart )
60     [ "ls" <chart> ] dip chd ;
61
62 : <radar> ( seq -- chart )
63     [ "rs" <chart> ] dip chd ;
64
65 : <qr-code> ( str -- chart )
66     [ "qr" <chart> ] dip 1array chl ;
67
68 : <formula> ( str -- chart )
69     [ "tx" <chart> ] dip 1array chl f >>width f >>height ;
70
71 <PRIVATE
72
73 : chart>url ( chart -- url )
74     [ URL" http://chart.googleapis.com/chart" clone ] dip {
75         [ type>> "cht" set-query-param ]
76         [
77             [ width>> ] [ height>> ] bi 2dup and [
78                 "%sx%s" sprintf "chs" set-query-param
79             ] [ 2drop ] if
80         ]
81         [ title>> "chtt" set-query-param ]
82         [ data>> "t:" prepend "chd" set-query-param ]
83         [ data-scale>> [ "chds" set-query-param ] when* ]
84         [ labels>> "chl" set-query-param ]
85         [
86             background>> [
87                 rgba>hex "bg,s," prepend "chf" set-query-param
88             ] when*
89         ]
90         [
91             foreground>> [
92                 rgba>hex "chco" set-query-param
93             ] when*
94         ]
95         [ margin>> [ x,y "chma" set-query-param ] when* ]
96         [ bar-width>> [ "chbh" set-query-param ] when* ]
97     } cleave ;
98
99 PRIVATE>
100
101 : chart. ( chart -- )
102     chart>url present dup length 2000 < [ http-image. ] [
103         "?" split1 swap http-post nip
104         "png" (image-class) load-image* image.
105     ] if ;