16

我想绘制如图所示的图表。

我已经通过 aChartEngine 尝试过,但它没有成功。

在此处输入图像描述

4

4 回答 4

15

您可以创建 a SurfaceView,您可以在其中绘制到方法Canvas中的a onDraw()。要绘制图表,您可以使用Path类,它的moveTo()lineTo()方法。要更改线条的外观,请使用Paint该类。然后使用 CanvasesdrawPath()方法,该方法接受一个Path, 和一个Paint对象。我认为它比 OpenGL 更容易开始。

一些教程

更新: @Shakti Malik 发现了一个非常漂亮的库,看起来很容易使用:MPAndroidChart

于 2012-09-22T17:44:15.417 回答
2

试试 OpenGL ES 怎么样?

您可以创建一个将扩展 GLSurfaceView 的 GraphView

示例代码-

public class GraphView extends GLSurfaceView {

private Renderer renderer;

public GraphView(Context context) {
    super(context);
    renderer = new GraphRenderer();
    setRenderer(renderer);
}
}

还有你的 GraphRender

ublic class GraphRenderer implements Renderer {

public void onDrawFrame(GL10 gl) {
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 1.0f);
gl.glColor4f(1, 0, 0, .5f);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);

float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

}

private void drawGraph(GL10 gl) {
gl.glLineWidth(1.0f);

// put your code here ..


}

public static int loadShader(int type, String shaderCode) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}

}

你可以试试这个方法。

于 2012-09-22T17:26:27.197 回答
0

Canvas + Paint的示例代码:

在您的 XML 布局中:

<com.y30.histogramdisplay.GraphView
    android:id="@+id/histogram_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" />

在活动中:

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   GraphView graphView = (GraphView)findViewById(R.id.histogram_view);
   int graphArray[] = new int[256];
   for(int i = 0; i < graphArray.length; ++i) {
       graphArray[i] = i % 50;
   }
   graphView.setGraphArray(graphArray);
}

和新的观点:

public class GraphView extends View {

   int m_graphArray[] = null;
   int m_maxY = 0;

   Paint m_paint;


   public GraphView(Context context) {
       super(context);
       init();
   }

   public GraphView(Context context, AttributeSet attrs) {
       super(context, attrs);
       init();
   }

   public GraphView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
       init();
   }

   private void init() {
       m_paint = new Paint();
       m_paint.setColor(Color.BLUE);
       m_paint.setStrokeWidth(10);
   }

   public void setGraphArray(int Xi_graphArray[], int Xi_maxY)
   {
       m_graphArray = Xi_graphArray;
       m_maxY = Xi_maxY;
   }

   public void setGraphArray(int Xi_graphArray[])
   {
       int maxY = 0;
       for(int i = 0; i < Xi_graphArray.length; ++i)
       {
           if(Xi_graphArray[i] > maxY)
           {
               maxY = Xi_graphArray[i];
           }
       }
       setGraphArray(Xi_graphArray, maxY);
   }


   @Override
   protected void onDraw(Canvas canvas) {
       super.onDraw(canvas);

       if(m_graphArray == null)
       {
           return;
       }

       int maxX = m_graphArray.length;

       float factorX = getWidth() / (float)maxX;
       float factorY = getHeight() / (float)m_maxY;

       for(int i = 1; i < m_graphArray.length; ++i) {
           int x0 = i - 1;
           int y0 = m_graphArray[i-1];
           int x1 = i;
           int y1 = m_graphArray[i];

           int sx = (int)(x0 * factorX);
           int sy = getHeight() - (int)(y0* factorY);
           int ex = (int)(x1*factorX);
           int ey = getHeight() - (int)(y1* factorY);
           canvas.drawLine(sx, sy, ex, ey, m_paint);
        }
    }
 }
于 2018-06-13T18:09:15.793 回答
0
implementation 'com.jjoe64:graphview:4.2.1'

eduGrades = 新字符串[5]; behGrades = 新字符串 [5];

    eduGrades[0] = getString(R.string.fail);
    eduGrades[1] = getString(R.string.pass);
    eduGrades[2] = getString(R.string.good);
    eduGrades[3] = getString(R.string.very_good);
    eduGrades[4] = getString(R.string.excellent);

    behGrades[0] = getString(R.string.baad);
    behGrades[1] = getString(R.string.accepted);
    behGrades[2] =  getString(R.string.good);
    behGrades[3] =  getString(R.string.very_good);
    behGrades[4] =  getString(R.string.excellent);

DataPoint[] eduDp = new DataPoint[results.size()]; DataPoint[] behDp = new DataPoint[results.size()];

            dates = new String[results.size()];

            for (int i = 0; i < results.size(); i++) {

                dates[i] = results.get(i).getDateOfNote();

                eduDp[i] = new DataPoint(i, (double) results.get(i).getEducationEvaluationSign());
                behDp[i] = new DataPoint(i, (double) results.get(i).getBehaviorEvaluationSign());

            }

            LineGraphSeries<DataPoint> eduSeries = new LineGraphSeries<>(eduDp);
            educationalGraphView.addSeries(eduSeries);
            eduSeries.setDrawBackground(true);
            eduSeries.setColor(getResources().getColor(R.color.blue));
            eduSeries.setBackgroundColor(getResources().getColor(R.color.blue));
            StaticLabelsFormatter staticLabelsFormatter;
            staticLabelsFormatter = new StaticLabelsFormatter(educationalGraphView);
            staticLabelsFormatter.setVerticalLabels(eduGrades);
            staticLabelsFormatter.setHorizontalLabels(dates);

            educationalGraphView.getGridLabelRenderer().setHorizontalLabelsColor(getResources().getColor(R.color.colorPrimaryDark));
            educationalGraphView.getGridLabelRenderer().setVerticalLabelsColor(getResources().getColor(R.color.colorPrimaryDark));
            educationalGraphView.getGridLabelRenderer().setGridColor(getResources().getColor(R.color.white));
            educationalGraphView.getGridLabelRenderer().setHorizontalLabelsAngle(145);
            educationalGraphView.getGridLabelRenderer().setTextSize(23f);
            educationalGraphView.getGridLabelRenderer().setLabelsSpace(20);
            educationalGraphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
于 2019-04-17T20:10:59.360 回答