You should be automatically redirected . If not, visit
http://newlisper.wordpress.com
and update your bookmarks.

06/10/2008

Going API

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:

--- (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") ---

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 
    {%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 here 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)

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home