Search in sources :

Example 11 with Transformer

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

the class HorizontalBarChartRenderer 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(5f);
        float posOffset = 0f;
        float negOffset = 0f;
        final boolean drawValueAboveBar = mChart.isDrawValueAboveBarEnabled();
        for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
            IBarDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            boolean isInverted = mChart.isInverted(dataSet.getAxisDependency());
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            final float halfTextHeight = Utils.calcTextHeight(mValuePaint, "10") / 2f;
            IValueFormatter formatter = dataSet.getValueFormatter();
            // 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 y = (buffer.buffer[j + 1] + buffer.buffer[j + 3]) / 2f;
                    if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 1]))
                        break;
                    if (!mViewPortHandler.isInBoundsX(buffer.buffer[j]))
                        continue;
                    if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
                        continue;
                    BarEntry entry = dataSet.getEntryForIndex(j / 4);
                    float val = entry.getY();
                    String formattedValue = formatter.getFormattedValue(val, entry, i, mViewPortHandler);
                    // calculate the correct offset depending on the draw position of the value
                    float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
                    posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
                    negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
                    if (isInverted) {
                        posOffset = -posOffset - valueTextWidth;
                        negOffset = -negOffset - valueTextWidth;
                    }
                    if (dataSet.isDrawValuesEnabled()) {
                        drawValue(c, formattedValue, buffer.buffer[j + 2] + (val >= 0 ? posOffset : negOffset), y + halfTextHeight, dataSet.getValueTextColor(j / 2));
                    }
                    if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                        Drawable icon = entry.getIcon();
                        float px = buffer.buffer[j + 2] + (val >= 0 ? posOffset : negOffset);
                        float py = y;
                        px += iconsOffset.x;
                        py += iconsOffset.y;
                        Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                    }
                }
            // if each value of a potential stack should be drawn
            } else {
                Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
                int bufferIndex = 0;
                int index = 0;
                while (index < dataSet.getEntryCount() * mAnimator.getPhaseX()) {
                    BarEntry entry = dataSet.getEntryForIndex(index);
                    int color = dataSet.getValueTextColor(index);
                    float[] vals = entry.getYVals();
                    // in between
                    if (vals == null) {
                        if (!mViewPortHandler.isInBoundsTop(buffer.buffer[bufferIndex + 1]))
                            break;
                        if (!mViewPortHandler.isInBoundsX(buffer.buffer[bufferIndex]))
                            continue;
                        if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[bufferIndex + 1]))
                            continue;
                        float val = entry.getY();
                        String formattedValue = formatter.getFormattedValue(val, entry, i, mViewPortHandler);
                        // calculate the correct offset depending on the draw position of the value
                        float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
                        posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
                        negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
                        if (isInverted) {
                            posOffset = -posOffset - valueTextWidth;
                            negOffset = -negOffset - valueTextWidth;
                        }
                        if (dataSet.isDrawValuesEnabled()) {
                            drawValue(c, formattedValue, buffer.buffer[bufferIndex + 2] + (entry.getY() >= 0 ? posOffset : negOffset), buffer.buffer[bufferIndex + 1] + halfTextHeight, color);
                        }
                        if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                            Drawable icon = entry.getIcon();
                            float px = buffer.buffer[bufferIndex + 2] + (entry.getY() >= 0 ? posOffset : negOffset);
                            float py = buffer.buffer[bufferIndex + 1];
                            px += iconsOffset.x;
                            py += iconsOffset.y;
                            Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                        }
                    } 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] = y * phaseY;
                        }
                        trans.pointValuesToPixel(transformed);
                        for (int k = 0; k < transformed.length; k += 2) {
                            final float val = vals[k / 2];
                            String formattedValue = formatter.getFormattedValue(val, entry, i, mViewPortHandler);
                            // calculate the correct offset depending on the draw position of the value
                            float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
                            posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
                            negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
                            if (isInverted) {
                                posOffset = -posOffset - valueTextWidth;
                                negOffset = -negOffset - valueTextWidth;
                            }
                            final boolean drawBelow = (val == 0.0f && negY == 0.0f && posY > 0.0f) || val < 0.0f;
                            float x = transformed[k] + (drawBelow ? negOffset : posOffset);
                            float y = (buffer.buffer[bufferIndex + 1] + buffer.buffer[bufferIndex + 3]) / 2f;
                            if (!mViewPortHandler.isInBoundsTop(y))
                                break;
                            if (!mViewPortHandler.isInBoundsX(x))
                                continue;
                            if (!mViewPortHandler.isInBoundsBottom(y))
                                continue;
                            if (dataSet.isDrawValuesEnabled()) {
                                drawValue(c, formattedValue, x, y + halfTextHeight, 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) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) BarEntry(com.github.mikephil.charting.data.BarEntry) BarBuffer(com.github.mikephil.charting.buffer.BarBuffer) HorizontalBarBuffer(com.github.mikephil.charting.buffer.HorizontalBarBuffer) IValueFormatter(com.github.mikephil.charting.formatter.IValueFormatter) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Example 12 with Transformer

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

the class BarLineChartBase method init.

@Override
protected void init() {
    super.init();
    mAxisLeft = new YAxis(AxisDependency.LEFT);
    mAxisRight = new YAxis(AxisDependency.RIGHT);
    mLeftAxisTransformer = new Transformer(mViewPortHandler);
    mRightAxisTransformer = new Transformer(mViewPortHandler);
    mAxisRendererLeft = new YAxisRenderer(mViewPortHandler, mAxisLeft, mLeftAxisTransformer);
    mAxisRendererRight = new YAxisRenderer(mViewPortHandler, mAxisRight, mRightAxisTransformer);
    mXAxisRenderer = new XAxisRenderer(mViewPortHandler, mXAxis, mLeftAxisTransformer);
    setHighlighter(new ChartHighlighter(this));
    mChartTouchListener = new BarLineChartTouchListener(this, mViewPortHandler.getMatrixTouch(), 3f);
    mGridBackgroundPaint = new Paint();
    mGridBackgroundPaint.setStyle(Style.FILL);
    // mGridBackgroundPaint.setColor(Color.WHITE);
    // light
    mGridBackgroundPaint.setColor(Color.rgb(240, 240, 240));
    // grey
    mBorderPaint = new Paint();
    mBorderPaint.setStyle(Style.STROKE);
    mBorderPaint.setColor(Color.BLACK);
    mBorderPaint.setStrokeWidth(Utils.convertDpToPixel(1f));
}
Also used : Transformer(com.github.mikephil.charting.utils.Transformer) ChartHighlighter(com.github.mikephil.charting.highlight.ChartHighlighter) YAxisRenderer(com.github.mikephil.charting.renderer.YAxisRenderer) Paint(android.graphics.Paint) XAxisRenderer(com.github.mikephil.charting.renderer.XAxisRenderer) YAxis(com.github.mikephil.charting.components.YAxis) BarLineChartTouchListener(com.github.mikephil.charting.listener.BarLineChartTouchListener)

Example 13 with Transformer

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

the class BarChartRenderer method drawHighlighted.

@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
    BarData barData = mChart.getBarData();
    for (Highlight high : indices) {
        IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
        if (set == null || !set.isHighlightEnabled())
            continue;
        BarEntry e = set.getEntryForXValue(high.getX(), high.getY());
        if (!isInBoundsX(e, set))
            continue;
        Transformer trans = mChart.getTransformer(set.getAxisDependency());
        mHighlightPaint.setColor(set.getHighLightColor());
        mHighlightPaint.setAlpha(set.getHighLightAlpha());
        boolean isStack = (high.getStackIndex() >= 0 && e.isStacked()) ? true : false;
        final float y1;
        final float y2;
        if (isStack) {
            if (mChart.isHighlightFullBarEnabled()) {
                y1 = e.getPositiveSum();
                y2 = -e.getNegativeSum();
            } else {
                Range range = e.getRanges()[high.getStackIndex()];
                y1 = range.from;
                y2 = range.to;
            }
        } else {
            y1 = e.getY();
            y2 = 0.f;
        }
        prepareBarHighlight(e.getX(), y1, y2, barData.getBarWidth() / 2f, trans);
        setHighlightDrawPos(high, mBarRect);
        c.drawRect(mBarRect, mHighlightPaint);
    }
}
Also used : Highlight(com.github.mikephil.charting.highlight.Highlight) Transformer(com.github.mikephil.charting.utils.Transformer) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) Range(com.github.mikephil.charting.highlight.Range) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 14 with Transformer

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

