diff --git a/ckanext/basiccharts/plugin.py b/ckanext/basiccharts/plugin.py index 1560a7b..021ebfd 100644 --- a/ckanext/basiccharts/plugin.py +++ b/ckanext/basiccharts/plugin.py @@ -29,7 +29,8 @@ def update_config(self, config): def info(self): schema = { 'y_axis': [not_empty], - 'show_legends': [ignore_missing] + 'show_legends': [ignore_missing], + 'count_rows': [ignore_missing] } if self.GROUP_BY_IS_REQUIRED: @@ -49,6 +50,7 @@ def setup_template_variables(self, context, data_dict): resource = data_dict['resource'] resource_view = data_dict['resource_view'] resource_view['show_legends'] = bool(resource_view.get('show_legends')) + resource_view['count_rows'] = bool(resource_view.get('count_rows')) fields = _get_fields_without_id(resource) @@ -194,7 +196,7 @@ def _fields_as_string(self, resource_view): def _view_data(resource_view): data = { 'resource_id': resource_view['resource_id'], - 'limit': int(resource_view.get('limit', 100)) + 'limit': 50000 } filters = resource_view.get('filters', {}) diff --git a/ckanext/basiccharts/theme/public/basiccharts.js b/ckanext/basiccharts/theme/public/basiccharts.js index 89a0279..1773972 100644 --- a/ckanext/basiccharts/theme/public/basiccharts.js +++ b/ckanext/basiccharts/theme/public/basiccharts.js @@ -38,9 +38,12 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; hits.reverse(); } } - data = prepareDataForPlot(fields, hits, config.xaxis, config.yaxis, params); - + if (fields[params.x_axis]){ + for (let key = 0; key < data.length; key++) { + config['xaxis']['ticks'].push([key, data[key]['label']]); + } + } $.plot(elementId, data, config); }); } @@ -49,7 +52,7 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; var query = { filters: [], sort: [], - size: 1000 + size: 50000 }; if (params.filters) { @@ -115,6 +118,7 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; text: { mode: "categories", tickColor: "rgba(0, 0, 0, 0)", + ticks: [], tickFormatter: function (value, axis) { return value; } @@ -124,7 +128,7 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; }; config = { - yaxis: axisConfigByType[yAxisType], + yaxis: 'numeric', colors: ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf', '#999999'] @@ -137,7 +141,7 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; show: true, label: { show: true, - threshold: 0.05 + threshold: 0.5 } } }, @@ -149,7 +153,7 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; }, tooltip: true, tooltipOpts: { - content: "%p.0%, %s" + content: "%s: %p.0% (%y.0)" } }); } else { @@ -163,7 +167,7 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; }, tooltip: true, tooltipOpts: { - content: "%s | "+params.x_axis+": %x | "+params.y_axis+": %y", + content: "%s: %y.0", xDateFormat: "%d/%m/%Y", yDateFormat: "%d/%m/%Y" } @@ -173,7 +177,6 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; if (xAxisType) { config.xaxis = axisConfigByType[xAxisType]; } - return config; } @@ -197,11 +200,34 @@ this.ckan.views.basiccharts = this.ckan.views.basiccharts || {}; if (x === null) { return; } - result[group_by] = result[group_by] || []; - result[group_by].push([xParsed, yParsed]); + if (params['count_rows']){ + if (result[group_by]){ + result[group_by][0][1] = result[group_by][0][1] + 1; + }else{ + result[group_by] = result[group_by] || []; + result[group_by].push([xParsed, 1]); + } + }else if (typeof yParsed == 'number') { + if (typeof yParsed == 'number'){ + result[group_by] = result[group_by] || []; + if (result[group_by].length > 0){ + result[group_by][0][1] = result[group_by][0][1] + yParsed; + }else{ + result[group_by].push([xParsed, yParsed]); + } + } + }else{ + result[group_by] = result[group_by] || []; + result[group_by].push([xParsed, yParsed]); + } } else { - result[group_by] = result[group_by] || 0; - result[group_by] = result[group_by] + yParsed; + if (params['count_rows'] || typeof yParsed == 'string'){ + result[group_by] = result[group_by] || 0; + result[group_by] = result[group_by] + 1; + }else{ + result[group_by] = result[group_by] || 0; + result[group_by] = result[group_by] + yParsed; + } } }); return result; diff --git a/ckanext/basiccharts/theme/templates/barchart_form.html b/ckanext/basiccharts/theme/templates/barchart_form.html index 5a5463a..02edbc1 100644 --- a/ckanext/basiccharts/theme/templates/barchart_form.html +++ b/ckanext/basiccharts/theme/templates/barchart_form.html @@ -6,4 +6,9 @@ form.checkbox('horizontal', label=_('Horizontal'), value=True, checked=resource_view['horizontal']) }} + +{{ + form.checkbox('count_rows', label=_("Count Rows"), + value=True, checked=resource_view['count_rows']) +}} {% endblock %} diff --git a/ckanext/basiccharts/theme/templates/linechart_form.html b/ckanext/basiccharts/theme/templates/linechart_form.html index db0850c..8f0537e 100644 --- a/ckanext/basiccharts/theme/templates/linechart_form.html +++ b/ckanext/basiccharts/theme/templates/linechart_form.html @@ -11,4 +11,9 @@ form.checkbox('show_legends', label=_("Show groups' legend"), value=True, checked=resource_view['show_legends']) }} + +{{ + form.checkbox('count_rows', label=_("Count Rows"), + value=True, checked=resource_view['count_rows']) +}} {% endblock %} diff --git a/ckanext/basiccharts/theme/templates/piechart_form.html b/ckanext/basiccharts/theme/templates/piechart_form.html index be39b8c..557ec30 100644 --- a/ckanext/basiccharts/theme/templates/piechart_form.html +++ b/ckanext/basiccharts/theme/templates/piechart_form.html @@ -7,4 +7,10 @@ form.checkbox('show_legends', label=_("Show groups' legend"), value=True, checked=resource_view['show_legends']) }} + +{{ + form.checkbox('count_rows', label=_("Count Rows"), + value=True, checked=resource_view['count_rows']) +}} + {% endblock %}