Skip to content

Commit b480bcc

Browse files
committed
graph aspect ratio
1 parent df4387a commit b480bcc

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/main/java/com/cleanroommc/modularui/drawable/graph/GraphAxis.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ void applyPadding(GraphView graphView) {
102102
off += textRenderer.getFontHeight() + AXIS_LABEL_OFFSET;
103103
}
104104
graphView.sx0 += off;
105-
graphView.sx1 -= off;
106105
}
107106
}
108107

src/main/java/com/cleanroommc/modularui/drawable/graph/GraphDrawable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,9 @@ public GraphDrawable minorGridLineColor(int color) {
252252
this.minorGridLineColor = color;
253253
return this;
254254
}
255+
256+
public GraphDrawable graphAspectRatio(float aspectRatio) {
257+
this.view.setAspectRatio(aspectRatio);
258+
return this;
259+
}
255260
}

src/main/java/com/cleanroommc/modularui/drawable/graph/GraphView.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
@ApiStatus.Experimental
88
public class GraphView {
99

10+
float aspectRatio = 0;
1011
// screen rectangle
1112
float sx0, sy0, sx1, sy1;
1213
// graph rectangle
@@ -15,15 +16,19 @@ public class GraphView {
1516
float zeroX, zeroY;
1617

1718
void postResize() {
18-
float w = sx1 - sx0, h = sy1 - sy0;
19-
if (w > h) {
20-
float d = w - h;
21-
sx0 += d / 2;
22-
sx1 -= d / 2;
23-
} else if (h > w) {
24-
float d = h - w;
25-
sy0 += d / 2;
26-
sy1 -= d / 2;
19+
if (this.aspectRatio > 0) {
20+
float w = sx1 - sx0, h = sy1 - sy0;
21+
float properW = this.aspectRatio * h;
22+
if (w > properW) {
23+
float d = w - properW;
24+
sx0 += d / 2;
25+
sx1 -= d / 2;
26+
} else if (w < properW) {
27+
float properH = w / this.aspectRatio;
28+
float d = h - properH;
29+
sy0 += d / 2;
30+
sy1 -= d / 2;
31+
}
2732
}
2833
this.zeroX = g2sX(0);
2934
this.zeroY = g2sY(0);
@@ -93,4 +98,12 @@ public float getZeroX() {
9398
public float getZeroY() {
9499
return zeroY;
95100
}
101+
102+
public void setAspectRatio(float aspectRatio) {
103+
this.aspectRatio = aspectRatio;
104+
}
105+
106+
public float getAspectRatio() {
107+
return aspectRatio;
108+
}
96109
}

0 commit comments

Comments
 (0)