/**
  * Creates a new graphical view.
  *
  * @param view the graphical view
  * @param chart the chart to be drawn
  */
 public TouchHandler(GraphicalView view, AbstractChart chart) {
   graphicalView = view;
   zoomR = graphicalView.getZoomRectangle();
   if (chart instanceof XYChart) {
     mRenderer = ((XYChart) chart).getRenderer();
   } else {
     mRenderer = ((RoundChart) chart).getRenderer();
   }
   if (mRenderer.isPanEnabled()) {
     mPan = new Pan(chart);
   }
   if (mRenderer.isZoomEnabled()) {
     mPinchZoom = new Zoom(chart, true, 1);
   }
 }
  /**
   * Handles the touch event.
   *
   * @param event the touch event
   */
  public boolean handleTouch(MotionEvent event) {
    int action = event.getAction();
    if (mRenderer != null && action == MotionEvent.ACTION_MOVE) {
      if (oldX >= 0 || oldY >= 0) {
        float newX = event.getX(0);
        float newY = event.getY(0);
        if (event.getPointerCount() > 1
            && (oldX2 >= 0 || oldY2 >= 0)
            && mRenderer.isZoomEnabled()) {
          float newX2 = event.getX(1);
          float newY2 = event.getY(1);
          float newDeltaX = Math.abs(newX - newX2);
          float newDeltaY = Math.abs(newY - newY2);
          float oldDeltaX = Math.abs(oldX - oldX2);
          float oldDeltaY = Math.abs(oldY - oldY2);
          float zoomRate = 1;

          float tan1 = Math.abs(newY - oldY) / Math.abs(newX - oldX);
          float tan2 = Math.abs(newY2 - oldY2) / Math.abs(newX2 - oldX2);
          if (tan1 <= 0.25 && tan2 <= 0.25) {
            // horizontal pinch zoom, |deltaY| / |deltaX| is [0 ~ 0.25], 0.25 is
            // the approximate value of tan(PI / 12)
            zoomRate = newDeltaX / oldDeltaX;
            applyZoom(zoomRate, Zoom.ZOOM_AXIS_X);
          } else if (tan1 >= 3.73 && tan2 >= 3.73) {
            // pinch zoom vertically, |deltaY| / |deltaX| is [3.73 ~ infinity],
            // 3.732 is the approximate value of tan(PI / 2 - PI / 12)
            zoomRate = newDeltaY / oldDeltaY;
            applyZoom(zoomRate, Zoom.ZOOM_AXIS_Y);
          } else {
            // pinch zoom diagonally
            if (Math.abs(newX - oldX) >= Math.abs(newY - oldY)) {
              zoomRate = newDeltaX / oldDeltaX;
            } else {
              zoomRate = newDeltaY / oldDeltaY;
            }
            applyZoom(zoomRate, Zoom.ZOOM_AXIS_XY);
          }
          oldX2 = newX2;
          oldY2 = newY2;
        } else if (mRenderer.isPanEnabled()) {
          mPan.apply(oldX, oldY, newX, newY);
          oldX2 = 0;
          oldY2 = 0;
        }
        oldX = newX;
        oldY = newY;
        graphicalView.repaint();
        return true;
      }
    } else if (action == MotionEvent.ACTION_DOWN) {
      oldX = event.getX(0);
      oldY = event.getY(0);
      if (mRenderer != null && mRenderer.isZoomEnabled() && zoomR.contains(oldX, oldY)) {
        if (oldX < zoomR.left + zoomR.width() / 3) {
          graphicalView.zoomIn();
        } else if (oldX < zoomR.left + zoomR.width() * 2 / 3) {
          graphicalView.zoomOut();
        } else {
          graphicalView.zoomReset();
        }
        return true;
      }
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
      oldX = 0;
      oldY = 0;
      oldX2 = 0;
      oldY2 = 0;
      if (action == MotionEvent.ACTION_POINTER_UP) {
        oldX = -1;
        oldY = -1;
      }
    }
    return !mRenderer.isClickEnabled();
  }