Skip to content

Commit

Permalink
incorporate changes request by cjcenizal
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese authored and cjcenizal committed Nov 10, 2016
1 parent 3b6652f commit 0f7e70a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
61 changes: 34 additions & 27 deletions src/ui/public/utils/brush_event.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
import moment from 'moment';
import buildRangeFilter from 'ui/filter_manager/lib/range';
export default function brushEventProvider(timefilter) {
return function ($state) {
return function (event) {
if (event.data.xAxisField && event.data.xAxisField.type === 'date') {
let from = moment(event.range[0]);
let to = moment(event.range[1]);
return $state => {
return event => {
if (!event.data.xAxisField) {
return;
}

if (to - from === 0) return;
switch (event.data.xAxisField.type) {
case 'date':
let from = moment(event.range[0]);
let to = moment(event.range[1]);

timefilter.time.from = from;
timefilter.time.to = to;
timefilter.time.mode = 'absolute';
} else if (event.data.xAxisField && event.data.xAxisField.type === 'number') {
let existingFilter = null;
$state.filters.forEach(function (it) {
if (it.meta && it.meta.key === event.data.xAxisField.name) {
existingFilter = it;
}
});
if (to - from === 0) return;

timefilter.time.from = from;
timefilter.time.to = to;
timefilter.time.mode = 'absolute';
break;

const range = {gte: event.range[0], lt: event.range[event.range.length - 1]};
if (existingFilter) {
existingFilter.range[event.data.xAxisField.name] = range;
} else {
const newFilter = buildRangeFilter(
event.data.xAxisField,
range,
event.data.indexPattern,
event.data.xAxisFormatter);
$state.$newFilters = [newFilter];
}
case 'number':
if (event.range.length <= 1) return;

const existingFilter = $state.filters.find(filter => (
filter.meta && filter.meta.key === event.data.xAxisField.name
));

const range = {gte: event.range[0], lt: event.range[event.range.length - 1]};
if (existingFilter) {
existingFilter.range[event.data.xAxisField.name] = range;
} else {
const newFilter = buildRangeFilter(
event.data.xAxisField,
range,
event.data.indexPattern,
event.data.xAxisFormatter);
$state.$newFilters = [newFilter];
}
break;
}
};
};
Expand Down
20 changes: 18 additions & 2 deletions src/ui/public/vislib/lib/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ export default function DispatchClass(Private, config) {

function simulateClickWithBrushEnabled(d, i) {
if (!validBrushClick(d3.event)) return;
if (xScale.invert) {

if (isQuantitativeScale(xScale)) {
const bar = d3.select(this);
const startX = d3.mouse(svg.node());
const startXInv = xScale.invert(startX[0]);
Expand All @@ -213,7 +214,6 @@ export default function DispatchClass(Private, config) {
return this.addEvent('mousedown', simulateClickWithBrushEnabled);
};


/**
* Mouseover Behavior
*
Expand Down Expand Up @@ -309,6 +309,22 @@ export default function DispatchClass(Private, config) {
};
}

/**
* Determine if d3.Scale is quantitative
*
* @param element {d3.Scale}
* @method isQuantitativeScale
* @returns {boolean}
*/
function isQuantitativeScale(scale) {
//Invert is a method that only exists on quantiative scales
if (scale.invert) {
return true;
} else {
return false;
}
}

function validBrushClick(event) {
return event.button === 0;
}
Expand Down

0 comments on commit 0f7e70a

Please sign in to comment.