Java Code Examples for com.github.mikephil.charting.utils.Utils#calcTextHeight()

The following examples show how to use com.github.mikephil.charting.utils.Utils#calcTextHeight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XAxisRenderer.java    From android-kline with Apache License 2.0 6 votes vote down vote up
protected void computeSize() {

        String longest = mXAxis.getLongestLabel();

        mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
        mAxisLabelPaint.setTextSize(mXAxis.getTextSize());

        final FSize labelSize = Utils.calcTextSize(mAxisLabelPaint, longest);

        final float labelWidth = labelSize.width;
        final float labelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");

        final FSize labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(
                labelWidth,
                labelHeight,
                mXAxis.getLabelRotationAngle());


        mXAxis.mLabelWidth = Math.round(labelWidth);
        mXAxis.mLabelHeight = Math.round(labelHeight);
        mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width);
        mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);

        FSize.recycleInstance(labelRotatedSize);
        FSize.recycleInstance(labelSize);
    }
 
Example 2
Source File: Legend.java    From android-kline with Apache License 2.0 6 votes vote down vote up
/**
 * returns the maximum height in pixels across all legend labels
 *
 * @param p the paint object used for rendering the text
 * @return
 */
public float getMaximumEntryHeight(Paint p) {

    float max = 0f;

    for (LegendEntry entry : mEntries) {
        String label = entry.label;
        if (label == null) continue;

        float length = (float) Utils.calcTextHeight(p, label);

        if (length > max)
            max = length;
    }

    return max;
}
 
Example 3
Source File: YAxis.java    From android-kline with Apache License 2.0 5 votes vote down vote up
/**
 * This is for HorizontalBarChart vertical spacing.
 *
 * @param p
 * @return
 */
public float getRequiredHeightSpace(Paint p) {

    p.setTextSize(mTextSize);

    String label = getLongestLabel();
    return (float) Utils.calcTextHeight(p, label) + getYOffset() * 2f;
}
 
Example 4
Source File: XAxisRenderer.java    From NetKnight with Apache License 2.0 5 votes vote down vote up
public void computeAxis(float xValMaximumLength, List<String> xValues) {

        mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
        mAxisLabelPaint.setTextSize(mXAxis.getTextSize());

        StringBuilder widthText = new StringBuilder();

        int xValChars = Math.round(xValMaximumLength);

        for (int i = 0; i < xValChars; i++) {
            widthText.append('h');
        }

        final FSize labelSize = Utils.calcTextSize(mAxisLabelPaint, widthText.toString());

        final float labelWidth = labelSize.width;
        final float labelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");

        final FSize labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(
                labelWidth,
                labelHeight,
                mXAxis.getLabelRotationAngle());

        StringBuilder space = new StringBuilder();
        int xValSpaceChars = mXAxis.getSpaceBetweenLabels();

        for (int i = 0; i < xValSpaceChars; i++) {
            space.append('h');
        }

        final FSize spaceSize = Utils.calcTextSize(mAxisLabelPaint, space.toString());

        mXAxis.mLabelWidth = Math.round(labelWidth + spaceSize.width);
        mXAxis.mLabelHeight = Math.round(labelHeight);
        mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width + spaceSize.width);
        mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);

        mXAxis.setValues(xValues);
    }
 
Example 5
Source File: XAxisRenderer.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
public void computeAxis(float xValMaximumLength, List<String> xValues) {

        mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
        mAxisLabelPaint.setTextSize(mXAxis.getTextSize());

        StringBuilder widthText = new StringBuilder();

        int xValChars = Math.round(xValMaximumLength);

        for (int i = 0; i < xValChars; i++) {
            widthText.append('h');
        }

        final FSize labelSize = Utils.calcTextSize(mAxisLabelPaint, widthText.toString());

        final float labelWidth = labelSize.width;
        final float labelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");

        final FSize labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(
                labelWidth,
                labelHeight,
                mXAxis.getLabelRotationAngle());

        StringBuilder space = new StringBuilder();
        int xValSpaceChars = mXAxis.getSpaceBetweenLabels();

        for (int i = 0; i < xValSpaceChars; i++) {
            space.append('h');
        }

        final FSize spaceSize = Utils.calcTextSize(mAxisLabelPaint, space.toString());

        mXAxis.mLabelWidth = Math.round(labelWidth + spaceSize.width);
        mXAxis.mLabelHeight = Math.round(labelHeight);
        mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width + spaceSize.width);
        mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);

        mXAxis.setValues(xValues);
    }
 