the class BarChartRenderer method drawDataSet.

protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
    Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
    mBarBorderPaint.setColor(dataSet.getBarBorderColor());
    mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));
    final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;
    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();
    // draw the bar shadow before the values
    if (mChart.isDrawBarShadowEnabled()) {
        mShadowPaint.setColor(dataSet.getBarShadowColor());
        BarData barData = mChart.getBarData();
        final float barWidth = barData.getBarWidth();
        final float barWidthHalf = barWidth / 2.0f;
        float x;
        for (int i = 0, count = Math.min((int) (Math.ceil((float) (dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount()); i < count; i++) {
            BarEntry e = dataSet.getEntryForIndex(i);
            x = e.getX();
            mBarShadowRectBuffer.left = x - barWidthHalf;
            mBarShadowRectBuffer.right = x + barWidthHalf;
            trans.rectValueToPixel(mBarShadowRectBuffer);
            if (!mViewPortHandler.isInBoundsLeft(mBarShadowRectBuffer.right))
                continue;
            if (!mViewPortHandler.isInBoundsRight(mBarShadowRectBuffer.left))
                break;
            mBarShadowRectBuffer.top = mViewPortHandler.contentTop();
            mBarShadowRectBuffer.bottom = mViewPortHandler.contentBottom();
            c.drawRect(mBarShadowRectBuffer, mShadowPaint);
        }
    }
    // initialize the buffer
    BarBuffer buffer = mBarBuffers[index];
    buffer.setPhases(phaseX, phaseY);
    buffer.setDataSet(index);
    buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
    buffer.setBarWidth(mChart.getBarData().getBarWidth());
    buffer.feed(dataSet);
    trans.pointValuesToPixel(buffer.buffer);
    final boolean isSingleColor = dataSet.getColors().size() == 1;
    if (isSingleColor) {
        mRenderPaint.setColor(dataSet.getColor());
    }
    for (int j = 0; j < buffer.size(); j += 4) {
        if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
            continue;
        if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
            break;
        if (!isSingleColor) {
            // Set the color for the currently drawn value. If the index
            // is out of bounds, reuse colors.
            mRenderPaint.setColor(dataSet.getColor(j / 4));
        }
        c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], mRenderPaint);
        if (drawBorder) {
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], mBarBorderPaint);
        }
    }
}
Also used : Transformer(com.github.mikephil.charting.utils.Transformer) BarData(com.github.mikephil.charting.data.BarData) BarEntry(com.github.mikephil.charting.data.BarEntry) Paint(android.graphics.Paint) BarBuffer(com.github.mikephil.charting.buffer.BarBuffer)

