AchartEngineLine Chart in android - Line disappears while panning

久未见 提交于 2019-12-25 01:56:14

问题


I am facing an issue in AchartEngine Scatter chart in android. The line between each point, disappears when panning after set zoom to a level. I had tried hardwareAcceleration to false and setLayerType to null and all. But, issue remains.

Achartengine version 1.1.0. Android device - Galaxy S4, S3. Can you please help me on this ?

Screenshot is placed here at, http://i.stack.imgur.com/hq5r4.png

Thanks

JRH


回答1:


After two days of effort, I was able to solve the issue. It was due to Float.NaN for one of the Path value, while drawing the line.

Issue was AbstractChart -> drawPath method. In that calculateDrawPoints() method returns a float array, which contains Float.NaN in one case. I dont know the issue behind it. But I could overcome that with Canvas.drawLine method.

Thanks

JRH




回答2:


Its because AbstarctChart==> calculateDrawPoints() method returns float array which contains Float.NaN

Its because during calculation, sometimes value returns 0, and after dividing value with 0, we get Float.NaN Anything divided by zero is undefined

So update your calculateDrawPoints() method code with following

private static float[] calculateDrawPoints(float p1x, float p1y, float p2x, float p2y,
      int screenHeight, int screenWidth) {
    float drawP1x = 0;
    float drawP1y;
    float drawP2x = 0;
    float drawP2y;

    if (p1y > screenHeight) {
      // Intersection with the top of the screen
      float m = (p2y - p1y) / (p2x - p1x);
      if (m != 0) {
        drawP1x = (screenHeight - p1y + m * p1x) / m;
      } else {
        drawP1x = (screenHeight - p1y + m * p1x);
      }
      drawP1y = screenHeight;

      if (drawP1x < 0) {
        // If Intersection is left of the screen we calculate the intersection
        // with the left border
        drawP1x = 0;
        drawP1y = p1y - m * p1x;
      } else if (drawP1x > screenWidth) {
        // If Intersection is right of the screen we calculate the intersection
        // with the right border
        drawP1x = screenWidth;
        drawP1y = m * screenWidth + p1y - m * p1x;
      }
    } else if (p1y < 0) {
      float n = p2x - p1x;
      float m = 0;
      if (n != 0) {
        m = (p2y - p1y) / n;
      } else {
        m = (p2y - p1y);
      }
      if (m != 0) {
        drawP1x = (-p1y + m * p1x) / m;
      } else {
        drawP1x = (-p1y + m * p1x);
      }
      drawP1y = 0;
      if (drawP1x < 0) {
        drawP1x = 0;
        drawP1y = p1y - m * p1x;
      } else if (drawP1x > screenWidth) {
        drawP1x = screenWidth;
        drawP1y = m * screenWidth + p1y - m * p1x;
      }
    } else {
      // If the point is in the screen use it
      drawP1x = p1x;
      drawP1y = p1y;
    }

    if (p2y > screenHeight) {
      float n = (p2x - p1x);
      float m = 0;
      if (n != 0) {
        m = (p2y - p1y) / n;
      } else {
        m = (p2y - p1y);
      }
      if (m != 0) {
        drawP2x = (screenHeight - p1y + m * p1x) / m;
      } else {
        drawP2x = (screenHeight - p1y + m * p1x);
      }

      drawP2y = screenHeight;
      if (drawP2x < 0) {
        drawP2x = 0;
        drawP2y = p1y - m * p1x;
      } else if (drawP2x > screenWidth) {
        drawP2x = screenWidth;
        drawP2y = m * screenWidth + p1y - m * p1x;
      }
    } else if (p2y < 0) {
      float n = (p2x - p1x);
      float m = 0;
      if (n != 0) {
        m = (p2y - p1y) / n;
      } else {
        m = (p2y - p1y);
      }
      if (m != 0) {
        drawP2x = (-p1y + m * p1x) / m;
      } else {
        drawP2x = (-p1y + m * p1x);
      }
      drawP2y = 0;
      if (drawP2x < 0) {
        drawP2x = 0;
        drawP2y = p1y - m * p1x;
      } else if (drawP2x > screenWidth) {
        drawP2x = screenWidth;
        drawP2y = m * screenWidth + p1y - m * p1x;
      }
    } else {
      // If the point is in the screen use it
      drawP2x = p2x;
      drawP2y = p2y;
    }
    return new float[] { drawP1x, drawP1y, drawP2x, drawP2y };
  }

That will resolve your issue



来源:https://stackoverflow.com/questions/21914703/achartengineline-chart-in-android-line-disappears-while-panning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!