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

« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total

Sparkline: from PIL to S60

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
# modified from  Joe Gregorio's article at
# http://www.xml.com/pub/a/2005/06/22/sparklines.html

from appuifw import *
import e32

lock = e32.Ao_lock()
c = Canvas()
app.body = c
draw = c._draw

red, green, blue, gray = 0xff0000, 0x00ff00, 0x0000ff, 0x777777

def plot_sparkline(results, step=2, height=20, \
    min_m=None, max_m=None, last_m=None, \
    min_color=blue, max_color=green, last_color=red):
    coords = zip(range(1,len(results)*step+1, step), \
       [height - 3  - y/(101.0/(height-4)) for y in results])
    draw.line(coords, gray)
    if min_m:
        min_pt = coords[results.index(min(results))]
        draw.rectangle([min_pt[0]-1, min_pt[1]-1, min_pt[0]+1, min_pt[1]+1], fill=min_color)
    if max_m:
        max_pt = coords[results.index(max(results))]
        draw.rectangle([max_pt[0]-1, max_pt[1]-1, max_pt[0]+1, max_pt[1]+1], fill=max_color)
    if last_m:
        end = coords[-1]
        draw.rectangle([end[0]-1, end[1]-1, end[0]+1, end[1]+1], fill=last_color)


results = [88,84,82,92,82,86,66,82,44,64,66,88,96,80,24,26, \
        14,0,0,26,8,6,6,24,52,66,36,6,10,14,30]
plot_sparkline(results, 3, 30, min_m=1, max_m=1, last_m=1)

app.exit_key_handler = lock.signal
lock.wait()

drawing circle function

Just to make drawing a circle easier.
Here is a function that will draw yellow circle with black outline.
# from appuifw import *
# c = Canvas()
# app.body = c

def circle(x,y,radius=5, outline=0, fill=0xffff00, width=1):
  c.ellipse((x-radius, y-radius, x+radius, y+radius), outline, fill, width)

You may use other default values.

Then I create a function to show circles randomly
import e32
from random import randint, choice

sleep = e32.ao_sleep
colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff]

def rand_circle(n):
  c.clear()
  for i in range(n):
    circle(randint(0,176), randint(0,144), randint(5,20), fill=choice(colors))
    sleep(0.1)

# now show it
rand_circle(30)

Canvas example

A few days ago, I posted an example for py_s60 1.1.0
Now that 1.1.3 is released, here's an updated version.
# mincanvas.py : minimal canvas example
# It draws to screen directly (doesn't use canvas callback)
# this code is for 1.1.3 version

import e32, appuifw, graphics
from key_codes import *

app = appuifw.app
screen_x, screen_y = 176, 208
x = y = 0
vx = vy = 1
running = 1

def set_exit():
    global running
    running = 0

def add_dir(dx, dy):
    global vx, vy
    vx += dx
    vy += dy

# change screen, body
app.screen = 'full'
c = appuifw.Canvas()
app.body = c
draw = graphics.Draw(c)

# bind 6 keys
app.exit_key_handler= set_exit
c.bind(EKeyRightArrow,lambda:add_dir(1, 0))
c.bind(EKeyLeftArrow,lambda:add_dir(-1, 0))
c.bind(EKeyUpArrow,lambda:add_dir(0, -1))
c.bind(EKeyDownArrow,lambda:add_dir(0, 1))
c.bind(EKeyDevice3, draw.clear)  # press joy stick

# main loop
while running:
    # move x, y
    x = (x + vx) % screen_x
    y = (y + vy) % screen_y
    # plot a red dot at (x, y)
    draw.point((x, y), 0xff0000)
    e32.ao_sleep(0.1)
« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total