Browse Source

[search] Added DiscreteBarChart support

Enrico Berti 11 years ago
parent
commit
542cb5d
2 changed files with 40 additions and 20 deletions
  1. 18 8
      apps/search/src/search/templates/search2.mako
  2. 22 12
      apps/search/static/js/charts.ko.js

+ 18 - 8
apps/search/src/search/templates/search2.mako

@@ -775,14 +775,24 @@ function rangePieChartDataTransformer(data) {
 function barChartDataTransformer(data) {
   var _data = [];
   $(data.counts).each(function (cnt, item) {
-    item.widget_id = data.widget_id;  
-    _data.push({
-      series: 0,
-      x: item.from,
-      x_end: item.to,
-      y: item.value,
-      obj: item
-    });
+    item.widget_id = data.widget_id;
+    if (typeof item.from != "undefined"){
+      _data.push({
+        series: 0,
+        x: item.from,
+        x_end: item.to,
+        y: item.value,
+        obj: item
+      });
+    }
+    else {
+      _data.push({
+        series: 0,
+        x: item.value,
+        y: item.count,
+        obj: item
+      });
+    }
   });
   return _data;
 }

+ 22 - 12
apps/search/static/js/charts.ko.js

@@ -81,7 +81,6 @@ ko.bindingHandlers.timelineChart = {
 };
 
 function barChart(element, options, isTimeline) {
-
   var _data = options.transformer(options.data);
   $(element).height(300);
 
@@ -91,25 +90,36 @@ function barChart(element, options, isTimeline) {
       _chart = nv.models.multiBarWithFocusChart();
       _chart.enableSelection();
       _chart.onSelectRange(options.onSelectRange);
-    }
-    else {
-      _chart = nv.models.multiBarChart();
-    }
-    _chart.margin({bottom: 100})
-        .transitionDuration(300);
-
-    _chart.multibar
-        .hideable(true);
-    if (isTimeline) {
       _chart.xAxis
           .showMaxMin(true)
           .tickFormat(d3.time.format("%Y-%m-%d %H:%M:%S"));
+      _chart.multibar.hideable(true);
     }
     else {
-      _chart.xAxis
+      var _isDiscrete = false;
+      for (var i=0;i<_data.length;i++){
+        if (isNaN(_data[i].x * 1)){
+          _isDiscrete = true;
+          break;
+        }
+      }
+      if (_isDiscrete){
+        _chart = nv.models.discreteBarChart()
+        .x(function(d) { return d.x })
+        .y(function(d) { return d.y })
+        .staggerLabels(true);
+      }
+      else {
+        _chart = nv.models.multiBarChart();
+        _chart.xAxis
           .showMaxMin(true)
           .tickFormat(d3.format(',0f'));
+        _chart.multibar.hideable(true);
+      }
     }
+    _chart.margin({bottom: 100})
+        .transitionDuration(300);
+
     _chart.yAxis
         .tickFormat(d3.format(',0f'));