线图Android不使用任何库或Google Charts API

时间:2014-01-09 18:30:30

标签: android linegraph

我需要在Android中编写折线图,而不使用任何库或API。

我必须为绘图线写drawMethod。输入将是XY的坐标。我从文件中读到了这个。

3 个答案:

答案 0 :(得分:1)

没有API,因此您因为PathEffect API而无法使用path tracing?这是限制,因为它是家庭作业吗?

答案 1 :(得分:1)

尝试类似的东西 -

public void drawChart(int count) {
    System.out.println(count);
    for (int k = 1; k <= count; k++) {

        View view = new View(this);
        view.setBackgroundColor(Color.parseColor("#ff6233"));
        view.setLayoutParams(new LinearLayout.LayoutParams(30, 400));
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view .getLayoutParams();
        params.setMargins(5, 0, 0, 0); // substitute parameters for left,top, right, bottom
        view.setLayoutParams(params); linearChart.addView(view);
    }
}

您可以查看这些链接 -

http://www.androidhub4you.com/2013/05/custom-bar-chart-in-android-dynamic.html

http://www.androidhub4you.com/2013/05/bar-chart-example-in-android-simple-bar.html

答案 2 :(得分:0)

写了代码:

@Override
protected void onDraw(Canvas canvas) {
    float border = 20;
    float horstart = border * 2;
    float height = getHeight();
    float width = getWidth() - 1;
    float max = getMax();
    float min = getMin();
    float diff = max - min;
    float graphheight = height - (2 * border);
    float graphwidth = width - (2 * border);

    paint.setTextAlign(Align.LEFT);
    int vers = verlabels.length - 1;
    for (int i = 0; i < verlabels.length; i++) {
        paint.setColor(Color.DKGRAY);
        float y = ((graphheight / vers) * i) + border;
        canvas.drawLine(horstart, y, width, y, paint);
        paint.setColor(Color.WHITE);
        canvas.drawText(verlabels[i], 0, y, paint);
    }
    int hors = horlabels.length - 1;
    for (int i = 0; i < horlabels.length; i++) {
        paint.setColor(Color.DKGRAY);
        float x = ((graphwidth / hors) * i) + horstart;
        canvas.drawLine(x, height - border, x, border, paint);
        paint.setTextAlign(Align.CENTER);
        if (i==horlabels.length-1)
            paint.setTextAlign(Align.RIGHT);
        if (i==0)
            paint.setTextAlign(Align.LEFT);
        paint.setColor(Color.WHITE);
        canvas.drawText(horlabels[i], x, height - 4, paint);
    }

    paint.setTextAlign(Align.CENTER);
    canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);

    if (max != min) {
        paint.setColor(Color.BLUE);
        if (type == BAR) {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);
            }
        } else {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            float halfcol = colwidth / 2;
            float lasth = 0;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                if (i > 0)
                    canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
                lasth = h;
            }
        }
    }
}