Example 6
Source File: YAxisRenderer.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
/**
 * draws the y-axis labels to the screen
 */
@Override
public void renderAxisLabels(Canvas c) {

    if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled()) {
        return;
    }

    float[] positions = getTransformedPositions();

    mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
    mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
    mAxisLabelPaint.setColor(mYAxis.getTextColor());

    float xoffset = mYAxis.getXOffset();
    float yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset();

    AxisDependency dependency = mYAxis.getAxisDependency();
    YAxisLabelPosition labelPosition = mYAxis.getLabelPosition();

    float xPos = 0f;

    if (dependency == AxisDependency.LEFT) {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            mAxisLabelPaint.setTextAlign(Align.RIGHT);
            xPos = mViewPortHandler.offsetLeft() - xoffset;
        } else {
            mAxisLabelPaint.setTextAlign(Align.LEFT);
            xPos = mViewPortHandler.offsetLeft() + xoffset;
        }

    } else {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            mAxisLabelPaint.setTextAlign(Align.LEFT);
            xPos = mViewPortHandler.contentRight() + xoffset;
        } else {
            mAxisLabelPaint.setTextAlign(Align.RIGHT);
            xPos = mViewPortHandler.contentRight() - xoffset;
        }
    }

    drawYLabels(c, xPos, positions, yoffset);
}
 
Example 7
Source File: YAxisRenderer.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
/**
 * Draws the LimitLines associated with this axis to the screen.
 *
 * @param c
 */
