/** * This will move the center of the current viewport to the specified * x and y value. * This also refreshes the chart by calling invalidate(). * * @param xValue * @param yValue * @param axis - which axis should be used as a reference for the y axis */ public void centerViewTo(float xValue, float yValue, AxisDependency axis) { float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY(); float xInView = getXAxis().mAxisRange / mViewPortHandler.getScaleX(); Runnable job = MoveViewJob.getInstance(mViewPortHandler, xValue - xInView / 2f, yValue + yInView / 2f, getTransformer(axis), this); addViewportJob(job); }
@Override public boolean onDoubleTap(MotionEvent e) { mLastGesture = ChartGesture.DOUBLE_TAP; OnChartGestureListener l = mChart.getOnChartGestureListener(); if (l != null) { l.onChartDoubleTapped(e); } // check if double-tap zooming is enabled if (mChart.isDoubleTapToZoomEnabled() && mChart.getData().getEntryCount() > 0) { MPPointF trans = getTrans(e.getX(), e.getY()); mChart.zoom(mChart.isScaleXEnabled() ? 1.4f : 1f, mChart.isScaleYEnabled() ? 1.4f : 1f, trans.x, trans.y); if (mChart.isLogEnabled()) Log.i("BarlineChartTouch", "Double-Tap, Zooming In, x: " + trans.x + ", y: " + trans.y); MPPointF.recycleInstance(trans); } return super.onDoubleTap(e); }
@Override public void notifyDataSetChanged() { if (mData == null) { if (mLogEnabled) Log.i(LOG_TAG, "Preparing... DATA NOT SET."); return; } else { if (mLogEnabled) Log.i(LOG_TAG, "Preparing..."); } if (mRenderer != null) mRenderer.initBuffers(); calcMinMax(); mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum, mAxisLeft.isInverted()); mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum, mAxisRight.isInverted()); mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false); if (mLegend != null) mLegendRenderer.computeLegend(mData); calculateOffsets(); }
/** * Moves the left side of the current viewport to the specified x-position. * This also refreshes the chart by calling invalidate(). * * @param xValue */ public void moveViewToX(float xValue) { Runnable job = MoveViewJob.getInstance(mViewPortHandler, xValue, 0f, getTransformer(AxisDependency.LEFT), this); addViewportJob(job); }
/** * Centers the viewport to the specified y value on the y-axis. * This also refreshes the chart by calling invalidate(). * * @param yValue * @param axis - which axis should be used as a reference for the y-axis */ public void centerViewToY(float yValue, AxisDependency axis) { float valsInView = getAxisRange(axis) / mViewPortHandler.getScaleY(); Runnable job = MoveViewJob.getInstance(mViewPortHandler, 0f, yValue + valsInView / 2f, getTransformer(axis), this); addViewportJob(job); }
/** * Zooms out to original size. */ public void resetZoom() { mViewPortHandler.resetZoom(mZoomMatrixBuffer); mViewPortHandler.refresh(mZoomMatrixBuffer, this, false); // Range might have changed, which means that Y-axis labels // could have changed in size, affecting Y-axis size. // So we need to recalculate offsets. calculateOffsets(); postInvalidate(); }
if (!mChart.isDragEnabled() && (!mChart.isScaleXEnabled() && !mChart.isScaleYEnabled())) return true; mChart.disableScroll(); if (mChart.isPinchZoomEnabled()) { mTouchMode = PINCH_ZOOM; } else { if (mChart.isScaleXEnabled() != mChart.isScaleYEnabled()) { mTouchMode = mChart.isScaleXEnabled() ? X_ZOOM : Y_ZOOM; } else { mTouchMode = mSavedXDist > mSavedYDist ? X_ZOOM : Y_ZOOM; mChart.disableScroll(); float x = mChart.isDragXEnabled() ? event.getX() - mTouchStartPoint.x : 0.f; float y = mChart.isDragYEnabled() ? event.getY() - mTouchStartPoint.y : 0.f; mChart.disableScroll(); if (mChart.isScaleXEnabled() || mChart.isScaleYEnabled()) performZoom(event); if (mChart.isDragEnabled()) { boolean shouldPan = !mChart.isFullyZoomedOut() || !mChart.hasNoDragOffset(); if ((mChart.isDragXEnabled() || distanceY >= distanceX) &&
calculateLegendOffsets(mOffsetsBuffer); offsetTop += getExtraTopOffset(); offsetRight += getExtraRightOffset(); offsetBottom += getExtraBottomOffset(); offsetLeft += getExtraLeftOffset(); prepareOffsetMatrix(); prepareValuePxMatrix();
drawGridBackground(canvas); autoScale(); if (valuesToHighlight()) mRenderer.drawHighlighted(canvas, mIndicesToHighlight); mAxisRendererRight.renderAxisLabels(canvas); if (isClipValuesToContentEnabled()) { clipRestoreCount = canvas.save(); canvas.clipRect(mViewPortHandler.getContentRect()); drawDescription(canvas); drawMarkers(canvas);
mDecelerationVelocity.x *= mChart.getDragDecelerationFrictionCoef(); mDecelerationVelocity.y *= mChart.getDragDecelerationFrictionCoef(); mDecelerationCurrentPoint.y, 0); float dragDistanceX = mChart.isDragXEnabled() ? mDecelerationCurrentPoint.x - mTouchStartPoint.x : 0.f; float dragDistanceY = mChart.isDragYEnabled() ? mDecelerationCurrentPoint.y - mTouchStartPoint.y : 0.f; mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, false); mChart.calculateOffsets(); mChart.postInvalidate();
@Override public void run() { Matrix save = mRunMatrixBuffer; mViewPortHandler.zoom(scaleX, scaleY, save); mViewPortHandler.refresh(save, view, false); float yValsInView = ((BarLineChartBase) view).getAxis(axisDependency).mAxisRange / mViewPortHandler.getScaleY(); float xValsInView = ((BarLineChartBase) view).getXAxis().mAxisRange / mViewPortHandler.getScaleX(); pts[0] = xValue - xValsInView / 2f; pts[1] = yValue + yValsInView / 2f; mTrans.pointValuesToPixel(pts); mViewPortHandler.translate(pts, save); mViewPortHandler.refresh(save, view, false); ((BarLineChartBase) view).calculateOffsets(); view.postInvalidate(); recycleInstance(this); }
chart.getAxisLeft().setStartAtZero(false); chart.getAxisRight().setStartAtZero(false); chart.getAxisLeft().setDrawGridLines(false); chart.getAxisRight().setDrawGridLines(false); chart.getXAxis().setDrawGridLines(false); chart.getAxisLeft().setValueFormatter(new GraphValueFormatter()); chart.getAxisRight().setValueFormatter(new GraphValueFormatter()); chart.setDrawBorders(false); chart.setDescription(""); chart.setTouchEnabled(false); chart.setScaleEnabled(false); chart.setPinchZoom(false); chart.setDoubleTapToZoomEnabled(false); chart.setDrawGridBackground(false); chart.getData().setHighlightEnabled(true); chart.getAxisLeft().setAxisMaxValue(PitchCalculator.maxPitch.floatValue()); chart.getAxisRight().setAxisMaxValue(PitchCalculator.maxPitch.floatValue()); chart.getAxisRight().setAxisMinValue(PitchCalculator.minPitch.floatValue()); chart.getAxisLeft().setAxisMinValue(PitchCalculator.minPitch.floatValue()); chart.getAxisLeft().setValueFormatter(new GraphValueFormatter()); chart.getAxisRight().setValueFormatter(new GraphValueFormatter()); chart.setDrawBorders(false);
/** * Zooms by the specified scale factor to the specified values on the specified axis. * * @param scaleX * @param scaleY * @param xValue * @param yValue * @param axis * @param duration */ @TargetApi(11) public void zoomAndCenterAnimated(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis, long duration) { MPPointD origin = getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop(), axis); Runnable job = AnimatedZoomJob.getInstance(mViewPortHandler, this, getTransformer(axis), getAxis(axis), mXAxis .mAxisRange, scaleX, scaleY, mViewPortHandler.getScaleX(), mViewPortHandler.getScaleY(), xValue, yValue, (float) origin.x, (float) origin.y, duration); addViewportJob(job); MPPointD.recycleInstance(origin); }
/** * Performs auto scaling of the axis by recalculating the minimum and maximum y-values based on the entries currently in view. */ protected void autoScale() { final float fromX = getLowestVisibleX(); final float toX = getHighestVisibleX(); mData.calcMinMaxY(fromX, toX); mXAxis.calculate(mData.getXMin(), mData.getXMax()); // calculate axis range (min / max) according to provided data if (mAxisLeft.isEnabled()) mAxisLeft.calculate(mData.getYMin(AxisDependency.LEFT), mData.getYMax(AxisDependency.LEFT)); if (mAxisRight.isEnabled()) mAxisRight.calculate(mData.getYMin(AxisDependency.RIGHT), mData.getYMax(AxisDependency.RIGHT)); calculateOffsets(); }
protected void setup(Chart<?> chart) { mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); // no description text chart.getDescription().setEnabled(false); // enable touch gestures chart.setTouchEnabled(true); if (chart instanceof BarLineChartBase) { BarLineChartBase mChart = (BarLineChartBase) chart; mChart.setDrawGridBackground(false); // enable scaling and dragging mChart.setDragEnabled(true); mChart.setScaleEnabled(true); // if disabled, scaling can be done on x- and y-axis separately mChart.setPinchZoom(false); YAxis leftAxis = mChart.getAxisLeft(); leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines leftAxis.setTypeface(mTf); leftAxis.setTextSize(8f); leftAxis.setTextColor(Color.DKGRAY); leftAxis.setValueFormatter(new PercentFormatter()); XAxis xAxis = mChart.getXAxis(); xAxis.setTypeface(mTf); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setTextSize(8f); xAxis.setTextColor(Color.DKGRAY); mChart.getAxisRight().setEnabled(false); } }
/** * Resets all custom offsets set via setViewPortOffsets(...) method. Allows * the chart to again calculate all offsets automatically. */ public void resetViewPortOffsets() { mCustomViewPortEnabled = false; calculateOffsets(); }
if (!mChart.isDragEnabled() && (!mChart.isScaleXEnabled() && !mChart.isScaleYEnabled())) return true; mChart.disableScroll(); if (mChart.isPinchZoomEnabled()) { mTouchMode = PINCH_ZOOM; } else { if (mChart.isScaleXEnabled() != mChart.isScaleYEnabled()) { mTouchMode = mChart.isScaleXEnabled() ? X_ZOOM : Y_ZOOM; } else { mTouchMode = mSavedXDist > mSavedYDist ? X_ZOOM : Y_ZOOM; mChart.disableScroll(); float x = mChart.isDragXEnabled() ? event.getX() - mTouchStartPoint.x : 0.f; float y = mChart.isDragYEnabled() ? event.getY() - mTouchStartPoint.y : 0.f; mChart.disableScroll(); if (mChart.isScaleXEnabled() || mChart.isScaleYEnabled()) performZoom(event); if (mChart.isDragEnabled()) { boolean shouldPan = !mChart.isFullyZoomedOut() || !mChart.hasNoDragOffset(); if ((mChart.isDragXEnabled() || distanceY >= distanceX) &&
calculateLegendOffsets(mOffsetsBuffer); offsetTop += getExtraTopOffset(); offsetRight += getExtraRightOffset(); offsetBottom += getExtraBottomOffset(); offsetLeft += getExtraLeftOffset(); prepareOffsetMatrix(); prepareValuePxMatrix();
drawGridBackground(canvas); autoScale(); if (valuesToHighlight()) mRenderer.drawHighlighted(canvas, mIndicesToHighlight); mAxisRendererRight.renderAxisLabels(canvas); if (isClipValuesToContentEnabled()) { clipRestoreCount = canvas.save(); canvas.clipRect(mViewPortHandler.getContentRect()); drawDescription(canvas); drawMarkers(canvas);