Search in sources :

Example 1 with DataPointInterface

use of com.jjoe64.graphview.series.DataPointInterface in project GraphView by appsthatmatter.

the class CursorMode method drawLegend.

protected void drawLegend(Canvas canvas) {
    mTextPaint.setTextSize(mStyles.textSize);
    mTextPaint.setColor(mStyles.textColor);
    int shapeSize = (int) (mStyles.textSize * 0.8d);
    // width
    int legendWidth = mStyles.width;
    if (legendWidth == 0) {
        // auto
        legendWidth = cachedLegendWidth;
        if (legendWidth == 0) {
            Rect textBounds = new Rect();
            for (Map.Entry<BaseSeries, DataPointInterface> entry : mCurrentSelection.entrySet()) {
                String txt = getTextForSeries(entry.getKey(), entry.getValue());
                mTextPaint.getTextBounds(txt, 0, txt.length(), textBounds);
                legendWidth = Math.max(legendWidth, textBounds.width());
            }
            if (legendWidth == 0)
                legendWidth = 1;
            // add shape size
            legendWidth += shapeSize + mStyles.padding * 2 + mStyles.spacing;
            cachedLegendWidth = legendWidth;
        }
    }
    float legendPosX = mPosX - mStyles.margin - legendWidth;
    if (legendPosX < 0) {
        legendPosX = 0;
    }
    // rect
    float legendHeight = (mStyles.textSize + mStyles.spacing) * (mCurrentSelection.size() + 1) - mStyles.spacing;
    float legendPosY = mPosY - legendHeight - 4.5f * mStyles.textSize;
    if (legendPosY < 0) {
        legendPosY = 0;
    }
    float lLeft;
    float lTop;
    lLeft = legendPosX;
    lTop = legendPosY;
    float lRight = lLeft + legendWidth;
    float lBottom = lTop + legendHeight + 2 * mStyles.padding;
    mRectPaint.setColor(mStyles.backgroundColor);
    canvas.drawRoundRect(new RectF(lLeft, lTop, lRight, lBottom), 8, 8, mRectPaint);
    mTextPaint.setFakeBoldText(true);
    canvas.drawText(mGraphView.getGridLabelRenderer().getLabelFormatter().formatLabel(mCurrentSelectionX, true), lLeft + mStyles.padding, lTop + mStyles.padding / 2 + mStyles.textSize, mTextPaint);
    mTextPaint.setFakeBoldText(false);
    int i = 1;
    for (Map.Entry<BaseSeries, DataPointInterface> entry : mCurrentSelection.entrySet()) {
        mRectPaint.setColor(entry.getKey().getColor());
        canvas.drawRect(new RectF(lLeft + mStyles.padding, lTop + mStyles.padding + (i * (mStyles.textSize + mStyles.spacing)), lLeft + mStyles.padding + shapeSize, lTop + mStyles.padding + (i * (mStyles.textSize + mStyles.spacing)) + shapeSize), mRectPaint);
        canvas.drawText(getTextForSeries(entry.getKey(), entry.getValue()), lLeft + mStyles.padding + shapeSize + mStyles.spacing, lTop + mStyles.padding / 2 + mStyles.textSize + (i * (mStyles.textSize + mStyles.spacing)), mTextPaint);
        i++;
    }
}
Also used : RectF(android.graphics.RectF) Rect(android.graphics.Rect) BaseSeries(com.jjoe64.graphview.series.BaseSeries) DataPointInterface(com.jjoe64.graphview.series.DataPointInterface) HashMap(java.util.HashMap) Map(java.util.Map) Paint(android.graphics.Paint)

Example 2 with DataPointInterface

use of com.jjoe64.graphview.series.DataPointInterface in project GraphView by appsthatmatter.

the class CursorMode method findCurrentDataPoint.

private void findCurrentDataPoint() {
    double selX = 0;
    mCurrentSelection.clear();
    for (Series series : mGraphView.getSeries()) {
        if (series instanceof BaseSeries) {
            DataPointInterface p = ((BaseSeries) series).findDataPointAtX(mPosX);
            if (p != null) {
                selX = p.getX();
                mCurrentSelection.put((BaseSeries) series, p);
            }
        }
    }
    if (!mCurrentSelection.isEmpty()) {
        mCurrentSelectionX = selX;
    }
}
Also used : BaseSeries(com.jjoe64.graphview.series.BaseSeries) Series(com.jjoe64.graphview.series.Series) BaseSeries(com.jjoe64.graphview.series.BaseSeries) DataPointInterface(com.jjoe64.graphview.series.DataPointInterface)

Example 3 with DataPointInterface

use of com.jjoe64.graphview.series.DataPointInterface in project GraphView by appsthatmatter.

the class Viewport method calcCompleteRange.

/**
     * caches the complete range (minX, maxX, minY, maxY)
     * by iterating all series and all datapoints and
     * stores it into #mCompleteRange
     *
     * for the x-range it will respect the series on the
     * second scale - not for y-values
     */
public void calcCompleteRange() {
    List<Series> series = mGraphView.getSeries();
    List<Series> seriesInclusiveSecondScale = new ArrayList<>(mGraphView.getSeries());
    if (mGraphView.mSecondScale != null) {
        seriesInclusiveSecondScale.addAll(mGraphView.mSecondScale.getSeries());
    }
    mCompleteRange.set(0d, 0d, 0d, 0d);
    if (!seriesInclusiveSecondScale.isEmpty() && !seriesInclusiveSecondScale.get(0).isEmpty()) {
        double d = seriesInclusiveSecondScale.get(0).getLowestValueX();
        for (Series s : seriesInclusiveSecondScale) {
            if (!s.isEmpty() && d > s.getLowestValueX()) {
                d = s.getLowestValueX();
            }
        }
        mCompleteRange.left = d;
        d = seriesInclusiveSecondScale.get(0).getHighestValueX();
        for (Series s : seriesInclusiveSecondScale) {
            if (!s.isEmpty() && d < s.getHighestValueX()) {
                d = s.getHighestValueX();
            }
        }
        mCompleteRange.right = d;
        if (!series.isEmpty() && !series.get(0).isEmpty()) {
            d = series.get(0).getLowestValueY();
            for (Series s : series) {
                if (!s.isEmpty() && d > s.getLowestValueY()) {
                    d = s.getLowestValueY();
                }
            }
            mCompleteRange.bottom = d;
            d = series.get(0).getHighestValueY();
            for (Series s : series) {
                if (!s.isEmpty() && d < s.getHighestValueY()) {
                    d = s.getHighestValueY();
                }
            }
            mCompleteRange.top = d;
        }
    }
    // calc current viewport bounds
    if (mYAxisBoundsStatus == AxisBoundsStatus.AUTO_ADJUSTED) {
        mYAxisBoundsStatus = AxisBoundsStatus.INITIAL;
    }
    if (mYAxisBoundsStatus == AxisBoundsStatus.INITIAL) {
        mCurrentViewport.top = mCompleteRange.top;
        mCurrentViewport.bottom = mCompleteRange.bottom;
    }
    if (mXAxisBoundsStatus == AxisBoundsStatus.AUTO_ADJUSTED) {
        mXAxisBoundsStatus = AxisBoundsStatus.INITIAL;
    }
    if (mXAxisBoundsStatus == AxisBoundsStatus.INITIAL) {
        mCurrentViewport.left = mCompleteRange.left;
        mCurrentViewport.right = mCompleteRange.right;
    } else if (mXAxisBoundsManual && !mYAxisBoundsManual && mCompleteRange.width() != 0) {
        // get highest/lowest of current viewport
        // lowest
        double d = Double.MAX_VALUE;
        for (Series s : series) {
            Iterator<DataPointInterface> values = s.getValues(mCurrentViewport.left, mCurrentViewport.right);
            while (values.hasNext()) {
                double v = values.next().getY();
                if (d > v) {
                    d = v;
                }
            }
        }
        if (d != Double.MAX_VALUE) {
            mCurrentViewport.bottom = d;
        }
        // highest
        d = Double.MIN_VALUE;
        for (Series s : series) {
            Iterator<DataPointInterface> values = s.getValues(mCurrentViewport.left, mCurrentViewport.right);
            while (values.hasNext()) {
                double v = values.next().getY();
                if (d < v) {
                    d = v;
                }
            }
        }
        if (d != Double.MIN_VALUE) {
            mCurrentViewport.top = d;
        }
    }
    // fixes blank screen when range is zero
    if (mCurrentViewport.left == mCurrentViewport.right)
        mCurrentViewport.right++;
    if (mCurrentViewport.top == mCurrentViewport.bottom)
        mCurrentViewport.top++;
}
Also used : Series(com.jjoe64.graphview.series.Series) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator)

Aggregations

BaseSeries (com.jjoe64.graphview.series.BaseSeries)2 DataPointInterface (com.jjoe64.graphview.series.DataPointInterface)2 Series (com.jjoe64.graphview.series.Series)2 Paint (android.graphics.Paint)1 Rect (android.graphics.Rect)1 RectF (android.graphics.RectF)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1