Search in sources :

Example 6 with MPPointF

use of com.github.mikephil.charting.utils.MPPointF in project MPAndroidChart by PhilJay.

the class RadarChartRenderer method drawHighlighted.

@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
    float sliceangle = mChart.getSliceAngle();
    // calculate the factor that is needed for transforming the value to
    // pixels
    float factor = mChart.getFactor();
    MPPointF center = mChart.getCenterOffsets();
    MPPointF pOut = MPPointF.getInstance(0, 0);
    RadarData radarData = mChart.getData();
    for (Highlight high : indices) {
        IRadarDataSet set = radarData.getDataSetByIndex(high.getDataSetIndex());
        if (set == null || !set.isHighlightEnabled())
            continue;
        RadarEntry e = set.getEntryForIndex((int) high.getX());
        if (!isInBoundsX(e, set))
            continue;
        float y = (e.getY() - mChart.getYChartMin());
        Utils.getPosition(center, y * factor * mAnimator.getPhaseY(), sliceangle * high.getX() * mAnimator.getPhaseX() + mChart.getRotationAngle(), pOut);
        high.setDraw(pOut.x, pOut.y);
        // draw the lines
        drawHighlightLines(c, pOut.x, pOut.y, set);
        if (set.isDrawHighlightCircleEnabled()) {
            if (!Float.isNaN(pOut.x) && !Float.isNaN(pOut.y)) {
                int strokeColor = set.getHighlightCircleStrokeColor();
                if (strokeColor == ColorTemplate.COLOR_NONE) {
                    strokeColor = set.getColor(0);
                }
                if (set.getHighlightCircleStrokeAlpha() < 255) {
                    strokeColor = ColorTemplate.colorWithAlpha(strokeColor, set.getHighlightCircleStrokeAlpha());
                }
                drawHighlightCircle(c, pOut, set.getHighlightCircleInnerRadius(), set.getHighlightCircleOuterRadius(), set.getHighlightCircleFillColor(), strokeColor, set.getHighlightCircleStrokeWidth());
            }
        }
    }
    MPPointF.recycleInstance(center);
    MPPointF.recycleInstance(pOut);
}
Also used : RadarEntry(com.github.mikephil.charting.data.RadarEntry) Highlight(com.github.mikephil.charting.highlight.Highlight) IRadarDataSet(com.github.mikephil.charting.interfaces.datasets.IRadarDataSet) MPPointF(com.github.mikephil.charting.utils.MPPointF) RadarData(com.github.mikephil.charting.data.RadarData) Paint(android.graphics.Paint)

Example 7 with MPPointF

use of com.github.mikephil.charting.utils.MPPointF in project MPAndroidChart by PhilJay.

the class ScatterChartRenderer method drawValues.

@Override
public void drawValues(Canvas c) {
    // if values are drawn
    if (isDrawingValuesAllowed(mChart)) {
        List<IScatterDataSet> dataSets = mChart.getScatterData().getDataSets();
        for (int i = 0; i < mChart.getScatterData().getDataSetCount(); i++) {
            IScatterDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            mXBounds.set(mChart, dataSet);
            float[] positions = mChart.getTransformer(dataSet.getAxisDependency()).generateTransformedValuesScatter(dataSet, mAnimator.getPhaseX(), mAnimator.getPhaseY(), mXBounds.min, mXBounds.max);
            float shapeSize = Utils.convertDpToPixel(dataSet.getScatterShapeSize());
            MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
            iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
            iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
            for (int j = 0; j < positions.length; j += 2) {
                if (!mViewPortHandler.isInBoundsRight(positions[j]))
                    break;
                // make sure the lines don't do shitty things outside bounds
                if ((!mViewPortHandler.isInBoundsLeft(positions[j]) || !mViewPortHandler.isInBoundsY(positions[j + 1])))
                    continue;
                Entry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
                if (dataSet.isDrawValuesEnabled()) {
                    drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, positions[j], positions[j + 1] - shapeSize, dataSet.getValueTextColor(j / 2 + mXBounds.min));
                }
                if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                    Drawable icon = entry.getIcon();
                    Utils.drawImage(c, icon, (int) (positions[j] + iconsOffset.x), (int) (positions[j + 1] + iconsOffset.y), icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                }
            }
            MPPointF.recycleInstance(iconsOffset);
        }
    }
}
Also used : Entry(com.github.mikephil.charting.data.Entry) IScatterDataSet(com.github.mikephil.charting.interfaces.datasets.IScatterDataSet) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable)

Example 8 with MPPointF

use of com.github.mikephil.charting.utils.MPPointF in project MPAndroidChart by PhilJay.

the class LineChartRenderer method drawValues.

