Battery graph in android using GraphView library?

2020-07-27 23:39发布

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:enter image description here

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.

0条回答
登录 后发表回答