|
10 | 10 | import android.graphics.Paint; |
11 | 11 | import android.graphics.Paint.Style; |
12 | 12 | import android.graphics.PointF; |
| 13 | +import android.graphics.RectF; |
13 | 14 | import android.util.AttributeSet; |
14 | 15 | import android.util.Log; |
15 | 16 | import android.view.MotionEvent; |
16 | 17 |
|
| 18 | +import com.github.mikephil.charting.components.Legend; |
17 | 19 | import com.github.mikephil.charting.components.Legend.LegendPosition; |
18 | 20 | import com.github.mikephil.charting.components.XAxis.XAxisPosition; |
19 | 21 | import com.github.mikephil.charting.components.YAxis; |
@@ -299,9 +301,13 @@ protected void prepareValuePxMatrix() { |
299 | 301 | Log.i(LOG_TAG, "Preparing Value-Px Matrix, xmin: " + mXAxis.mAxisMinimum + ", xmax: " |
300 | 302 | + mXAxis.mAxisMaximum + ", xdelta: " + mXAxis.mAxisRange); |
301 | 303 |
|
302 | | - mRightAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, mXAxis.mAxisRange, mAxisRight.mAxisRange, |
| 304 | + mRightAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, |
| 305 | + mXAxis.mAxisRange, |
| 306 | + mAxisRight.mAxisRange, |
303 | 307 | mAxisRight.mAxisMinimum); |
304 | | - mLeftAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, mXAxis.mAxisRange, mAxisLeft.mAxisRange, |
| 308 | + mLeftAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, |
| 309 | + mXAxis.mAxisRange, |
| 310 | + mAxisLeft.mAxisRange, |
305 | 311 | mAxisLeft.mAxisMinimum); |
306 | 312 | } |
307 | 313 |
|
@@ -355,56 +361,103 @@ protected void calcMinMax() { |
355 | 361 | .RIGHT)); |
356 | 362 | } |
357 | 363 |
|
| 364 | + protected void calculateLegendOffsets(RectF offsets) { |
| 365 | + |
| 366 | + offsets.left = 0.f; |
| 367 | + offsets.right = 0.f; |
| 368 | + offsets.top = 0.f; |
| 369 | + offsets.bottom = 0.f; |
| 370 | + |
| 371 | + // setup offsets for legend |
| 372 | + if (mLegend != null && mLegend.isEnabled() && !mLegend.isDrawInsideEnabled()) { |
| 373 | + switch (mLegend.getOrientation()) { |
| 374 | + case VERTICAL: |
| 375 | + |
| 376 | + switch (mLegend.getHorizontalAlignment()) { |
| 377 | + case LEFT: |
| 378 | + offsets.left += Math.min(mLegend.mNeededWidth, |
| 379 | + mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()) |
| 380 | + + mLegend.getXOffset(); |
| 381 | + break; |
| 382 | + |
| 383 | + case RIGHT: |
| 384 | + offsets.right += Math.min(mLegend.mNeededWidth, |
| 385 | + mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()) |
| 386 | + + mLegend.getXOffset(); |
| 387 | + break; |
| 388 | + |
| 389 | + case CENTER: |
| 390 | + |
| 391 | + switch (mLegend.getVerticalAlignment()) { |
| 392 | + case TOP: |
| 393 | + offsets.top += Math.min(mLegend.mNeededHeight, |
| 394 | + mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) |
| 395 | + + mLegend.getYOffset(); |
| 396 | + |
| 397 | + if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled()) |
| 398 | + offsets.top += getXAxis().mLabelRotatedHeight; |
| 399 | + break; |
| 400 | + |
| 401 | + case BOTTOM: |
| 402 | + offsets.bottom += Math.min(mLegend.mNeededHeight, |
| 403 | + mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) |
| 404 | + + mLegend.getYOffset(); |
| 405 | + |
| 406 | + if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled()) |
| 407 | + offsets.bottom += getXAxis().mLabelRotatedHeight; |
| 408 | + break; |
| 409 | + |
| 410 | + default: |
| 411 | + break; |
| 412 | + } |
| 413 | + } |
| 414 | + |
| 415 | + break; |
| 416 | + |
| 417 | + case HORIZONTAL: |
| 418 | + |
| 419 | + switch (mLegend.getVerticalAlignment()) { |
| 420 | + case TOP: |
| 421 | + offsets.top += Math.min(mLegend.mNeededHeight, |
| 422 | + mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) |
| 423 | + + mLegend.getYOffset(); |
| 424 | + |
| 425 | + if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled()) |
| 426 | + offsets.top += getXAxis().mLabelRotatedHeight; |
| 427 | + break; |
| 428 | + |
| 429 | + case BOTTOM: |
| 430 | + offsets.bottom += Math.min(mLegend.mNeededHeight, |
| 431 | + mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) |
| 432 | + + mLegend.getYOffset(); |
| 433 | + |
| 434 | + if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled()) |
| 435 | + offsets.bottom += getXAxis().mLabelRotatedHeight; |
| 436 | + break; |
| 437 | + |
| 438 | + default: |
| 439 | + break; |
| 440 | + } |
| 441 | + break; |
| 442 | + } |
| 443 | + } |
| 444 | + } |
| 445 | + |
| 446 | + private RectF mOffsetsBuffer = new RectF(); |
| 447 | + |
358 | 448 | @Override |
359 | 449 | public void calculateOffsets() { |
360 | 450 |
|
361 | 451 | if (!mCustomViewPortEnabled) { |
362 | 452 |
|
363 | 453 | float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f; |
364 | 454 |
|
365 | | - // setup offsets for legend |
366 | | - if (mLegend != null && mLegend.isEnabled()) { |
367 | | - |
368 | | - if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART |
369 | | - || mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) { |
370 | | - |
371 | | - offsetRight += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth() |
372 | | - * mLegend.getMaxSizePercent()) |
373 | | - + mLegend.getXOffset() * 2f; |
374 | | - |
375 | | - } else if (mLegend.getPosition() == LegendPosition.LEFT_OF_CHART |
376 | | - || mLegend.getPosition() == LegendPosition.LEFT_OF_CHART_CENTER) { |
377 | | - |
378 | | - offsetLeft += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth() |
379 | | - * mLegend.getMaxSizePercent()) |
380 | | - + mLegend.getXOffset() * 2f; |
| 455 | + calculateLegendOffsets(mOffsetsBuffer); |
381 | 456 |
|
382 | | - } else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT |
383 | | - || mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT |
384 | | - || mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) { |
385 | | - |
386 | | - // It's possible that we do not need this offset anymore as it |
387 | | - // is available through the extraOffsets, but changing it can mean |
388 | | - // changing default visibility for existing apps. |
389 | | - float yOffset = mLegend.mTextHeightMax; |
390 | | - |
391 | | - offsetBottom += Math.min(mLegend.mNeededHeight + yOffset, |
392 | | - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); |
393 | | - |
394 | | - } else if (mLegend.getPosition() == LegendPosition.ABOVE_CHART_LEFT |
395 | | - || mLegend.getPosition() == LegendPosition.ABOVE_CHART_RIGHT |
396 | | - || mLegend.getPosition() == LegendPosition.ABOVE_CHART_CENTER) { |
397 | | - |
398 | | - // It's possible that we do not need this offset anymore as it |
399 | | - // is available through the extraOffsets, but changing it can mean |
400 | | - // changing default visibility for existing apps. |
401 | | - float yOffset = mLegend.mTextHeightMax; |
402 | | - |
403 | | - offsetTop += Math.min(mLegend.mNeededHeight + yOffset, |
404 | | - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); |
405 | | - |
406 | | - } |
407 | | - } |
| 457 | + offsetLeft += mOffsetsBuffer.left; |
| 458 | + offsetTop += mOffsetsBuffer.top; |
| 459 | + offsetRight += mOffsetsBuffer.right; |
| 460 | + offsetBottom += mOffsetsBuffer.bottom; |
408 | 461 |
|
409 | 462 | // offsets for y-labels |
410 | 463 | if (mAxisLeft.needsOffset()) { |
|
0 commit comments