Python, Django, Open Flash Chart (pyofc2)

Graphs are a pain in the ass. I first attempted to get PYOFC2 to work about a year ago and gave up, I just didn't care enough. Now I want graphs, and have figured out how to use it (at least for a pie chart). urls.py

from django.conf.urls.defaults import *
from django.conf import settings

# the chart data views
urlpatterns = patterns('yourapp.views',
    ('^data/$','chart_data_two'),
)

urlpatterns += patterns('django.views.generic.simple',
    (r'^$', 'direct_to_template', {'template': 'index.html'}),
)

templates/index.html

Chart!








<h1>Pie Chart!</h1>
<div id="chart_demo" class="chart"> </div>

ls -l static/*

-rwxr-xr-x 1 akonkol akonkol    773 2011-03-02 16:08 expressInstall.swf
-rw-r--r-- 1 akonkol akonkol 263109 2011-03-02 16:08 open-flash-chart.swf
-rw-r--r-- 1 akonkol akonkol   9759 2011-03-02 16:08 swfobject.js

yourapp/views.py

from django.http import HttpResponse
from pyofc2  import *
import random
import time

def chart_data_two(request):
    t = title(text='Crapface Graph')
    p1 = pie()
    values = [ pie_value(label="crap", value=4), pie_value(label="face", value=20)]
    p1.values = values
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(p1)
    return HttpResponse(chart.render())

Produces: crap_face References demodjofc2_demo

Tagged as charts , django graphing , python
Written by Andrew Konkol on March 3rd, 2011

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.