morris.js/lib/morris.coffee

43 lines
1,000 B
CoffeeScript
Raw Normal View History

2012-09-01 12:04:06 +02:00
Morris = window.Morris = {}
2012-03-01 09:06:38 +01:00
2012-09-01 12:04:06 +02:00
$ = jQuery
2012-08-27 23:39:16 +02:00
2012-09-01 12:04:06 +02:00
# Very simple event-emitter class.
#
2012-09-16 00:05:48 +02:00
# @private
2012-08-27 23:39:16 +02:00
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...)
2012-09-01 12:04:06 +02:00
# Make long numbers prettier by inserting commas.
2012-04-01 23:29:37 +02:00
#
2012-09-16 00:05:48 +02:00
# @example
# Morris.commas(1234567) -> '1,234,567'
2012-04-01 23:29:37 +02:00
Morris.commas = (num) ->
if num?
2012-04-01 23:29:37 +02:00
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
'-'
2012-04-01 23:29:37 +02:00
2012-09-16 00:05:48 +02:00
# Zero-pad numbers to two characters wide.
2012-04-01 23:29:37 +02:00
#
2012-09-16 00:05:48 +02:00
# @example
# Morris.pad2(1) -> '01'
Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number