Skip to content

Commit adf5a1d

Browse files
authored
fix label offset calculation when labelPosition is horizontal (#379)
* fix label offset calc when labelPosition is horizontal * revert docs changes
1 parent ee8fbf1 commit adf5a1d

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

src/components/graph/graph.builder.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,14 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
196196
const t = 1 / transform;
197197
const nodeSize = node.size || config.node.size;
198198

199-
let offset;
200199
const isSizeNumericValue = typeof nodeSize !== "object";
201-
200+
let offset = 0;
202201
if (isSizeNumericValue) {
203202
offset = nodeSize;
204203
} else if (labelPosition === "top" || labelPosition === "bottom") {
205204
offset = nodeSize.height;
206-
} else {
207-
nodeSize.width;
205+
} else if (labelPosition === "right" || labelPosition === "left") {
206+
offset = nodeSize.width;
208207
}
209208

210209
const fontSize = node.fontSize || config.node.fontSize;

src/components/node/node.helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function buildSvgSymbol(size = CONST.DEFAULT_NODE_SIZE, symbolTypeDesc = CONST.S
6161

6262
/**
6363
* return dx, dy, and potentially alignmentBaseline and textAnchor props to put label in correct position relative to node
64-
* @param {number | undefined} dx - default computed offset of label to the right of the node
64+
* @param {number | undefined} dx - default computed offset of label
6565
* @param {'left' | 'right' | 'top' | 'bottom' | 'center' | undefined} labelPosition - user specified position of label relative to node
6666
* @returns {{dx: string, dy: string} | {dx: string, dy: string, textAnchor: string, dominantBaseline: string}}
6767
* props to put text svg for label in correct spot. default case returns just dx and dy, without textAnchor and dominantBaseline

test/graph/graph.builder.spec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,61 @@ describe("Graph Helper", () => {
188188
expect(props.fontSize).toEqual(6);
189189
});
190190
});
191+
describe("when node.size is numeric value", () => {
192+
test("should calculate label offset correctly", () => {
193+
const props = graphHelper.buildNodeProps(
194+
{ ...that.node, fontSize: 1, size: 100 },
195+
that.config,
196+
undefined,
197+
undefined,
198+
undefined,
199+
1
200+
);
201+
202+
expect(props.dx).toEqual(3.5);
203+
});
204+
});
205+
describe("when node.size is object and labelPosition is top", () => {
206+
test("should calculate label offset correctly", () => {
207+
const props = graphHelper.buildNodeProps(
208+
{ ...that.node, fontSize: 1, size: { height: 100, width: 100 }, labelPosition: "top" },
209+
that.config,
210+
undefined,
211+
undefined,
212+
undefined,
213+
1
214+
);
215+
216+
expect(props.dx).toEqual(3.5);
217+
});
218+
});
219+
describe("when node.size is object and labelPosition is right", () => {
220+
test("should calculate label offset correctly", () => {
221+
const props = graphHelper.buildNodeProps(
222+
{ ...that.node, fontSize: 1, size: { height: 100, width: 100 }, labelPosition: "right" },
223+
that.config,
224+
undefined,
225+
undefined,
226+
undefined,
227+
1
228+
);
229+
230+
expect(props.dx).toEqual(3.5);
231+
});
232+
});
233+
describe("when node.size is object and labelPosition is center", () => {
234+
test("should calculate label offset correctly", () => {
235+
const props = graphHelper.buildNodeProps(
236+
{ ...that.node, fontSize: 1, size: { height: 100, width: 100 }, labelPosition: "center" },
237+
that.config,
238+
undefined,
239+
undefined,
240+
undefined,
241+
1
242+
);
243+
244+
expect(props.dx).toEqual(2.5);
245+
});
246+
});
191247
});
192248
});

0 commit comments

Comments
 (0)