Search in sources :

Example 41 with MPPointF

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

the class BarLineChartTouchListener method performZoom.

/**
     * Performs the all operations necessary for pinch and axis zoom.
     *
     * @param event
     */
private void performZoom(MotionEvent event) {
    if (event.getPointerCount() >= 2) {
        // two finger zoom
        OnChartGestureListener l = mChart.getOnChartGestureListener();
        // get the distance between the pointers of the touch event
        float totalDist = spacing(event);
        if (totalDist > mMinScalePointerDistance) {
            // get the translation
            MPPointF t = getTrans(mTouchPointCenter.x, mTouchPointCenter.y);
            ViewPortHandler h = mChart.getViewPortHandler();
            // take actions depending on the activated touch mode
            if (mTouchMode == PINCH_ZOOM) {
                mLastGesture = ChartGesture.PINCH_ZOOM;
                // total scale
                float scale = totalDist / mSavedDist;
                boolean isZoomingOut = (scale < 1);
                boolean canZoomMoreX = isZoomingOut ? h.canZoomOutMoreX() : h.canZoomInMoreX();
                boolean canZoomMoreY = isZoomingOut ? h.canZoomOutMoreY() : h.canZoomInMoreY();
                float scaleX = (mChart.isScaleXEnabled()) ? scale : 1f;
                float scaleY = (mChart.isScaleYEnabled()) ? scale : 1f;
                if (canZoomMoreY || canZoomMoreX) {
                    mMatrix.set(mSavedMatrix);
                    mMatrix.postScale(scaleX, scaleY, t.x, t.y);
                    if (l != null)
                        l.onChartScale(event, scaleX, scaleY);
                }
            } else if (mTouchMode == X_ZOOM && mChart.isScaleXEnabled()) {
                mLastGesture = ChartGesture.X_ZOOM;
                float xDist = getXDist(event);
                // x-axis scale
                float scaleX = xDist / mSavedXDist;
                boolean isZoomingOut = (scaleX < 1);
                boolean canZoomMoreX = isZoomingOut ? h.canZoomOutMoreX() : h.canZoomInMoreX();
                if (canZoomMoreX) {
                    mMatrix.set(mSavedMatrix);
                    mMatrix.postScale(scaleX, 1f, t.x, t.y);
                    if (l != null)
                        l.onChartScale(event, scaleX, 1f);
                }
            } else if (mTouchMode == Y_ZOOM && mChart.isScaleYEnabled()) {
                mLastGesture = ChartGesture.Y_ZOOM;
                float yDist = getYDist(event);
                // y-axis scale
                float scaleY = yDist / mSavedYDist;
                boolean isZoomingOut = (scaleY < 1);
                boolean canZoomMoreY = isZoomingOut ? h.canZoomOutMoreY() : h.canZoomInMoreY();
                if (canZoomMoreY) {
                    mMatrix.set(mSavedMatrix);
                    mMatrix.postScale(1f, scaleY, t.x, t.y);
                    if (l != null)
                        l.onChartScale(event, 1f, scaleY);
                }
            }
            MPPointF.recycleInstance(t);
        }
    }
}
Also used : MPPointF(com.github.mikephil.charting.utils.MPPointF) ViewPortHandler(com.github.mikephil.charting.utils.ViewPortHandler)

Example 42 with MPPointF

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

the class PieRadarChartBase method calculateOffsets.

