Has anyone gotten the (excellent) ChartDirector charting library to work with zope? Here's what I have tried: 1. Created an ExternalMethod named barchart.py, in the Extensions folder: #!/usr/bin/python2.3 def drawBarChart(): from pychartdir import * #The data for the bar chart data = [85, 156, 179.5, 211, 123] #The labels for the bar chart labels = ["Mon", "Tue", "Wed", "Thu", "Fri"] #Create a XYChart object of size 250 x 250 pixels c = XYChart(250, 250) #Set the plotarea at (30, 20) and of size 200 x 200 pixels c.setPlotArea(30, 20, 200, 200) #Add a bar chart layer using the given data c.addBarLayer(data) #Set the x axis labels using the given labels c.xAxis().setLabels(labels) #output the chart print "Content-type: image/png\n" binaryPrint(c.makeChart2(PNG)) 2. Add an external method in the ZMI id: barchart title: black module name: barchart function name: drawBarChart 3. Create a dtml method to call it, named callchart.htm with contents: <img src="<dtml-var barchart>"> 4. When run, this returns: none What am I doing wrong? Thanks! Harlow Pinson Indepth Learning Email: hpinson@indepthl.com Web: http://www.indepthl.com Voice: 505-994-2135 FAX: 208-475-7678
----- Original Message ----- From: <hpinson@indepthl.com>
Has anyone gotten the (excellent) ChartDirector charting library to work with zope?
Here's what I have tried:
1. Created an ExternalMethod named barchart.py, in the Extensions folder: #output the chart print "Content-type: image/png\n" binaryPrint(c.makeChart2(PNG))
Where is 'binaryPrint' printing to (a file? stdio?)? I am not familiar with ChartDirector, but with PIL we have to trick it to write to a 'ram file' and then return that, here are the relevant extracts from an external method that uses PIL: <snip> class RamFile: def __init__(self): self.contents = [] def write(self,s): self.contents.append(s) def read(self): return string.join(self.contents,'') </snip> <snip> canvas.drawString("S", 7,263, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.flush() rfile = RamFile() canvas.save(file=rfile,format="jpeg") return rfile.read() </snip> HTH Jonathan
Am Samstag, den 12.02.2005, 08:06 -0500 schrieb Jonathan Hobbs:
----- Original Message ----- From: <hpinson@indepthl.com>
Has anyone gotten the (excellent) ChartDirector charting library to work with zope?
Here's what I have tried:
1. Created an ExternalMethod named barchart.py, in the Extensions folder: #output the chart print "Content-type: image/png\n" binaryPrint(c.makeChart2(PNG))
Where is 'binaryPrint' printing to (a file? stdio?)? I am not familiar with ChartDirector, but with PIL we have to trick it to write to a 'ram file' and then return that, here are the relevant extracts from an external method that uses PIL:
<snip> class RamFile: def __init__(self): self.contents = []
def write(self,s): self.contents.append(s)
def read(self): return string.join(self.contents,'') </snip>
<snip> canvas.drawString("S", 7,263, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.flush() rfile = RamFile() canvas.save(file=rfile,format="jpeg") return rfile.read() </snip>
Hm. Wonder why you dont just use cStringIO here? Tino
Hi Harlow, You're getting confused because you have a cgi environment. An externbal method is a "Zope thing" though, it needs to return whatever you want Zope to return to the browser... hpinson@indepthl.com wrote:
#!/usr/bin/python2.3
You don't need this, this will never be executed as a script.
def drawBarChart():
from pychartdir import *
from cStringIO imort StringIO output = StringIO()
#The data for the bar chart data = [85, 156, 179.5, 211, 123]
#The labels for the bar chart labels = ["Mon", "Tue", "Wed", "Thu", "Fri"]
#Create a XYChart object of size 250 x 250 pixels c = XYChart(250, 250)
#Set the plotarea at (30, 20) and of size 200 x 200 pixels c.setPlotArea(30, 20, 200, 200)
#Add a bar chart layer using the given data c.addBarLayer(data)
#Set the x axis labels using the given labels c.xAxis().setLabels(labels)
#output the chart
print >> output, "Content-type: image/png\n"
binaryPrint(c.makeChart2(PNG))
# you need to get the above to append what it generates to 'output'. return output.getvalue() cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
participants (4)
-
Chris Withers -
hpinson@indepthl.com -
Jonathan Hobbs -
Tino Wildenhain