(archive 'newLISPer)

October 5, 2008

Going API

Filed under: newLISP — newlisper @ 23:02
Tags:

Not functional in the archive version.

I’ve been playing with the Google chart API, and it’s a great way of inserting charts into your web pages. Here, for example, is a pie chart showing the relative sizes of the different directories in this web site:

And this is the code in the post that generates the chart.

— (Route.Blog:pie-chart “600×200” “t:10.79,1.07,42.80,2.15,19.42,3.59,18.70,1.43” “p3” “views|resources|jack-the-glypher|includes|images|dragonfly-framework|downloads|databases”) —

This chart is generated by Google automatically whenever the page is loaded. Originally it was a live chart, produced by examining the current file system every time the page was loaded, but this made the page take too long to generate… :)

Charts can be generated using this newLISP function:

(define (pie-chart
  (chs "400x200")
  (chd "t:60,40")
  (cht "p3")
  (chl "Hello|World")
  (chalt "Sample Google Chart"))
  (format
    {<img src="http://chart.apis.google.com/chart?chs=%s&chd=%s&cht=%s&chl=%s" alt="%s">}
    chs chd cht chl chalt))

which is called like this:

- --(Route.Blog:pie-chart  "600x200" "t:10.79,1.07,42.80,2.15,19.42,3.59,18.70,1.43" "p3" "views|resources|jack-the-glypher|includes|images|dragonfly-framework|downloads|databases") - --

although there shouldn’t be spaces between the three “-” signs (see lispinwebpages” for details).

This following little bit of code is useful for converting numbers into percentages, because the pie chart expects percentage values by default:

(define (make-percentages)
  (let ((total 0))
     (set 'total (apply add (args)))
     (map (fn (f) (mul 100 (div f total))) (args))))

(make-percentages 300 800)
;->(27.27272727 72.72727273)

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Create a free website or blog at WordPress.com.