@Override
public void calculateOffsets() {
    float legendLeft = 0f, legendRight = 0f, legendBottom = 0f, legendTop = 0f;
    if (mLegend != null && mLegend.isEnabled() && !mLegend.isDrawInsideEnabled()) {
        float fullLegendWidth = Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent());
        switch(mLegend.getOrientation()) {
            case VERTICAL:
                {
                    float xLegendOffset = 0.f;
                    if (mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.LEFT || mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.RIGHT) {
                        if (mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.CENTER) {
                            // this is the space between the legend and the chart
                            final float spacing = Utils.convertDpToPixel(13f);
                            xLegendOffset = fullLegendWidth + spacing;
                        } else {
                            // this is the space between the legend and the chart
                            float spacing = Utils.convertDpToPixel(8f);
                            float legendWidth = fullLegendWidth + spacing;
                            float legendHeight = mLegend.mNeededHeight + mLegend.mTextHeightMax;
                            MPPointF center = getCenter();
                            float bottomX = mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.RIGHT ? getWidth() - legendWidth + 15.f : legendWidth - 15.f;
                            float bottomY = legendHeight + 15.f;
                            float distLegend = distanceToCenter(bottomX, bottomY);
                            MPPointF reference = getPosition(center, getRadius(), getAngleForPoint(bottomX, bottomY));
                            float distReference = distanceToCenter(reference.x, reference.y);
                            float minOffset = Utils.convertDpToPixel(5f);
                            if (bottomY >= center.y && getHeight() - legendWidth > getWidth()) {
                                xLegendOffset = legendWidth;
                            } else if (distLegend < distReference) {
                                float diff = distReference - distLegend;
                                xLegendOffset = minOffset + diff;
                            }
                            MPPointF.recycleInstance(center);
                            MPPointF.recycleInstance(reference);
                        }
                    }
                    switch(mLegend.getHorizontalAlignment()) {
                        case LEFT:
                            legendLeft = xLegendOffset;
                            break;
                        case RIGHT:
                            legendRight = xLegendOffset;
                            break;
                        case CENTER:
                            switch(mLegend.getVerticalAlignment()) {
                                case TOP:
                                    legendTop = Math.min(mLegend.mNeededHeight, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
                                    break;
                                case BOTTOM:
                                    legendBottom = Math.min(mLegend.mNeededHeight, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
                                    break;
                            }
                            break;
                    }
                }
                break;
            case HORIZONTAL:
                float yLegendOffset = 0.f;
                if (mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.TOP || mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.BOTTOM) {
                    // It's possible that we do not need this offset anymore as it
                    //   is available through the extraOffsets, but changing it can mean
                    //   changing default visibility for existing apps.
                    float yOffset = getRequiredLegendOffset();
                    yLegendOffset = Math.min(mLegend.mNeededHeight + yOffset, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
                    switch(mLegend.getVerticalAlignment()) {
                        case TOP:
                            legendTop = yLegendOffset;
                            break;
                        case BOTTOM:
                            legendBottom = yLegendOffset;
                            break;
                    }
                }
                break;
        }
        legendLeft += getRequiredBaseOffset();
        legendRight += getRequiredBaseOffset();
        legendTop += getRequiredBaseOffset();
        legendBottom += getRequiredBaseOffset();
    }
    float minOffset = Utils.convertDpToPixel(mMinOffset);
    if (this instanceof RadarChart) {
        XAxis x = this.getXAxis();
        if (x.isEnabled() && x.isDrawLabelsEnabled()) {
            minOffset = Math.max(minOffset, x.mLabelRotatedWidth);
        }
    }
    legendTop += getExtraTopOffset();
    legendRight += getExtraRightOffset();
    legendBottom += getExtraBottomOffset();
    legendLeft += getExtraLeftOffset();
    float offsetLeft = Math.max(minOffset, legendLeft);
    float offsetTop = Math.max(minOffset, legendTop);
    float offsetRight = Math.max(minOffset, legendRight);
    float offsetBottom = Math.max(minOffset, Math.max(getRequiredBaseOffset(), legendBottom));
    mViewPortHandler.restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom);
    if (mLogEnabled)
        Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom);
}
Also used : MPPointF(com.github.mikephil.charting.utils.MPPointF) XAxis(com.github.mikephil.charting.components.XAxis)

Example 43 with MPPointF

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

the class YAxisRendererRadarChart method renderLimitLines.

@Override
public void renderLimitLines(Canvas c) {
    List<LimitLine> limitLines = mYAxis.getLimitLines();
    if (limitLines == null)
        return;
    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);
    for (int i = 0; i < limitLines.size(); i++) {
        LimitLine l = limitLines.get(i);
        if (!l.isEnabled())
            continue;
        mLimitLinePaint.setColor(l.getLineColor());
        mLimitLinePaint.setPathEffect(l.getDashPathEffect());
        mLimitLinePaint.setStrokeWidth(l.getLineWidth());
        float r = (l.getLimit() - mChart.getYChartMin()) * factor;
        Path limitPath = mRenderLimitLinesPathBuffer;
        limitPath.reset();
        for (int j = 0; j < mChart.getData().getMaxEntryCountSet().getEntryCount(); j++) {
            Utils.getPosition(center, r, sliceangle * j + mChart.getRotationAngle(), pOut);
            if (j == 0)
                limitPath.moveTo(pOut.x, pOut.y);
            else
                limitPath.lineTo(pOut.x, pOut.y);
        }
        limitPath.close();
        c.drawPath(limitPath, mLimitLinePaint);
    }
    MPPointF.recycleInstance(center);
    MPPointF.recycleInstance(pOut);
}
Also used : Path(android.graphics.Path) MPPointF(com.github.mikephil.charting.utils.MPPointF) LimitLine(com.github.mikephil.charting.components.LimitLine)

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