Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DoubleXLabelAxisRenderer extends XAxisRenderer {
- private IAxisValueFormatter topValueFormatter;
- public DoubleXLabelAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer transformer, IAxisValueFormatter topValueFormatter) {
- super(viewPortHandler, xAxis, transformer);
- this.topValueFormatter = topValueFormatter;
- }
- @Override
- public void renderAxisLabels(Canvas c) {
- if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled())
- return;
- float yoffset = mXAxis.getYOffset();
- mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
- mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
- mAxisLabelPaint.setColor(mXAxis.getTextColor());
- MPPointF pointF = MPPointF.getInstance(0, 0);
- if (mXAxis.getPosition() == XAxis.XAxisPosition.TOP) {
- pointF.x = 0.5f;
- pointF.y = 1.0f;
- drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF);
- } else if (mXAxis.getPosition() == XAxis.XAxisPosition.TOP_INSIDE) {
- pointF.x = 0.5f;
- pointF.y = 1.0f;
- drawLabels(c, mViewPortHandler.contentTop() + yoffset + mXAxis.mLabelRotatedHeight, pointF);
- } else if (mXAxis.getPosition() == XAxis.XAxisPosition.BOTTOM) {
- pointF.x = 0.5f;
- pointF.y = 0.0f;
- drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF);
- } else if (mXAxis.getPosition() == XAxis.XAxisPosition.BOTTOM_INSIDE) {
- pointF.x = 0.5f;
- pointF.y = 0.0f;
- drawLabels(c, mViewPortHandler.contentBottom() - yoffset - mXAxis.mLabelRotatedHeight, pointF);
- } else { // BOTH SIDED
- pointF.x = 0.5f;
- pointF.y = 1.0f;
- drawLabelsTop(c, mViewPortHandler.contentTop() - yoffset, pointF);
- pointF.x = 0.5f;
- pointF.y = 0.0f;
- drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF);
- }
- MPPointF.recycleInstance(pointF);
- }
- private void drawLabelsTop(Canvas c, float pos, MPPointF anchor) {
- final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
- boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();
- float[] positions = new float[mXAxis.mEntryCount * 2];
- for (int i = 0; i < positions.length; i += 2) {
- // only fill x values
- if (centeringEnabled) {
- positions[i] = mXAxis.mCenteredEntries[i / 2];
- } else {
- positions[i] = mXAxis.mEntries[i / 2];
- }
- }
- mTrans.pointValuesToPixel(positions);
- for (int i = 0; i < positions.length; i += 2) {
- float x = positions[i];
- if (mViewPortHandler.isInBoundsX(x)) {
- String label = topValueFormatter.getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
- if (mXAxis.isAvoidFirstLastClippingEnabled()) {
- // avoid clipping of the last
- if (i == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1) {
- float width = Utils.calcTextWidth(mAxisLabelPaint, label);
- if (width > mViewPortHandler.offsetRight() * 2
- && x + width > mViewPortHandler.getChartWidth())
- x -= width / 2;
- // avoid clipping of the first
- } else if (i == 0) {
- float width = Utils.calcTextWidth(mAxisLabelPaint, label);
- x += width / 2;
- }
- }
- drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment