Skip to content

Commit

Permalink
fix(candlestick): Fix rendering on rotated axis
Browse files Browse the repository at this point in the history
Fix rendering with negative values on rotated axis

Fix #3387
  • Loading branch information
netil authored Aug 30, 2023
1 parent deb4146 commit 17f5058
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
13 changes: 0 additions & 13 deletions src/ChartInternal/shape/candlestick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ export default {
candlestickEnter.append("line");
candlestickEnter.append("path");

if (!$root.candlestick) {
$root.candlestick = {};
}

$root.candlestick = candlestick.merge(candlestickEnter)
.style("opacity", initialOpacity);
},
Expand Down Expand Up @@ -155,8 +151,6 @@ export default {
*/
generateGetCandlestickPoints(indices, isSub = false): (d, i) => number[][] {
const $$ = this;
const {config} = $$;

const axis = isSub ? $$.axis.subX : $$.axis.x;
const targetsNum = $$.getIndicesMax(indices) + 1;
const barW: IOffset = $$.getBarW("candlestick", axis, targetsNum);
Expand Down Expand Up @@ -190,13 +184,6 @@ export default {
low: y(value.low)
};

// fix posY not to overflow opposite quadrant
if (config.axis_rotated && (
(d.value > 0 && posY.start < y0) || (d.value < 0 && y0 < posY.start)
)) {
posY.start = y0;
}

posY.start -= (y0 - offset);

points = [
Expand Down
45 changes: 45 additions & 0 deletions test/shape/candelstick-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {expect} from "chai";
import util from "../assets/util";
import {isArray} from "../../src/module/util";
import {$CANDLESTICK, $COMMON} from "../../src/config/classes";
import { line } from "d3-shape";
import { brushY } from "d3-brush";

describe("SHAPE CANDLESTICK", () => {
let chart;
Expand Down Expand Up @@ -153,6 +155,49 @@ describe("SHAPE CANDLESTICK", () => {
});
});

describe("rotated axis", () => {
before(() => {
args = {
data: {
columns: [
["data1",
{open: 100, high: 140, low: -40, close: -20}
]
],
type: "candlestick",
labels: true
},
axis: {
rotated: true,
x: {
type: "category"
}
},
grid: {
y: {
show: true
}
}
};
});

it("should rendered correctly.", () => {
const {$el: {candlestick}, scale: {y}} = chart.internal;
const data = chart.data("data1")[0].values[0].value;

const line = candlestick.select("line");
const path = candlestick.select("path").attr("d");

// check line position
expect(+line.attr("x1")).to.be.equal(y(data.low));
expect(+line.attr("x2")).to.be.equal(y(data.high));

// check path position
expect(path.indexOf(y(data.close)) > -1).to.be.true;
expect(path.indexOf(y(data.open)) > -1).to.be.true;
});
});

describe("candlestick + combination", () => {
before(() => {
args = {
Expand Down

0 comments on commit 17f5058

Please sign in to comment.