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

Pass the node to the supstance drag event listener #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 35 additions & 30 deletions dist/techan.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,8 +1243,8 @@ module.exports = function(d3_scale_linear, d3_extent, accessor_ohlc, plot, plotM
var group = plot.groupSelect(g, plot.dataMapper.array, p.accessor.d);

// 3x2 path's as wick and body can be styled slightly differently (stroke and fills)
plot.appendUpDownEqual(group.selection, p.accessor, ['candle', 'body']);
plot.appendUpDownEqual(group.selection, p.accessor, ['candle', 'wick']);
plot.appendPathsUpDownEqual(group.selection, p.accessor, ['candle', 'body']);
plot.appendPathsUpDownEqual(group.selection, p.accessor, ['candle', 'wick']);

candlestick.refresh(g);
}
Expand Down Expand Up @@ -1713,7 +1713,7 @@ module.exports = function(d3_scale_linear, d3_extent, accessor_ohlc, plot, plotM
function ohlc(g) {
var group = plot.groupSelect(g, plot.dataMapper.array, p.accessor.d);

plot.appendUpDownEqual(group.selection, p.accessor, 'ohlc');
plot.appendPathsUpDownEqual(group.selection, p.accessor, 'ohlc');

ohlc.refresh(g);
}
Expand Down Expand Up @@ -1786,35 +1786,30 @@ module.exports = function(d3_svg_line, d3_select) {
return line;
}

function up(accessor, d) {
return accessor.o(d) < accessor.c(d);
}

function down(accessor, d) {
return accessor.o(d) > accessor.c(d);
}

function groupUpDownEqual(data, accessor) {
return data.reduce(function(result, d) {
if (up(accessor, d)) result.up.push(d);
else if (down(accessor, d)) result.down.push(d);
else result.equal.push(d);
return result;
}, { up: [], down: [], equal: [] });
function upDownEqual(accessor) {
return {
up: function(d) { return accessor.o(d) < accessor.c(d); },
down: function(d) { return accessor.o(d) > accessor.c(d); },
equal: function(d) { return accessor.o(d) === accessor.c(d); }
};
}

function appendUpDownEqual(g, accessor, plotName, upDownEqual) {
function appendPathsGroupBy(g, accessor, plotName, classes) {
var plotNames = plotName instanceof Array ? plotName : [plotName];

upDownEqual = upDownEqual || groupUpDownEqual(g.datum(), accessor);
classes = classes || upDownEqual(accessor);

appendPlotType(g, upDownEqual.up, plotNames, 'up');
appendPlotType(g, upDownEqual.down, plotNames, 'down');
appendPlotType(g, upDownEqual.equal, plotNames, 'equal');
Object.keys(classes).forEach(function(key) {
appendPlotTypePath(g, classes[key], plotNames, key);
});
}

function appendPlotType(g, data, plotNames, direction) {
g.selectAll('path.' + plotNames.join('.') + '.' + direction).data([data])
function appendPathsUpDownEqual(g, accessor, plotName) {
appendPathsGroupBy(g, accessor, plotName, upDownEqual(accessor));
}

function appendPlotTypePath(g, data, plotNames, direction) {
g.selectAll('path.' + plotNames.join('.') + '.' + direction).data(function(d) { return [d.filter(data)]; })
.enter().append('path').attr('class', plotNames.join(' ') + ' ' + direction);
}

Expand All @@ -1841,9 +1836,9 @@ module.exports = function(d3_svg_line, d3_select) {
};
},

groupUpDownEqual: groupUpDownEqual,
appendPathsGroupBy: appendPathsGroupBy,

appendUpDownEqual: appendUpDownEqual,
appendPathsUpDownEqual: appendPathsUpDownEqual,

horizontalPathLine: function(accessor_date, x, accessor_value, y) {
return function(d) {
Expand Down Expand Up @@ -2124,7 +2119,7 @@ module.exports = function(d3_behavior_drag, d3_event, d3_select, d3_dispatch, ac
accessor.v(d, value);
annotationSelection.each(plot.annotation.update(annotation, d3_event().y));
refresh(g, plot, accessor, x, y, annotationSelection, annotation);
dispatch.drag(d);
dispatch.drag(d, g);
});

return plot.interaction.dragStartEndDispatch(drag, dispatch);
Expand All @@ -2150,6 +2145,7 @@ function supstancePath(accessor, x, y) {
return path.join(' ');
};
}

},{}],34:[function(require,module,exports){
'use strict';

Expand Down Expand Up @@ -2332,7 +2328,7 @@ module.exports = function(accessor_volume, plot, plotMixin) { // Injected depen
function volume(g) {
var group = plot.groupSelect(g, plot.dataMapper.array, p.accessor.d);

if(p.accessor.o && p.accessor.c) plot.appendUpDownEqual(group.selection, p.accessor, 'volume');
if(p.accessor.o && p.accessor.c) plot.appendPathsUpDownEqual(group.selection, p.accessor, 'volume');
else group.entry.append('path').attr('class', 'volume');

volume.refresh(g);
Expand Down Expand Up @@ -2683,7 +2679,16 @@ module.exports = function(d3_scale_linear, d3_time, d3_bisect, techan_util_rebin
return function(d) {
var value = visibleDomainLookup[+d];
if (value !== undefined) return visibleDomain[value];
return visibleDomain[d3_bisect(visibleDomain, d)];
var index = d3_bisect(visibleDomain, d);
if (index > 0) {
// d3_bisect gets the index of the closest value that is the greater than d,
// which may not be the value that is closest to d.
// If the closest value that is smaller than d is closer, choose that instead.
if ((+d - (+visibleDomain[index-1])) < (+visibleDomain[index] - +d)) {
index--;
}
}
return visibleDomain[index];
};
}

Expand Down
4 changes: 2 additions & 2 deletions dist/techan.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/techan.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/plot/supstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = function(d3_behavior_drag, d3_event, d3_select, d3_dispatch, ac
accessor.v(d, value);
annotationSelection.each(plot.annotation.update(annotation, d3_event().y));
refresh(g, plot, accessor, x, y, annotationSelection, annotation);
dispatch.drag(d);
dispatch.drag(d, g);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is handy, but it should align to d3 the api, generally passing (d, i) as parameters and this being the node as documented here https://github.com/mbostock/d3/wiki/Selections#on

The specified listener is invoked in the same manner as other operator functions, being passed the current datum d and index i, with the this context as the current DOM element.

});

return plot.interaction.dragStartEndDispatch(drag, dispatch);
Expand All @@ -82,4 +82,4 @@ function supstancePath(accessor, x, y) {

return path.join(' ');
};
}
}
11 changes: 10 additions & 1 deletion src/scale/financetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,16 @@ module.exports = function(d3_scale_linear, d3_time, d3_bisect, techan_util_rebin
return function(d) {
var value = visibleDomainLookup[+d];
if (value !== undefined) return visibleDomain[value];
return visibleDomain[d3_bisect(visibleDomain, d)];
var index = d3_bisect(visibleDomain, d);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this pull request #100?

if (index > 0) {
// d3_bisect gets the index of the closest value that is the greater than d,
// which may not be the value that is closest to d.
// If the closest value that is smaller than d is closer, choose that instead.
if ((+d - (+visibleDomain[index-1])) < (+visibleDomain[index] - +d)) {
index--;
}
}
return visibleDomain[index];
};
}

Expand Down
4 changes: 2 additions & 2 deletions test/spec/bundle/plot/supstanceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ techanModule('plot/supstance', function(specBuilder) {
});

it('Then should dispatch the drag event', function() {
expect(listeners.drag).toHaveBeenCalledWith({ value: 16 });
expect(listeners.drag).toHaveBeenCalledWith({ value: 16 }, jasmine.any(Object));
});
});

Expand Down Expand Up @@ -319,4 +319,4 @@ techanModule('plot/supstance', function(specBuilder) {
});
});
});
});
});
4 changes: 3 additions & 1 deletion test/spec/bundle/scale/financetimeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ techanModule('scale/financetime', function(specBuilder) {

it('Then ticks with specified interval and step count returns that number', function() {
expect(financetime.ticks(d3.time.day, 2)).toEqual([
new Date(2012,4,18),
new Date(2012,4,21),
new Date(2012,4,23),
new Date(2012,4,25),
Expand Down Expand Up @@ -425,6 +426,7 @@ techanModule('scale/financetime', function(specBuilder) {

it('Then ticks with specified interval and step count returns that number', function() {
expect(financetime.ticks(d3.time.day, 2)).toEqual([
new Date(2012,4,18),
new Date(2012,4,21),
new Date(2012,4,23),
new Date(2012,4,25),
Expand Down Expand Up @@ -627,4 +629,4 @@ techanModule('scale/financetime', function(specBuilder) {
});
});
});
});
});