@Override
public void drawValues(Canvas c) {
    if (isDrawingValuesAllowed(mChart)) {
        List<ILineDataSet> dataSets = mChart.getLineData().getDataSets();
        for (int i = 0; i < dataSets.size(); i++) {
            ILineDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
            // make sure the values do not interfear with the circles
            int valOffset = (int) (dataSet.getCircleRadius() * 1.75f);
            if (!dataSet.isDrawCirclesEnabled())
                valOffset = valOffset / 2;
            mXBounds.set(mChart, dataSet);
            float[] positions = trans.generateTransformedValuesLine(dataSet, mAnimator.getPhaseX(), mAnimator.getPhaseY(), mXBounds.min, mXBounds.max);
            MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
            iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
            iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
            for (int j = 0; j < positions.length; j += 2) {
                float x = positions[j];
                float y = positions[j + 1];
                if (!mViewPortHandler.isInBoundsRight(x))
                    break;
                if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y))
                    continue;
                Entry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
                if (dataSet.isDrawValuesEnabled()) {
                    drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, x, y - valOffset, dataSet.getValueTextColor(j / 2));
                }
                if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                    Drawable icon = entry.getIcon();
                    Utils.drawImage(c, icon, (int) (x + iconsOffset.x), (int) (y + iconsOffset.y), icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                }
            }
            MPPointF.recycleInstance(iconsOffset);
        }
    }
}
Also used : Entry(com.github.mikephil.charting.data.Entry) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) Transformer(com.github.mikephil.charting.utils.Transformer) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) Paint(android.graphics.Paint)

Example 9 with MPPointF

use of com.github.mikephil.charting.utils.MPPointF in project MPAndroidChart by PhilJay.

the class BarChartRenderer method drawValues.

@Override
public void drawValues(Canvas c) {
    // if values are drawn
    if (isDrawingValuesAllowed(mChart)) {
        List<IBarDataSet> dataSets = mChart.getBarData().getDataSets();
        final float valueOffsetPlus = Utils.convertDpToPixel(4.5f);
        float posOffset = 0f;
        float negOffset = 0f;
        boolean drawValueAboveBar = mChart.isDrawValueAboveBarEnabled();
        for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
            IBarDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            boolean isInverted = mChart.isInverted(dataSet.getAxisDependency());
            // calculate the correct offset depending on the draw position of
            // the value
            float valueTextHeight = Utils.calcTextHeight(mValuePaint, "8");
            posOffset = (drawValueAboveBar ? -valueOffsetPlus : valueTextHeight + valueOffsetPlus);
            negOffset = (drawValueAboveBar ? valueTextHeight + valueOffsetPlus : -valueOffsetPlus);
            if (isInverted) {
                posOffset = -posOffset - valueTextHeight;
                negOffset = -negOffset - valueTextHeight;
            }
            // get the buffer
            BarBuffer buffer = mBarBuffers[i];
            final float phaseY = mAnimator.getPhaseY();
            MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
            iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
            iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
            // if only single values are drawn (sum)
            if (!dataSet.isStacked()) {
                for (int j = 0; j < buffer.buffer.length * mAnimator.getPhaseX(); j += 4) {
                    float x = (buffer.buffer[j] + buffer.buffer[j + 2]) / 2f;
                    if (!mViewPortHandler.isInBoundsRight(x))
                        break;
                    if (!mViewPortHandler.isInBoundsY(buffer.buffer[j + 1]) || !mViewPortHandler.isInBoundsLeft(x))
                        continue;
                    BarEntry entry = dataSet.getEntryForIndex(j / 4);
                    float val = entry.getY();
                    if (dataSet.isDrawValuesEnabled()) {
                        drawValue(c, dataSet.getValueFormatter(), val, entry, i, x, val >= 0 ? (buffer.buffer[j + 1] + posOffset) : (buffer.buffer[j + 3] + negOffset), dataSet.getValueTextColor(j / 4));
                    }
                    if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                        Drawable icon = entry.getIcon();
                        float px = x;
                        float py = val >= 0 ? (buffer.buffer[j + 1] + posOffset) : (buffer.buffer[j + 3] + negOffset);
                        px += iconsOffset.x;
                        py += iconsOffset.y;
                        Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                    }
                }
            // if we have stacks
            } else {
                Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
                int bufferIndex = 0;
                int index = 0;
                while (index < dataSet.getEntryCount() * mAnimator.getPhaseX()) {
                    BarEntry entry = dataSet.getEntryForIndex(index);
                    float[] vals = entry.getYVals();
                    float x = (buffer.buffer[bufferIndex] + buffer.buffer[bufferIndex + 2]) / 2f;
                    int color = dataSet.getValueTextColor(index);
                    // in between
                    if (vals == null) {
                        if (!mViewPortHandler.isInBoundsRight(x))
                            break;
                        if (!mViewPortHandler.isInBoundsY(buffer.buffer[bufferIndex + 1]) || !mViewPortHandler.isInBoundsLeft(x))
                            continue;
                        if (dataSet.isDrawValuesEnabled()) {
                            drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, x, buffer.buffer[bufferIndex + 1] + (entry.getY() >= 0 ? posOffset : negOffset), color);
                        }
                        if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                            Drawable icon = entry.getIcon();
                            float px = x;
                            float py = buffer.buffer[bufferIndex + 1] + (entry.getY() >= 0 ? posOffset : negOffset);
                            px += iconsOffset.x;
                            py += iconsOffset.y;
                            Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                        }
                    // draw stack values
                    } else {
                        float[] transformed = new float[vals.length * 2];
                        float posY = 0f;
                        float negY = -entry.getNegativeSum();
                        for (int k = 0, idx = 0; k < transformed.length; k += 2, idx++) {
                            float value = vals[idx];
                            float y;
                            if (value == 0.0f && (posY == 0.0f || negY == 0.0f)) {
                                // Take care of the situation of a 0.0 value, which overlaps a non-zero bar
                                y = value;
                            } else if (value >= 0.0f) {
                                posY += value;
                                y = posY;
                            } else {
                                y = negY;
                                negY -= value;
                            }
                            transformed[k + 1] = y * phaseY;
                        }
                        trans.pointValuesToPixel(transformed);
                        for (int k = 0; k < transformed.length; k += 2) {
                            final float val = vals[k / 2];
                            final boolean drawBelow = (val == 0.0f && negY == 0.0f && posY > 0.0f) || val < 0.0f;
                            float y = transformed[k + 1] + (drawBelow ? negOffset : posOffset);
                            if (!mViewPortHandler.isInBoundsRight(x))
                                break;
                            if (!mViewPortHandler.isInBoundsY(y) || !mViewPortHandler.isInBoundsLeft(x))
                                continue;
                            if (dataSet.isDrawValuesEnabled()) {
                                drawValue(c, dataSet.getValueFormatter(), vals[k / 2], entry, i, x, y, color);
                            }
                            if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                                Drawable icon = entry.getIcon();
                                Utils.drawImage(c, icon, (int) (x + iconsOffset.x), (int) (y + iconsOffset.y), icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                            }
                        }
                    }
                    bufferIndex = vals == null ? bufferIndex + 4 : bufferIndex + 4 * vals.length;
                    index++;
                }
            }
            MPPointF.recycleInstance(iconsOffset);
        }
    }
}
Also used : Transformer(com.github.mikephil.charting.utils.Transformer) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) BarEntry(com.github.mikephil.charting.data.BarEntry) Paint(android.graphics.Paint) BarBuffer(com.github.mikephil.charting.buffer.BarBuffer)

