histogram.coffee 872 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
class @Histogram
  constructor: (@id, @rawData) ->
    @xTicks = []
    @yTicks = []
    @data = []
    @calculate()
    @render()

  calculate: ->
    for [score, count] in @rawData
11
      continue if score == null
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
      log_count = Math.log(count + 1)
      @data.push [score, log_count]
      @xTicks.push [score, score.toString()]
      @yTicks.push [log_count, count.toString()]

  render: ->
    $.plot $("#histogram_#{@id}"), [
      data: @data
      bars:
        show: true
        align: 'center'
        lineWidth: 0
        fill: 1.0
      color: "#b72121"
    ],
      xaxis:
        min: -1
29
        max: Math.max.apply Math, $.map(@xTicks, (data) -> data[0] + 1)
30 31 32 33
        ticks: @xTicks
        tickLength: 0
      yaxis:
        min: 0.0
34
        max: Math.max.apply Math, $.map(@yTicks, (data) -> data[0] * 1.1)
35 36
        ticks: @yTicks
        labelWidth: 50