aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/overview/trend-view.js
blob: 24c31e503b1ab362512a8d4d1a91df39783b318c (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
define(function () {

  function trans (left, top) {
    return 'translate(' + left + ', ' + top + ')';
  }

  var $ = jQuery;


  return Backbone.View.extend({

    initialize: function (options) {
      this.data = options.data;
      this.type = options.type || 'INT';
      this.width = 0;
      this.height = 0;
    },

    attachEvents: function () {
      this.detachEvents();
      var event = 'resize.trend-' + this.cid,
          update = _.throttle(_.bind(this.update, this), 50);
      $(window).on(event, update);
    },

    detachEvents: function () {
      var event = 'resize.trend-' + this.cid;
      $(window).off(event);
      return this;
    },

    render: function () {
      var that = this,
          data = this.data;
      this.container = d3.select(this.el);
      this.svg = this.container.append('svg')
          .classed('sonar-d3', true);
      this.plot = this.svg.append('g')
          .classed('plot', true);

      this.xScale = d3.time.scale()
          .domain(d3.extent(data, function (d) {
            return moment(d.val).toDate();
          }))
          .nice();
      this.yScale = d3.scale.linear()
          .domain(d3.extent(data, function (d) {
            return d.count;
          }))
          .nice();

      this.line = d3.svg.line()
          .x(function (d) {
            return that.xScale(moment(d.val).toDate());
          })
          .y(function (d) {
            return that.yScale(d.count);
          })
          .interpolate('linear');

      this.xScaleTicks = this.xScale.ticks(5);
      this.yScaleTicks = this.yScale.ticks(3);

      this.xTicks = this.xScaleTicks.map(function (tick) {
        return that.plot.append('text')
            .datum(tick)
            .text(that.xScale.tickFormat()(tick))
            .attr('dy', '0')
            .style('text-anchor', 'middle')
            .style('font-size', '10px')
            .style('font-weight', '300')
            .style('fill', '#aaa');
      });
      this.yTicks = this.yScaleTicks.map(function (tick) {
        return that.plot.append('text')
            .datum(tick)
            .text(window.formatMeasure(tick, that.type))
            .attr('dy', '5px')
            .style('text-anchor', 'end')
            .style('font-size', '10px')
            .style('font-weight', '300')
            .style('fill', '#aaa');
      });

      this.xTickLines = this.xScaleTicks.map(function (tick) {
        return that.plot.append('line')
            .datum(tick)
            .style('stroke', '#eee')
            .style('shape-rendering', 'crispedges');
      });
      this.yTickLines = this.yScaleTicks.map(function (tick) {
        return that.plot.append('line')
            .datum(tick)
            .style('stroke', '#eee')
            .style('shape-rendering', 'crispedges');
      });

      this.path = this.plot.append('path')
          .datum(data)
          .classed('line', true)
          .style('stroke', 'rgb(31, 119, 180)');

      this.attachEvents();

      return this;
    },

    update: function () {
      var that = this,
          width = this.$el.closest('.overview-trend').width(),
          height = 150,
          marginLeft = 20,
          marginRight = 50,
          marginTop = 5,
          marginBottom = 25,
          availableWidth = width - marginLeft - marginRight,
          availableHeight = height - marginTop - marginBottom;

      this.svg
          .attr('width', width)
          .attr('height', height);

      this.plot.attr('transform', trans(marginLeft, marginTop));
      this.xScale.range([0, availableWidth]);
      this.yScale.range([availableHeight, 0]);

      this.path
          .attr('d', this.line);

      this.xTicks.forEach(function (tick) {
        tick
            .attr('x', that.xScale(tick.datum()))
            .attr('y', availableHeight + 20);
      });

      this.yTicks.forEach(function (tick) {
        tick
            .attr('x', availableWidth + 50)
            .attr('y', that.yScale(tick.datum()));
      });

      this.xTickLines.forEach(function (tick) {
        tick
            .attr('x1', that.xScale(tick.datum()))
            .attr('x2', that.xScale(tick.datum()))
            .attr('y1', 0)
            .attr('y2', availableHeight);
      });

      this.yTickLines.forEach(function (tick) {
        tick
            .attr('x1', 0)
            .attr('x2', availableWidth)
            .attr('y1', that.yScale(tick.datum()))
            .attr('y2', that.yScale(tick.datum()));
      });

      return this;
    }

  });

});