Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe("getLabelPosition", () => {
};
const labelLocation = "onAxis";
const expected = [
[400, 400 + 1.25 * fontSize], // X Label at [Right edge of the graph, vertical center of the graph]
[-1.5 * fontSize, -2 * fontSize], // Y Label at [Horizontal center of the graph, 2x fontSize above the top edge]
[400, 400 + 1.25 * fontSize],
[-1.5 * fontSize, -2 * fontSize],
];

expect(getLabelPosition(graphInfo, labelLocation, [1, 1])).toEqual(
Expand All @@ -79,8 +79,8 @@ describe("getLabelPosition", () => {
};
const labelLocation = "onAxis";
const expected = [
[400, -2 * fontSize], // X Label at [Right edge of the graph, vertical center of the graph]
[400 + 1.25 * fontSize, -2 * fontSize], // Y Label at [Horizontal center of the graph, 2x fontSize above the top edge]
[400, -2 * fontSize],
[400 + 1.25 * fontSize, -2 * fontSize],
];

expect(getLabelPosition(graphInfo, labelLocation, [1, 1])).toEqual(
Expand All @@ -99,8 +99,8 @@ describe("getLabelPosition", () => {
};
const labelLocation = "alongEdge";
const expected = [
[200, 400 + fontSize * 1.5], // X Label at [Horizontal center of the graph, 1x fontSize below the bottom edge]
[-fontSize * 1.5, 200 - fontSize], // Y label at [1x fontSize to the left of the left edge, vertical center of the graph]
[200, 400 + fontSize * 1.5],
[-fontSize * 1.25, 200 - fontSize],
];

expect(getLabelPosition(graphInfo, labelLocation, [1, 1])).toEqual(
Expand All @@ -119,8 +119,8 @@ describe("getLabelPosition", () => {
};
const labelLocation = "alongEdge";
const expected = [
[200, 400 + fontSize * 1.5], // X Label at [Horizontal center of the graph, 1x fontSize below the bottom edge]
[-fontSize * 1.5, 200 - fontSize], // Y label at [1x fontSize to the left of the left edge, vertical center of the graph]
[200, 400 + fontSize * 1.5],
[-fontSize * 1.25, 200 - fontSize],
];

expect(getLabelPosition(graphInfo, labelLocation, [1, 1])).toEqual(
Expand All @@ -139,8 +139,8 @@ describe("getLabelPosition", () => {
};
const labelLocation = "alongEdge";
const expected = [
[200, 400 + 3 * fontSize], // X Label at [Horizontal center of the graph, 3x fontSize below the bottom edge]
[-3 * fontSize, 200 - fontSize], // Y label at [3x fontSize to the left of the left edge, vertical center of the graph]
[200, 400 + 3 * fontSize],
[-2.75 * fontSize, 200 - fontSize],
];

expect(getLabelPosition(graphInfo, labelLocation, [1, 1])).toEqual(
Expand All @@ -160,8 +160,8 @@ describe("getLabelPosition", () => {
};
const labelLocation = "alongEdge";
const expected = [
[200, 400 + 3 * fontSize], // X Label at [Horizontal center of the graph, 3x fontSize below the bottom edge]
[-3 * fontSize, 200 - fontSize], // Y label at [3x fontSize to the left of the left edge, vertical center of the graph]
[200, 400 + 3 * fontSize],
[-2.75 * fontSize, 200 - fontSize],
];

expect(getLabelPosition(graphInfo, labelLocation, [1, 1])).toEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ export const getLabelPosition = (
? [0, fontSize * 3] // Move the label down by 2 font sizes if the y-axis min is positive
: [0, fontSize * 1.5]; // Move the label down by 1.5 font sizes if the y-axis min is negative

// Determine if the x-axis min is negative and close to zero.
const isCloseToZero =
graphInfo.range[X][MIN] < 0 && graphInfo.range[X][MIN] > -0.05;
// Determine if the x-axis min is negative and relatively close to zero, based on the scale of the x-axis range.
const isRelativelyCloseToZero =
graphInfo.range[X][MIN] < 0 &&
Math.abs(graphInfo.range[X][MIN]) <
(graphInfo.range[X][MAX] - graphInfo.range[X][MIN]) * 0.05;
Copy link
Contributor

Choose a reason for hiding this comment

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

Yessss


// Determine if the tick labels extend beyond the left edge of the graph, either
// because the x-axis is wholly positive or because the x-axis min is negative and close to zero.
const needsExtraSpacing = graphInfo.range[X][MIN] >= 0 || isCloseToZero;
const needsExtraSpacing =
graphInfo.range[X][MIN] >= 0 || isRelativelyCloseToZero;

// When tick labels extend beyond the left edge of the graph, we need to account for their
// width to prevent the main axis label from overlapping with them.
Expand Down Expand Up @@ -140,6 +143,13 @@ export const getLabelPosition = (
(graphInfo.range[Y][MIN] + graphInfo.range[Y][MAX]) / 2,
];

// If we're VERY close to zero, we want to account for the strange grid offset to avoid
// the y-axis label from getting too far from the tick labels.
if (isRelativelyCloseToZero) {
yAxisLabelLocation[X] =
yAxisLabelLocation[X] - graphInfo.range[X][MIN];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

YESSSSS


// Convert the Vector2 coordinates to pixel coordinates and add the offsets
const xLabel = vec.add(
pointToPixel(xAxisLabelLocation, graphInfo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,21 @@ export const MafsWithLabelsAlongEdgeWithCloseToZeroXMin: Story = {
},
};

export const MafsWithLabelsAlongEdgeWithCloseToZeroXMinMultipliedBy1000: Story =
{
args: {
question: interactiveGraphQuestionBuilder()
.withXRange(-30, 840)
.withYRange(-2.8, 63)
.withTickStep(200, 10)
.withGridStep(50, 5)
.withSnapStep(25, 2)
.withAxisLabels("Time (seconds)", "Distance (meters)")
.withLabelLocation("alongEdge")
.build(),
},
};

export const MafsWithLabelsAlongEdgeZoomed: Story = {
args: {
question: interactiveGraphQuestionBuilder()
Expand Down
Loading