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

The following examples show how to use com.github.mikephil.charting.utils.Utils#calcTextWidth() . 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: YAxis.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * This is for normal (not horizontal) charts horizontal spacing.
 *
 * @param p
 * @return
 */
public float getRequiredWidthSpace(Paint p) {

    p.setTextSize(mTextSize);

    String label = getLongestLabel();
    float width = (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;

    float minWidth = getMinWidth();
    float maxWidth = getMaxWidth();

    if (minWidth > 0.f) {
        minWidth = Utils.convertDpToPixel(minWidth);
    }

    if (maxWidth > 0.f && maxWidth != Float.POSITIVE_INFINITY) {
        maxWidth = Utils.convertDpToPixel(maxWidth);
    }

    width = Math.max(minWidth, Math.min(width, maxWidth > 0.0 ? maxWidth : width));

    return width;
}
 
Example 2
Source File: Legend.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
/**
 * returns the maximum length in pixels across all legend labels + formsize
 * + formtotextspace
 *
 * @param p the paint object used for rendering the text
 * @return
 */
public float getMaximumEntryWidth(Paint p) {

    float max = 0f;
    float maxFormSize = 0f;
    float formToTextSpace = Utils.convertDpToPixel(mFormToTextSpace);

    for (LegendEntry entry : mEntries) {
        final float formSize = Utils.convertDpToPixel(
                Float.isNaN(entry.formSize)
                ? mFormSize : entry.formSize);
        if (formSize > maxFormSize)
            maxFormSize = formSize;

        String label = entry.label;
        if (label == null) continue;

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

        if (length > max)
            max = length;
    }

    return max + maxFormSize + formToTextSpace;
}
 
Example 3
Source File: Legend.java    From android-kline with Apache License 2.0 6 votes vote down vote up
/**
 * returns the maximum length in pixels across all legend labels + formsize
 * + formtotextspace
 *
 * @param p the paint object used for rendering the text
 * @return
 */
public float getMaximumEntryWidth(Paint p) {

    float max = 0f;
    float maxFormSize = 0f;
    float formToTextSpace = Utils.convertDpToPixel(mFormToTextSpace);

    for (LegendEntry entry : mEntries) {
        final float formSize = Utils.convertDpToPixel(
                Float.isNaN(entry.formSize)
                ? mFormSize : entry.formSize);
        if (formSize > maxFormSize)
            maxFormSize = formSize;

        String label = entry.label;
        if (label == null) continue;

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

        if (length > max)
            max = length;
    }

    return max + maxFormSize + formToTextSpace;
}
 
Example 4
Source File: XAxisRenderer.java    From iMoney with Apache License 2.0 6 votes vote down vote up
public void computeAxis(float xValAverageLength, List<String> xValues) {

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

        StringBuffer a = new StringBuffer();

        int max = (int) Math.round(xValAverageLength
                + mXAxis.getSpaceBetweenLabels());

        for (int i = 0; i < max; i++) {
            a.append("h");
        }

        mXAxis.mLabelWidth = Utils.calcTextWidth(mAxisLabelPaint, a.toString());
        mXAxis.mLabelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");
        mXAxis.setValues(xValues);
    }
 
Example 5
Source File: XAxisRendererHorizontalBarChart.java    From iMoney with Apache License 2.0 5 votes vote down vote up
@Override
public void computeAxis(float xValAverageLength, List<String> xValues) {
    
    mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
    mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
    mXAxis.setValues(xValues);

    String longest = mXAxis.getLongestLabel();
    mXAxis.mLabelWidth = (int) (Utils.calcTextWidth(mAxisLabelPaint, longest) + mXAxis.getXOffset() * 3.5f);
    mXAxis.mLabelHeight = Utils.calcTextHeight(mAxisLabelPaint, longest);
}
 
Example 6
Source File: BarLineChartBase.java    From Notification-Analyser with MIT License 5 votes vote down vote up
/**
 * setup the x-axis labels
 */
protected void prepareXLabels() {

    StringBuffer a = new StringBuffer();

    int max = (int) Math.round(mCurrentData.getXValAverageLength()
            + mXLabels.getSpaceBetweenLabels());

    for (int i = 0; i < max; i++) {
        a.append("h");
    }

    mXLabels.mLabelWidth = Utils.calcTextWidth(mXLabelPaint, a.toString());
    mXLabels.mLabelHeight = Utils.calcTextHeight(mXLabelPaint, "Q");
}
 
Example 7
Source File: RadarChart.java    From Notification-Analyser with MIT License 5 votes vote down vote up
/**
 * setup the x-axis labels
 */
private void prepareXLabels() {

    StringBuffer a = new StringBuffer();

    int max = (int) Math.round(mCurrentData.getXValAverageLength());

    for (int i = 0; i < max; i++) {
        a.append("h");
    }

    mXLabels.mLabelWidth = Utils.calcTextWidth(mXLabelPaint, a.toString());
    mXLabels.mLabelHeight = Utils.calcTextWidth(mXLabelPaint, "Q");
}
 
Example 8
Source File: Legend.java    From NetKnight with Apache License 2.0 5 votes vote down vote up
/**
 * calculates the full width the fully drawn legend will use in pixels
 * 
 * @return
 */
public float getFullWidth(Paint labelpaint) {

    float width = 0f;

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

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

            // make a step to the left
            if (mColors[i] != ColorTemplate.COLOR_SKIP)
                width += mFormSize + mFormToTextSpace;

            width += Utils.calcTextWidth(labelpaint, mLabels[i]);

            if (i < mLabels.length - 1)
                width += mXEntrySpace;
        } else {
            width += mFormSize;
            if (i < mLabels.length - 1)
                width += mStackSpace;
        }
    }

    return width;
}
 
