22import json
33
44
5- def as_json (fn ):
6- "Decorator that converts output of function to JSON string"
7- import ipdb ; ipdb .set_trace ()
8- @wraps (fn )
9- def _json (fn ):
10- return json .dumps (fn ())
11- return _json
12-
13-
145class DuplicateLabelException (Exception ):
156 """Exception raised when an attempt is made to
167 label a new series with a label already in use"""
178
189
19-
2010class Flot (object ):
11+ """
12+ Represents a ``flot`` graph
13+
14+ This is a Python representation of a flot graph with the
15+ goal preserving the flot attribute names and organization
16+ of the options. A Flot instance will allow you to
17+ use your Python data structures as is and will handle
18+ the details of converting to valid JSON with items
19+ formatted properly for ``flot``. (Handy for time series
20+ for example)
21+ """
2122
2223 def __init__ (self ):
2324 self ._series = []
2425 self ._options = {}
2526
2627 @property
27- # @as_json
28- def series_list (self ):
28+ def series_json (self ):
29+ """
30+ Returns a string with each data series
31+ associated with this graph formatted as JSON,
32+ suitable for passing to the ``$.plot`` method.
33+ """
2934 return json .dumps (self ._series )
3035
31- # @property
32- # @as_json
33- def options (self ):
34- pass
36+ @property
37+ def options_json (self ):
38+ """
39+ Returns a JSON string representing the global options
40+ for this graph in a format suitable for passing to
41+ the ``$.plot`` method as the options parameter.
42+ """
43+ return json .dumps (self ._options )
3544
3645 #add_bars
3746 #add_line
@@ -40,22 +49,22 @@ def __getattr__(self, value):
4049 if value .startswith ('add_' ):
4150 return partial (self .add_series_type , value [4 :])
4251
43- def add_series_type (self , type , series , label = None , ** kwargs ):
52+ def add_series_type (self , line_type , series , label = None , ** kwargs ):
4453 method = getattr (self , 'add_series' )
45- return method (series , label , ** {type : {'show' : True }})
54+ return method (series , label , ** {line_type : {'show' : True }})
4655
4756 def add_series (self , series , label = None , ** kwargs ):
4857 """
4958 A series is a list of pairs (2-tuples)
5059 """
51- s = {'data' : series }
60+ new_series = {'data' : series }
5261 if label and label in [x .get ('label' , None ) for x in self ._series ]:
5362 raise DuplicateLabelException
5463 elif label :
55- s .update (label = label )
56- for lt in ('bars' , 'lines' , 'points' ):
57- if lt in kwargs :
58- s .update ({lt : kwargs [lt ]})
59- self ._series .append (s )
64+ new_series .update (label = label )
65+ for line_type in ('bars' , 'lines' , 'points' ):
66+ if line_type in kwargs :
67+ new_series .update ({line_type : kwargs [line_type ]})
68+ self ._series .append (new_series )
6069
6170
0 commit comments