Archived:Using graphs in PySymbian
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"
#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
#this five lines will display the unit on the y axis i.e. height of graph
#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)
#the code will allow the user to exit the application when he presses the exit key
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.
some other graphs:
--ck.umraliya 18:36, 19 July 2008 (EEST)