Skip to content

Commit

Permalink
fix(tooltip): Fix wrong .tooltip.show() on rotated axis
Browse files Browse the repository at this point in the history
- Adjust y coordinate for event on rotated axis
- Update d3's format specifier doc link

Fix #3476
  • Loading branch information
netil committed Oct 23, 2023
1 parent 041e144 commit 9b90c81
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/ChartInternal/interactions/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export default {

if (element) {
const isMultipleX = $$.isMultipleX();
const isRotated = config.axis_rotated;
let {width, left, top} = element.getBoundingClientRect();

if (hasAxis && !hasRadar && !isMultipleX) {
Expand All @@ -197,9 +198,11 @@ export default {
}

const x = left + (mouse ? mouse[0] : 0) + (
isMultipleX || config.axis_rotated ? 0 : (width / 2)
isMultipleX || isRotated ? 0 : (width / 2)
);
const y = top + (mouse ? mouse[1] : 0);

// value 4, is to adjust coordinate value set from: scale.ts - updateScales(): $$.getResettedPadding(1)
const y = top + (mouse ? mouse[1] : 0) + (isRotated ? 4 : 0);
const params = {
screenX: x,
screenY: y,
Expand Down
2 changes: 1 addition & 1 deletion src/config/Options/axis/x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default {
* @memberof Options
* @type {Function|string}
* @default undefined
* @see [D3's time specifier](https://github.com/d3/d3-time-format#locale_format)
* @see [D3's time specifier](https://d3js.org/d3-time-format#locale_format)
* @example
* axis: {
* x: {
Expand Down
2 changes: 1 addition & 1 deletion src/config/Options/data/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default {
* type: "timeseries"
* }
* }
* @see [D3's time specifier](https://github.com/d3/d3-time-format#locale_format)
* @see [D3's time specifier](https://d3js.org/d3-time-format#locale_format)
*/
data_xFormat: "%Y-%m-%d",

Expand Down
72 changes: 72 additions & 0 deletions test/api/tooltip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,76 @@ describe("API tooltip", () => {
expect(tooltip.select(".value").text()).to.be.equal("34.6%");
});
});

describe("on rotated axis", () => {
before(() => {
args = {
data: {
columns: [
["data1", 150, 140, 110, 100, 300],
["data1", 30, 200, 100, 400, 150],
["data2", 130, 340, 200, 500, 250]
],
type: "line"
},
axis: {
rotated: true
}
};
});

function checkTooltip(x, categoryName?) {
const type = chart.config("axis.x.type");
let value = x;

// when
chart.tooltip.show({x});

let th = chart.$.tooltip.select("th").text();

if (type === "timeseries") {
value = chart.internal.format.xAxisTick(x);
}

expect(isNaN(th) ? th : +th).to.be.equal(categoryName ?? value);
}

it("check for indexed x axis type", () => {
chart.xs().data1.forEach(v => {
checkTooltip(v);
});
});

it("set options", () => {
args.data.x = "x";
args.data.columns.unshift(
["x", "2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-01-05"]
);
args.axis.x = {
type: "timeseries",
tick: {
format: "%Y-%m-%d"
}
};
});

it("check for timeseries x axis type", () => {
chart.xs().data1.forEach(v => {
checkTooltip(+v);
});
});

it("set options", () => {
args.data.columns[0] = ["x", "a", "b", "c", "d", "e"];
args.axis.x = {
type: "category"
};
});

it("check for category x axis type", () => {
chart.categories().forEach((v, i) => {
checkTooltip(i, v);
});
});
});
});
2 changes: 1 addition & 1 deletion test/internals/tooltip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ describe("TOOLTIP", function() {
chart.$.chart.style("margin-top", "100px");
chart.tooltip.show({index:1});

expect(chart.$.tooltip.select("th").text()).to.be.equal("0");
expect(chart.$.tooltip.select("th").text()).to.be.equal("1");

chart.$.chart.style("margin-top", null);
});
Expand Down

0 comments on commit 9b90c81

Please sign in to comment.