Example 15 with Transformer

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

the class CandleStickChartRenderer method drawDataSet.

@SuppressWarnings("ResourceAsColor")
protected void drawDataSet(Canvas c, ICandleDataSet dataSet) {
    Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
    float phaseY = mAnimator.getPhaseY();
    float barSpace = dataSet.getBarSpace();
    boolean showCandleBar = dataSet.getShowCandleBar();
    mXBounds.set(mChart, dataSet);
    mRenderPaint.setStrokeWidth(dataSet.getShadowWidth());
    // draw the body
    for (int j = mXBounds.min; j <= mXBounds.range + mXBounds.min; j++) {
        // get the entry
        CandleEntry e = dataSet.getEntryForIndex(j);
        if (e == null)
            continue;
        final float xPos = e.getX();
        final float open = e.getOpen();
        final float close = e.getClose();
        final float high = e.getHigh();
        final float low = e.getLow();
        if (showCandleBar) {
            // calculate the shadow
            mShadowBuffers[0] = xPos;
            mShadowBuffers[2] = xPos;
            mShadowBuffers[4] = xPos;
            mShadowBuffers[6] = xPos;
            if (open > close) {
                mShadowBuffers[1] = high * phaseY;
                mShadowBuffers[3] = open * phaseY;
                mShadowBuffers[5] = low * phaseY;
                mShadowBuffers[7] = close * phaseY;
            } else if (open < close) {
                mShadowBuffers[1] = high * phaseY;
                mShadowBuffers[3] = close * phaseY;
                mShadowBuffers[5] = low * phaseY;
                mShadowBuffers[7] = open * phaseY;
            } else {
                mShadowBuffers[1] = high * phaseY;
                mShadowBuffers[3] = open * phaseY;
                mShadowBuffers[5] = low * phaseY;
                mShadowBuffers[7] = mShadowBuffers[3];
            }
            trans.pointValuesToPixel(mShadowBuffers);
            if (dataSet.getShadowColorSameAsCandle()) {
                if (open > close)
                    mRenderPaint.setColor(dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getDecreasingColor());
                else if (open < close)
                    mRenderPaint.setColor(dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getIncreasingColor());
                else
                    mRenderPaint.setColor(dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getNeutralColor());
            } else {
                mRenderPaint.setColor(dataSet.getShadowColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getShadowColor());
            }
            mRenderPaint.setStyle(Paint.Style.STROKE);
            c.drawLines(mShadowBuffers, mRenderPaint);
            // calculate the body
            mBodyBuffers[0] = xPos - 0.5f + barSpace;
            mBodyBuffers[1] = close * phaseY;
            mBodyBuffers[2] = (xPos + 0.5f - barSpace);
            mBodyBuffers[3] = open * phaseY;
            trans.pointValuesToPixel(mBodyBuffers);
            // draw body differently for increasing and decreasing entry
            if (open > close) {
                if (dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE) {
                    mRenderPaint.setColor(dataSet.getColor(j));
                } else {
                    mRenderPaint.setColor(dataSet.getDecreasingColor());
                }
                mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle());
                c.drawRect(mBodyBuffers[0], mBodyBuffers[3], mBodyBuffers[2], mBodyBuffers[1], mRenderPaint);
            } else if (open < close) {
                if (dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE) {
                    mRenderPaint.setColor(dataSet.getColor(j));
                } else {
                    mRenderPaint.setColor(dataSet.getIncreasingColor());
                }
                mRenderPaint.setStyle(dataSet.getIncreasingPaintStyle());
                c.drawRect(mBodyBuffers[0], mBodyBuffers[1], mBodyBuffers[2], mBodyBuffers[3], mRenderPaint);
            } else {
                if (dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE) {
                    mRenderPaint.setColor(dataSet.getColor(j));
                } else {
                    mRenderPaint.setColor(dataSet.getNeutralColor());
                }
                c.drawLine(mBodyBuffers[0], mBodyBuffers[1], mBodyBuffers[2], mBodyBuffers[3], mRenderPaint);
            }
        } else {
            mRangeBuffers[0] = xPos;
            mRangeBuffers[1] = high * phaseY;
            mRangeBuffers[2] = xPos;
            mRangeBuffers[3] = low * phaseY;
            mOpenBuffers[0] = xPos - 0.5f + barSpace;
            mOpenBuffers[1] = open * phaseY;
            mOpenBuffers[2] = xPos;
            mOpenBuffers[3] = open * phaseY;
            mCloseBuffers[0] = xPos + 0.5f - barSpace;
            mCloseBuffers[1] = close * phaseY;
            mCloseBuffers[2] = xPos;
            mCloseBuffers[3] = close * phaseY;
            trans.pointValuesToPixel(mRangeBuffers);
            trans.pointValuesToPixel(mOpenBuffers);
            trans.pointValuesToPixel(mCloseBuffers);
            // draw the ranges
            int barColor;
            if (open > close)
                barColor = dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getDecreasingColor();
            else if (open < close)
                barColor = dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getIncreasingColor();
            else
                barColor = dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getNeutralColor();
            mRenderPaint.setColor(barColor);
            c.drawLine(mRangeBuffers[0], mRangeBuffers[1], mRangeBuffers[2], mRangeBuffers[3], mRenderPaint);
            c.drawLine(mOpenBuffers[0], mOpenBuffers[1], mOpenBuffers[2], mOpenBuffers[3], mRenderPaint);
            c.drawLine(mCloseBuffers[0], mCloseBuffers[1], mCloseBuffers[2], mCloseBuffers[3], mRenderPaint);
        }
    }
}
Also used : Transformer(com.github.mikephil.charting.utils.Transformer) CandleEntry(com.github.mikephil.charting.data.CandleEntry) Paint(android.graphics.Paint)

Aggregations

Transformer (com.github.mikephil.charting.utils.Transformer)16 Paint (android.graphics.Paint)10 Entry (com.github.mikephil.charting.data.Entry)6 BarEntry (com.github.mikephil.charting.data.BarEntry)5 Drawable (android.graphics.drawable.Drawable)4 BarBuffer (com.github.mikephil.charting.buffer.BarBuffer)4 MPPointF (com.github.mikephil.charting.utils.MPPointF)4 BarData (com.github.mikephil.charting.data.BarData)3 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)3 HorizontalBarBuffer (com.github.mikephil.charting.buffer.HorizontalBarBuffer)2 BubbleEntry (com.github.mikephil.charting.data.BubbleEntry)2 CandleEntry (com.github.mikephil.charting.data.CandleEntry)2 Highlight (com.github.mikephil.charting.highlight.Highlight)2 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 YAxis (com.github.mikephil.charting.components.YAxis)1 BubbleData (com.github.mikephil.charting.data.BubbleData)1 IValueFormatter (com.github.mikephil.charting.formatter.IValueFormatter)1 ChartHighlighter (com.github.mikephil.charting.highlight.ChartHighlighter)1