@Override
public void renderLimitLines(Canvas c) {

    List<LimitLine> limitLines = mYAxis.getLimitLines();

    if (limitLines == null || limitLines.size() <= 0)
        return;

    float[] pts = mRenderLimitLinesBuffer;
    pts[0] = 0;
    pts[1] = 0;
    Path limitLinePath = mRenderLimitLines;
    limitLinePath.reset();

    for (int i = 0; i < limitLines.size(); i++) {

        LimitLine l = limitLines.get(i);

        if (!l.isEnabled())
            continue;

        int clipRestoreCount = c.save();
        mLimitLineClippingRect.set(mViewPortHandler.getContentRect());
        mLimitLineClippingRect.inset(0.f, -l.getLineWidth());
        c.clipRect(mLimitLineClippingRect);

        mLimitLinePaint.setStyle(Paint.Style.STROKE);
        mLimitLinePaint.setColor(l.getLineColor());
        mLimitLinePaint.setStrokeWidth(l.getLineWidth());
        mLimitLinePaint.setPathEffect(l.getDashPathEffect());

        pts[1] = l.getLimit();

        mTrans.pointValuesToPixel(pts);

        limitLinePath.moveTo(mViewPortHandler.contentLeft(), pts[1]);
        limitLinePath.lineTo(mViewPortHandler.contentRight(), pts[1]);

        c.drawPath(limitLinePath, mLimitLinePaint);
        limitLinePath.reset();
        // c.drawLines(pts, mLimitLinePaint);

        String label = l.getLabel();

        // if drawing the limit-value label is enabled
        if (label != null && !label.equals("")) {

            mLimitLinePaint.setStyle(l.getTextStyle());
            mLimitLinePaint.setPathEffect(null);
            mLimitLinePaint.setColor(l.getTextColor());
            mLimitLinePaint.setTypeface(l.getTypeface());
            mLimitLinePaint.setStrokeWidth(0.5f);
            mLimitLinePaint.setTextSize(l.getTextSize());

            final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
            float xOffset = Utils.convertDpToPixel(4f) + l.getXOffset();
            float yOffset = l.getLineWidth() + labelLineHeight + l.getYOffset();

            final LimitLine.LimitLabelPosition position = l.getLabelPosition();

            if (position == LimitLine.LimitLabelPosition.RIGHT_TOP) {

                mLimitLinePaint.setTextAlign(Align.RIGHT);
                c.drawText(label,
                        mViewPortHandler.contentRight() - xOffset,
                        pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

            } else if (position == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {

                mLimitLinePaint.setTextAlign(Align.RIGHT);
                c.drawText(label,
                        mViewPortHandler.contentRight() - xOffset,
                        pts[1] + yOffset, mLimitLinePaint);

            } else if (position == LimitLine.LimitLabelPosition.LEFT_TOP) {

                mLimitLinePaint.setTextAlign(Align.LEFT);
                c.drawText(label,
                        mViewPortHandler.contentLeft() + xOffset,
                        pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

            } else {

                mLimitLinePaint.setTextAlign(Align.LEFT);
                c.drawText(label,
                        mViewPortHandler.offsetLeft() + xOffset,
                        pts[1] + yOffset, mLimitLinePaint);
            }
        }

        c.restoreToCount(clipRestoreCount);
    }
}
 
Example 8
Source File: BarLineChartBase.java    From Notification-Analyser with MIT License 4 votes vote down vote up
/**
 * draws the y-axis labels to the screen
 */
private void drawYLabels() {

    if (!mDrawYLabels)
        return;

    float[] positions = new float[mYLabels.mEntryCount * 2];

    for (int i = 0; i < positions.length; i += 2) {
        // only fill y values, x values are not needed since the y-labels
        // are
        // static on the x-axis
        positions[i + 1] = mYLabels.mEntries[i / 2];
    }

    transformPointArray(positions);

    mYLabelPaint.setTypeface(mYLabels.getTypeface());
    mYLabelPaint.setTextSize(mYLabels.getTextSize());
    mYLabelPaint.setColor(mYLabels.getTextColor());

    float xoffset = Utils.convertDpToPixel(5f);
    float yoffset = Utils.calcTextHeight(mYLabelPaint, "A") / 2.5f;

    // determine position and draw adequately
    if (mYLabels.getPosition() == YLabelPosition.LEFT) {

        mYLabelPaint.setTextAlign(Align.RIGHT);
        drawYLabels(mOffsetLeft - xoffset, positions, yoffset);

    } else if (mYLabels.getPosition() == YLabelPosition.RIGHT) {

        mYLabelPaint.setTextAlign(Align.LEFT);
        drawYLabels(getWidth() - mOffsetRight + xoffset, positions, yoffset);

    } else if (mYLabels.getPosition() == YLabelPosition.RIGHT_INSIDE) {

        mYLabelPaint.setTextAlign(Align.RIGHT);
        drawYLabels(getWidth() - mOffsetRight - xoffset, positions, yoffset);

    } else if (mYLabels.getPosition() == YLabelPosition.LEFT_INSIDE) {

        mYLabelPaint.setTextAlign(Align.LEFT);
        drawYLabels(mOffsetLeft + xoffset, positions, yoffset);

    } else { // BOTH SIDED Y-AXIS LABELS

        // draw left legend
        mYLabelPaint.setTextAlign(Align.RIGHT);
        drawYLabels(mOffsetLeft - xoffset, positions, yoffset);

        // draw right legend
        mYLabelPaint.setTextAlign(Align.LEFT);
        drawYLabels(getWidth() - mOffsetRight + xoffset, positions, yoffset);
    }
}
 
Example 9
Source File: XAxisRendererHorizontalBarChart.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
/**
 * Draws the LimitLines associated with this axis to the screen.
 * This is the standard YAxis renderer using the XAxis limit lines.
 *
 * @param c
 */
@Override
public void renderLimitLines(Canvas c) {

	List<LimitLine> limitLines = mXAxis.getLimitLines();

	if (limitLines == null || limitLines.size() <= 0)
		return;

	float[] pts = new float[2];
	Path limitLinePath = new Path();

	for (int i = 0; i < limitLines.size(); i++) {

		LimitLine l = limitLines.get(i);

           if(!l.isEnabled())
               continue;

		mLimitLinePaint.setStyle(Paint.Style.STROKE);
		mLimitLinePaint.setColor(l.getLineColor());
		mLimitLinePaint.setStrokeWidth(l.getLineWidth());
		mLimitLinePaint.setPathEffect(l.getDashPathEffect());

		pts[1] = l.getLimit();

		mTrans.pointValuesToPixel(pts);

		limitLinePath.moveTo(mViewPortHandler.contentLeft(), pts[1]);
		limitLinePath.lineTo(mViewPortHandler.contentRight(), pts[1]);

		c.drawPath(limitLinePath, mLimitLinePaint);
		limitLinePath.reset();
		// c.drawLines(pts, mLimitLinePaint);

		String label = l.getLabel();

		// if drawing the limit-value label is enabled
		if (label != null && !label.equals("")) {

			mLimitLinePaint.setStyle(l.getTextStyle());
			mLimitLinePaint.setPathEffect(null);
			mLimitLinePaint.setColor(l.getTextColor());
			mLimitLinePaint.setStrokeWidth(0.5f);
			mLimitLinePaint.setTextSize(l.getTextSize());

               final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
               float xOffset = Utils.convertDpToPixel(4f) + l.getXOffset();
               float yOffset = l.getLineWidth() + labelLineHeight + l.getYOffset();

               final LimitLine.LimitLabelPosition position = l.getLabelPosition();

			if (position == LimitLine.LimitLabelPosition.RIGHT_TOP) {

				mLimitLinePaint.setTextAlign(Align.RIGHT);
				c.drawText(label,
                           mViewPortHandler.contentRight() - xOffset,
						pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

			} else if (position == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {

                   mLimitLinePaint.setTextAlign(Align.RIGHT);
                   c.drawText(label,
                           mViewPortHandler.contentRight() - xOffset,
                           pts[1] + yOffset, mLimitLinePaint);

               } else if (position == LimitLine.LimitLabelPosition.LEFT_TOP) {

                   mLimitLinePaint.setTextAlign(Align.LEFT);
                   c.drawText(label,
                           mViewPortHandler.contentLeft() + xOffset,
                           pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

               } else {

				mLimitLinePaint.setTextAlign(Align.LEFT);
				c.drawText(label,
                           mViewPortHandler.offsetLeft() + xOffset,
						pts[1] + yOffset, mLimitLinePaint);
			}
		}
	}
}
 
Example 10
Source File: YAxisRendererHorizontalBarChart.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
/**
 * draws the y-axis labels to the screen
 */
@Override
public void renderAxisLabels(Canvas c) {

    if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled()) {
        return;
    }

    float[] positions = getTransformedPositions();

    mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
    mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
    mAxisLabelPaint.setColor(mYAxis.getTextColor());
    mAxisLabelPaint.setTextAlign(Align.CENTER);

    float baseYOffset = Utils.convertDpToPixel(2.5f);
    float textHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");

    AxisDependency dependency = mYAxis.getAxisDependency();
    YAxisLabelPosition labelPosition = mYAxis.getLabelPosition();

    float yPos = 0f;

    if (dependency == AxisDependency.LEFT) {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            yPos = mViewPortHandler.contentTop() - baseYOffset;
        } else {
            yPos = mViewPortHandler.contentTop() - baseYOffset;
        }

    } else {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            yPos = mViewPortHandler.contentBottom() + textHeight + baseYOffset;
        } else {
            yPos = mViewPortHandler.contentBottom() + textHeight + baseYOffset;
        }
    }

    drawYLabels(c, yPos, positions, mYAxis.getYOffset());
}
 
Example 11
Source File: YAxisRendererHorizontalBarChart.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
/**
 * draws the y-axis labels to the screen
 */
@Override
public void renderAxisLabels(Canvas c) {

    if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled())
        return;

    float[] positions = new float[mYAxis.mEntryCount * 2];

    for (int i = 0; i < positions.length; i += 2) {
        // only fill y values, x values are not needed since the y-labels
        // are
        // static on the x-axis
        positions[i] = mYAxis.mEntries[i / 2];
    }

    mTrans.pointValuesToPixel(positions);

    mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
    mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
    mAxisLabelPaint.setColor(mYAxis.getTextColor());
    mAxisLabelPaint.setTextAlign(Align.CENTER);

    float baseYOffset = Utils.convertDpToPixel(2.5f);
    float textHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");

    AxisDependency dependency = mYAxis.getAxisDependency();
    YAxisLabelPosition labelPosition = mYAxis.getLabelPosition();

    float yPos = 0f;

    if (dependency == AxisDependency.LEFT) {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            yPos = mViewPortHandler.contentTop() - baseYOffset;
        } else {
            yPos = mViewPortHandler.contentTop() - baseYOffset;
        }

    } else {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            yPos = mViewPortHandler.contentBottom() + textHeight + baseYOffset;
        } else {
            yPos = mViewPortHandler.contentBottom() + textHeight + baseYOffset;
        }
    }

    drawYLabels(c, yPos, positions, mYAxis.getYOffset());
}
 
