Guest User

Untitled

a guest
Oct 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. public class DoubleXLabelAxisRenderer extends XAxisRenderer {
  2.  
  3. private IAxisValueFormatter topValueFormatter;
  4.  
  5. public DoubleXLabelAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer transformer, IAxisValueFormatter topValueFormatter) {
  6. super(viewPortHandler, xAxis, transformer);
  7. this.topValueFormatter = topValueFormatter;
  8. }
  9.  
  10. @Override
  11. public void renderAxisLabels(Canvas c) {
  12.  
  13. if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled())
  14. return;
  15.  
  16. float yoffset = mXAxis.getYOffset();
  17.  
  18. mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
  19. mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
  20. mAxisLabelPaint.setColor(mXAxis.getTextColor());
  21.  
  22. MPPointF pointF = MPPointF.getInstance(0, 0);
  23. if (mXAxis.getPosition() == XAxis.XAxisPosition.TOP) {
  24. pointF.x = 0.5f;
  25. pointF.y = 1.0f;
  26. drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF);
  27.  
  28. } else if (mXAxis.getPosition() == XAxis.XAxisPosition.TOP_INSIDE) {
  29. pointF.x = 0.5f;
  30. pointF.y = 1.0f;
  31. drawLabels(c, mViewPortHandler.contentTop() + yoffset + mXAxis.mLabelRotatedHeight, pointF);
  32.  
  33. } else if (mXAxis.getPosition() == XAxis.XAxisPosition.BOTTOM) {
  34. pointF.x = 0.5f;
  35. pointF.y = 0.0f;
  36. drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF);
  37.  
  38. } else if (mXAxis.getPosition() == XAxis.XAxisPosition.BOTTOM_INSIDE) {
  39. pointF.x = 0.5f;
  40. pointF.y = 0.0f;
  41. drawLabels(c, mViewPortHandler.contentBottom() - yoffset - mXAxis.mLabelRotatedHeight, pointF);
  42.  
  43. } else { // BOTH SIDED
  44. pointF.x = 0.5f;
  45. pointF.y = 1.0f;
  46. drawLabelsTop(c, mViewPortHandler.contentTop() - yoffset, pointF);
  47. pointF.x = 0.5f;
  48. pointF.y = 0.0f;
  49. drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF);
  50. }
  51. MPPointF.recycleInstance(pointF);
  52. }
  53.  
  54. private void drawLabelsTop(Canvas c, float pos, MPPointF anchor) {
  55.  
  56. final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
  57. boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();
  58.  
  59. float[] positions = new float[mXAxis.mEntryCount * 2];
  60.  
  61. for (int i = 0; i < positions.length; i += 2) {
  62.  
  63. // only fill x values
  64. if (centeringEnabled) {
  65. positions[i] = mXAxis.mCenteredEntries[i / 2];
  66. } else {
  67. positions[i] = mXAxis.mEntries[i / 2];
  68. }
  69. }
  70.  
  71. mTrans.pointValuesToPixel(positions);
  72.  
  73. for (int i = 0; i < positions.length; i += 2) {
  74.  
  75. float x = positions[i];
  76.  
  77. if (mViewPortHandler.isInBoundsX(x)) {
  78.  
  79. String label = topValueFormatter.getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
  80.  
  81. if (mXAxis.isAvoidFirstLastClippingEnabled()) {
  82.  
  83. // avoid clipping of the last
  84. if (i == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1) {
  85. float width = Utils.calcTextWidth(mAxisLabelPaint, label);
  86.  
  87. if (width > mViewPortHandler.offsetRight() * 2
  88. && x + width > mViewPortHandler.getChartWidth())
  89. x -= width / 2;
  90.  
  91. // avoid clipping of the first
  92. } else if (i == 0) {
  93.  
  94. float width = Utils.calcTextWidth(mAxisLabelPaint, label);
  95. x += width / 2;
  96. }
  97. }
  98.  
  99. drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees);
  100. }
  101. }
  102. }
  103. }
Add Comment
Please, Sign In to add comment