Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range selection #252

Merged
merged 4 commits into from
Oct 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/morris.bar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ class Morris.Bar extends Morris.Grid
else
@options.barColors[sidx % @options.barColors.length]

# hit test - returns the index of the row beneath the given coordinate
# hit test - returns the index of the row at the given x-coordinate
#
hitTest: (x, y) ->
hitTest: (x) ->
return null if @data.length == 0
x = Math.max(Math.min(x, @right), @left)
Math.min(@data.length - 1,
Expand All @@ -139,14 +139,14 @@ class Morris.Bar extends Morris.Grid
#
# @private
onGridClick: (x, y) =>
index = @hitTest(x, y)
index = @hitTest(x)
@fire 'click', index, @options.data[index], x, y

# hover movement event handler
#
# @private
onHoverMove: (x, y) =>
index = @hitTest(x, y)
index = @hitTest(x)
@hover.update(@hoverContentForRow(index)...)

# hover out event handler
Expand Down
55 changes: 48 additions & 7 deletions lib/morris.grid.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Morris.Grid extends Morris.EventEmitter
@elementHeight = null
@dirty = false

# range selection
@selectFrom = null

# more stuff
@init() if @init

Expand All @@ -38,9 +41,19 @@ class Morris.Grid extends Morris.EventEmitter
# hover
@el.bind 'mousemove', (evt) =>
offset = @el.offset()
@fire 'hovermove', evt.pageX - offset.left, evt.pageY - offset.top
x = evt.pageX - offset.left
if @selectFrom
left = @data[@hitTest(Math.min(x, @selectFrom))]._x
right = @data[@hitTest(Math.max(x, @selectFrom))]._x
width = right - left
@selectionRect.attr({ x: left, width: width })
else
@fire 'hovermove', x, evt.pageY - offset.top

@el.bind 'mouseout', (evt) =>
@el.bind 'mouseleave', (evt) =>
if @selectFrom
@selectionRect.hide()
@selectFrom = null
@fire 'hoverout'

@el.bind 'touchstart touchmove touchend', (evt) =>
Expand All @@ -53,6 +66,21 @@ class Morris.Grid extends Morris.EventEmitter
offset = @el.offset()
@fire 'gridclick', evt.pageX - offset.left, evt.pageY - offset.top

if @options.rangeSelect
@selectionRect = @raphael.rect(0, 0, 0, @el.innerHeight())
.attr({ fill: @options.rangeSelectColor, stroke: false })
.toBack()
.hide()

@el.bind 'mousedown', (evt) =>
offset = @el.offset()
@startRange evt.pageX - offset.left

@el.bind 'mouseup', (evt) =>
offset = @el.offset()
@endRange evt.pageX - offset.left
@fire 'hovermove', evt.pageX - offset.left, evt.pageY - offset.top

@postInit() if @postInit

# Default options
Expand Down Expand Up @@ -93,6 +121,8 @@ class Morris.Grid extends Morris.EventEmitter
'#3a5f0b'
'#005502'
]
rangeSelect: null
rangeSelectColor: '#eef'

# Update the data series and redraw the chart.
#
Expand Down Expand Up @@ -299,11 +329,6 @@ class Morris.Grid extends Morris.EventEmitter
else
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"

updateHover: (x, y) ->
hit = @hitTest(x, y)
if hit?
@hover.update(hit...)

# draw y axis labels, horizontal lines
#
drawGrid: ->
Expand Down Expand Up @@ -351,6 +376,22 @@ class Morris.Grid extends Morris.EventEmitter
.attr('stroke', @options.gridLineColor)
.attr('stroke-width', @options.gridStrokeWidth)

# Range selection
#
startRange: (x) ->
@hover.hide()
@selectFrom = x
@selectionRect.attr({ x: x, width: 0 }).show()

endRange: (x) ->
if @selectFrom
start = Math.min(@selectFrom, x)
end = Math.max(@selectFrom, x)
@options.rangeSelect.call @el,
start: @data[@hitTest(start)].x
end: @data[@hitTest(end)].x
@selectFrom = null

# Parse a date into a javascript timestamp
#
#
Expand Down
8 changes: 4 additions & 4 deletions lib/morris.line.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class Morris.Line extends Morris.Grid
if y? then @transY(y) else y
row._ymax = Math.min.apply(null, [@bottom].concat(y for y in row._y when y?))

# hit test - returns the index of the row beneath the given coordinate
# hit test - returns the index of the row at the given x-coordinate
#
hitTest: (x, y) ->
hitTest: (x) ->
return null if @data.length == 0
# TODO better search algo
for r, index in @data.slice(1)
Expand All @@ -70,14 +70,14 @@ class Morris.Line extends Morris.Grid
#
# @private
onGridClick: (x, y) =>
index = @hitTest(x, y)
index = @hitTest(x)
@fire 'click', index, @options.data[index], x, y

# hover movement event handler
#
# @private
onHoverMove: (x, y) =>
index = @hitTest(x, y)
index = @hitTest(x)
@displayHoverForRow(index)

# hover out event handler
Expand Down
85 changes: 67 additions & 18 deletions morris.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion morris.min.js

Large diffs are not rendered by default.