11package net.youapps.calcyou.ui.screens.graphing
22
33import androidx.compose.ui.geometry.Offset
4- import androidx.compose.ui.geometry.Size
54import androidx.compose.ui.graphics.Color
65import androidx.compose.ui.graphics.drawscope.DrawScope
76import androidx.compose.ui.text.TextMeasurer
@@ -46,7 +45,7 @@ fun DrawScope.drawGridLines(window: Window, lineWidth: Float, gridLinesColor: Co
4645
4746internal 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