From 80714867fcb4e7c188b679fbbf6c746d67cdeb32 Mon Sep 17 00:00:00 2001 From: Masayuki Tanaka Date: Thu, 9 May 2013 22:37:57 +0900 Subject: [PATCH] Fix bar selection --- c3.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/c3.js b/c3.js index b82e777..814e930 100644 --- a/c3.js +++ b/c3.js @@ -426,14 +426,15 @@ //-- Util --// - function dist (_this, _x, _y) { - var mouse = d3.mouse(_this) - return Math.sqrt(Math.pow(x(_x)-mouse[0],2)+Math.pow(y(_y)-mouse[1],2)) - } - function isWithinBar (_this, _x, _y) { - var mouse = d3.mouse(_this) - var xoffset = d3.select(_this).attr("width")/2 + 10, yoffset = 10 - var sx = _x - xoffset, ex = _x + xoffset, ey = _y - yoffset + function isWithinCircle (_this, _r) { + var mouse = d3.mouse(_this), d3_this = d3.select(_this) + var cx = d3_this.attr("cx")*1, cy = d3_this.attr("cy")*1 + return Math.sqrt(Math.pow(cx-mouse[0],2)+Math.pow(cy-mouse[1],2)) < _r + } + function isWithinBar (_this) { + var mouse = d3.mouse(_this), d3_this = d3.select(_this) + var x = d3_this.attr("x")*1, y = d3_this.attr("y")*1, w = d3_this.attr("width")*1 + var sx = x - 10, ex = x + w + 10, ey = y - 10 return sx < mouse[0] && mouse[0] < ex && ey < mouse[1] } function isWithinRegions (x, regions) { @@ -725,11 +726,12 @@ d3.select('.event-rect-'+i).style('cursor', null) }) .filter(function(d){ + var _this = d3.select(this) if (this.nodeName === 'circle') { - return dist(this, d.x, d.value) < __point_select_r + return isWithinCircle(this, __point_select_r) } else if (this.nodeName === 'rect') { - return isWithinBar(this, x(d.x), y(d.value)) + return isWithinBar(this, _this.attr('x'), _this.attr('y')) } }) .each(function(d){ @@ -747,11 +749,11 @@ _selected = _this.classed('_s_') var isWithin = false, toggle if (this.nodeName === 'circle') { - isWithin = dist(this, d.x, d.value) < __point_select_r*1.5 + isWithin = isWithinCircle(this, __point_select_r*1.5) toggle = togglePoint } else if (this.nodeName === 'rect') { - isWithin = isWithinBar(this, x(d.x), y(d.value)) + isWithin = isWithinBar(this) toggle = toggleBar } if (__data_selection_grouped || isWithin) { @@ -801,9 +803,9 @@ var _this = d3.select(this), _selected = _this.classed('_s_'), _included = _this.classed('_i_'), - _x = x(d.x), _y = y(d.value), - _offset = _this.attr('width')/2, - _within = min_x < _x+_offset && _x-_offset < max_x && min_y < _y && _y < max_y + _x = _this.attr("x")*1, _y = _this.attr("y")*1 + _w = _this.attr('width')*1, + _within = min_x < _x+_w && _x < max_x && _y < max_y if (_within ^ _included) { _this.classed('_i_', !_included) // TODO: included/unincluded callback here