Example 12
Source File: ColorContentYAxisRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
@Override
public void renderLimitLines(Canvas c) {

    List<LimitLine> limitLines = mYAxis.getLimitLines();

    if (limitLines == null || limitLines.size() <= 0)
        return;

    float[] pts = mRenderLimitLinesBuffer;
    pts[0] = 0;
    pts[1] = 0;
    Path limitLinePath = mRenderLimitLines;
    limitLinePath.reset();

    for (int i = 0; i < limitLines.size(); i++) {

        LimitLine l = limitLines.get(i);

        if (!l.isEnabled())
            continue;

        int clipRestoreCount = c.save();
        mLimitLineClippingRect.set(mViewPortHandler.getContentRect());
        mLimitLineClippingRect.inset(0.f, -l.getLineWidth());
        c.clipRect(mLimitLineClippingRect);

        mLimitLinePaint.setStyle(Paint.Style.STROKE);
        mLimitLinePaint.setColor(l.getLineColor());
        mLimitLinePaint.setStrokeWidth(l.getLineWidth());
        mLimitLinePaint.setPathEffect(l.getDashPathEffect());

        pts[1] = l.getLimit();

        mTrans.pointValuesToPixel(pts);

        limitLinePath.moveTo(mViewPortHandler.contentLeft(), pts[1]);
        limitLinePath.lineTo(mViewPortHandler.contentRight(), pts[1]);

        c.drawPath(limitLinePath, mLimitLinePaint);
        limitLinePath.reset();
        // c.drawLines(pts, mLimitLinePaint);

        String label = l.getLabel();

        // if drawing the limit-value label is enabled
        if (label != null && !label.equals("")) {

            mLimitLinePaint.setStyle(l.getTextStyle());
            mLimitLinePaint.setPathEffect(null);
            mLimitLinePaint.setColor(l.getTextColor());
            mLimitLinePaint.setTypeface(l.getTypeface());
            mLimitLinePaint.setStrokeWidth(0.5f);
            mLimitLinePaint.setTextSize(l.getTextSize());

            final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
            float xOffset = convertDpToPixel(4f) + l.getXOffset();
            float yOffset = l.getLineWidth() + labelLineHeight + l.getYOffset();

            final LimitLine.LimitLabelPosition position = l.getLabelPosition();

            if (!mUseDefaultLimitLineLabelXOffset) {
                xOffset= convertDpToPixel(1f);
            }

            if (position == LimitLine.LimitLabelPosition.RIGHT_TOP) {

                mLimitLinePaint.setTextAlign(Paint.Align.RIGHT);
                c.drawText(label,
                        mViewPortHandler.contentRight() - xOffset,
                        pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

            } else if (position == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {

                mLimitLinePaint.setTextAlign(Paint.Align.RIGHT);
                c.drawText(label,
                        mViewPortHandler.contentRight() - xOffset,
                        pts[1] + yOffset, mLimitLinePaint);

            } else if (position == LimitLine.LimitLabelPosition.LEFT_TOP) {

                mLimitLinePaint.setTextAlign(Paint.Align.LEFT);
                c.drawText(label,
                        mViewPortHandler.contentLeft() + xOffset,
                        pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

            } else {

                mLimitLinePaint.setTextAlign(Paint.Align.LEFT);
                c.drawText(label,
                        mViewPortHandler.offsetLeft() + xOffset,
                        pts[1] + yOffset, mLimitLinePaint);
            }
        }

        c.restoreToCount(clipRestoreCount);
    }
}
 
Example 13
Source File: YAxisRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
/**
 * draws the y-axis labels to the screen
 */
@Override
public void renderAxisLabels(Canvas c) {

    if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled())
        return;

    float[] positions = new float[mYAxis.mEntryCount * 2];

    for (int i = 0; i < positions.length; i += 2) {
        // only fill y values, x values are not needed since the y-labels
        // are
        // static on the x-axis
        positions[i + 1] = mYAxis.mEntries[i / 2];
    }

    mTrans.pointValuesToPixel(positions);

    mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
    mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
    mAxisLabelPaint.setColor(mYAxis.getTextColor());

    float xoffset = mYAxis.getXOffset();
    float yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset();

    AxisDependency dependency = mYAxis.getAxisDependency();
    YAxisLabelPosition labelPosition = mYAxis.getLabelPosition();

    float xPos = 0f;

    if (dependency == AxisDependency.LEFT) {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            mAxisLabelPaint.setTextAlign(Align.RIGHT);
            xPos = mViewPortHandler.offsetLeft() - xoffset;
        } else {
            mAxisLabelPaint.setTextAlign(Align.LEFT);
            xPos = mViewPortHandler.offsetLeft() + xoffset;
        }

    } else {

        if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
            mAxisLabelPaint.setTextAlign(Align.LEFT);
            xPos = mViewPortHandler.contentRight() + xoffset;
        } else {
            mAxisLabelPaint.setTextAlign(Align.RIGHT);
            xPos = mViewPortHandler.contentRight() - xoffset;
        }
    }

    drawYLabels(c, xPos, positions, yoffset);
}
 