Example 9
Source File: YAxis.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
/**
 * This is for normal (not horizontal) charts horizontal spacing.
 *
 * @param p
 * @return
 */
public float getRequiredWidthSpace(Paint p) {

    p.setTextSize(mTextSize);

    String label = getLongestLabel();
    return (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;
}
 
Example 10
Source File: Legend.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
/**
 * calculates the full width the fully drawn legend will use in pixels
 * 
 * @return
 */
public float getFullWidth(Paint labelpaint) {

    float width = 0f;

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

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

            // make a step to the left
            if (mColors[i] != ColorTemplate.COLOR_SKIP)
                width += mFormSize + mFormToTextSpace;

            width += Utils.calcTextWidth(labelpaint, mLabels[i]);

            if (i < mLabels.length - 1)
                width += mXEntrySpace;
        } else {
            width += mFormSize;
            if (i < mLabels.length - 1)
                width += mStackSpace;
        }
    }

    return width;
}
 
Example 11
Source File: Legend.java    From iMoney with Apache License 2.0 5 votes vote down vote up
/**
 * calculates the full width the fully drawn legend will use in pixels
 * 
 * @return
 */
public float getFullWidth(Paint labelpaint) {

    float width = 0f;

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

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

            // make a step to the left
            if (mColors[i] != ColorTemplate.COLOR_SKIP)
                width += mFormSize + mFormToTextSpace;

            width += Utils.calcTextWidth(labelpaint, mLabels[i]);

            if (i < mLabels.length - 1)
                width += mXEntrySpace;
        } else {
            width += mFormSize;
            if (i < mLabels.length - 1)
                width += mStackSpace;
        }
    }

    return width;
}
 
Example 12
Source File: Legend.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
/**
 * returns the maximum length in pixels across all legend labels + formsize
 * + formtotextspace
 *
 * @param p the paint object used for rendering the text
 * @return
 */
