uk swigers

kitchener waterloo dating

online dating ireland

executive dating toronto

date boys

single parents personals

dating married women

cottonwood singles

escort 4500

dating ad

hot women wallpaper

deborah couples

men's dating

rochester personals

ads online personal

vancouver dating service

san francisco swinger clubs

find sex parties

adulte friend

pantyhose websites

singles 1984

indian single girls

hotlive girls

personals aol com

couples two

escortgirls

recovery dating sites

single women meet

free asian dating service

match making web site

personals europe

uk catholic singles

singles personal

where are single men

single rich women

love match compatibility

international online dating

messianic jewish dating

jewish singles

girls on web cam

dating free senior

gays personals

chat escort

kinky chat rooms

denver single women

date ladies

college dating services

disabled dating agency

date indiana

sean paul singles

date professionals

women looking for sex

harrison singles

single man looking

cam shows

west palm escort

free personal profile

blonde hot women

live lesbian chat

dating and friendship site

lopez singles

couples vacations

uk singels

christian singles dallas

dating internet review

singles in norway

whores online

ballet singles

lesbian penpals

miami velvet swing club

swap finder

friend finders com

find sex dates

dating and marriage

dating free local online

dating parent single site

dark sex chat

speed datin

couples counseling atlanta

cam sex show

prayers for singles

mujeres escort

find a date online

reader personals

www wifeswap com

shining star singles

callgirls com

meeting single men

gay jewish dating

dc prostitutes

uk gay chat rooms

meet latin women

hot live girls

phone numbers sex

adultvideo com

fling personals

mobile sex lines

singles clubs surrey

web dating uk

dating free site web

Google Charts from Clojure 1

Posted by jonathan on February 16, 2008

I wrote up a very simple example of calling Google Charts api from Clojure.

Example Google Charts Graph

Note: Rich Hickey actually emailed with some suggestions, and these have been added. I added a comment on the one I found most interesting, which is a function literal. Thanks Rich!

(in-ns 'google-chart)
(clojure/refer 'clojure)

(def *amp* "&")

; ---- connect to google charts and retrieve a chart ----
(defn get-google-chart [request]
  (new javax.swing.ImageIcon (new java.net.URL request) "Google Chart"))

; ---- pop-up a png in a dialog frame ----
(import '(javax.swing JLabel JFrame))

(defn show-chart [request]
  (println "Showing chart: " request)
  (doto (new JFrame "Google Chart")
    (add (new JLabel (get-google-chart request)))
    (pack)
    (setVisible true)))

; display the hello-world chart
;(show-chart (str "http://chart.apis.google.com/chart?"
;                 "cht=p3&chd=s:hW&chs=250×100&chl=Hello|World"))

; ---- simple encoding to 0 - 61 in chars ----

(defn encode [val scale]
  (if (nil? val)
      "_"
      (let [val (. Math (floor (* 61 (/ val scale))))]
        (nth "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" val))))

; ---- build a chart ----

(defn base-url []
  "http://chart.apis.google.com/chart?")

(defn chart-size [x y]
  (str "chs=" x "x" y))

(defn chart-type [type]
  (str "cht=" type))

(defn chart-data [data]
  (let [mx (apply max data)]
    (apply str "chd=s:" (map #(encode % mx) data))))   ; function literal

;### fix spaces -> +
(defn chart-title [title]
  (str "chtt=" title))

(defn chart-axis [x y]
  ; really basic, just an x and y axis
  (apply str "chxt=x,y" *amp*
         "chxl=0:" (concat (interleave (repeat "|") x)
                           ["|1:"] (interleave (repeat "|") y))))

;; Test it out ...
(comment
(show-chart
  (str
    (base-url)
    (chart-size 480 480) *amp*
    (chart-type "lc") *amp*
    (chart-data (map (fn [x] (* x x)) (range -50 51))) *amp*
    (chart-title "x+squared") *amp*
    (chart-axis ["-5" "0" "5"] ["" "" "y-axis"]))))

; Test it out ...
(show-chart
  (str
    (base-url)
    (chart-size 480 240) *amp*
    (chart-type "lc") *amp*
    (chart-data [188.5 186 181.5 183 179.5 181]) *amp*
    (chart-title "Jonathan+Weight") *amp*
    (chart-axis ["Week+1" "Week+2" "Week+3"] ["" "" "190+lbs"])))
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. jonathan Sat, 16 Feb 2008 19:08:41 UTC

    Yes, I eventually managed to get this post to display correctly by editing the pre style which works, versus the code style which appears to unset itself whenever it hits a new line.

    Aside from function literals in Clojure, which are new, I only just noticed that we can now use ‘(comment … )’ as a macro (I’m guessing) to ignore contained text. Excellent.

Comments