Example 14
Source File: PieChartRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
@Override
public void drawValues(Canvas c) {

    PointF center = mChart.getCenterCircleBox();

    // get whole the radius
    float r = mChart.getRadius();
    float rotationAngle = mChart.getRotationAngle();
    float[] drawAngles = mChart.getDrawAngles();
    float[] absoluteAngles = mChart.getAbsoluteAngles();

    float off = r / 10f * 3.6f;

    if (mChart.isDrawHoleEnabled()) {
        off = (r - (r / 100f * mChart.getHoleRadius())) / 2f;
    }

    r -= off; // offset to keep things inside the chart

    PieData data = mChart.getData();
    List<PieDataSet> dataSets = data.getDataSets();
    boolean drawXVals = mChart.isDrawSliceTextEnabled();

    int cnt = 0;

    for (int i = 0; i < dataSets.size(); i++) {

        PieDataSet dataSet = dataSets.get(i);

        if (!dataSet.isDrawValuesEnabled() && !drawXVals)
            continue;

        // apply the text-styling defined by the DataSet
        applyValueTextStyle(dataSet);

        float lineHeight = Utils.calcTextHeight(mValuePaint, "Q")
                + Utils.convertDpToPixel(4f);

        List<Entry> entries = dataSet.getYVals();

        for (int j = 0, maxEntry = Math.min(
                (int) Math.ceil(entries.size() * mAnimator.getPhaseX()), entries.size()); j < maxEntry; j++) {

            Entry entry = entries.get(j);

            // offset needed to center the drawn text in the slice
            float offset = drawAngles[cnt] / 2;

            // calculate the text position
            float x = (float) (r
                    * Math.cos(Math.toRadians((rotationAngle + absoluteAngles[cnt] - offset)
                    * mAnimator.getPhaseY())) + center.x);
            float y = (float) (r
                    * Math.sin(Math.toRadians((rotationAngle + absoluteAngles[cnt] - offset)
                            * mAnimator.getPhaseY())) + center.y);

            float value = mChart.isUsePercentValuesEnabled() ? entry.getVal()
                    / data.getYValueSum() * 100f : entry.getVal();

            ValueFormatter formatter = dataSet.getValueFormatter();

            boolean drawYVals = dataSet.isDrawValuesEnabled();

            // draw everything, depending on settings
            if (drawXVals && drawYVals) {

                drawValue(c, formatter, value, entry, 0, x, y);

                if (j < data.getXValCount())
                    c.drawText(data.getXVals().get(j), x, y + lineHeight,
                            mValuePaint);

            } else if (drawXVals && !drawYVals) {
                if (j < data.getXValCount())
                    c.drawText(data.getXVals().get(j), x, y + lineHeight / 2f, mValuePaint);
            } else if (!drawXVals && drawYVals) {

                drawValue(c, formatter, value, entry, 0, x, y + lineHeight / 2f);
            }

            cnt++;
        }
    }
}
 
