PyGDChart
ZGDChart も良いのですが、GDChartのバージョンが少々古いです。用途によっては新しい機能が使いたくなることもあるのではないでしょうか。
そのようなときにはとりあえず、External Method を使うことで新しいバージョンを使うことができます。個人的には Scatter が使いたかったのです。
- まずはインストール
- gd-2.0.27 - gdchart0.11.4dev - pygdchart2alpha2 pygdchart2beta1.tar.gzというアーカイブ名なのですが、展開したら alpha2 でした
とりあえず、configure して make して su して make install で通ったり python setup.py buid とか install などでどうにかなったのでよく覚えてません。
- External Method を作る
自分のやりたいことに合わせて、external method を作ります。私の作ったのを例示します
import gdchart2 import tempfile import os def wc(title, label, data, scatter, scsize, width=640, height=480, sccolor="red", scstyle="CIRCLE"): sgraph = gdchart2.Line() sgraph.width = width sgraph.height = height sgraph.title = title sgraph.plot_color = 0x8888ff sgraph.xtitle_color = 0x446688 sgraph.ytitle_color = 0x446688 sgraph.xlabel_color = 0x446688 sgraph.ylabel_color = 0x446688 sgraph.bg_color = "white" sgraph.bg_transparent = True sgraph.ylabel_density = 25 sgraph.ylabel_fmt =%6.2fsgraph.setLabels(label) sgraph.setData(data[0], data[1]) # この辺は適当に変えてくださいねscats = [] for i in scatter: s = gdchart2.Scatter(i[0], i[1], scstyle, scsize, sccolor) scats.append(s) sgraph.setScatter(scats)
# 理由はわからないんですが、自分で tempfile を作ってあげないと # ちゃんと動作しませんでした。 tf = tempfile.mkstemp() tfn = tf[1] sgraph.draw(tfn) tff = file(tfn) vol = tff.read(-1) tff.close() os.remove(tfn)
return vol
単体で動くことを確認したら、External Method として登録します。とりあえず gdchart という名前にしてみました
- PythonScript
Zope/Plone から表示するには PythonScript でこれをラッピングするのが手軽じゃないかと思いました
# ここまでにmytitle, label, (graph1, graph2), scat, scsize, width などを設定する request.RESPONSE.setHeader(
content-type,image/png) request.RESPONSE.setHeader(pragma,no-cache) print context.gdchart(mytitle, label, (graph1, graph2), scat, scsize, width) return printedとりあえず、表示できたみたいです(^^;;