Browse Source

[search] Improve stability of selected stacked bars

Stacked property is also persisted now
Enrico Berti 10 years ago
parent
commit
5642efe3ee

+ 6 - 0
apps/search/src/search/models.py

@@ -297,6 +297,12 @@ class Collection(models.Model):
         if 'extraSeries' not in properties:
           properties['extraSeries'] = []
 
+      if facet['widgetType'] == 'heatmap-widget' and 'stacked' not in properties:
+        properties['stacked'] = True
+
+      if facet['widgetType'] == 'bar-widget' and 'stacked' not in properties:
+        properties['stacked'] = False
+
       if facet['widgetType'] == 'map-widget' and facet['type'] == 'field':
         facet['type'] = 'pivot'
         properties['facets'] = []

+ 1 - 1
apps/search/src/search/static/search/js/search.ko.js

@@ -121,7 +121,7 @@ var Query = function (vm, query) {
     } else {
       $.each(self.fqs(), function (index, fq) {
         if (fq.id() == data.widget_id) {
-          var f = $.grep(fq.filter(), function(f) { return JSON.stringify(f.value()) == JSON.stringify(data.facet.value); });
+          var f = $.grep(fq.filter(), function(f) { return ko.toJSON(f.value()) == ko.toJSON(data.facet.value); });
           if (f.length > 0) {
             fq.filter.remove(f[0]);
             if (fq.filter().length == 0) {

+ 7 - 6
apps/search/src/search/templates/search.mako

@@ -846,10 +846,10 @@ ${ dashboard.layout_skeleton() }
       <!-- /ko -->
     </div>
 
-    <div data-bind="barChart: {datum: {counts: counts(), widget_id: $parent.id(), label: label()}, stacked: false, field: field, label: label(),
+    <div data-bind="barChart: {datum: {counts: counts(), widget_id: $parent.id(), label: label()}, stacked: $root.collection.getFacetById($parent.id()).properties.stacked(), field: field, label: label(),
       fqs: $root.query.fqs,
       transformer: ($data.type == 'range-up' ? barChartRangeUpDataTransformer : barChartDataTransformer),
-      onStateChange: function(state){ },
+      onStateChange: function(state){ $root.collection.getFacetById($parent.id()).properties.stacked(state.stacked); },
       onClick: function(d) {
         if (d.obj.field != undefined) {
           if ($data.type == 'range-up') {
@@ -1054,11 +1054,11 @@ ${ dashboard.layout_skeleton() }
       <div class="clearfix"></div>
 
       <!-- ko if: properties.scope() == 'stack' -->
-        <div data-bind="barChart: {datum: {counts: $parent.counts(), widget_id: $parent.id(), label: $parent.label()}, stacked: true,
+        <div data-bind="barChart: {datum: {counts: $parent.counts(), widget_id: $parent.id(), label: $parent.label()}, stacked: $root.collection.getFacetById($parent.id()).properties.stacked(),
           isPivot: true,
           fqs: $root.query.fqs,
           transformer: pivotChartDataTransformer,
-          onStateChange: function(state){ },
+          onStateChange: function(state){ $root.collection.getFacetById($parent.id()).properties.stacked(state.stacked); },
           onClick: function(d) {
             $root.query.togglePivotFacet({facet: d.obj, widget_id: id()});
           },
@@ -1605,16 +1605,17 @@ function pivotChartDataTransformer(rawDatum) {
   $(rawDatum.counts).each(function (cnt, item) {
     item.widget_id = rawDatum.widget_id;
 
+    var _key = Array.isArray(item.value) ? item.value[1] : item.value;
     var _category = null;
     _categories.forEach(function (category) {
-      if (category.key == item.value) {
+      if (category.key == _key) {
         _category = category;
       }
     });
 
     if (_category == null) {
       _category = {
-        key: item.value,
+        key: _key,
         values: []
       };
       _categories.push(_category);

+ 1 - 0
apps/search/src/search/views.py

@@ -503,6 +503,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
   if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):
     properties['mincount'] = 1
     properties['facets'] = []
+    properties['stacked'] = True
     properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 5}
 
     if widget_type == 'map-widget':

+ 7 - 0
desktop/core/src/desktop/static/desktop/js/ko.charts.js

@@ -153,6 +153,11 @@ ko.bindingHandlers.barChart = {
     var _datum = _options.transformer(_options.datum);
     var _chart = $(element).data("chart");
     if (_chart) {
+
+      if (_chart.multibar){
+        _chart.multibar.stacked(typeof _options.stacked != "undefined" ? _options.stacked : false);
+      }
+
       var _d3 = d3.select($(element).find("svg")[0]);
       _d3.datum(_datum)
         .transition().duration(150)
@@ -162,6 +167,8 @@ ko.bindingHandlers.barChart = {
           }
         }).call(_chart);
 
+
+
       if (_chart.selectBars) {
         var _field = (typeof _options.field == "function") ? _options.field() : _options.field;
         $.each(_options.fqs(), function (cnt, item) {