Example 15
Source File: ColorContentYAxisRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
@Override
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {

    final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
    final int to = mYAxis.isDrawTopYLabelEntryEnabled()
            ? mYAxis.mEntryCount
            : (mYAxis.mEntryCount - 1);

    int originalColor = mAxisLabelPaint.getColor();

    float textHeight = Utils.calcTextHeight(mAxisLabelPaint, "A");
    float yoffset = textHeight / 2.5f + mYAxis.getYOffset();
    float space = Utils.convertDpToPixel(1f);
    if (!mUseDefaultLabelXOffset) {
        if (mYAxis.getAxisDependency() == YAxis.AxisDependency.LEFT) {
            fixedPosition -= mYAxis.getXOffset();
        } else {
            fixedPosition += mYAxis.getXOffset();
        }
    }

    // draw
    for (int i = from; i < to; i++) {

        if (mLabelColorArray != null && i >= 0 && i < mLabelColorArray.length) {
            int labelColor = mLabelColorArray[i];
            mAxisLabelPaint.setColor(labelColor);
        } else {
            mAxisLabelPaint.setColor(originalColor);
        }
        String text = mYAxis.getFormattedLabel(i);

        float y = positions[i * 2 + 1] + offset;

        if (mLabelInContent) {
            if (i == from) {
                y = y - offset - space-1F;
            } else if (i == (to - 1)) {
                y = y - yoffset + textHeight + space+1F;
            }
        }

        c.drawText(text, fixedPosition, y, mAxisLabelPaint);
    }
    mAxisLabelPaint.setColor(originalColor);
}
 
Example 16
Source File: XAxisRendererCurrentDay.java    From shinny-futures-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
    float[] position = new float[]{
            0f, 0f
    };
    int labelWidth = 0;
    int labelHeight = 0;
    int count = mXAxis.getXLabels().size();

    for (int i = 0; i < count; i++) {
        /*获取label对应key值,也就是x轴坐标0,60,121,182,242*/
        int ix = mXAxis.getXLabels().keyAt(i);

        position[0] = ix;

        /*在图表中的x轴转为像素,方便绘制text*/
        mTrans.pointValuesToPixel(position);

        /*x轴越界*/
        if (mViewPortHandler.isInBoundsX(position[0])) {

            String label = mXAxis.getXLabels().valueAt(i);

            if (label != null) {

                if (labelWidth == 0) labelWidth = Utils.calcTextWidth(mAxisLabelPaint, label);
                if (labelHeight == 0)
                    labelHeight = Utils.calcTextHeight(mAxisLabelPaint, label);

                //右出界
                if ((labelWidth / 2 + position[0]) > mChart.getViewPortHandler().contentRight()) {
                    position[0] = mViewPortHandler.contentRight() - labelWidth / 2;
                } else if ((position[0] - labelWidth / 2) < mChart.getViewPortHandler().contentLeft()) {
                    //左出界
                    position[0] = mViewPortHandler.offsetLeft() + labelWidth / 2;
                }

                c.drawText(label, position[0],
                        pos + mChart.getViewPortHandler().offsetBottom() / 2 + labelHeight / 2,
                        mAxisLabelPaint);
            }

        }

    }
}
 
Example 17
Source File: YAxisRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
/**
 * Draws the LimitLines associated with this axis to the screen.
 *
 * @param c
 */
