forked from morrisjs/morris.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorris.coffee
43 lines (38 loc) · 1006 Bytes
/
morris.coffee
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
Morris = window.Morris = {}
$ = jQuery
# Very simple event-emitter class.
#
# @private
class Morris.EventEmitter
on: (name, handler) ->
unless @handlers?
@handlers = {}
unless @handlers[name]?
@handlers[name] = []
@handlers[name].push(handler)
@
fire: (name, args...) ->
if @handlers? and @handlers[name]?
for handler in @handlers[name]
handler(args...)
# Make long numbers prettier by inserting commas.
#
# @example
# Morris.commas(1234567) -> '1,234,567'
Morris.commas = (num) ->
if num?
ret = if num < 0 then "-" else ""
absnum = Math.abs(num)
intnum = Math.floor(absnum).toFixed(0)
ret += intnum.replace(/(?=(?:\d{3})+$)(?!^)/g, ',')
strabsnum = absnum.toString()
if strabsnum.length > intnum.length
ret += strabsnum.slice(intnum.length)
ret
else
'-'
# Zero-pad numbers to two characters wide.
#
# @example
# Morris.pad2(1) -> '01'
Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number