Open Flash Chart

Il est possible via un élément flash de générer dans des pages html des graphiques.

Vous pouvez trouver le module flash qui réalise cette prouesse à l’adresse suivante:
http://teethgrinder.co.uk/open-flash-chart/.

Il existe sur le site de nombreux exemples, et notament des exemples qui utilises php. Il existe aussi une façon simple de mettre en oeuvre cet outils. Sur un serveur web vous devez trouver :

  • le fichier open-flash-chart.swf
  • un fichier swflash.cab
  • un fichier html
  • un fichier de donnée: data1.txt

exemple de fichier de donnée

&title=My Graph Title,{font-size:20px}&
&x_axis_steps=1&
&y_legend=Legend Left,15,#ff4a00&
&y2_legend=Legend Right,15,#4aff00&
&y_ticks=5,10,5&
&filled_bar=50,#005aff,#666666,Bar Stats,10&
&line_hollow_2=2,#05a0FF,Line Stats,10,5&
&values=0,1,2,3,4,5,6,7,8,9&
&values_2=10,11,12,13,14,15,16,17,18,19&
&y2_lines=2&
&show_y2=true&
&x_labels=x_0,x_1,x_2,x_3,x_4,x_5,x_6,x_7,x_8,x_9&
&y_min=0&
&y_max=20&

exemple de fichier html

Vous pouvez trouver dans le répertoire data des exemples complet dans une archive data/flashchart.zip

il est possible de créer un module python pyofc permettant à partir d’une connexion avec une base de donnée générer des graphiques (il faudra créer le fichier html auparavant)

version = '0.1'
author = 'Frederic Aoustin'
description = 'generate data for open flash chart'

class Chart:

    def __init__(self, file, cursor):
        try:
            self._desc = cursor.description
            self._data = cursor.fetchall()
            self._file = open(file,'w')
        except Exception as e:
            raise Exception, 'Error in chart, %s' % e

    def generate(self):
        pass

class PieChart(Chart):
    """cursor:
    |   |title|
    |lb1|val1 |
    |lb2|val2 |
    |lb3|val3 |
    """

    txt = """&bg_colour=#FDFDFD&
&pie=60,#505050,{font-size: 12px; color: #404040;}&
&values=%(values)s&
&pie_labels=%(labels)s&
&colours=#5E83BF,#C11B01,#D2D2FB,#B0C101,#d01f3c,#356aa0,#C79810,#80a033,#9933CC,#CC3399&
"""

    def __init__(self, file, cursor):
        Chart.__init__(self, file, cursor)

    def generate(self):
        id = {}
        id['labels'] = ','.join([str(item[0]) for item in self._data])
        id['values'] = ','.join([str(item[1]) for item in self._data])
        self._file.write(self.txt % id)
        self._file.close()

class BarSketchChart(Chart):
    """cursor:
    |   |y1  |y2  |
    |lb1|val1|val4|
    |lb2|val2|val5|
    |lb3|val3|val6|
    """
    txt = """&x_label_style=11,#303030,2&
&y_label_style=11,#303030&
&y_ticks=0,10,1&
&x_labels=%(labels)s&
&y_min=0&
&y_max=%(max_value)s&
&bg_colour=#FDFDFD&
&x_grid_colour=#FDFDFD&
&y_grid_colour=#FDFDFD&
"""
    txt2 = """&bar_sketch%(cnt)s=55,6,%(color)s,#000000,%(name)s,10&
&values%(cnt)s=%(values)s&
"""
    color = ["#5E83BF","#9933CC","#80a033","#C11B01","#CC3399","#D2D2FB","#B0C101","#d01f3c","#356aa0","#C79810","#80a033","#9933CC","#CC3399"]

    def __init__(self, file, cursor):
        Chart.__init__(self, file, cursor)

    def generate(self):
        id = {}
        id['labels'] = ','.join([str(item[0]) for item in self._data])
        ln = len(self._desc)-1
        m = []
        for i in self._data:
            for j in i[1:]:
                m.append(j)
        id['max_value'] = max(m)
        st = self.txt % id
        k=1
        for i in range(1,ln+1):
            id = {}
            id['values'] = ','.join([str(item[i]) for item in self._data])
            id['name'] = self._desc[i][0]
            id['color'] = self.color[i]
            id['max_value'] = max(m)
            if k == 1:
                id['cnt'] = ''
            else:
                id['cnt']='_%s' % k
            st = st + self.txt2 % id
            k = k+1
        self._file.write(st)
        self._file.close()