@Override
public void renderLimitLines(Canvas c) {

    List<LimitLine> limitLines = mYAxis.getLimitLines();

    if (limitLines == null || limitLines.size() <= 0)
        return;

    float[] pts = new float[2];
    Path limitLinePath = new Path();

    for (int i = 0; i < limitLines.size(); i++) {

        LimitLine l = limitLines.get(i);

        mLimitLinePaint.setStyle(Paint.Style.STROKE);
        mLimitLinePaint.setColor(l.getLineColor());
        mLimitLinePaint.setStrokeWidth(l.getLineWidth());
        mLimitLinePaint.setPathEffect(l.getDashPathEffect());

        pts[1] = l.getLimit();

        mTrans.pointValuesToPixel(pts);

        limitLinePath.moveTo(mViewPortHandler.contentLeft(), pts[1]);
        limitLinePath.lineTo(mViewPortHandler.contentRight(), pts[1]);

        c.drawPath(limitLinePath, mLimitLinePaint);
        limitLinePath.reset();
        // c.drawLines(pts, mLimitLinePaint);

        String label = l.getLabel();

        // if drawing the limit-value label is enabled
        if (label != null && !label.equals("")) {

            mLimitLinePaint.setStyle(l.getTextStyle());
            mLimitLinePaint.setPathEffect(null);
            mLimitLinePaint.setColor(l.getTextColor());
            mLimitLinePaint.setTypeface(l.getTypeface());
            mLimitLinePaint.setStrokeWidth(0.5f);
            mLimitLinePaint.setTextSize(l.getTextSize());

            final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
            float xOffset = Utils.convertDpToPixel(4f);
            float yOffset = l.getLineWidth() + labelLineHeight;

            final LimitLine.LimitLabelPosition position = l.getLabelPosition();

            if (position == LimitLine.LimitLabelPosition.RIGHT_TOP) {

                mLimitLinePaint.setTextAlign(Align.RIGHT);
                c.drawText(label,
                        mViewPortHandler.contentRight() - xOffset,
                        pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

            } else if (position == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {

                mLimitLinePaint.setTextAlign(Align.RIGHT);
                c.drawText(label,
                        mViewPortHandler.contentRight() - xOffset,
                        pts[1] + yOffset, mLimitLinePaint);

            } else if (position == LimitLine.LimitLabelPosition.LEFT_TOP) {

                mLimitLinePaint.setTextAlign(Align.LEFT);
                c.drawText(label,
                        mViewPortHandler.contentLeft() + xOffset,
                        pts[1] - yOffset + labelLineHeight, mLimitLinePaint);

            } else {

                mLimitLinePaint.setTextAlign(Align.LEFT);
                c.drawText(label,
                        mViewPortHandler.offsetLeft() + xOffset,
                        pts[1] + yOffset, mLimitLinePaint);
            }
        }
    }
}
 
Example 18
Source File: Legend.java    From iMoney with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the full height of the drawn legend.
 * 
 * @param mLegendLabelPaint
 * @return
 */
public float getFullHeight(Paint labelpaint) {

    float height = 0f;

    for (int i = 0; i < mLabels.length; i++) {

        // grouped forms have null labels
        if (mLabels[i] != null) {

            height += Utils.calcTextHeight(labelpaint, mLabels[i]);

            if (i < mLabels.length - 1)
                height += mYEntrySpace;
        }
    }

    return height;
}
 
Example 19
Source File: YAxisRendererCurrentDay.java    From shinny-futures-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
    for (int i = 0; i < mYAxis.mEntryCount; i++) {
        String text = mYAxis.getFormattedLabel(i);
        if (!mYAxis.isDrawTopYLabelEntryEnabled() && i >= mYAxis.mEntryCount - 1) return;
        int labelHeight = Utils.calcTextHeight(mAxisLabelPaint, text);

        float pos = positions[i * 2 + 1] + offset;
        if ((pos - labelHeight) < mViewPortHandler.contentTop()) {
            pos = mViewPortHandler.contentTop() + offset * 2.5f + 3;
        } else if ((pos + labelHeight / 2) > mViewPortHandler.contentBottom()) {
            pos = mViewPortHandler.contentBottom() - 3;
        }

        try {
            float data = Float.parseFloat(text.replaceAll("[^\\d.-]", ""));
            if (text.contains("%")) {
                if (data == 0) {
                    text = text.replace("-", "");
                    mAxisLabelPaint.setColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.kline_text));
                } else if (data < 0) {
                    mAxisLabelPaint.setColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.text_green));
                } else {
                    text = "+" + text;
                    mAxisLabelPaint.setColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.text_red));
                }
            } else {
                if (data > mYAxis.getBaseValue()) {
                    mAxisLabelPaint.setColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.text_red));
                } else if (data < mYAxis.getBaseValue()) {
                    mAxisLabelPaint.setColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.text_green));
                } else {
                    mAxisLabelPaint.setColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.kline_text));
                }
            }
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
        }

        c.drawText(text, fixedPosition, pos, mAxisLabelPaint);
    }
}
 
Example 20
Source File: YAxis.java    From iMoney with Apache License 2.0 3 votes vote down vote up
public float getRequiredHeightSpace(Paint p) {

		p.setTextSize(mTextSize);

		String label = getLongestLabel();
		return (float) Utils.calcTextHeight(p, label) + Utils.convertDpToPixel(2.5f) * 2f + getYOffset();
	}