Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callable fields, Morris preUnits, postUnits & ykeys, DjangoJSONEncoder #19

Merged
merged 7 commits into from Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions graphos/encoders.py
@@ -0,0 +1,5 @@
from django.core.serializers.json import DjangoJSONEncoder


class GraphosEncoder(DjangoJSONEncoder):
pass
7 changes: 5 additions & 2 deletions graphos/renderers/base.py
Expand Up @@ -3,19 +3,22 @@
from django.template.loader import render_to_string
from ..exceptions import GraphosException
from ..utils import DEFAULT_HEIGHT, DEFAULT_WIDTH, get_random_string
from ..encoders import GraphosEncoder


class BaseChart(object):

def __init__(self, data_source, html_id=None,
width=None, height=None,
options=None, *args, **kwargs):
options=None, encoder=GraphosEncoder,
*args, **kwargs):
self.data_source = data_source
self.html_id = html_id or get_random_string()
self.height = height or DEFAULT_HEIGHT
self.width = width or DEFAULT_WIDTH
self.options = options or {}
self.header = data_source.get_header()
self.encoder = encoder
self.context_data = kwargs

def get_data(self):
Expand All @@ -31,7 +34,7 @@ def get_options(self):
return options

def get_options_json(self):
return json.dumps(self.get_options())
return json.dumps(self.get_options(), cls=self.encoder)

def get_template(self):
raise GraphosException("Not Implemented")
Expand Down
2 changes: 1 addition & 1 deletion graphos/renderers/flot.py
Expand Up @@ -28,7 +28,7 @@ def get_series_objects(self):
return series_objects

def get_series_objects_json(self):
return json.dumps(self.get_series_objects())
return json.dumps(self.get_series_objects(), cls=self.encoder)

def get_options(self):
options = get_default_options()
Expand Down
4 changes: 2 additions & 2 deletions graphos/renderers/highcharts.py
Expand Up @@ -15,10 +15,10 @@ def get_series(self):
serieses = []
for i, name in enumerate(series_names):
serieses.append({"name": name, "data": column(data, i+1)[1:]})
return json.dumps(serieses)
return json.dumps(serieses, cls=self.encoder)

def get_categories(self):
return json.dumps(column(self.get_data(), 0)[1:])
return json.dumps(column(self.get_data(), 0)[1:], cls=self.encoder)

def get_x_axis_title(self):
return self.get_data()[0][0]
Expand Down
11 changes: 7 additions & 4 deletions graphos/renderers/morris.py
Expand Up @@ -11,13 +11,16 @@ def get_data_json(self):
for row in data_only:
rows.append(dict(zip(header, row)))

return json.dumps(rows)
return json.dumps(rows, cls=self.encoder)

def get_category_key(self):
return self.data_source.get_header()[0]

def get_y_keys(self):
return json.dumps(self.data_source.get_header()[1:])
try:
return json.dumps(self.options['ykeys'], cls=self.encoder)
except KeyError:
return json.dumps(self.data_source.get_header()[1:], cls=self.encoder)


def get_template(self):
Expand All @@ -37,10 +40,10 @@ def chart_type(self):
class DonutChart(BaseMorrisChart):
def get_data_json(self):
data_only = self.get_data()[1:]
return json.dumps([{"label": el[0], "value": el[1]} for el in data_only])
return json.dumps([{"label": el[0], "value": el[1]} for el in data_only], cls=self.encoder)

def chart_type(self):
return "Donut"

def get_template(self):
return "graphos/morris/donut_chart.html"
return "graphos/morris/donut_chart.html"
4 changes: 2 additions & 2 deletions graphos/renderers/yui.py
Expand Up @@ -12,7 +12,7 @@ def get_data_json(self):
for row in data_only:
rows.append(dict(zip(header, row)))

return json.dumps(rows)
return json.dumps(rows, cls=self.encoder)

def get_category_key(self):
return self.data_source.get_header()[0]
Expand All @@ -34,4 +34,4 @@ def get_template(self):

class PieChart(BaseYuiChart):
def get_template(self):
return "graphos/yui/pie_chart.html"
return "graphos/yui/pie_chart.html"
3 changes: 2 additions & 1 deletion graphos/sources/model.py
Expand Up @@ -5,7 +5,8 @@
def get_field_values(row, fields):
data = []
for field in fields:
data.append(getattr(row, field))
value = getattr(row, field)
data.append(value if not callable(value) else value())
return data


Expand Down
7 changes: 5 additions & 2 deletions graphos/templates/graphos/morris/chart.html
Expand Up @@ -12,6 +12,9 @@
ykeys: {{ chart.get_y_keys|safe }},
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: {{ chart.get_y_keys|safe }}
labels: {{ chart.get_y_keys|safe }},
// post units
postUnits: '{{ chart.get_options.postUnits|safe }}',
preUnits: '{{ chart.get_options.preUnits|safe }}',
});
</script>
</script>