Skip to content
Merged
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
32 changes: 32 additions & 0 deletions src/utils/scales-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,37 @@ export function _getSmallestDistanceIndex(values, scaleObject) {
return result;
}

/**
* This is a workaround for issue that ordinal scale
* does not have invert method implemented in d3-scale.
* @param {Object} Ordinal d3-scale object.
* @returns {void}
* @private
*/

function addInvertFunctionToOrdinalScaleObject(scale) {
if (scale.invert) {
return;
}

scale.invert = function invert(value) {
const [lower, upper] = scale.range();
const start = Math.min(lower, upper);
const stop = Math.max(lower, upper);

if (value < start + scale.padding() * scale.step()) {
return scale.domain()[0];
}

if (value > stop - scale.padding() * scale.step()) {
return scale.domain()[scale.domain().length - 1];
}

const index = Math.floor((value - start - scale.padding() * scale.step()) / scale.step());
return scale.domain()[index];
}
}

/**
* Crate a scale function from the scale object.
* @param {Object} scaleObject Scale object.
Expand Down Expand Up @@ -179,6 +210,7 @@ export function getScaleFnFromScaleObject(scaleObject) {
.range(range);
if (type === ORDINAL_SCALE_TYPE) {
scale.padding(0.5);
addInvertFunctionToOrdinalScaleObject(scale);
}
return scale;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/utils/scales-utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,22 @@ test('scales-utils #getScaleFnFromScaleObject', t => {
[-1, 1],
'should build a generic domain that reflects about zero'
);

const ordinalScale = getScaleFnFromScaleObject({
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add a test for this that also capture padding? or does this do that already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are already testing padding of 0.5 (which is padding we use in react-vis).
This is so-called 'outer' padding (padding before first bar and after last bar).
Inner padding is set to 1.0 in d3-scale, as we are really using 'point-scale' from d3-scale.

Copy link
Contributor

Choose a reason for hiding this comment

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

👌

type: 'ordinal',
domain: ["a", "b", "c", "d", "e"],
range: [20, 120]
});

t.equal(ordinalScale.invert(-10), "a");
t.equal(ordinalScale.invert(25), "a");
t.equal(ordinalScale.invert(40), "a");
t.equal(ordinalScale.invert(60), "b");
t.equal(ordinalScale.invert(80), "c");
t.equal(ordinalScale.invert(100), "d");
t.equal(ordinalScale.invert(115), "e");
t.equal(ordinalScale.invert(130), "e");

t.end();
});

Expand Down