Categories: Python | Tags: , | 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改成生成二进制图片的代码就行了

RELATED POSTS:

  1. PyChartDirector中文教程[2]:Hello, world!
  2. PyChartDirector中文教程[4]:简单的饼状图
  3. PyChartDirector中文教程[5]:3D饼图
  4. PyChartDirector中文教程[6]:多深度饼图
  5. PyChartDirector中文教程[7]:饼图的标签
  6. PyChartDirector中文教程[8]:饼图的图例
No comments yet.
;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)

你可以使用@somebody:开头, 来邮件通知somebody你回复了他的留言(用户名区分大小写).