mirror of
https://github.com/morrisjs/morris.js.git
synced 2024-11-10 21:36:34 +01:00
expose some Morris.Hover options also make absolute position possible
This commit is contained in:
parent
4f53479cf0
commit
d69f73e39e
@ -31,7 +31,10 @@ Morris.Line({
|
|||||||
data: day_data,
|
data: day_data,
|
||||||
xkey: 'period',
|
xkey: 'period',
|
||||||
ykeys: ['licensed', 'sorned'],
|
ykeys: ['licensed', 'sorned'],
|
||||||
labels: ['Licensed', 'SORN']
|
labels: ['Licensed', 'SORN'],
|
||||||
|
hoverOptions: {
|
||||||
|
position: 'absolute right'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</pre>
|
</pre>
|
||||||
</body>
|
</body>
|
||||||
|
@ -7,7 +7,7 @@ class Morris.Bar extends Morris.Grid
|
|||||||
@cumulative = @options.stacked
|
@cumulative = @options.stacked
|
||||||
|
|
||||||
if @options.hideHover isnt 'always'
|
if @options.hideHover isnt 'always'
|
||||||
@hover = new Morris.Hover(parent: @el)
|
@hover = new Morris.Hover $.extend({}, @options.hoverOptions, parent: @el)
|
||||||
@on('hovermove', @onHoverMove)
|
@on('hovermove', @onHoverMove)
|
||||||
@on('hoverout', @onHoverOut)
|
@on('hoverout', @onHoverOut)
|
||||||
|
|
||||||
@ -26,6 +26,7 @@ class Morris.Bar extends Morris.Grid
|
|||||||
'#9440ed'
|
'#9440ed'
|
||||||
]
|
]
|
||||||
xLabelMargin: 50
|
xLabelMargin: 50
|
||||||
|
hoverOptions: {}
|
||||||
|
|
||||||
# Do any size-related calculations
|
# Do any size-related calculations
|
||||||
#
|
#
|
||||||
|
@ -2,14 +2,25 @@ class Morris.Hover
|
|||||||
# Displays contextual information in a floating HTML div.
|
# Displays contextual information in a floating HTML div.
|
||||||
|
|
||||||
@defaults:
|
@defaults:
|
||||||
class: 'morris-hover morris-default-style'
|
cssPrefix: 'morris'
|
||||||
|
position: 'auto'
|
||||||
|
class: ['-hover', '-default-style']
|
||||||
|
|
||||||
constructor: (options = {}) ->
|
constructor: (options = {}) ->
|
||||||
@options = $.extend {}, Morris.Hover.defaults, options
|
@options = $.extend {}, Morris.Hover.defaults, options
|
||||||
@el = $ "<div class='#{@options.class}'></div>"
|
@el = $ "<div class='#{@buildClassAttr(@options.class)}'></div>"
|
||||||
@el.hide()
|
@el.hide()
|
||||||
@options.parent.append(@el)
|
@options.parent.append(@el)
|
||||||
|
|
||||||
|
buildClassAttr: (c) ->
|
||||||
|
if typeof c is 'string'
|
||||||
|
c = [c]
|
||||||
|
classes = []
|
||||||
|
for class_ in c
|
||||||
|
classes.push(if class_.indexOf('-') is 0 then @options.cssPrefix + class_ else class_)
|
||||||
|
classes.join ' '
|
||||||
|
|
||||||
|
|
||||||
update: (html, x, y) ->
|
update: (html, x, y) ->
|
||||||
@html(html)
|
@html(html)
|
||||||
@show()
|
@show()
|
||||||
@ -19,19 +30,44 @@ class Morris.Hover
|
|||||||
@el.html(content)
|
@el.html(content)
|
||||||
|
|
||||||
moveTo: (x, y) ->
|
moveTo: (x, y) ->
|
||||||
|
p = @options.cssPrefix
|
||||||
|
@el.removeClass "#{p}-hover-below #{p}-hover-above #{p}-hover-left #{p}-hover-right"
|
||||||
|
|
||||||
parentWidth = @options.parent.innerWidth()
|
parentWidth = @options.parent.innerWidth()
|
||||||
parentHeight = @options.parent.innerHeight()
|
parentHeight = @options.parent.innerHeight()
|
||||||
hoverWidth = @el.outerWidth()
|
hoverWidth = @el.outerWidth()
|
||||||
hoverHeight = @el.outerHeight()
|
hoverHeight = @el.outerHeight()
|
||||||
left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth)
|
|
||||||
if y?
|
if @options.position in ['absolute', 'absolute above']
|
||||||
|
left = x - hoverWidth / 2
|
||||||
top = y - hoverHeight - 10
|
top = y - hoverHeight - 10
|
||||||
if top < 0
|
@el.addClass "#{p}-hover-above"
|
||||||
top = y + 10
|
else if @options.position is 'absolute below'
|
||||||
if top + hoverHeight > parentHeight
|
left = x - hoverWidth / 2
|
||||||
top = parentHeight / 2 - hoverHeight / 2
|
top = y + 10
|
||||||
|
@el.addClass "#{p}-hover-below"
|
||||||
|
else if @options.position is 'absolute left'
|
||||||
|
left = x - hoverWidth - 10
|
||||||
|
top = y - hoverHeight / 2
|
||||||
|
@el.addClass "#{p}-hover-left"
|
||||||
|
else if @options.position is 'absolute right'
|
||||||
|
left = x + 10
|
||||||
|
top = y - hoverHeight / 2
|
||||||
|
@el.addClass "#{p}-hover-right"
|
||||||
else
|
else
|
||||||
top = parentHeight / 2 - hoverHeight / 2
|
left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth)
|
||||||
|
if y?
|
||||||
|
top = y - hoverHeight - 10
|
||||||
|
if top < 0
|
||||||
|
top = y + 10
|
||||||
|
if top + hoverHeight > parentHeight
|
||||||
|
top = parentHeight / 2 - hoverHeight / 2
|
||||||
|
else
|
||||||
|
@el.addClass "#{p}-hover-below"
|
||||||
|
else
|
||||||
|
@el.addClass "#{p}-hover-above"
|
||||||
|
else
|
||||||
|
top = parentHeight / 2 - hoverHeight / 2
|
||||||
@el.css(left: left + "px", top: top + "px")
|
@el.css(left: left + "px", top: top + "px")
|
||||||
|
|
||||||
show: ->
|
show: ->
|
||||||
|
@ -11,7 +11,7 @@ class Morris.Line extends Morris.Grid
|
|||||||
@pointShrink = Raphael.animation r: @options.pointSize, 25, 'linear'
|
@pointShrink = Raphael.animation r: @options.pointSize, 25, 'linear'
|
||||||
|
|
||||||
if @options.hideHover isnt 'always'
|
if @options.hideHover isnt 'always'
|
||||||
@hover = new Morris.Hover(parent: @el)
|
@hover = new Morris.Hover $.extend({}, @options.hoverOptions, parent: @el)
|
||||||
@on('hovermove', @onHoverMove)
|
@on('hovermove', @onHoverMove)
|
||||||
@on('hoverout', @onHoverOut)
|
@on('hoverout', @onHoverOut)
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ class Morris.Line extends Morris.Grid
|
|||||||
xLabelFormat: null
|
xLabelFormat: null
|
||||||
xLabelMargin: 50
|
xLabelMargin: 50
|
||||||
continuousLine: true
|
continuousLine: true
|
||||||
hideHover: false
|
hoverOptions: {}
|
||||||
|
|
||||||
# Do any size-related calculations
|
# Do any size-related calculations
|
||||||
#
|
#
|
||||||
|
76
morris.js
76
morris.js
@ -474,7 +474,9 @@
|
|||||||
Morris.Hover = (function() {
|
Morris.Hover = (function() {
|
||||||
|
|
||||||
Hover.defaults = {
|
Hover.defaults = {
|
||||||
"class": 'morris-hover morris-default-style'
|
cssPrefix: 'morris',
|
||||||
|
position: 'auto',
|
||||||
|
"class": ['-hover', '-default-style']
|
||||||
};
|
};
|
||||||
|
|
||||||
function Hover(options) {
|
function Hover(options) {
|
||||||
@ -482,11 +484,24 @@
|
|||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
this.options = $.extend({}, Morris.Hover.defaults, options);
|
this.options = $.extend({}, Morris.Hover.defaults, options);
|
||||||
this.el = $("<div class='" + this.options["class"] + "'></div>");
|
this.el = $("<div class='" + (this.buildClassAttr(this.options["class"])) + "'></div>");
|
||||||
this.el.hide();
|
this.el.hide();
|
||||||
this.options.parent.append(this.el);
|
this.options.parent.append(this.el);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Hover.prototype.buildClassAttr = function(c) {
|
||||||
|
var class_, classes, _i, _len;
|
||||||
|
if (typeof c === 'string') {
|
||||||
|
c = [c];
|
||||||
|
}
|
||||||
|
classes = [];
|
||||||
|
for (_i = 0, _len = c.length; _i < _len; _i++) {
|
||||||
|
class_ = c[_i];
|
||||||
|
classes.push(class_.indexOf('-') === 0 ? this.options.cssPrefix + class_ : class_);
|
||||||
|
}
|
||||||
|
return classes.join(' ');
|
||||||
|
};
|
||||||
|
|
||||||
Hover.prototype.update = function(html, x, y) {
|
Hover.prototype.update = function(html, x, y) {
|
||||||
this.html(html);
|
this.html(html);
|
||||||
this.show();
|
this.show();
|
||||||
@ -498,22 +513,46 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
Hover.prototype.moveTo = function(x, y) {
|
Hover.prototype.moveTo = function(x, y) {
|
||||||
var hoverHeight, hoverWidth, left, parentHeight, parentWidth, top;
|
var hoverHeight, hoverWidth, left, p, parentHeight, parentWidth, top, _ref;
|
||||||
|
p = this.options.cssPrefix;
|
||||||
|
this.el.removeClass("" + p + "-hover-below " + p + "-hover-above " + p + "-hover-left " + p + "-hover-right");
|
||||||
parentWidth = this.options.parent.innerWidth();
|
parentWidth = this.options.parent.innerWidth();
|
||||||
parentHeight = this.options.parent.innerHeight();
|
parentHeight = this.options.parent.innerHeight();
|
||||||
hoverWidth = this.el.outerWidth();
|
hoverWidth = this.el.outerWidth();
|
||||||
hoverHeight = this.el.outerHeight();
|
hoverHeight = this.el.outerHeight();
|
||||||
left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth);
|
if ((_ref = this.options.position) === 'absolute' || _ref === 'absolute above') {
|
||||||
if (y != null) {
|
left = x - hoverWidth / 2;
|
||||||
top = y - hoverHeight - 10;
|
top = y - hoverHeight - 10;
|
||||||
if (top < 0) {
|
this.el.addClass("" + p + "-hover-above");
|
||||||
top = y + 10;
|
} else if (this.options.position === 'absolute below') {
|
||||||
if (top + hoverHeight > parentHeight) {
|
left = x - hoverWidth / 2;
|
||||||
top = parentHeight / 2 - hoverHeight / 2;
|
top = y + 10;
|
||||||
}
|
this.el.addClass("" + p + "-hover-below");
|
||||||
}
|
} else if (this.options.position === 'absolute left') {
|
||||||
|
left = x - hoverWidth - 10;
|
||||||
|
top = y - hoverHeight / 2;
|
||||||
|
this.el.addClass("" + p + "-hover-left");
|
||||||
|
} else if (this.options.position === 'absolute right') {
|
||||||
|
left = x + 10;
|
||||||
|
top = y - hoverHeight / 2;
|
||||||
|
this.el.addClass("" + p + "-hover-right");
|
||||||
} else {
|
} else {
|
||||||
top = parentHeight / 2 - hoverHeight / 2;
|
left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth);
|
||||||
|
if (y != null) {
|
||||||
|
top = y - hoverHeight - 10;
|
||||||
|
if (top < 0) {
|
||||||
|
top = y + 10;
|
||||||
|
if (top + hoverHeight > parentHeight) {
|
||||||
|
top = parentHeight / 2 - hoverHeight / 2;
|
||||||
|
} else {
|
||||||
|
this.el.addClass("" + p + "-hover-below");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.el.addClass("" + p + "-hover-above");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
top = parentHeight / 2 - hoverHeight / 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this.el.css({
|
return this.el.css({
|
||||||
left: left + "px",
|
left: left + "px",
|
||||||
@ -557,9 +596,9 @@
|
|||||||
r: this.options.pointSize
|
r: this.options.pointSize
|
||||||
}, 25, 'linear');
|
}, 25, 'linear');
|
||||||
if (this.options.hideHover !== 'always') {
|
if (this.options.hideHover !== 'always') {
|
||||||
this.hover = new Morris.Hover({
|
this.hover = new Morris.Hover($.extend({}, this.options.hoverOptions, {
|
||||||
parent: this.el
|
parent: this.el
|
||||||
});
|
}));
|
||||||
this.on('hovermove', this.onHoverMove);
|
this.on('hovermove', this.onHoverMove);
|
||||||
return this.on('hoverout', this.onHoverOut);
|
return this.on('hoverout', this.onHoverOut);
|
||||||
}
|
}
|
||||||
@ -577,7 +616,7 @@
|
|||||||
xLabelFormat: null,
|
xLabelFormat: null,
|
||||||
xLabelMargin: 50,
|
xLabelMargin: 50,
|
||||||
continuousLine: true,
|
continuousLine: true,
|
||||||
hideHover: false
|
hoverOptions: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
Line.prototype.calc = function() {
|
Line.prototype.calc = function() {
|
||||||
@ -1121,9 +1160,9 @@
|
|||||||
Bar.prototype.init = function() {
|
Bar.prototype.init = function() {
|
||||||
this.cumulative = this.options.stacked;
|
this.cumulative = this.options.stacked;
|
||||||
if (this.options.hideHover !== 'always') {
|
if (this.options.hideHover !== 'always') {
|
||||||
this.hover = new Morris.Hover({
|
this.hover = new Morris.Hover($.extend({}, this.options.hoverOptions, {
|
||||||
parent: this.el
|
parent: this.el
|
||||||
});
|
}));
|
||||||
this.on('hovermove', this.onHoverMove);
|
this.on('hovermove', this.onHoverMove);
|
||||||
return this.on('hoverout', this.onHoverOut);
|
return this.on('hoverout', this.onHoverOut);
|
||||||
}
|
}
|
||||||
@ -1133,7 +1172,8 @@
|
|||||||
barSizeRatio: 0.75,
|
barSizeRatio: 0.75,
|
||||||
barGap: 3,
|
barGap: 3,
|
||||||
barColors: ['#0b62a4', '#7a92a3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'],
|
barColors: ['#0b62a4', '#7a92a3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'],
|
||||||
xLabelMargin: 50
|
xLabelMargin: 50,
|
||||||
|
hoverOptions: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
Bar.prototype.calc = function() {
|
Bar.prototype.calc = function() {
|
||||||
|
2
morris.min.js
vendored
2
morris.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user