Skip to content

Commit

Permalink
Adds events range
Browse files Browse the repository at this point in the history
  • Loading branch information
fran6co committed Sep 23, 2014
1 parent 93c7441 commit a570589
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ <h1>Time Events</h1>
labels: ['Licensed', 'SORN'],
events: [
'2011-04',
['2011-05', '2011-06'],
'2011-08'
]
});
Expand Down
41 changes: 31 additions & 10 deletions lib/morris.grid.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,17 @@ class Morris.Grid extends Morris.EventEmitter
@events = []
if @options.events.length > 0
if @options.parseTime
@events = (Morris.parseDate(e) for e in @options.events)
for e in @options.events
if e instanceof Array
[from, to] = e
@events.push([Morris.parseDate(from), Morris.parseDate(to)])
else
@events.push(Morris.parseDate(e))
else
@events = @options.events
@xmax = Math.max(@xmax, Math.max(@events...))
@xmin = Math.min(@xmin, Math.min(@events...))
flatEvents = $.map @events, (e) -> e
@xmax = Math.max(@xmax, Math.max(flatEvents...))
@xmin = Math.min(@xmin, Math.min(flatEvents...))

if @xmin is @xmax
@xmin -= 1
Expand Down Expand Up @@ -441,15 +447,30 @@ class Morris.Grid extends Morris.EventEmitter
.attr('stroke-width', @options.goalStrokeWidth)

drawEvent: (event, color) ->
x = Math.floor(@transX(event)) + 0.5
if not @options.horizontal
path = "M#{x},#{@yStart}V#{@yEnd}"
if event instanceof Array
[from, to] = event
from = Math.floor(@transX(from)) + 0.5
to = Math.floor(@transX(to)) + 0.5

if not @options.horizontal
@raphael.rect(from, @yEnd, to-from, @yStart-@yEnd)
.attr({ fill: color, stroke: false })
.toBack()
else
@raphael.rect(@yStart, from, @yEnd-@yStart, to-from)
.attr({ fill: color, stroke: false })
.toBack()

else
path = "M#{@yStart},#{x}H#{@yEnd}"
x = Math.floor(@transX(event)) + 0.5
if not @options.horizontal
path = "M#{x},#{@yStart}V#{@yEnd}"
else
path = "M#{@yStart},#{x}H#{@yEnd}"

@raphael.path(path)
.attr('stroke', color)
.attr('stroke-width', @options.eventStrokeWidth)
@raphael.path(path)
.attr('stroke', color)
.attr('stroke-width', @options.eventStrokeWidth)

drawYAxisLabel: (xPos, yPos, text) ->
@raphael.text(xPos, yPos, text)
Expand Down

0 comments on commit a570589

Please sign in to comment.