1

我发现我很棒的图书馆来绘制图表。我想创建一个在 x 轴上显示时间和在 y 轴上显示电池百分比的显示器。我的目标是创建这样的图表:在此处输入图像描述

您可以在左侧看到百分比,在底部看到日期/时间。从这个示例代码开始: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);
    }
}

我有什么要改变的?我怎样才能创造我想要的东西?任何人都可以帮助我吗?谢谢你。

4

0 回答 0