class BarGlassChart(BarSketchChart):
    """cursor:
    |   |y1  |y2  |
    |lb1|val1|val4|
    |lb2|val2|val5|
    |lb3|val3|val6|
    """
    txt2 = """&bar_glass%(cnt)s=55,%(color)s,,%(name)s,10&
&values%(cnt)s=%(values)s&
"""

    def __init__(self, file, cursor):
        BarSketchChart.__init__(self, file, cursor)

class LineHollowChart(BarSketchChart):
    """cursor:
    |   |y1  |y2  |
    |lb1|val1|val4|
    |lb2|val2|val5|
    |lb3|val3|val6|
    """
    txt2 = """&line_hollow%(cnt)s=2,%(color)s,%(name)s,10,4&
&values%(cnt)s=%(values)s&
"""

    def __init__(self, file, cursor):
        BarSketchChart.__init__(self, file, cursor)

class AreaHollowChart(BarSketchChart):
    """cursor:
    |   |y1  |y2  |
    |lb1|val1|val4|
    |lb2|val2|val5|
    |lb3|val3|val6|
    """
    txt2 = """&area_hollow%(cnt)s=2,3,25,%(color)s,%(name)s,10,%(color)s&
&values%(cnt)s=%(values)s&
"""

    def __init__(self, file, cursor):
        BarSketchChart.__init__(self, file, cursor)

exemple d’utilisation avec une connexion odbc

import pyodbc
import pyofc
connect = pyodbc.connect('DSN=TESTPG;UID=postgres;PWD=postgres')
cursor = connect.cursor()
cursor.execute("select x,y from test3")
pyofc.PieChart('test1.txt',cursor).generate()
cursor.execute("select x,y,z from test3")
pyofc.BarSketchChart('test2.txt',cursor).generate()
cursor.execute("select x,y,z from test3")
pyofc.BarGlassChart('test3.txt',cursor).generate()
cursor.execute("select x,y,z from test3")
pyofc.AreaHollowChart('test4.txt',cursor).generate()

ou un programme plus souple

import pyodbc
import pyofc

link = {'query1':{
                    'con' : 'DSN=TESTPG;UID=postgres;PWD=postgres',
                    'query':"select x,y from test3",
                    'chart':'PieChart',
                    'file':'test1.txt'
                 },
        'query2':{
                    'con' : 'DSN=TESTPG;UID=postgres;PWD=postgres',
                    'query':"select x,y,z from test3",
                    'chart':'BarSketchChart',
                    'file':'test2.txt'
                 },
        'query3':{
                    'con' : 'DSN=TESTPG;UID=postgres;PWD=postgres',
                    'query':"select x,y,z from test3",
                    'chart':'BarGlassChart',
                    'file':'test3.txt'
                 },
        'query4':{
                    'con' : 'DSN=TESTPG;UID=postgres;PWD=postgres',
                    'query':"select x,y,z from test3",
                    'chart':'AreaHollowChart',
                    'file':'test4.txt'
                 },
        }

for i in link.keys():
    connect = pyodbc.connect(link[i]['con'])
    cursor = connect.cursor()
    cursor.execute(link[i]['query'])
    if link[i]['chart']=='PieChart':
        pyofc.PieChart(link[i]['file'],cursor).generate()
    if link[i]['chart']=='BarSketchChart':
        pyofc.BarSketchChart(link[i]['file'],cursor).generate()
    if link[i]['chart']=='BarGlassChart':
        pyofc.BarGlassChart(link[i]['file'],cursor).generate()
    if link[i]['chart']=='LineHollowChart':
        pyofc.LineHollowChart(link[i]['file'],cursor).generate()
    if link[i]['chart']=='AreaHollowChart':
        pyofc.AreaHollowChart(link[i]['file'],cursor).generate()