Java Code Examples for lecho.lib.hellocharts.model.Viewport#height()

The following examples show how to use lecho.lib.hellocharts.model.Viewport#height() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractChartView.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: ChartScroller.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
public boolean computeScrollOffset(ChartComputator computator) {
    if (scroller.computeScrollOffset()) {
        // The scroller isn't finished, meaning a fling or programmatic pan operation is
        // currently active.

        final Viewport maxViewport = computator.getMaximumViewport();

        computator.computeScrollSurfaceSize(surfaceSizeBuffer);

        final float currXRange = maxViewport.left + maxViewport.width() * scroller.getCurrX() /
                surfaceSizeBuffer.x;
        final float currYRange = maxViewport.top - maxViewport.height() * scroller.getCurrY() /
                surfaceSizeBuffer.y;

        computator.setViewportTopLeft(currXRange, currYRange);

        return true;
    }

    return false;
}
 
Example 3
Source File: GoodBadChartActivity.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_good_bad, container, false);

    chart = (LineChartView) rootView.findViewById(R.id.chart);

    generateDefaultData();
    chart.setLineChartData(data);

    // Increase viewport height for better look
    Viewport v = chart.getMaximumViewport();
    float dy = v.height() * 0.2f;
    v.inset(0, -dy);
    chart.setMaximumViewport(v);
    chart.setCurrentViewport(v);

    return rootView;
}
 
Example 4
Source File: PreviewColumnChartActivity.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
private void previewY() {
    Viewport tempViewport = new Viewport(chart.getMaximumViewport());
    float dy = tempViewport.height() / 4;
    tempViewport.inset(0, dy);
    previewChart.setCurrentViewportWithAnimation(tempViewport);
    previewChart.setZoomType(ZoomType.VERTICAL);
}
 
Example 5
Source File: PreviewColumnChartActivity.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
private void previewXY() {
    // Better to not modify viewport of any chart directly so create a copy.
    Viewport tempViewport = new Viewport(chart.getMaximumViewport());
    // Make temp viewport smaller.
    float dx = tempViewport.width() / 4;
    float dy = tempViewport.height() / 4;
    tempViewport.inset(dx, dy);
    previewChart.setCurrentViewportWithAnimation(tempViewport);
}
 
Example 6
Source File: PreviewLineChartActivity.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
private void previewY() {
    Viewport tempViewport = new Viewport(chart.getMaximumViewport());
    float dy = tempViewport.height() / 4;
    tempViewport.inset(0, dy);
    previewChart.setCurrentViewportWithAnimation(tempViewport);
    previewChart.setZoomType(ZoomType.VERTICAL);
}
 
Example 7
Source File: PreviewLineChartActivity.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
private void previewXY() {
    // Better to not modify viewport of any chart directly so create a copy.
    Viewport tempViewport = new Viewport(chart.getMaximumViewport());
    // Make temp viewport smaller.
    float dx = tempViewport.width() / 4;
    float dy = tempViewport.height() / 4;
    tempViewport.inset(dx, dy);
    previewChart.setCurrentViewportWithAnimation(tempViewport);
}
 
Example 8
Source File: AbstractChartView.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 9
Source File: AxesRenderer.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 10
Source File: ChartScroller.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
public boolean scroll(ChartComputator computator, float distanceX, float distanceY, ScrollResult scrollResult) {

        // Scrolling uses math based on the viewport (as opposed to math using pixels). Pixel offset is the offset in
        // screen pixels, while viewport offset is the offset within the current viewport. For additional
        // information on
        // surface sizes and pixel offsets, see the docs for {@link computeScrollSurfaceSize()}. For additional
        // information about the viewport, see the comments for {@link mCurrentViewport}.

        final Viewport maxViewport = computator.getMaximumViewport();
        final Viewport visibleViewport = computator.getVisibleViewport();
        final Viewport currentViewport = computator.getCurrentViewport();
        final Rect contentRect = computator.getContentRectMinusAllMargins();

        final boolean canScrollLeft = currentViewport.left > maxViewport.left;
        final boolean canScrollRight = currentViewport.right < maxViewport.right;
        final boolean canScrollTop = currentViewport.top < maxViewport.top;
        final boolean canScrollBottom = currentViewport.bottom > maxViewport.bottom;

        boolean canScrollX = false;
        boolean canScrollY = false;

        if (canScrollLeft && distanceX <= 0) {
            canScrollX = true;
        } else if (canScrollRight && distanceX >= 0) {
            canScrollX = true;
        }

        if (canScrollTop && distanceY <= 0) {
            canScrollY = true;
        } else if (canScrollBottom && distanceY >= 0) {
            canScrollY = true;
        }

        if (canScrollX || canScrollY) {

            computator.computeScrollSurfaceSize(surfaceSizeBuffer);

            float viewportOffsetX = distanceX * visibleViewport.width() / contentRect.width();
            float viewportOffsetY = -distanceY * visibleViewport.height() / contentRect.height();

            computator
                    .setViewportTopLeft(currentViewport.left + viewportOffsetX, currentViewport.top + viewportOffsetY);
        }

        scrollResult.canScrollX = canScrollX;
        scrollResult.canScrollY = canScrollY;

        return canScrollX || canScrollY;
    }