Skip to content

Commit

Permalink
[#1251] Update Recline Graph. Enables graph
Browse files Browse the repository at this point in the history
type and fields to be selected/saved.
  • Loading branch information
John authored and John committed Nov 13, 2013
1 parent e66b413 commit 94238a9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
47 changes: 42 additions & 5 deletions ckanext/reclinepreview/plugin.py
Expand Up @@ -3,10 +3,27 @@
from ckan.common import json
import ckan.plugins as p
import ckan.plugins.toolkit as toolkit
import ckan.lib.navl.dictization_functions as df

log = getLogger(__name__)
ignore_empty = p.toolkit.get_validator('ignore_empty')
natural_number_validator = p.toolkit.get_validator('natural_number_validator')
Invalid = df.Invalid


def in_list(list_possible_values):
'''
Validator that checks that the input value is one of the given
possible values.
:param list_possible_values: function that returns list of possible values
for validated field
:type possible_values: function
'''
def validate(key, data, errors, context):
if not data[key] in list_possible_values():
raise Invalid('"{0}" is not a valid parameter'.format(data[key]))
return validate


class ReclineView(p.SingletonPlugin):
Expand Down Expand Up @@ -61,27 +78,47 @@ class ReclineGraph(ReclineView):
This extension views resources using a Recline graph.
'''

graph_field_types = ['numeric', 'int4', 'timestamp']

graph_types = [{'value': 'lines-and-points',
'text': 'Lines and points'},
{'value': 'lines', 'text': 'Lines'},
{'value': 'points', 'text': 'Points'},
{'value': 'bars', 'text': 'Bars'},
{'value': 'columns', 'text': 'Columns'}]

datastore_fields = []

def info(self):
# TODO: add validators
# in_list validator here is passed functions because this
# method does not know what the possible values of the
# datastore fields are (requires datastore search)
self.schema.update({
'graph_type': [ignore_empty],
'group_column': [ignore_empty],
'series_a': [ignore_empty]
'graph_type': [ignore_empty, in_list(self.list_graph_types)],
'group': [ignore_empty, in_list(self.list_datastore_fields)],
'series': [ignore_empty, in_list(self.list_datastore_fields)]
})
return {'name': 'recline_graph',
'title': 'Graph',
'schema': self.schema}

def list_graph_types(self):
return [t['value'] for t in self.graph_types]

def list_datastore_fields(self):
return [t['value'] for t in self.datastore_fields]

def _datastore_fields(self, resource):
data = {'resource_id': resource['id'], 'limit': 0}
fields = toolkit.get_action('datastore_search')({}, data)['fields']
return [{'value': f['id'], 'text': f['id']} for f in fields
if f['type'] in self.graph_field_types]

def setup_template_variables(self, context, data_dict):
self.datastore_fields = self._datastore_fields(data_dict['resource'])
vars = ReclineView.setup_template_variables(self, context, data_dict)
vars.update({'graph_types': self.graph_types})
vars.update({'graph_types': self.graph_types,
'graph_fields': self.datastore_fields})
return vars

def form_template(self, context, data_dict):
Expand Down
12 changes: 9 additions & 3 deletions ckanext/reclinepreview/theme/public/preview_recline.js
Expand Up @@ -70,7 +70,9 @@ this.ckan.module('reclinepreview', function (jQuery, _) {
if(reclineView.view_type === "recline_graph") {
view = new recline.View.Graph({
model: dataset,
el: this.el
state: {"graphType": reclineView.graph_type,
"group": reclineView.group,
"series": [reclineView.series]}
});
} else if(reclineView.view_type == "recline_map") {
view = new recline.View.Map({
Expand All @@ -79,13 +81,17 @@ this.ckan.module('reclinepreview', function (jQuery, _) {
});
} else {
view = new recline.View.SlickGrid({
model: dataset,
el: this.el
model: dataset
});
}

this.el.replaceWith(view.el);
view.visible = true;
view.render();

if(reclineView.view_type === "recline_graph") {
view.redraw();
}
},

normalizeUrl: function (url) {
Expand Down
Expand Up @@ -4,5 +4,5 @@
{{ form.input('limit', id='field-limit', label=_('Number of rows'), placeholder=_('eg: 100'), value=data.limit, error=errors.limit, classes=['control-medium']) }}

{{ form.select('graph_type', label=_('Graph type'), options=graph_types, selected=data.graph_type, error=errors.graph_type) }}
{{ form.select('group_column', label=_('Group column (Axis 1)'), options=columns, selected=data.group_column, error=errors.graph_column) }}
{{ form.select('series_a', label=_('Series A (Axis 2)'), options=columns, selected=data.series_a, error=errors.series_a) }}
{{ form.select('group', label=_('Group (Axis 1)'), options=graph_fields, selected=data.group, error=errors.group) }}
{{ form.select('series', label=_('Series (Axis 2)'), options=graph_fields, selected=data.series, error=errors.series) }}

0 comments on commit 94238a9

Please sign in to comment.