|
May
01
|
|
|
Categories: Python
| Tags: ChartDirector, Python
| Views: 1,992
|
前面, 我们画了第一个图, 并把它保存到了文件, 下面我们看看怎么把它直接插入到网页中:
文件: pythondemo_cgi\simplebar.py
#!/usr/bin/python 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 labels on the x axis. c.xAxis().setLabels(labels) # Output the chart print "Content-type: image/png\n" binaryPrint(c.makeChart2(PNG))
代码中, 前面部分和hello world的例子相同, 只有后面这两句有所不同:
print "Content-type: image/png\n" binaryPrint(c.makeChart2(PNG))
这两句是输出一个MIME类型的头部, 并输出一个准备送往浏览器的二进制图片.
上面的代码只是生成了一个二进制图片,那么我们如果把它插入到网页中呢?
其实和普通的图片的插入方法是一样的:
<HTML> <BODY> <h1>Hello World!</h1> <p>Hi, this is my first web page with ChartDirector charts.</p> <IMG SRC="http://aaa.bbb.ccc.ddd/cgi-bin/pythondemo_cgi/simplebar.py"> More HTML elements ...... </BODY> </HTML>
在后面的例子中, 我们将不再例举CGI的例子,
因为只要将保存成图片的例子最后的makechart改成生成二进制图片的代码就行了
From 迷途知返, post PyChartDirector中文教程[3]:嵌入网页
RELATED POSTS:
Leave a comment
| Trackback

