stat_graph_contributors_graph.js.coffee 5.1 KB
Newer Older
1 2
#= require d3

C
Ciro Santilli 已提交
3
class @ContributorsGraph
4 5
  MARGIN:
    top: 20
D
Dmitriy Zaporozhets 已提交
6 7
    right: 20
    bottom: 30
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
    left: 50
  x_domain: null
  y_domain: null
  dates: []
  @set_x_domain: (data) =>
    @prototype.x_domain = data
  @set_y_domain: (data) =>
    @prototype.y_domain = [0, d3.max(data, (d) ->
      d.commits = d.commits ? d.additions ? d.deletions
    )]
  @init_x_domain: (data) =>
    @prototype.x_domain = d3.extent(data, (d) ->
     d.date
    )
  @init_y_domain: (data) =>
    @prototype.y_domain = [0, d3.max(data, (d) ->
      d.commits = d.commits ? d.additions ? d.deletions
    )]
  @init_domain: (data) =>
    @init_x_domain(data)
    @init_y_domain(data)
  @set_dates: (data) =>
    @prototype.dates = data
  set_x_domain: ->
    @x.domain(@x_domain)
  set_y_domain: ->
    @y.domain(@y_domain)
  set_domain: ->
    @set_x_domain()
    @set_y_domain()
  create_scale: (width, height) ->
    @x = d3.time.scale().range([0, width]).clamp(true)
    @y = d3.scale.linear().range([height, 0]).nice()
  draw_x_axis: ->
    @svg.append("g").attr("class", "x axis").attr("transform", "translate(0, #{@height})")
D
Dmitriy Zaporozhets 已提交
43
    .call(@x_axis)
44 45 46 47 48
  draw_y_axis: ->
    @svg.append("g").attr("class", "y axis").call(@y_axis)
  set_data: (data) ->
    @data = data

C
Ciro Santilli 已提交
49
class @ContributorsMasterGraph extends ContributorsGraph
50
  constructor: (@data) ->
51
    @width = $('.content').width() - 70
D
Dmitriy Zaporozhets 已提交
52
    @height = 200
53 54 55 56 57 58 59 60 61
    @x = null
    @y = null
    @x_axis = null
    @y_axis = null
    @area = null
    @svg = null
    @brush = null
    @x_max_domain = null
  process_dates: (data) ->
D
Dmitriy Zaporozhets 已提交
62
    dates = @get_dates(data)
63 64 65 66 67 68 69 70 71 72 73 74 75
    @parse_dates(data)
    ContributorsGraph.set_dates(dates)
  get_dates: (data) ->
    _.pluck(data, 'date')
  parse_dates: (data) ->
    parseDate = d3.time.format("%Y-%m-%d").parse
    data.forEach((d) ->
      d.date = parseDate(d.date)
    )
  create_scale: ->
    super @width, @height
  create_axes: ->
    @x_axis = d3.svg.axis().scale(@x).orient("bottom")
76
    @y_axis = d3.svg.axis().scale(@y).orient("left").ticks(5)
77 78 79 80 81 82 83 84 85 86 87
  create_svg: ->
    @svg = d3.select("#contributors-master").append("svg")
    .attr("width", @width + @MARGIN.left + @MARGIN.right)
    .attr("height", @height + @MARGIN.top + @MARGIN.bottom)
    .attr("class", "tint-box")
    .append("g")
    .attr("transform", "translate(" + @MARGIN.left + "," + @MARGIN.top + ")")
  create_area: (x, y) ->
    @area = d3.svg.area().x((d) ->
      x(d.date)
    ).y0(@height).y1((d) ->
D
Dmitriy Zaporozhets 已提交
88 89
      xa = d.commits = d.commits ? d.additions ? d.deletions
      y(xa)
90 91
    ).interpolate("basis")
  create_brush: ->
D
Dmitriy Zaporozhets 已提交
92
    @brush = d3.svg.brush().x(@x).on("brushend", @update_content)
93
  draw_path: (data) ->
D
Dmitriy Zaporozhets 已提交
94
    @svg.append("path").datum(data).attr("class", "area").attr("d", @area)
95
  add_brush: ->
D
Dmitriy Zaporozhets 已提交
96
    @svg.append("g").attr("class", "selection").call(@brush).selectAll("rect").attr("height", @height)
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
  update_content: =>
    ContributorsGraph.set_x_domain(if @brush.empty() then @x_max_domain else @brush.extent())
    $("#brush_change").trigger('change')
  draw: ->
    @process_dates(@data)
    @create_scale()
    @create_axes()
    ContributorsGraph.init_domain(@data)
    @x_max_domain = @x_domain
    @set_domain()
    @create_area(@x, @y)
    @create_svg()
    @create_brush()
    @draw_path(@data)
    @draw_x_axis()
    @draw_y_axis()
    @add_brush()
  redraw: ->
    @process_dates(@data)
    ContributorsGraph.set_y_domain(@data)
    @set_y_domain()
    @svg.select("path").datum(@data)
    @svg.select("path").attr("d", @area)
    @svg.select(".y.axis").call(@y_axis)

C
Ciro Santilli 已提交
122
class @ContributorsAuthorGraph extends ContributorsGraph
123
  constructor: (@data) ->
124 125 126 127 128
    # Don't split graph size in half for mobile devices.
    if $(window).width() < 768
      @width = $('.content').width() - 80
    else
      @width = ($('.content').width() / 2) - 100
D
Dmitriy Zaporozhets 已提交
129
    @height = 200
130 131 132 133 134 135 136 137 138 139
    @x = null
    @y = null
    @x_axis = null
    @y_axis = null
    @area = null
    @svg = null
    @list_item = null
  create_scale: ->
    super @width, @height
  create_axes: ->
140 141
    @x_axis = d3.svg.axis().scale(@x).orient("bottom").ticks(8)
    @y_axis = d3.svg.axis().scale(@y).orient("left").ticks(5)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  create_area: (x, y) ->
    @area = d3.svg.area().x((d) ->
      parseDate = d3.time.format("%Y-%m-%d").parse
      x(parseDate(d))
    ).y0(@height).y1((d) =>
      if @data[d]? then y(@data[d]) else y(0)
    ).interpolate("basis")
  create_svg: ->
    @list_item = d3.selectAll(".person")[0].pop()
    @svg = d3.select(@list_item).append("svg")
    .attr("width", @width + @MARGIN.left + @MARGIN.right)
    .attr("height", @height + @MARGIN.top + @MARGIN.bottom)
    .attr("class", "spark")
    .append("g")
    .attr("transform", "translate(" + @MARGIN.left + "," + @MARGIN.top + ")")
  draw_path: (data) ->
158
    @svg.append("path").datum(data).attr("class", "area-contributor").attr("d", @area)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  draw: ->
    @create_scale()
    @create_axes()
    @set_domain()
    @create_area(@x, @y)
    @create_svg()
    @draw_path(@dates)
    @draw_x_axis()
    @draw_y_axis()
  redraw: ->
    @set_domain()
    @svg.select("path").datum(@dates)
    @svg.select("path").attr("d", @area)
    @svg.select(".x.axis").call(@x_axis)
    @svg.select(".y.axis").call(@y_axis)