Browse Source

HUE-2679 [search] Fix bindings for PieChart and BarChart for un-inited charts

Fixes for Beeswax, Impala and Spark
Enrico Berti 10 years ago
parent
commit
d0dca1b15b

+ 3 - 1
desktop/core/src/desktop/static/desktop/ext/js/d3.v3.js

@@ -4638,7 +4638,9 @@ d3 = function() {
   d3.transform = function(string) {
     var g = d3_document.createElementNS(d3.ns.prefix.svg, "g");
     return (d3.transform = function(string) {
-      g.setAttribute("transform", string);
+      if (string != null && string.indexOf("null") == -1 && string.indexOf("NaN") == -1) {
+        g.setAttribute("transform", string);
+      }
       var t = g.transform.baseVal.consolidate();
       return new d3_transform(t ? t.matrix : d3_transformIdentity);
     })(string);

+ 61 - 6
desktop/core/src/desktop/static/desktop/js/ko.charts.js

@@ -118,17 +118,29 @@ ko.bindingHandlers.pieChart = {
   },
   update: function (element, valueAccessor) {
     var _options = valueAccessor();
+    var _data = _options.transformer(_options.data);
     var _chart = $(element).data("chart");
     if (_chart) {
       var _d3 = d3.select($(element).find("svg")[0]);
-      var _data = _options.transformer(_options.data);
       _d3.datum(_data)
             .transition().duration(150)
             .each("end", _options.onComplete != null ? _options.onComplete : void(0))
             .call(_chart);
-      _chart.update();
+
+      if (_options.fqs) {
+          $.each(_options.fqs(), function (cnt, item) {
+            if (item.id() == _options.data.widget_id && item.field() == _options.field()) {
+              _chart.selectSlices($.map(item.filter(), function (it) {
+                return it.value();
+              }));
+            }
+          });
+        }
+      chartsNormalState();
+    }
+    else if (_data.length > 0) {
+      ko.bindingHandlers.pieChart.init(element, valueAccessor);
     }
-    chartsNormalState();
   }
 };
 
@@ -138,10 +150,10 @@ ko.bindingHandlers.barChart = {
   },
   update: function (element, valueAccessor) {
     var _options = valueAccessor();
+    var _datum = _options.transformer(_options.datum);
     var _chart = $(element).data("chart");
     if (_chart) {
       var _d3 = d3.select($(element).find("svg")[0]);
-      var _datum = _options.transformer(_options.datum);
       _d3.datum(_datum)
         .transition().duration(150)
         .each("end", function () {
@@ -149,8 +161,32 @@ ko.bindingHandlers.barChart = {
             _options.onComplete();
           }
         }).call(_chart);
+      if (_chart.selectBars) {
+        var _field = (typeof _options.field == "function") ? _options.field() : _options.field;
+        $.each(_options.fqs(), function (cnt, item) {
+          if (item.id() == _options.datum.widget_id) {
+
+            if (item.field() == _field) {
+              _chart.selectBars($.map(item.filter(), function (it) {
+                return it.value();
+              }));
+            }
+            if (item.field().indexOf(":") > -1) {
+              _chart.selectBars({
+                field: item.field(),
+                selected: $.map(item.filter(), function (it) {
+                  return it.value();
+                })
+              });
+            }
+          }
+        });
+      }
+      chartsNormalState();
+    }
+    else if (_datum.length > 0) {
+      ko.bindingHandlers.barChart.init(element, valueAccessor);
     }
-    chartsNormalState();
   }
 };
 
@@ -182,8 +218,27 @@ ko.bindingHandlers.timelineChart = {
 };
 
 ko.bindingHandlers.lineChart = {
-  update: function (element, valueAccessor) {
+  init: function (element, valueAccessor) {
     lineChartBuilder(element, valueAccessor(), false);
+  },
+  update: function (element, valueAccessor) {
+    var _options = valueAccessor();
+    var _datum = _options.transformer(_options.datum);
+    var _chart = $(element).data("chart");
+    if (_chart) {
+      var _d3 = d3.select($(element).find("svg")[0]);
+      _d3.datum(_datum)
+        .transition().duration(150)
+        .each("end", function () {
+          if (_options.onComplete != null) {
+            _options.onComplete();
+          }
+        }).call(_chart);
+      chartsNormalState();
+    }
+    else if (_datum.length > 0) {
+      ko.bindingHandlers.lineChart.init(element, valueAccessor);
+    }
   }
 };