Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Sparkline: from PIL to S60 (See related posts)

py_s60 1.1.3 has its drawing API very similar to PIL.
Porting from PIL to S60 is very easy. Here's an example
that I port a sparkline drawing function from
Joe Gregorio's article at xml.com
   1  
   2  # modified from  Joe Gregorio's article at
   3  # http://www.xml.com/pub/a/2005/06/22/sparklines.html
   4  
   5  from appuifw import *
   6  import e32
   7  
   8  lock = e32.Ao_lock()
   9  c = Canvas()
  10  app.body = c
  11  draw = c._draw
  12  
  13  red, green, blue, gray = 0xff0000, 0x00ff00, 0x0000ff, 0x777777
  14  
  15  def plot_sparkline(results, step=2, height=20, \
  16      min_m=None, max_m=None, last_m=None, \
  17      min_color=blue, max_color=green, last_color=red):
  18      coords = zip(range(1,len(results)*step+1, step), \
  19         [height - 3  - y/(101.0/(height-4)) for y in results])
  20      draw.line(coords, gray)
  21      if min_m:
  22          min_pt = coords[results.index(min(results))]
  23          draw.rectangle([min_pt[0]-1, min_pt[1]-1, min_pt[0]+1, min_pt[1]+1], fill=min_color)
  24      if max_m:
  25          max_pt = coords[results.index(max(results))]
  26          draw.rectangle([max_pt[0]-1, max_pt[1]-1, max_pt[0]+1, max_pt[1]+1], fill=max_color)
  27      if last_m:
  28          end = coords[-1]
  29          draw.rectangle([end[0]-1, end[1]-1, end[0]+1, end[1]+1], fill=last_color)
  30  
  31  
  32  results = [88,84,82,92,82,86,66,82,44,64,66,88,96,80,24,26, \
  33          14,0,0,26,8,6,6,24,52,66,36,6,10,14,30]
  34  plot_sparkline(results, 3, 30, min_m=1, max_m=1, last_m=1)
  35  
  36  app.exit_key_handler = lock.signal
  37  lock.wait()

You need to create an account or log in to post comments to this site.


Click here to browse all 5827 code snippets

Related Posts