summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/tools/wtstats/template/views/viz.js
blob: 8ee0441a742a40f10f528fb49c8cbdbfa89cca17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var AmpersandView = require('ampersand-view'),
  _ = require('lodash'),
  $ = require('jquery'),
  debug = require('debug')('view:viz');

module.exports = AmpersandView.extend({

  _values: {},
  _autoWidth: false,
  _autoHeight: false,

  props: {
    data: 'any',
    className: 'any',
    vizFn: 'any',
    debounceRender: {
      type: 'boolean', 
      default: true
    },
    renderMode: {
      type: 'string',
      values: ['canvas', 'svg', 'html'],
      default: 'svg'
    },
    width: {
      type: 'any',
      default: 'auto'
    },
    height: {
      type: 'any',
      default: 400
    },
  },

  bindings: {
    'width': [
      {
        type: 'attribute',
        name: 'width',
        hook: 'viz-container'
      }
    ],
    'height': {
      type: 'attribute',
      name: 'height',
      hook: 'viz-container'
    },
    'className': {
      type: 'attribute',
      name: 'class',
      hook: 'viz-container'
    }
  },

  initialize: function(opts) {
    if (this.width === 'auto' || this.width === undefined) {
      this._autoWidth = true;
      this.width = 0;
    }
    if (this.height === 'auto' || this.height === undefined) {
      this._autoHeight = true;
      this.height = 0;
    }

    if (this._autoWidth || this._autoHeight) {
      if (this.debounceRender) {
        window.addEventListener('resize', _.debounce(this.redraw.bind(this), 100));
      } else {
        window.addEventListener('resize',this.redraw.bind(this));        
      }
    }

    // pick canvas or svg template
    switch (this.renderMode) {
      case 'canvas': 
        this.template = '<canvas data-hook="viz-container" id="canvas"></canvas>'; 
        break;
      case 'svg':
        this.template = '<svg data-hook="viz-container"></svg>';
        break;
      case 'html':
        this.template = '<div data-hook="viz-container"></div>';
        break;
    }
  },

  _measure: function() {
    if (this.el) {
      if (this._autoWidth) {
        this.width = $(this.el).parent().width();
      }
      if (this._autoHeight) {
        this.height = $(this.el).parent().height();
      }
    }
  },

  _chooseDataSource: function() {
    if (this.model !== undefined) {
      this.data = this.model.toJSON();
    } else if (this.collection !== undefined) {
      this.data = this.collection.toJSON();
    }
  },

  remove: function() {
    window.removeEventListener('resize', this._onResize);
    return this;
  },

  transform: function(data) {
    return data;
  },

  render: function() {
    this._chooseDataSource();
    this.data = this.transform(this.data);
    this.renderWithTemplate(this);

    // measure only if width or height is missing
    this._measure();

    // call viz function
    if (this.vizFn) {
      this.vizFn = this.vizFn({
        width: this.width,
        height: this.height,
        data: this.data,
        el: this.el,
      });
    }
    return this;
  },

  redraw: function() {
    this._chooseDataSource();
    this.data = this.transform(this.data);

    this._measure();

    if (this.vizFn) {
      this.vizFn({
        width: this.width,
        height: this.height,
        data: this.data,
        el: this.el,
      });
    }
  }
});

/**
 * Shortcut so you don't have to know anything about ampersand
 * and can just cut right to the d3.
 */
module.exports.create = function(className, vizFn) {
  return module.exports.extend({
    className: className,
    vizFn: vizFn
  });
};