Feb 10 2009

meio-pyofc – A Python binding for Open Flash Chart 2

vbmendes @ 22:26

meio-pyofc is a python binding for Open Flash Chart 2. It provides a python API to generate the JSON format required by the Open Flash Chart.

Source

The source is in a github repository named meio-pyofc.

Ticket and bug report system

The ticket and bug report system for this libary is provided by Lighthouse App:

Enter the ticket and bug report system

Features

  • Generate the chart like declaring dictionaries;
  • API with the same names as the generated JSON;
  • Eliminates empty keys from the generated JSON even if it is declared in the python object;
  • Possibility to declare a tuple with default colors to be used in the series. Each series will have one of the tuple’s colors;
  • Possibilty to turn all positive values to green, all negative values to red and all zero values to blue.

Usage

Here is the simplest example of meio-pyofc usage. It generates a string with the JSON for a line chart.

plot1 = Line({'values': range(-10,10,2)})
chart = Chart()
chart['elements'].append(plot1)
print chart.encode()

API Reference

This code is based in the Open Flash Chart 2 JSON format. If you find yourself confused about the usage of any of the specified keys described below, please check it in Open Flash Chart 2 docs.

All classes in the meio-pyofc is a subclass of a modified class named OfcDict.

OfcDict – meiopyofc.util.OfcDict

It has an attribute named types that must be a dictionary where the keys are the keys that the OfcDict instance can have, and the values are it’s respective type.

For example:

from meiopyofc.util import OfcDict</p>

<p>class MyOfcDict(OfcDict):
    types = {
        'name': str,
        'age': int
    }

The instances of above class can only have two keys: name and age. The name must an instance of str and age must be an instance of int.

</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>my_dict_instance = MyOfcDict()
      my_dict_instance['name'] = 'teste'
      my_dict_instance['age'] = 2
      my_dict_instance
      {'age': 2, 'name': 'teste'}</p>
      
      <p>my_dict_instance['gender'] = 'male'
      Traceback (most recent call last):
      AttributeError: gender is not an allowed attribute of MyOfcDict
      

In code above, gender isn’t an allowed key for MyOfcDict.

If a value different from the specified type is passed to it, then it will try to convert it to the specified type.

</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>my_dict_instance['name'] = []
      my_dict_instance['age'] = '10'
      my_dict_instance
      {'age': 10, 'name': '[]'}
      

You may also pass a dictionary with the values to the specified keys as an argument to instantiate a new OfcDict instance. But if you pass to it a dictionary with a key that is not allowed by the OfcDict, it will raise an Exception.

</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>my_dict_intance = MyOfcDict({'age': 10, 'name': 'test'})
      my_dict_instance
      {'age': 10, 'name': '[]'}</p>
      
      <p>my_dict_intance = MyOfcDict({'age': 10, 'name': 'test','gender': 'male'})
      Traceback (most recent call last):
      AttributeError: gender is not an allowed attribute of MyOfcDict
      

OfcDict also has a method to del it’s dictionary keys that the value evaluates do False.

</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>my_dict_instance = MyOfcDict({'age': 10, 'name': None})
      my_dict_instance.clean_nulls()
      my_dict_instance
      {'age': 10}
      

Chart – meiopyofc.Chart

Chart is a meiopyofc.util.OfcDict subclass to handle chart data. It has some specified keys and some special functions. It may also receive an auto_ranges parameter. It defaults to True, and determine if the labels of y axis will be calculated based on the chart’s series or not.

Specified keys
Special functions
  • calculate_range_y: Update the labels of y axis based on the chart’s series values. The min label will be the min_value-1 and the max label will be the max_value+1. If the min_value is bigger then zero, it will be zero. The steps of the labels will be the int value of (max_value-min_value)/5.
  • encode: Returns the JSON string that represents this chart.

Title – meiopyofc.elements.Title

Title is a meiopyofc.util.OfcDict subclass and stores data related to the chart’s title. It has some specified keys.

Specified keys
  • text: It’s a string with the title text. If no text is provided the style is ignored and the chart will have no title.
  • style: It’s a limited CSS like string.

XLegend – meiopyofc.elements.XLegend

XLegend works just like meiopyofc.elements.Title. The only difference is that it is used to display a legend to describe x axis labels.

YLegend – meiopyofc.elements.YLegend

YLegend works just like meiopyofc.elements.Title. The only difference is that it is used to display a legend to describe y axis labels.

Colour – meiopyofc.elements.Colour

Just a subclass of str. Created for future improvements like validation and conversions between color systems.

meiopyofc.elements.Colour instances. Created for future improvements.

Label – meiopyofc.elements.Label

Label is a meiopyofc.util.OfcDict subclass to handle labels data. Labels can be either a label of x axis or a label of y axis.It has some specified keys.
Specified keys
  • colour: A meiopyofc.elements.Colour instance. It is used to define the label’s text color.
  • text: It’s a string with the label text.
  • size: An integer used to define the text size.
  • rotate: It’s a string that represents the rotation of the label. It may have three different values: vertical, horizontal and diagonal.

XLabel – meiopyofc.elements.xlabel

XLabel is a subclass of meiopyofc.elements.Label.
Specified keys
It has all meiopyofc.elements.Label‘s specified keys with some extras.
  • visible: Boolean
  • align: String
  • x: An integer specifing the x position of the label.

YLabel – meiopyofc.elements.xlabel

YLabel is a subclass of Label. It has all Label’s specified types with some extras.
Specified keys
  • y: An integer specifing the y position of the label.

meiopyofc.elements.Label instances. Created for future improvements.

Labels – meiopyofc.elements.Labels

Labels is a subclass of Label. It is used to store a list of labels and generic properties to be applied to all labels inside it.

Specified keys

It has all meiopyofc.elements.Label‘s specified keys with some extras.

  • steps: An integer specifing how many labels will be invisible between two visible ones.
  • labels: A meiopyofc.elements.LabelList instance. It stores all it’s labels.

XLabels – meiopyofc.elements.XLabels

XLabels is a subclass of Labels. It is used to store a list of labels for x axis and generic properties to be applied to all labels inside it.

Specified keys

It has all meiopyofc.elements.Labels‘s specified keys with some extras.

  • align: String.
  • visible: Boolean.
  • visible-steps: Integer.

YLabels – meiopyofc.elements.YLabels

YLabels is a subclass of Labels. It is used to store a list of labels for y axis and generic properties to be applied to all labels inside it.

Specified keys

It has all meiopyofc.elements.Labels‘s specified keys with some extras.

  • show-labels: Boolean.

This documentation is still in development. Last edited: 12 February, 2009

One Response to “meio-pyofc – A Python binding for Open Flash Chart 2”

  1. Alex says:

    Hi, thanks for sharing this library.

    I was wondering whether you have a comparison between meio-pyofc and other similar libraries, ex: PyOFC2.

    I am someone who has no prior exposure to any of these libraries, an overview of the available tools would be very handy. In this specific case, I wonder what was the rationale behind deciding to role out your own library, rather than use an existing tool.

    Certainly, you’ve had your reasons and you are confident that your solution has some advantages – it would be much easier for the rest of the world if your arguments were public :-)

Leave a Reply