public float getMaximumEntryWidth(Paint p) {

    float max = 0f;
    float maxFormSize = 0f;
    float formToTextSpace = Utils.convertDpToPixel(mFormToTextSpace);

    for (LegendEntry entry : mEntries) {
        final float formSize = Utils.convertDpToPixel(
                Float.isNaN(entry.formSize)
                        ? mFormSize : entry.formSize);
        if (formSize > maxFormSize) {
            maxFormSize = formSize;
        }

        String label = entry.label;
        if (label == null) {
            continue;
        }

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

        if (length > max) {
            max = length;
        }
    }

    return max + maxFormSize + formToTextSpace;
}
 
Example 13
Source File: TimeXAxisRenderer.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
@Override
    protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
        float[] position = new float[]{0f, 0f};
        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);
            if (mXAxis.isCenterAxisLabelsEnabled()) {
                float offset = mXAxis.getXLabels().keyAt(count - 1) / (count - 1);
                position[0] = ix + offset / 2;
            } else {
                position[0] = ix;
            }
            /*在图表中的x轴转为像素,方便绘制text*/
            mTrans.pointValuesToPixel(position);
            /*x轴越界*/
//            if (mViewPortHandler.isInBoundsX(position[0])) {
            String label = mXAxis.getXLabels().valueAt(i);
            /*文本长度*/
            int labelWidth = Utils.calcTextWidth(mAxisLabelPaint, label);
            /*右出界*/
            if ((labelWidth / 2 + position[0]) > mChart.getViewPortHandler().contentRight()) {
                position[0] = mChart.getViewPortHandler().contentRight() - labelWidth / 2;
            } else if ((position[0] - labelWidth / 2) < mChart.getViewPortHandler().contentLeft()) {//左出界
                position[0] = mChart.getViewPortHandler().contentLeft() + labelWidth / 2;
            }
            c.drawText(label, position[0], pos + Utils.convertPixelsToDp(mChart.getViewPortHandler().offsetBottom() + 10),
                    mAxisLabelPaint);
//            }
        }
    }
 
Example 14
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 15
Source File: YAxis.java    From android-kline with Apache License 2.0 4 votes vote down vote up
/**
 * This is for normal (not horizontal) charts horizontal spacing.
 *
 * @param p
 * @return
 */
