Android中的电池图使用GraphView库?

时间:2013-08-28 07:36:04

标签: android datetime graph batterylevel android-graphview

我找到了一个很棒的图书馆来绘制图表。我想创建一个在x轴上显示时间和y电池百分比的显示器。我的目标是创建一个这样的图形:enter image description here

您可以在左侧看到百分比,在底部看到日期/时间。从这个示例代码开始:https://github.com/jjoe64/GraphView-Demos/blob/master/src/com/jjoe64/graphviewdemos/CustomLabelFormatterActivity.java我想插入正确的数据,但我从未使用过图表。这是活动

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);
    }
}

我要改变什么?我如何创造我想要的东西?任何人都可以帮助我?谢谢。

0 个答案:

没有答案