×
Namespaces

Variants
Actions
Revision as of 02:03, 29 July 2008 by hartti (Talk | contribs)

Archived:Using graphs in PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Contents

Graphs in Python

The following code generate graphs by very simple inputs by the user. Initially this code is made to draw three graphs. Entire information about the code is given as comment lines.

import e32, appuifw,graphics
 
#title of the application
appuifw.app.title = u"GRAPHS BY CHINTAN"
 
def quit():
app_lock.signal()
 
def draw():
 
#title of graph 1 assigned to n1. height of graph 1 assigned to x1
n1 = appuifw.query(u"Title Of Graph 1","text")
x1 = appuifw.query(u"Enter Height Of Graph 1 (From 1 To 250):","number")
n2 = appuifw.query(u"Title Of Graph 2","text")
x2 = appuifw.query(u"Enter Height Of Graph 2 (From 1 To 250):","number")
n3 = appuifw.query(u"Title Of Graph 3","text")
x3 = appuifw.query(u"Enter Height Of Graph 3 (From 1 To 250):","number")
 
#In python the height of rectangle will increase as the y co-ordinate of second point decreases.
#since we are using the given height as a coordinate of rectangle
#we have to substract them from 270. this value is the maximum value of y co-ordinate for graph.
y1 = 270 - x1
y2 = 270 - x2
y3 = 270 - x3
 
#the height is converted into string to display it on the top of the graph
x11 = str(x1)
x22 = str(x2)
x33 = str(x3)
 
#defining the canvas
canvas = appuifw.Canvas()
appuifw.app.body = canvas
 
#this code line clears the canvas with white color.
#the two lines are the x and y axis lines of our graph
canvas.clear(0xfefefe)
canvas.line((40,270,260,270), 0)
canvas.line((259,10,259,270), 0)
 
#this five lines will display the unit on the y axis i.e. height of graph
canvas.text((259,220),u"-50")
canvas.text((259,170),u"-100")
canvas.text((259,120),u"-150")
canvas.text((259,70),u"-200")
canvas.text((259,20),u"-250")
 
#the code will display the height on the top of graph
#the text will always stick to the height
#whatever it is ranging from 1 to 250
canvas.text((60,y1-5),u"  %s"%(x11), fill=0xffff00)
canvas.text((125,y2-5),u"  %s"%(x22), fill=0x0000ff)
canvas.text((190,y3-5),u"  %s"%(x33), fill=0xbe6a0e)
 
#this code lines shows the name of the graph on x-axis
canvas.text((60,285), u" %s"%(n1), fill=0x4b2192)
canvas.text((125,285), u" %s"%(n2), fill=0x4b2192)
canvas.text((190,285), u" %s"%(n3), fill=0x4b2192)
 
#and now finally draw the thing for which the entire application is written i.e. the graphs.
canvas.rectangle((60,y1,115,270), outline=0xff0000, fill=0xffff00)
canvas.rectangle((125,y2,180,270), outline=0xff0000, fill=0x0000ff)
canvas.rectangle((190,y3,245,270), outline=0xff0000, fill=0xbe6a0e)
 
draw()
 
#the code will allow the user to exit the application when he presses the exit key
appuifw.app.exit_key_handler=quit
app_lock=e32.Ao_lock()
app_lock.wait()

Screenshots

The code just looks long but its very easy to understand. Once the graph is drawn a screenshot function can be used to save the image if user wants. Some of the images of the above code are given below.

Graph name:

File:11.jpeg


Graph size:

File:12.jpeg


Graph axis:

File:13.jpeg


Graph:

File:14.jpeg


some other graphs:

File:24.jpeg File:34.jpeg


--ck.umraliya 18:36, 19 July 2008 (EEST)

54 page views in the last 30 days.
×