public float getRequiredWidthSpace(Paint p) {

    p.setTextSize(mTextSize);

    String label = getLongestLabel();
    float width = (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;

    float minWidth = getMinWidth();
    float maxWidth = getMaxWidth();

    if (minWidth > 0.f)
        minWidth = Utils.convertDpToPixel(minWidth);

    if (maxWidth > 0.f && maxWidth != Float.POSITIVE_INFINITY)
        maxWidth = Utils.convertDpToPixel(maxWidth);

    width = Math.max(minWidth, Math.min(width, maxWidth > 0.0 ? maxWidth : width));

    return width;
}
 
Example 16
Source File: YAxis.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
/**
 * This is for normal (not horizontal) charts horizontal spacing.
 *
 * @param p
 * @return
 */
public float getRequiredWidthSpace(Paint p) {

    p.setTextSize(mTextSize);

    String label = getLongestLabel();
    float width = (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;

    float minWidth = getMinWidth();
    float maxWidth = getMaxWidth();

    if (minWidth > 0.f)
        minWidth = Utils.convertDpToPixel(minWidth);

    if (maxWidth > 0.f && maxWidth != Float.POSITIVE_INFINITY)
        maxWidth = Utils.convertDpToPixel(maxWidth);

    width = Math.max(minWidth, Math.min(width, maxWidth > 0.0 ? maxWidth : width));

    return width;
}
 
Example 17
Source File: YAxis.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
/**
 * This is for normal (not horizontal) charts horizontal spacing.
 *
 * @param p
 * @return
 */
public float getRequiredWidthSpace(Paint p) {

    p.setTextSize(mTextSize);

    String label = getLongestLabel();
    float width = (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;

    float minWidth = getMinWidth();
    float maxWidth = getMaxWidth();

    if (minWidth > 0.f)
        minWidth = Utils.convertDpToPixel(minWidth);

    if (maxWidth > 0.f && maxWidth != Float.POSITIVE_INFINITY)
        maxWidth = Utils.convertDpToPixel(maxWidth);

    width = Math.max(minWidth, Math.min(width, maxWidth > 0.0 ? maxWidth : width));

    return width;
}
 
Example 18
Source File: Chart.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
/**
     * Draws the description text in the bottom right corner of the chart (per default)
     */
    protected void drawDescription(Canvas c) {

        // check if description should be drawn
        if (mDescription != null && mDescription.isEnabled()) {
            MPPointF position = mDescription.getPosition();
            mDescPaint.setTypeface(mDescription.getTypeface());
            mDescPaint.setTextSize(mDescription.getTextSize());
            mDescPaint.setColor(mDescription.getTextColor());
            mDescPaint.setTextAlign(mDescription.getTextAlign());

            if (mIsDescriptionCustom) {
                float xpos = mViewPortHandler.contentLeft();
                if (position == null) {
                    for (int i = 0; i < mDescriptionLabels.length; i++) {
                        xpos = xpos + Utils.calcTextWidth(mDescPaint, mDescriptionLabels[i]) + 20;
                        mDescPaint.setColor(mDescriptionColors[i]);
                        c.drawText(mDescriptionLabels[i], xpos,
                                mViewPortHandler.contentTop() + 5 - Utils.calcTextHeight(mDescPaint, mDescription.toString()) / 2, mDescPaint);
                    }

                } else {
                    for (int i = 0; i < mDescriptionLabels.length; i++) {
                        mDescPaint.setColor(mDescriptionColors[i]);
                        xpos = xpos + Utils.calcTextWidth(mDescPaint, mDescriptionLabels[i]) + 20;
                        c.drawText(mDescriptionLabels[i], xpos + position.x, position.y, mDescPaint);
                    }
                }
            } else if (!"".equals(mDescription.getText())) {

                if (position == null) {
                    c.drawText(mDescription.getText(), mViewPortHandler.contentLeft() + Utils.calcTextWidth(mDescPaint, mDescription.getText()) + 10,
                            mViewPortHandler.contentTop() + 5 - Utils.calcTextHeight(mDescPaint, mDescription.getText()) / 2, mDescPaint);
                } else {
                    c.drawText(mDescription.getText(), position.x, position.y, mDescPaint);
                }
            }


//            float x, y;
//
//            // if no position specified, draw on default position
//            if (position == null) {
//                x = getWidth() - mViewPortHandler.offsetRight() - mDescription.getXOffset()*2;
//                y = mViewPortHandler.contentTop() + mViewPortHandler.offsetTop() + mDescription.getYOffset()*2;
//            } else {
//                x = position.x;
//                y = position.y;
//            }
//
//            c.drawText(mDescription.getText(), x, y, mDescPaint);
        }
    }
 
Example 19
Source File: XAxisRendererKline.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[] positions = new float[mXAxis.mEntryCount * 2];
    int labelHeight = 0;
    int labelWidth;

    for (int i = 0; i < positions.length; i += 2) {
        positions[i] = mXAxis.mEntries[i / 2];
    }

    mTrans.pointValuesToPixel(positions);
    int count = positions.length;
    for (int i = 0; i < count; i += 2) {
        float x = positions[i];
        if (mViewPortHandler.isInBoundsX(x)) {
            String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);

            if (label != null) {

                if (i != 0) {
                    int index = label.indexOf("/");
                    label = label.substring(index + 1);
                }

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

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

                c.drawText(label, x,
                        pos + mChart.getViewPortHandler().offsetBottom() / 2 + labelHeight / 2,
                        mAxisLabelPaint);
            }
        }

    }
}
 
Example 20
Source File: YAxis.java    From iMoney with Apache License 2.0 3 votes vote down vote up
public float getRequiredWidthSpace(Paint p) {

		p.setTextSize(mTextSize);

		String label = getLongestLabel();
		return (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;
	}