Skip to content

Commit 65d873c

Browse files
committed
fix(graphing): clipped tick values on smaller resolution devices
1 parent 20bf107 commit 65d873c

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

app/src/main/java/net/youapps/calcyou/ui/screens/graphing/GraphingUtils.kt

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.youapps.calcyou.ui.screens.graphing
22

33
import androidx.compose.ui.geometry.Offset
4-
import androidx.compose.ui.geometry.Size
54
import androidx.compose.ui.graphics.Color
65
import androidx.compose.ui.graphics.drawscope.DrawScope
76
import androidx.compose.ui.text.TextMeasurer
@@ -46,7 +45,7 @@ fun DrawScope.drawGridLines(window: Window, lineWidth: Float, gridLinesColor: Co
4645

4746
internal fun Float.toDisplayString(): String {
4847
return when {
49-
(this % 1f == 0f) && (this in 0.0001f..10000f) -> {
48+
(this % 1f == 0f) && (abs(this) in 0.0001f..10000f) -> {
5049
this.toInt().toString()
5150
}
5251

@@ -108,20 +107,27 @@ fun DrawScope.drawAxes(
108107
// Offset(xDraw, yDraw - halfTickLength),
109108
// lineWidth
110109
// )
111-
val textWidth = 200 / canvasScale
112-
val textHeight = 40 / canvasScale
110+
111+
val textSize = 40 / canvasScale
113112
val textPadding = 20 / canvasScale
114-
drawText(
115-
textMeasurer,
116-
xDisplayValue,
117-
topLeft = Offset(xDraw - textWidth / 2, yDraw + textPadding),
118-
size = Size(textWidth, textHeight),
113+
114+
val textLayoutResult = textMeasurer.measure(
115+
text = xDisplayValue,
119116
style = TextStyle(
120117
color = axesColor,
118+
fontSize = textSize.toSp(),
121119
textAlign = TextAlign.Center,
122120
fontWeight = FontWeight.Medium
123121
)
124122
)
123+
124+
drawText(
125+
textLayoutResult,
126+
topLeft = Offset(
127+
xDraw - textLayoutResult.size.width / 2,
128+
yDraw + textPadding),
129+
130+
)
125131
}
126132

127133
val yTickRange = IntRange(
@@ -143,15 +149,24 @@ fun DrawScope.drawAxes(
143149
// Offset(xDraw + halfTickLength, yDraw),
144150
// lineWidth
145151
// )
146-
val textWidth = 200 / canvasScale
147-
val textHeight = 40 / canvasScale
152+
153+
val textSize = 40 / canvasScale
148154
val textPadding = 20 / canvasScale
149-
drawText(
150-
textMeasurer,
155+
156+
val textLayoutResult = textMeasurer.measure(
151157
yDisplayValue,
152-
topLeft = Offset(xDraw + textPadding, yDraw - textHeight / 2),
153-
size = Size(textWidth, textHeight),
154-
style = TextStyle(color = axesColor, fontWeight = FontWeight.Medium)
158+
style = TextStyle(
159+
color = axesColor,
160+
fontSize = textSize.toSp(),
161+
fontWeight = FontWeight.Medium
162+
)
163+
)
164+
165+
drawText(
166+
textLayoutResult,
167+
topLeft = Offset(
168+
xDraw + textPadding,
169+
yDraw - textLayoutResult.size.height / 2),
155170
)
156171
}
157172
}

0 commit comments

Comments
 (0)