diff --git a/extra/google/charts/authors.txt b/extra/google/charts/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/google/charts/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/google/charts/charts.factor b/extra/google/charts/charts.factor new file mode 100644 index 0000000000..d97a97bceb --- /dev/null +++ b/extra/google/charts/charts.factor @@ -0,0 +1,105 @@ +! Copyright (C) 2011 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: accessors arrays assocs colors.hex combinators formatting +http.client images.http images.loader images.loader.private +images.viewer kernel math math.order present sequences splitting +urls ; + +IN: google.charts + +TUPLE: chart type width height title data data-scale labels +background foreground margin bar-width ; + +: ( type -- chart ) + chart new + swap >>type + 320 >>width + 240 >>height ; + +>data ] [ + [ infimum 0 min ] [ supremum 0 max ] bi 2array + x,y >>data-scale + ] bi ; + +: chl ( chart seq -- chart ) x|y >>labels ; + +: chd/chl ( chart assoc -- chart ) + [ values chd ] [ keys chl ] bi ; + +PRIVATE> + +: ( assoc -- chart ) + [ "p" ] dip chd/chl ; + +: ( assoc -- chart ) + [ "p3" ] dip chd/chl ; + +: ( assoc -- chart ) + [ "bvs" ] dip chd/chl ; + +: ( seq -- chart ) + [ "lc" ] dip chd ; + +: ( seq -- chart ) + [ "lxy" ] dip [ keys ] [ values ] bi + [ x,y ] bi@ "|" glue >>data ; + +: ( seq -- chart ) + [ "s" ] dip [ keys ] [ values ] bi + [ x,y ] bi@ "|" glue >>data ; + +: ( seq -- chart ) + [ "ls" ] dip chd ; + +: ( seq -- chart ) + [ "rs" ] dip chd ; + +: ( str -- chart ) + [ "qr" ] dip 1array chl ; + +: ( str -- chart ) + [ "tx" ] dip 1array chl f >>width f >>height ; + +url ( chart -- url ) + [ URL" http://chart.googleapis.com/chart" ] dip { + [ type>> "cht" set-query-param ] + [ + [ width>> ] [ height>> ] bi 2dup and [ + "%sx%s" sprintf "chs" set-query-param + ] [ 2drop ] if + ] + [ title>> "chtt" set-query-param ] + [ data>> "t:" prepend "chd" set-query-param ] + [ data-scale>> [ "chds" set-query-param ] when* ] + [ labels>> "chl" set-query-param ] + [ + background>> [ + rgba>hex "bg,s," prepend "chf" set-query-param + ] when* + ] + [ + foreground>> [ + rgba>hex "chco" set-query-param + ] when* + ] + [ margin>> [ x,y "chma" set-query-param ] when* ] + [ bar-width>> [ "chbh" set-query-param ] when* ] + } cleave ; + +PRIVATE> + +: chart. ( chart -- ) + chart>url present dup length 2000 < [ http-image. ] [ + "?" split1 swap http-post nip + "png" (image-class) load-image* image. + ] if ; diff --git a/extra/google/charts/summary.txt b/extra/google/charts/summary.txt new file mode 100644 index 0000000000..574659f9fe --- /dev/null +++ b/extra/google/charts/summary.txt @@ -0,0 +1 @@ +Google Chart API