use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.
the class ChartScroller method scroll.
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;
}
use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.
the class AbstractChartView method getZoomLevel.
@Override
public float getZoomLevel() {
Viewport maxViewport = getMaximumViewport();
Viewport currentViewport = getCurrentViewport();
return Math.max(maxViewport.width() / currentViewport.width(), maxViewport.height() / currentViewport.height());
}
use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.
the class AbstractChartView method canScrollHorizontally.
/**
* When embedded in a ViewPager, this will be called in order to know if we can scroll.
* If this returns true, the ViewPager will ignore the drag so that we can scroll our content.
* If this return false, the ViewPager will assume we won't be able to scroll and will consume the drag
*
* @param direction Amount of pixels being scrolled (x axis)
* @return true if the chart can be scrolled (ie. zoomed and not against the edge of the chart)
*/
@Override
public boolean canScrollHorizontally(int direction) {
if (getZoomLevel() <= 1.0) {
return false;
}
final Viewport currentViewport = getCurrentViewport();
final Viewport maximumViewport = getMaximumViewport();
if (direction < 0) {
return currentViewport.left > maximumViewport.left;
} else {
return currentViewport.right < maximumViewport.right;
}
}
use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.
the class AbstractChartView method moveTo.
@Override
public void moveTo(float x, float y) {
Viewport scrollViewport = computeScrollViewport(x, y);
setCurrentViewport(scrollViewport);
}
use of lecho.lib.hellocharts.model.Viewport in project hellocharts-android by lecho.
the class AbstractChartView method setZoomLevelWithAnimation.
@Override
public void setZoomLevelWithAnimation(float x, float y, float zoomLevel) {
Viewport zoomViewport = computeZoomViewport(x, y, zoomLevel);
setCurrentViewportWithAnimation(zoomViewport);
}