@@ -60,10 +60,7 @@ private static class LineData {
6060 private int mNextLineId = 1 ;
6161
6262 private float [] mGridLinesX ;
63- private boolean mShowGridLabelsX = true ;
64-
6563 private float [] mGridLinesY ;
66- private boolean mShowGridLabelsY = true ;
6764
6865 private Paint mGridPaintX ;
6966 private Paint mGridTextPaintX ;
@@ -140,15 +137,13 @@ public void setYRange(float minY, float maxY) {
140137 postInvalidate ();
141138 }
142139
143- public void setGridLinesX (float [] xValues , boolean showLabels ) {
140+ public void setGridLinesX (float [] xValues ) {
144141 mGridLinesX = xValues ;
145- mShowGridLabelsX = showLabels ;
146142 postInvalidate ();
147143 }
148144
149- public void setGridLinesY (float [] yValues , boolean showLabels ) {
145+ public void setGridLinesY (float [] yValues ) {
150146 mGridLinesY = yValues ;
151- mShowGridLabelsY = showLabels ;
152147 postInvalidate ();
153148 }
154149
@@ -195,21 +190,21 @@ private float mapYToView(float y, float scaleY, float offsetY) {
195190 return (mapped * scaleY ) + offsetY ;
196191 }
197192
198- private float mapXToView (float xVal , float graphWidth ) {
193+ private float mapXToView (float xVal , float graphWidth , float paddingLeft ) {
199194 if (mLogScaleX ) {
200195 if (mMinX <= 0 || mMaxX <= mMinX ) {
201- return 0.0f ;
196+ return paddingLeft ;
202197 }
203198 double logMin = Math .log (mMinX );
204199 double logMax = Math .log (mMaxX );
205200 double logX = Math .log (xVal );
206- return (float ) (((logX - logMin ) / (logMax - logMin )) * graphWidth );
201+ return (float ) (((logX - logMin ) / (logMax - logMin )) * graphWidth ) + paddingLeft ;
207202 } else {
208203 float rangeX = mMaxX - mMinX ;
209204 if (rangeX <= 0 ) {
210- return 0.0f ;
205+ return paddingLeft ;
211206 }
212- return ((xVal - mMinX ) / rangeX ) * graphWidth ;
207+ return ((xVal - mMinX ) / rangeX ) * graphWidth + paddingLeft ;
213208 }
214209 }
215210
@@ -230,8 +225,10 @@ protected void onDraw(Canvas canvas) {
230225 }
231226
232227 float paddingBottom = 60.0f ;
233- float paddingTop = 40.0f ;
234- float graphWidth = width ;
228+ float paddingTop = 60.0f ;
229+ float paddingLeft = 100.0f ;
230+ float paddingRight = 100.0f ;
231+ float graphWidth = width - paddingLeft - paddingRight ;
235232 float graphHeight = height - paddingBottom - paddingTop ;
236233 float offsetY = paddingTop + (graphHeight / 2.0f );
237234 float scaleY = -0.90f * (graphHeight / 2.0f );
@@ -253,7 +250,7 @@ protected void onDraw(Canvas canvas) {
253250 if (xVal < mMinX || xVal > mMaxX ) {
254251 continue ;
255252 }
256- float x = mapXToView (xVal , graphWidth );
253+ float x = mapXToView (xVal , graphWidth , paddingLeft );
257254 float y = mapYToView (line .yValues [i ], scaleY , offsetY );
258255 canvas .drawLine (x , yBottom , x , y , line .paint );
259256 }
@@ -265,7 +262,7 @@ protected void onDraw(Canvas canvas) {
265262 if (xVal < mMinX || xVal > mMaxX ) {
266263 continue ;
267264 }
268- float x1 = mapXToView (xVal , graphWidth );
265+ float x1 = mapXToView (xVal , graphWidth , paddingLeft );
269266 float y1 = mapYToView (line .yValues [i ], scaleY , offsetY );
270267 if (x0 != -1 ) {
271268 canvas .drawLine (x0 , y0 , x1 , y1 , line .paint );
@@ -280,13 +277,14 @@ protected void onDraw(Canvas canvas) {
280277 if (mGridLinesY != null ) {
281278 for (float yVal : mGridLinesY ) {
282279 float yPos = mapYToView (yVal , scaleY , offsetY );
283- canvas .drawLine (0 , yPos , width , yPos , mGridPaintY );
284- if (mShowGridLabelsY ) {
285- String label =
286- mUnitY .isEmpty () ? String .format (Locale .getDefault (), "%.0f" , yVal )
287- : String .format (Locale .getDefault (), "%.0f %s" , yVal , mUnitY );
288- canvas .drawText (label , 10 , yPos - 10 , mGridTextPaintY );
289- }
280+ canvas .drawLine (paddingLeft , yPos , width - paddingRight , yPos , mGridPaintY );
281+ String label = String .format (Locale .getDefault (), "%.0f" , yVal );
282+ mGridTextPaintY .setTextAlign (Paint .Align .LEFT );
283+ canvas .drawText (label , 10 , yPos - 10 , mGridTextPaintY );
284+ }
285+ if (!mUnitY .isEmpty ()) {
286+ mGridTextPaintY .setTextAlign (Paint .Align .LEFT );
287+ canvas .drawText (mUnitY , 10 , 30 , mGridTextPaintY );
290288 }
291289 }
292290
@@ -296,15 +294,20 @@ protected void onDraw(Canvas canvas) {
296294 if (xVal < mMinX || xVal > mMaxX ) {
297295 continue ;
298296 }
299- float x = mapXToView (xVal , graphWidth );
300- canvas .drawLine (x , 0 , x , height - paddingBottom , mGridPaintX );
301- if (mShowGridLabelsX ) {
302- String label =
303- mUnitX .isEmpty () ? String .format (Locale .getDefault (), "%.0f" , xVal )
304- : String .format (Locale .getDefault (), "%.0f %s" , xVal , mUnitX );
305- float xOffset = xVal >= mMaxX ? -120 : 5 ;
306- canvas .drawText (label , x + xOffset , height - 15 , mGridTextPaintX );
297+ float x = mapXToView (xVal , graphWidth , paddingLeft );
298+ canvas .drawLine (x , paddingTop , x , height - paddingBottom , mGridPaintX );
299+ String label ;
300+ if (Math .abs (xVal ) >= 1000 && xVal % 1000 == 0 ) {
301+ label = String .format (Locale .getDefault (), "%.0fk" , xVal / 1000 );
302+ } else {
303+ label = String .format (Locale .getDefault (), "%.0f" , xVal );
307304 }
305+ mGridTextPaintX .setTextAlign (Paint .Align .LEFT );
306+ canvas .drawText (label , x + 5 , height - 15 , mGridTextPaintX );
307+ }
308+ if (!mUnitX .isEmpty ()) {
309+ mGridTextPaintX .setTextAlign (Paint .Align .RIGHT );
310+ canvas .drawText (mUnitX , width - 5 , height - 15 , mGridTextPaintX );
308311 }
309312 }
310313 }
0 commit comments