android - 使用 Graphview 在 android 中绘制图形?

标签 android graph scrollview

我想在我的 android 应用程序上创建一个条形图。
我需要 scrollView 上的图表。

我已经试过了GraphViewAndroidPlot .

最佳答案

更新

GraphView 的新更新不支持这种方法

使用 GraphView:

我在下面做了一个函数,它接受三个数组作为参数。第一个采用 X 轴标签,下一个采用 Y 轴标签,最后一个采用要绘制的值。

代码

private void renderGraph(String[] xAxis, String[] yAxis, float[] data) {

        GraphViewData[] data = new GraphViewData[xAxis.length];//this class is defined below

        double v = 1, w = 1;
        int num = xAxis.length;
        for (int j = 0; j < num; j++) {
            v = data[j];
            data[j] = new GraphViewData(j, v);
        }
        GraphViewSeries example1 = new GraphViewSeries(data);
        GraphView graphView = new BarGraphView(this, "GRAPH TITLE");
        graphView.setVerticalLabels(yAxis);
        graphView.setHorizontalLabels(xAxis);
        graphView.addSeries(example1);
        example1.getStyle().color = Color.BLUE;
        graphView.setScalable(true);
        graphView.getGraphViewStyle().setTextSize(18);
        graphView.setScrollable(false);
        graphView.getGraphViewStyle().setGridColor(Color.DKGRAY);
        graphView.getGraphViewStyle().setGridStyle(GridStyle.VERTICAL);
        graphView.getGraphViewStyle().setNumHorizontalLabels(5);
        LinearLayout layout = (LinearLayout) findViewById(R.id.graphz);//graphz is defined in layout
        layout.addView(graphView);
    }

GraphViewData 类:

此类与文档中给出的类略有不同。

   public class GraphViewData implements GraphViewDataInterface {

    private double x, y;

    public GraphViewData(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }

    @Override
    public double getX() {
        // TODO Auto-generated method stub
        return this.x;
    }

    @Override
    public double getY() {
        // TODO Auto-generated method stub
        return this.y;
    }
}

附言

将此方法称为:

String[] xAxis = {"RED","WHITE","BLUE","GREEN"};
String[] yAxis = {"GOOD", "AVEGRAGE", "BAD"};
String[] data = {"1", "2", "1","2"};
renderGraph(xAxis, yAxis, data);

关于android - 使用 Graphview 在 android 中绘制图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543739/

相关文章:

java - 在 Android 中访问自动代理 URL?

java - Android Studio 错误 : "Method getText must be called from the UI Thread, 当前推断线程为工作线程

algorithm - 如何为 mst 的所有顶点对找到路径的最大边缘

java - 用 Java 创建简单的条形图 - 读取数据并输出条形图

android - ScrollView vs RecyclerView 适用于 Android 上的不同 child

android - 使用 ScrollView 的大列表

android - 在 ScrollView 上启用缩放

android - 宽度 :100% in CSS not rendering well in Android 4. 4

Android studio gradle build 更新失败

algorithm - 流程树问题的最优解