Skip to content

Commit

Permalink
Merge 2780b60 into 0b2be09
Browse files Browse the repository at this point in the history
  • Loading branch information
george-cz committed Jun 23, 2016
2 parents 0b2be09 + 2780b60 commit b83a56b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/chart/generateCategoricalChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,18 @@ const generateCategoricalChart = (ChartComponent, GraphicalChild) => {
&& e.chartY >= offset.top
&& e.chartY <= offset.top + offset.height;

const displayName = this.constructor.displayName;

const snapToLine = displayName.indexOf('LineChart') >= 0 || displayName.indexOf('AreaChart') >= 0

if (!isIn) { return null; }

const { layout } = this.props;
const axisMap = layout === 'horizontal' ? xAxisMap : yAxisMap;
const pos = layout === 'horizontal' ? e.chartX : e.chartY;
const axis = getAnyElementOfObject(axisMap);
const ticks = getTicksOfAxis(axis, false, true);
const activeIndex = calculateActiveTickIndex(pos, ticks);
const activeIndex = calculateActiveTickIndex(pos, ticks, snapToLine);

if (activeIndex >= 0) {
return {
Expand Down
36 changes: 20 additions & 16 deletions src/util/CartesianUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,25 +281,29 @@ export const getTicksOfAxis = (axis, isGrid, isAll) => {
));
};

export const calculateActiveTickIndex = (coordinate, ticks) => {
let index = -1;
const len = ticks.length;

if (len > 1) {
for (let i = 0; i < len; i++) {
if ((i === 0 && coordinate <= (ticks[i].coordinate + ticks[i + 1].coordinate) / 2)
|| (i > 0 && i < len - 1 && coordinate > (ticks[i].coordinate + ticks[i - 1].coordinate) / 2
&& coordinate <= (ticks[i].coordinate + ticks[i + 1].coordinate) / 2)
|| (i === len - 1 && coordinate > (ticks[i].coordinate + ticks[i - 1].coordinate) / 2)) {
index = i;
break;
export const calculateActiveTickIndex = (coordinate, ticks, snapToClosestLine) => {

let boundaries = ticks.map(tick => tick.coordinate);

let targetTickCoordinate = boundaries.reduce((lastResult, current, index) => {
if (snapToClosestLine) {
const distanceToLast = Math.abs(coordinate - lastResult)
const distanceToNext = boundaries[index + 1] !== undefined ? Math.abs(coordinate - boundaries[index + 1]) : null

if (distanceToNext !== null && distanceToNext < distanceToLast) {
return boundaries[index + 1]
}
else return lastResult
}
} else {
index = 0;
}
else {
if(coordinate >= current) {
return current;
}
return lastResult;
}
}, boundaries[0]);

return index;
return boundaries.indexOf(targetTickCoordinate)
};

/**
Expand Down

0 comments on commit b83a56b

Please sign in to comment.