Google Charts from Clojure

By | 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"])))

97 thoughts on “Google Charts from Clojure

  1. jonathan Post author

    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.

    Reply
  2. Tammara

    Generally I don’t read aarticle oon blogs, however I wish to say that this write-up very forced
    me to check out and do it! Your writying taste has been amazed me.
    Thaznk you, quite great post.

    Reply
    1. Janet

      Are there pro-people monetary policies in global capitalism? Are you suggesting we should change to communism? even in China this is not done. Or may be we may try the welfarism as you have in Canada and the nordic countries? Even there, govt does not employ everybody.Govt in canada does not spend 70 % of there resources on the civil servants and posDticianl.ioctor please give us another theory

      Reply
  3. americanmarine.org

    What’s up everybody, here every one is sharing these know-how, thus it’s pleasant to read this weblog, and I
    used to visit this web site all the time.

    Reply
  4. Ban camera han quoc tai Ninh Thuan

    I’ll immediately take hold of your rss feed as I can’t
    in finding your email subscription link or newsletter service.
    Do you have any? Kindly permit me recognize in order
    that I may just subscribe. Thanks.

    Reply
  5. reason 8 serial number

    If, on the other hand, you live in a more temperate climate,
    two quarts may seem excessive. Even that
    small amount of unexpected and unforeseen contact with an ex boyfriend can cause massive amount s of confusion, and set you back from your
    course of being a determined single woman. Plantain leaf is also very helpful in treating this problem.

    Reply
  6. csr racing astuce

    After exploring a few of the articles on your
    site, I truly like your way of blogging. I book-marked it to my bookmark webpage
    list and will be checking back soon. Please visit my website too and let me know
    your opinion.

    Reply
  7. dien dan rao vat nam dinh

    Oh my goodness! Incredible article dude!
    Many thanks, However I am encountering difficulties with your RSS.
    I don’t know why I can’t join it. Is there anybody
    getting similar RSS problems? Anyone who knows the answer can you kindly respond?
    Thanks!!

    Reply
  8. neko atsume kitty collectors cheats gold fish

    The accounting product can perform all the business accounting modules for its customers to manage
    their sales, payroll, finances, inventory and customers. Chucky was not placed
    on the list due to being ill. Meditation is recognized as an important practice on a
    wider and wider basis these days.

    Reply
  9. link

    Asking questions are genuinely pleasant thing if you are not understanding something totally, but this paragraph offers pleasant
    understanding even.

    Reply
  10. ????????????????????

    Hello everyone, it’s my first pay a visit at this site, and piece of writing is truly
    fruitful designed for me, keep up posting these
    articles or reviews.

    Reply
  11. visaxinum

    Hello I am so glad I found your web site, I really found
    you by error, while I was browsing on Bing for something else,
    Regardless I am here now and would just like to say kudos for
    a fantastic post and a all round entertaining blog (I also love the theme/design), I
    don’t have time to go through it all at the minute but I have saved it and
    also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the superb jo.

    Reply
  12. Topshop

    They could be made out of natural or fabricated bristles which might be retractable or protected with a cap.

    Reply
  13. Anime Movie

    It does not only depict the creativity and the independence of the Japanese people, it also reflects the strength of the Japanese by heart.
    I love going into Best Buy with a $50 gift card and walking up and down every
    single aisle until I have made the important decision on what to
    spend my gift card on. It is an art form that is truly contemporary, and it transcends race, culture and economic background.

    Reply
  14. Charity

    Likewise, have a look at this directory site at, they have a search function and have a very thorough
    directory of car locksmith professionals.

    Reply
  15. mp3juice

    Awesome things here. I’m very happy to peer your post.

    Thanks a lot and I am having a look forward to touch you. Will
    you kindly drop me a mail?

    Reply
  16. new year 2017

    You really make it seem so easy with your presentation but I find this matter to be actually something
    that I think I would never understand. It seems too complex
    and extremely broad for me. I’m looking forward for your next post, I will try to get the hang of it!

    Reply
  17. Florida

    Please let me know if you’re looking for a author for your blog.

    Yoou have some really good articles and I think I would be a good asset.
    If yoou ever want to take some of tthe load off,
    I’d love to write some artichles for your blog in exchange for a link back
    to mine. Please shoot me an email iif interested. Thanks!

    Reply
  18. Whitney

    Why people still use to read news papers when in this
    technological world everything is presented on web?

    Reply
  19. ??? ?????? ?? ???? ????

    I was suggested this blog through my cousin. I’m no longer certain whether or not this
    post is written by means of him as nobody else recognise such detailed approximately my
    trouble. You’re amazing! Thanks!

    Reply
  20. about.me

    The title of your optimizers products too. An excellent SEO strategy for a good idea
    to play new content or revise existing ones with a fine-tooth comb and
    ask about reciprocal links. In the second case,
    the more authority you have a domain name, these are fine, smart people.

    You too can become more profitable then most other sources because they contribute
    to the title and description attached to it. If search engine optimization UK or SEO experts so that it can be done naturally or organically.

    Reply
  21. gay

    Do you mind if I quote a few of your articles as long as I provide credit and sources back to your site?

    My blog site is in the very same niche as yours and my visitors would definitely benefit
    from a lot of the information you provide here.
    Please let me know if this ok with you.
    Many thanks!

    Reply
  22. page

    Hello friends, how is all, and what you want to say on the topic of this paragraph, in my view its in fact awesome in favor of
    me.

    Reply
  23. 2017 ford price

    Leesburg TaxiStep To Finding The Right Used Car For Your NeedsBy:
    Neal Weaver – Individual sellers offer the thrifty
    car rental used cars are also available if the clients get new cars Beaver Dam through the
    same.

    Reply
  24. buy computers

    Hi there to every , sinmce I am in fact eager of reading this webpage’s post to be
    updated daily. It contains pleasant information.

    Reply
  25. ski holiday verbier

    Have you ever thought about including a little bit more thaan just your articles?
    I mean, what you say iis important and all. But imagine
    iif you added some great images or video clips to give your posts more, “pop”!

    Your contenjt is excellent but with picds annd clips, this blog could undeniably be one of the greatest in its field.

    Awesome blog!

    Reply
  26. ????

    It can be confusing trying to find the best deals
    with so many to choose from. It may seem easy to improve
    your search engine optimisation by using relevant content, but it.
    Search engine optimization services are provided
    by companies employing content writers, SEO copywriters, website designers and other SEO experts who are all
    experienced and trained in the various disciplines of SEO
    and in dealing with the requirements of various organizations.

    Reply
  27. ????????????????????

    Do you mind if I quote a couple of your posts as long
    as I provide credit and sources back to your site? My website is in the very same niche as yours and
    my visitors would truly benefit from a lot of the
    information you present here. Please let me know if this okay with you.
    Thank you!

    Reply
  28. Leta

    If you actually are performing everything appropriate and weight loss
    isn’t happening, then it seems pretty clear that the calorie intake you came up
    with is off and demands to be adjusted.

    Reply
  29. como aumentar pênis

    What’s Taking place i’m new to this, I stumbled
    upon this I’ve found It absolutely useful and it has aided me out loads.
    I’m hoping to contribute & aid other customers like its aided me.

    Great job.

    Reply
  30. test

    I’m impressed, I have to admit. Seldom do I come across a blog that’s equally educative
    and amusing, and without a doubt, you have hit the nail on the
    head. The problem is an issue that not enough folks are speaking
    intelligently about. I am very happy that I came across this during
    my hunt for something relating to this.

    Reply
  31. støvsugertest.eu

    Hello this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise so I wanted to get advice from someone with experience.
    Any help would be enormously appreciated!

    Reply
  32. Juliann

    You should take part in a contest for one of the most useful sites
    on the web. I will highly recommend this site!

    Reply
  33. Ethan

    Magnificent goods from you, man. I have take into account your stuff prior to and
    you are just too magnificent. I actually like what you have got right here, really like
    what you’re saying and the best way in which you say it.
    You’re making it enjoyable and you continue to take care of to keep
    it smart. I cant wait to learn far more from you. That is actually a great web site.

    Reply
  34. okapy kuchenne

    Unquestionably imagine that that you stated. Your favourite reason appeared to be
    at the web the easiest factor to keep in mind of. I say to you,
    I certainly get annoyed whilst folks consider concerns that they just do
    not realize about. You controlled to hit the nail upon the highest and also outlined out the whole
    thing without having side effect , folks could take a signal.
    Will likely be back to get more. Thanks

    Reply
  35. plancha de pelo

    You’re so cool! I don’t suppose I have read a single thing like that before.
    So wonderful to discover someone with a few original thoughts on this subject.
    Seriously.. thanks for starting this up. This website is something that is required on the internet, someone with some originality!

    Reply
  36. Theda

    I have to thank you for the efforts you’ve put in writing this website.
    I really hope to check out the same high-grade content by
    you later on as well. In truth, your creative writing abilities has encouraged me to get my own blog
    now ;)

    Reply
  37. jerryhillyer.com

    Hey there! I could have sworn I’ve been to this website before but after reading
    through some of the post I realized it’s new to me.
    Anyways, I’m definitely delighted I found it and I’ll be bookmarking and checking back frequently!

    Reply
  38. beijing-forbiddencity.com

    I am not certain where you’re getting your information,
    however good topic. I needs to spend a while studying
    more or figuring out more. Thank you for excellent info I was
    searching for this info for my mission.

    Reply
  39. dammsugare bäst i test

    Howdy this is somewhat of off topic but I was wanting to know if blogs use
    WYSIWYG editors or if you have to manually code
    with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get guidance from someone with
    experience. Any help would be enormously appreciated!

    Reply
  40. matelas gonflable comparatif

    Thanks a bunch for sharing this with all folks you really recognise what you are speaking
    approximately! Bookmarked. Kindly also visit my web site =).

    We could have a hyperlink change arrangement among us

    Reply
  41. motosega classifica del 2017

    I blog often and I truly appreciate your information. This
    article has really peaked my interest. I am going to take a note of your site and keep checking for new information about once per week.
    I opted in for your Feed too.

    Reply
  42. Kacey

    great issues altogether, you just won a logo new reader.

    What may you suggest about your submit that you made a few days in the
    past? Any positive?

    Reply
  43. Marti

    Cabinet d’avocats contester testament olographe

    Un testament mystique est un acte écrit par lequel
    le testateur liste les personnes à qui il souhaite
    transmettre les biens au moment de son décès ainsi que dans quelles proportions.

    La représentation d’un avocats est obligatoire pour s’assurer
    de la validité d’un testament authentique ou, au contraire,
    le contester. Dans tous les cas, vous devez faire faire le testament
    par un avocat ou encore le déposer chez ce dernier.

    Reply
  44. cel mai bun polizor unghiular

    Thanks , I’ve recently been searching for info about this topic for a long time
    and yours is the greatest I’ve discovered so
    far. But, what about the bottom line? Are you
    positive in regards to the supply?

    Reply
  45. epilatoare

    Have you ever thought about creating an e-book or guest authoring on other blogs?
    I have a blog based upon on the same subjects you discuss and would love to have
    you share some stories/information. I know my readers would enjoy your work.
    If you are even remotely interested, feel free
    to shoot me an e mail.

    Reply
  46. Jesus Durden

    I was curious if you ever considered changing the layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two images. Maybe you could space it out better?

    Reply
  47. Ellis Orme

    I have to thank you for the efforts you’ve put in writing this website. I’m hoping to view the same high-grade blog posts from you later on as well. In fact, your creative writing abilities has encouraged me to get my own, personal blog now ;)

    Reply
  48. Kecantikan

    For latest information you have to pay a visit the web and on world-wide-web I found this web site
    as a best web page for newest updates.

    Reply
  49. OMEGA AVOCATS Succession

    Hi there superb blog! Does running a blog like this take a great deal of work?
    I’ve no expertise in computer programming however
    I had been hoping to start my own blog in the near future.
    Anyhow, if you have any recommendations or tips for new blog
    owners please share. I know this is off subject but I simply had
    to ask. Thank you!

    Reply
  50. Hunter

    Wonderful goods from you, man. I’ve understand your
    stuff previous to and you’re just too wonderful.
    I really like what you have acquired here, certainly like what you’re
    stating and the way in which you say it.
    You make it enjoyable and you still take care of to keep it
    sensible. I can’t wait to read far more from you.
    This is actually a terrific web site.

    Reply
  51. sdraietta prezzo

    I loved as much as you will receive carried out right here.

    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get bought an nervousness over that you wish be delivering the following.

    unwell unquestionably come further formerly again since
    exactly the same nearly a lot often inside case you shield this increase.

    Reply
  52. sechoir a linge classement

    Admiring the commitment you put into your website and detailed
    information you provide. It’s nice to come across a blog every once in a while that isn’t the same unwanted
    rehashed information. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google
    account.

    Reply
  53. pompe d'arrosage

    Aw, this was an incredibly nice post. Finding the time and actual effort to create a good article…
    but what can I say… I hesitate a lot and
    don’t seem to get anything done.

    Reply
  54. ollas express comparativa

    Hello, I think your website might be having
    browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet
    Explorer, it has some overlapping. I just wanted to
    give you a quick heads up! Other then that, great blog!

    Reply
  55. rodillos para bicicleta comparativa

    certainly like your web site however you have to
    take a look at the spelling on several of your posts.
    Many of them are rife with spelling issues and I find it very troublesome to tell the reality nevertheless
    I’ll surely come back again.

    Reply
  56. caisse a outils classement

    After I initially left a comment I seem to have clicked on the -Notify me when new
    comments are added- checkbox and now each time a comment is added I get 4 emails with the same comment.
    There has to be an easy method you are able to remove me from that service?
    Kudos!

    Reply
  57. videocitofono

    After I originally left a comment I seem to have
    clicked on the -Notify me when new comments are added- checkbox and now each time a comment is added
    I recieve four emails with the same comment.
    Is there an easy method you are able to remove me
    from that service? Many thanks!

    Reply
  58. Victoriver.com

    Excellent pieces. Keep writing such kind of information on your page.
    Im really impressed by it.
    Hello there, You have done an excellent job. I will certainly
    digg it and in my opinion recommend to my friends.
    I am sure they’ll be benefited from this website.

    Reply
  59. cronotermostato

    If some one needs to be updated with newest technologies therefore he must be visit this
    web page and be up to date every day.

    Reply
  60. aparatura zabezpieczaj?ca

    It’s a pity you don’t have a donate button! I’d definitely
    donate to this outstanding blog! I suppose for now i’ll settle for bookmarking
    and adding your RSS feed to my Google account. I look forward to fresh updates and will share this site with my
    Facebook group. Chat soon!

    Reply
  61. tosaerba prezzo

    Hi there, I wish for to subscribe for this webpage to obtain most up-to-date updates,
    thus where can i do it please help out.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *