Search in sources :

Example 11 with Viewport

use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.

the class AxesRenderer method prepareAutoGeneratedAxis.

private void prepareAutoGeneratedAxis(Axis axis, int position) {
    final Viewport visibleViewport = computator.getVisibleViewport();
    final Rect contentRect = computator.getContentRectMinusAllMargins();
    boolean isAxisVertical = isAxisVertical(position);
    float start, stop;
    int contentRectDimension;
    if (isAxisVertical) {
        start = visibleViewport.bottom;
        stop = visibleViewport.top;
        contentRectDimension = contentRect.height();
    } else {
        start = visibleViewport.left;
        stop = visibleViewport.right;
        contentRectDimension = contentRect.width();
    }
    FloatUtils.computeAutoGeneratedAxisValues(start, stop, Math.abs(contentRectDimension) / labelDimensionForStepsTab[position] / 2, autoValuesBufferTab[position]);
    //Reinitialize tab to hold lines coordinates.
    if (axis.hasLines() && (linesDrawBufferTab[position].length < autoValuesBufferTab[position].valuesNumber * 4)) {
        linesDrawBufferTab[position] = new float[autoValuesBufferTab[position].valuesNumber * 4];
    }
    //Reinitialize tabs to hold all raw and auto values.
    if (rawValuesTab[position].length < autoValuesBufferTab[position].valuesNumber) {
        rawValuesTab[position] = new float[autoValuesBufferTab[position].valuesNumber];
    }
    if (autoValuesToDrawTab[position].length < autoValuesBufferTab[position].valuesNumber) {
        autoValuesToDrawTab[position] = new float[autoValuesBufferTab[position].valuesNumber];
    }
    float rawValue;
    int valueToDrawIndex = 0;
    for (int i = 0; i < autoValuesBufferTab[position].valuesNumber; ++i) {
        if (isAxisVertical) {
            rawValue = computator.computeRawY(autoValuesBufferTab[position].values[i]);
        } else {
            rawValue = computator.computeRawX(autoValuesBufferTab[position].values[i]);
        }
        if (checkRawValue(contentRect, rawValue, axis.isInside(), position, isAxisVertical)) {
            rawValuesTab[position][valueToDrawIndex] = rawValue;
            autoValuesToDrawTab[position][valueToDrawIndex] = autoValuesBufferTab[position].values[i];
            ++valueToDrawIndex;
        }
    }
    valuesToDrawNumTab[position] = valueToDrawIndex;
}
Also used : Rect(android.graphics.Rect) Viewport(lecho.lib.hellocharts.model.Viewport) Paint(android.graphics.Paint)

Example 12 with Viewport

use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.

the class AxesRenderer method prepareCustomAxis.

private void prepareCustomAxis(Axis axis, int position) {
    final Viewport maxViewport = computator.getMaximumViewport();
    final Viewport visibleViewport = computator.getVisibleViewport();
    final Rect contentRect = computator.getContentRectMinusAllMargins();
    boolean isAxisVertical = isAxisVertical(position);
    float viewportMin, viewportMax;
    float scale = 1;
    if (isAxisVertical) {
        if (maxViewport.height() > 0 && visibleViewport.height() > 0) {
            scale = contentRect.height() * (maxViewport.height() / visibleViewport.height());
        }
        viewportMin = visibleViewport.bottom;
        viewportMax = visibleViewport.top;
    } else {
        if (maxViewport.width() > 0 && visibleViewport.width() > 0) {
            scale = contentRect.width() * (maxViewport.width() / visibleViewport.width());
        }
        viewportMin = visibleViewport.left;
        viewportMax = visibleViewport.right;
    }
    if (scale == 0) {
        scale = 1;
    }
    int module = (int) Math.max(1, Math.ceil((axis.getValues().size() * labelDimensionForStepsTab[position] * 1.5) / scale));
    //Reinitialize tab to hold lines coordinates.
    if (axis.hasLines() && (linesDrawBufferTab[position].length < axis.getValues().size() * 4)) {
        linesDrawBufferTab[position] = new float[axis.getValues().size() * 4];
    }
    //Reinitialize tabs to hold all raw values to draw.
    if (rawValuesTab[position].length < axis.getValues().size()) {
        rawValuesTab[position] = new float[axis.getValues().size()];
    }
    //Reinitialize tabs to hold all raw values to draw.
    if (valuesToDrawTab[position].length < axis.getValues().size()) {
        valuesToDrawTab[position] = new AxisValue[axis.getValues().size()];
    }
    float rawValue;
    int valueIndex = 0;
    int valueToDrawIndex = 0;
    for (AxisValue axisValue : axis.getValues()) {
        // Draw axis values that are within visible viewport.
        final float value = axisValue.getValue();
        if (value >= viewportMin && value <= viewportMax) {
            // Draw axis values that have 0 module value, this will hide some labels if there is no place for them.
            if (0 == valueIndex % module) {
                if (isAxisVertical) {
                    rawValue = computator.computeRawY(value);
                } else {
                    rawValue = computator.computeRawX(value);
                }
                if (checkRawValue(contentRect, rawValue, axis.isInside(), position, isAxisVertical)) {
                    rawValuesTab[position][valueToDrawIndex] = rawValue;
                    valuesToDrawTab[position][valueToDrawIndex] = axisValue;
                    ++valueToDrawIndex;
                }
            }
            // If within viewport - increment valueIndex;
            ++valueIndex;
        }
    }
    valuesToDrawNumTab[position] = valueToDrawIndex;
}
Also used : Rect(android.graphics.Rect) AxisValue(lecho.lib.hellocharts.model.AxisValue) Viewport(lecho.lib.hellocharts.model.Viewport) Paint(android.graphics.Paint)

Example 13 with Viewport

use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.

the class AbstractChartView method setZoomLevel.

@Override
public void setZoomLevel(float x, float y, float zoomLevel) {
    Viewport zoomViewport = computeZoomViewport(x, y, zoomLevel);
    setCurrentViewport(zoomViewport);
}
Also used : Viewport(lecho.lib.hellocharts.model.Viewport)

Example 14 with Viewport

use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.

the class AbstractChartView method computeScrollViewport.

private Viewport computeScrollViewport(float x, float y) {
    Viewport maxViewport = getMaximumViewport();
    Viewport currentViewport = getCurrentViewport();
    Viewport scrollViewport = new Viewport(currentViewport);
    if (maxViewport.contains(x, y)) {
        final float width = currentViewport.width();
        final float height = currentViewport.height();
        final float halfWidth = width / 2;
        final float halfHeight = height / 2;
        float left = x - halfWidth;
        float top = y + halfHeight;
        left = Math.max(maxViewport.left, Math.min(left, maxViewport.right - width));
        top = Math.max(maxViewport.bottom + height, Math.min(top, maxViewport.top));
        scrollViewport.set(left, top, left + width, top - height);
    }
    return scrollViewport;
}
Also used : Viewport(lecho.lib.hellocharts.model.Viewport)

Example 15 with Viewport

use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.

the class AbstractChartView method computeZoomViewport.

private Viewport computeZoomViewport(float x, float y, float zoomLevel) {
    final Viewport maxViewport = getMaximumViewport();
    Viewport zoomViewport = new Viewport(getMaximumViewport());
    if (maxViewport.contains(x, y)) {
        if (zoomLevel < 1) {
            zoomLevel = 1;
        } else if (zoomLevel > getMaxZoom()) {
            zoomLevel = getMaxZoom();
        }
        final float newWidth = zoomViewport.width() / zoomLevel;
        final float newHeight = zoomViewport.height() / zoomLevel;
        final float halfWidth = newWidth / 2;
        final float halfHeight = newHeight / 2;
        float left = x - halfWidth;
        float right = x + halfWidth;
        float top = y + halfHeight;
        float bottom = y - halfHeight;
        if (left < maxViewport.left) {
            left = maxViewport.left;
            right = left + newWidth;
        } else if (right > maxViewport.right) {
            right = maxViewport.right;
            left = right - newWidth;
        }
        if (top > maxViewport.top) {
            top = maxViewport.top;
            bottom = top - newHeight;
        } else if (bottom < maxViewport.bottom) {
            bottom = maxViewport.bottom;
            top = bottom + newHeight;
        }
        ZoomType zoomType = getZoomType();
        if (ZoomType.HORIZONTAL_AND_VERTICAL == zoomType) {
            zoomViewport.set(left, top, right, bottom);
        } else if (ZoomType.HORIZONTAL == zoomType) {
            zoomViewport.left = left;
            zoomViewport.right = right;
        } else if (ZoomType.VERTICAL == zoomType) {
            zoomViewport.top = top;
            zoomViewport.bottom = bottom;
        }
    }
    return zoomViewport;
}
Also used : ZoomType(lecho.lib.hellocharts.gesture.ZoomType) Viewport(lecho.lib.hellocharts.model.Viewport)

Aggregations

Viewport (lecho.lib.hellocharts.model.Viewport)16 Rect (android.graphics.Rect)4 Paint (android.graphics.Paint)2 ZoomType (lecho.lib.hellocharts.gesture.ZoomType)1 AxisValue (lecho.lib.hellocharts.model.AxisValue)1