Example 10 with MPPointF

use of com.github.mikephil.charting.utils.MPPointF in project MPAndroidChart by PhilJay.

the class CandleStickChartRenderer method drawValues.

@Override
public void drawValues(Canvas c) {
    // if values are drawn
    if (isDrawingValuesAllowed(mChart)) {
        List<ICandleDataSet> dataSets = mChart.getCandleData().getDataSets();
        for (int i = 0; i < dataSets.size(); i++) {
            ICandleDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
            mXBounds.set(mChart, dataSet);
            float[] positions = trans.generateTransformedValuesCandle(dataSet, mAnimator.getPhaseX(), mAnimator.getPhaseY(), mXBounds.min, mXBounds.max);
            float yOffset = Utils.convertDpToPixel(5f);
            MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
            iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
            iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
            for (int j = 0; j < positions.length; j += 2) {
                float x = positions[j];
                float y = positions[j + 1];
                if (!mViewPortHandler.isInBoundsRight(x))
                    break;
                if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y))
                    continue;
                CandleEntry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
                if (dataSet.isDrawValuesEnabled()) {
                    drawValue(c, dataSet.getValueFormatter(), entry.getHigh(), entry, i, x, y - yOffset, dataSet.getValueTextColor(j / 2));
                }
                if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                    Drawable icon = entry.getIcon();
                    Utils.drawImage(c, icon, (int) (x + iconsOffset.x), (int) (y + iconsOffset.y), icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                }
            }
            MPPointF.recycleInstance(iconsOffset);
        }
    }
}
Also used : ICandleDataSet(com.github.mikephil.charting.interfaces.datasets.ICandleDataSet) Transformer(com.github.mikephil.charting.utils.Transformer) CandleEntry(com.github.mikephil.charting.data.CandleEntry) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) Paint(android.graphics.Paint)

Aggregations

MPPointF (com.github.mikephil.charting.utils.MPPointF)43 Paint (android.graphics.Paint)13 Drawable (android.graphics.drawable.Drawable)9 RectF (android.graphics.RectF)5 TextPaint (android.text.TextPaint)5 Entry (com.github.mikephil.charting.data.Entry)5 PieEntry (com.github.mikephil.charting.data.PieEntry)4 Transformer (com.github.mikephil.charting.utils.Transformer)4 Path (android.graphics.Path)3 RadarEntry (com.github.mikephil.charting.data.RadarEntry)3 IPieDataSet (com.github.mikephil.charting.interfaces.datasets.IPieDataSet)3 SuppressLint (android.annotation.SuppressLint)2 BarBuffer (com.github.mikephil.charting.buffer.BarBuffer)2 Chart (com.github.mikephil.charting.charts.Chart)2 BarEntry (com.github.mikephil.charting.data.BarEntry)2 BubbleData (com.github.mikephil.charting.data.BubbleData)2 BubbleEntry (com.github.mikephil.charting.data.BubbleEntry)2 PieData (com.github.mikephil.charting.data.PieData)2 PieDataSet (com.github.mikephil.charting.data.PieDataSet)2 IValueFormatter (com.github.mikephil.charting.formatter.IValueFormatter)2