src/js/widgets/sparklineWidget.js
author veltr
Tue, 17 Apr 2012 20:19:46 +0200
branchnew-model
changeset 868 a525cc2214e7
parent 842 4ae2247a59f4
child 857 fa614dc66b0b
permissions -rw-r--r--
Started big refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     1
/** @class The constructor for the sparkline widget */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     2
IriSP.SparklineWidget = function(Popcorn, config, Serializer) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     3
  IriSP.Widget.call(this, Popcorn, config, Serializer);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     4
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     5
  this._oldAnnotation = null;
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
     6
  this._results = [];
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
     7
  
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
     8
  this.slices = this._config.slices || Math.floor(this.width/20);
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
     9
  if (!this.width) {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    10
      this.width = this.selector.width();
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    11
  }
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    12
  if (!this.height) {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    13
      this.height = 40;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    14
  }
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    15
  this.selector.css("height", this.height + "px");
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    16
  if (this._config.background) {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    17
      this.selector.css("background", this._config.background);
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    18
  }
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    19
};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    20
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    21
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    22
IriSP.SparklineWidget.prototype = new IriSP.Widget();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    23
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    24
IriSP.SparklineWidget.prototype.clear = function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    25
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    26
};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    27
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    28
/** draw the sparkline using jquery sparkline */
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    29
IriSP.SparklineWidget.prototype.draw = function() {
842
4ae2247a59f4 Changes for Cinecast
veltr
parents: 840
diff changeset
    30
    this.duration = this.getDuration();
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    31
    this.paper = new Raphael(this.selector[0], this.width, this.height);
835
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
    32
    var _this = this;
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    33
  
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    34
  var views = this._serializer._data.views;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    35
  var stat_view;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    36
  if (!IriSP.null_or_undefined(views)) {
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    37
    for (var i = 0; i < views.length; i++) {
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    38
      var view = views[i];
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    39
      if (view.id === "stat") {
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    40
          stat_view = view;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    41
          break;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    42
      }
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    43
    }
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    44
  }
600
4e669328ab6d fixed config bug.
hamidouk
parents: 532
diff changeset
    45
  
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    46
    var _ = IriSP.underscore;
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    47
  // If we've found the correct view, feed the directly the data from the view
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    48
  // to jquery sparkline. Otherwise, compute it ourselves.
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    49
    if (!IriSP.null_or_undefined(stat_view)) {
840
ac66e2240e1e bugfixes
veltr
parents: 835
diff changeset
    50
        //console.log("sparklinewidget : using stats embedded in the json");
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    51
        var _results = stat_view.meta.stat.split(",");      
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    52
    } else {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    53
        var _annotations = this._serializer._data.annotations,
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    54
            _sliceDuration = Math.floor( this.duration / this.slices),
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    55
            _results = _(_.range(this.slices)).map(function(_i) {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    56
                return _(_annotations).filter(function(_a){
842
4ae2247a59f4 Changes for Cinecast
veltr
parents: 840
diff changeset
    57
                    return (_a.begin <= (1 + _i) * _sliceDuration) && (_a.end >= _i * _sliceDuration)
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    58
                }).length;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    59
            });
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    60
    }
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    61
    var _max = Math.max(1, _(_results).max()),
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    62
        _h = this.height,
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    63
        _scale = (_h - this.lineWidth) / _max,
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    64
        _width = this.width / this.slices,
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    65
        _y = _(_results).map(function(_v) {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    66
            return _h - (_scale * _v);
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    67
        }),
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    68
        _d = _(_y).reduce(function(_memo, _v, _k) {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    69
               return _memo + ( _k
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    70
                   ? 'C' + (_k * _width) + ' ' + _y[_k - 1] + ' ' + (_k * _width) + ' ' + _v + ' ' + ((_k + .5) * _width) + ' ' + _v
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    71
                   : 'M0 ' + _v + 'L' + (.5*_width) + ' ' + _v )
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    72
            },'') + 'L' + this.width + ' ' + _y[_y.length - 1],
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    73
        _d2 = _d + 'L' + this.width + ' ' + this.height + 'L0 ' + this.height;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    74
    this.paper.path(_d2).attr({
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    75
        "stroke" : "none",
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    76
        "fill" : this.fillColor
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    77
    });
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    78
         
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    79
    this.paper.path(_d).attr({
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    80
        "fill" : "none",
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    81
        "stroke" : this.lineColor,
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    82
        "stroke-width" : this.lineWidth
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    83
    });
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    84
  
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    85
    this.rectangleProgress = this.paper.rect(0,0,0,this.height)
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    86
        .attr({
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    87
            "stroke" : "none",
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    88
            "fill" : "#808080",
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    89
            "opacity" : .3
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    90
        });
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    91
    this.ligneProgress = this.paper.path("M0 0L0 "+this.height).attr({"stroke":"#ff00ff", "line-width" : 2});
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    92
  // save the results in an array so that we can re-use them when a new annotation
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    93
  // is added.
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    94
  this._results = _results;
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    95
  
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    96
  this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.timeUpdateHandler));
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    97
//  this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", IriSP.wrap(this, this.handleNewAnnotation));
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    98
  
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
    99
  this.selector.click(IriSP.wrap(this, this.clickHandler));  
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   100
};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   101
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   102
/** react to a timeupdate event */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   103
IriSP.SparklineWidget.prototype.timeUpdateHandler = function() {
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   104
    var _currentTime = this._Popcorn.currentTime(),
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   105
        _x = (1000 * _currentTime / this.duration) * this.width;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   106
    this.rectangleProgress.attr({
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   107
        "width" : _x
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   108
    });
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   109
    this.ligneProgress.attr({
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   110
        "path" : "M" + _x + " 0L" + _x + " " + this.height
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   111
    });
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   112
                                  
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   113
}
529
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   114
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   115
/** handle clicks on the widget */
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   116
IriSP.SparklineWidget.prototype.clickHandler = function(event) {
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   117
  var relX = event.pageX - this.selector.offset().left;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   118
  var newTime = ((relX / this.width) * this.duration/1000).toFixed(2);
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
   119
    
532
5249bb0cd964 cosmetical change - moved css code from jquery.css to the stylesheet.
hamidouk
parents: 529
diff changeset
   120
  this._Popcorn.trigger("IriSP.SparklineWidget.clicked", newTime);
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   121
  this._Popcorn.currentTime(newTime);
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   122
};
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   123
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   124
/** react when a new annotation is added */
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   125
IriSP.SparklineWidget.prototype.handleNewAnnotation = function(annotation) {
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   126
//  var num_columns = this._results.length;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   127
//  var duration = this._serializer.getDuration();
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   128
//  var time_step = Math.round(duration / num_columns); /* the time interval between two columns */
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   129
//  var begin = +annotation.begin;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   130
//  var end = +annotation.end;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   131
//  
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   132
//  /* increment all the values between the beginning and the end of the annotation */
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   133
//  var index_begin = Math.floor(begin / time_step);
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   134
//  var index_end = Math.floor(end / time_step);
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   135
//  
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   136
//  for (var i = index_begin; i < Math.min(index_end, this._results.length); i++) {
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   137
//    this._results[i]++;
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   138
//  }
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   139
//  
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   140
//  this.selector.find(".Ldt-sparkLine").sparkline(this._results, {lineColor: "#7492b4", fillColor: "#aeaeb8",
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   141
//                                                           spotColor: "#b70056",
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 820
diff changeset
   142
//                                                           width: this.width, height: this.height});
529
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   143
};