Battery graph in android using GraphView library?

我的未来我决定 提交于 2019-12-22 00:38:12

问题


i found i great library to draw graphs. I want to create one that displays in the x axis the time and in y the battery percentage. My goal is create a graph like this:

You can see the percentage on the left and the date/time on bottom. Starting from this example code : https://github.com/jjoe64/GraphView-Demos/blob/master/src/com/jjoe64/graphviewdemos/CustomLabelFormatterActivity.java i want insert the correct data but i never used graphs. This is the activity

public class CustomLabelFormatterActivity extends Activity {
    /** Called when the activity is first created. */
    @SuppressLint("SimpleDateFormat")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graphs);

        // init example series data
        Random rand = new Random();
        int size = 20;
        GraphViewData[] data = new GraphViewData[size];
        for (int i=0; i<size; i++) {
            data[i] = new GraphViewData(i, rand.nextInt(20));
        }
        GraphViewSeries exampleSeries = new GraphViewSeries(data);

        GraphView graphView;
        if (getIntent().getStringExtra("type").equals("bar")) {
            graphView = new BarGraphView(
                    this // context
                    , "GraphViewDemo" // heading
            );
        } else {
            graphView = new LineGraphView(
                    this // context
                    , "GraphViewDemo" // heading
            );
        }
        graphView.addSeries(exampleSeries); // data
        graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
            @Override
            public String formatLabel(double value, boolean isValueX) {
                if (isValueX) {
                    if (value < 5) {
                        return "small";
                    } else if (value < 15) {
                        return "middle";
                    } else {
                        return "big";
                    }
                }
                return null; // let graphview generate Y-axis label for us
            }
        });
        LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
        layout.addView(graphView);

        /*
         * use Date as x axis label
         */
        long now = new Date().getTime();
        data = new GraphViewData[size];
        for (int i=0; i<size; i++) {
            data[i] = new GraphViewData(now+(i*60*60*24*1000), rand.nextInt(20)); // next day
        }
        exampleSeries = new GraphViewSeries(data);

        if (getIntent().getStringExtra("type").equals("bar")) {
            graphView = new BarGraphView(
                    this
                    , "GraphViewDemo"
            );
        } else {
            graphView = new LineGraphView(
                    this
                    , "GraphViewDemo"
            );
            ((LineGraphView) graphView).setDrawBackground(true);
        }
        graphView.addSeries(exampleSeries); // data

        /*
         * date as label formatter
         */
        final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d h.mm");
        graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
            @Override
            public String formatLabel(double value, boolean isValueX) {
                if (isValueX) {
                    Date d = new Date((long) value);
                    return dateFormat.format(d);
                }
                return null; // let graphview generate Y-axis label for us
            }
        });

        layout = (LinearLayout) findViewById(R.id.graph2);
        layout.addView(graphView);
    }
}

What have i to change?How can i create what i want?Anyone can help me? Thank you.

来源:https://stackoverflow.com/questions/18481886/battery-graph-in-android-using-graphview-library

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