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

fix(bar): Fix bar radius on inverted axis #3063

Merged
merged 1 commit into from
Jan 25, 2023
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
7 changes: 5 additions & 2 deletions src/ChartInternal/shape/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ export default {
const indexX = +isRotated;
const indexY = +!indexX;

const isNegative = d.value as number < 0;
const isUnderZero = d.value as number < 0;
const isInverted = config[`axis_${$$.axis.getId(d.id)}_inverted`];
const isNegative = (!isInverted && isUnderZero) || (isInverted && !isUnderZero);

const pathRadius = ["", ""];
let radius = 0;

Expand All @@ -197,7 +200,7 @@ export default {
// path string data shouldn't be containing new line chars
// https://github.com/naver/billboard.js/issues/530
const path = isRotated ?
`H${points[1][indexX] - radius} ${pathRadius[0]}V${points[2][indexY] - radius} ${pathRadius[1]}H${points[3][indexX]}` :
`H${points[1][indexX] + (isNegative ? radius : -radius)} ${pathRadius[0]}V${points[2][indexY] - radius} ${pathRadius[1]}H${points[3][indexX]}` :
`V${points[1][indexY] + (isNegative ? -radius : radius)} ${pathRadius[0]}H${points[2][indexX] - radius} ${pathRadius[1]}V${points[3][indexY]}`;

return `M${points[0][indexX]},${points[0][indexY]}${path}z`;
Expand Down
52 changes: 50 additions & 2 deletions test/shape/bar-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ describe("SHAPE BAR", () => {

it("check the rotated axis bar radius", () => {
checkRadius([
"M131.11111111111111,161.16666666666669H39.166666666666664 a10,10 1 0 0 -10,10V166.16666666666669 a10,10 1 0 0 10,10H131.11111111111111z",
"M131.11111111111111,161.16666666666669H59.166666666666664 a10,10 1 0 0 -10,10V166.16666666666669 a10,10 1 0 0 10,10H131.11111111111111z",
"M131.11111111111111,179.16666666666669H285 a10,10 0 0 1 10,10V184.16666666666669 a10,10 0 0 1 -10,10H131.11111111111111z"
]);
});
Expand Down Expand Up @@ -873,7 +873,55 @@ describe("SHAPE BAR", () => {
done();
}
});
})
});

it("set options", () => {
args = {
data: {
columns: [
["data1", 250],
["data2", -250]
],
type: "bar"
},
bar: {
radius: {
ratio: 0.5
}
},
axis: {
rotated: true
}
};
});

it("radius should be rendered correclty on rotated axis", () => {
const expected = [
'M295,85.80000000000001H477.23333333333323 a63.599999999999994,63.599999999999994 0 0 1 63.599999999999994,63.599999999999994V149.4 a63.599999999999994,63.599999999999994 0 0 1 -63.599999999999994,63.599999999999994H295z',
'M295,213H112.76666666666665 a63.599999999999994,63.599999999999994 1 0 0 -63.599999999999994,63.599999999999994V276.6 a63.599999999999994,63.599999999999994 1 0 0 63.599999999999994,63.599999999999994H295z'
];

chart.$.bar.bars.each(function() {
expect(this.getAttribute("d")).to.be.equal(expected.shift());
});
});

it("set options: axis.y.inverted=true", () => {
args.axis.y = {
inverted: true
};
});

it("radius should be rendered correclty on rotated & inverted axis", () => {
const expected = [
'M295,85.80000000000001H112.76666666666668 a63.599999999999994,63.599999999999994 1 0 0 -63.599999999999994,63.599999999999994V149.4 a63.599999999999994,63.599999999999994 1 0 0 63.599999999999994,63.599999999999994H295z',
'M295,213H477.23333333333323 a63.599999999999994,63.599999999999994 0 0 1 63.599999999999994,63.599999999999994V276.6 a63.599999999999994,63.599999999999994 0 0 1 -63.599999999999994,63.599999999999994H295z'
];

chart.$.bar.bars.each(function() {
expect(this.getAttribute("d")).to.be.equal(expected.shift());
});
});
});

describe("bar linear gradient", () => {
Expand Down