Android graphview将所有x轴值设置为最新值

时间:2013-06-20 16:53:24

标签: android-graphview

我创建了一个实时图表,其中x轴的值设置为显示生成相应y轴值的时间。图表不断更新。 发送到图表的值是以毫秒为单位的时间。我创建了一个函数,它根据时间以不同的方式产生时间字符串。这是代码:

GraphView heartRateGraph = new LineGraphView(this, "Heart-rate") {
@Override
protected String formatLabel(double value, boolean isValueX) {
if (isValueX) {
 return getTime((long) value);
} else {
  return super.formatLabel(value, isValueX);
}
}
};

private String getTime(long milliSeconds) {
String dateFormat;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
if(minute == 59 && second >= 57){
dateFormat = "HH:mm:ss";
}else{
dateFormat = "mm:ss";
}
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
return formatter.format(calendar.getTime());
}

问题是所有x轴值都设置为最新值,导致图形只显示扁平线。任何帮助都会受到极大关注。

以下是jjoe64请求的代码:

我使用线程来创建图形并用数据填充它们。

使用线程创建图表的代码:

    RTGKcalThread = new RealTimeGraphKcalThread(activity,
graphConstructor.getBarGraph(), monitor);

    RTHRThread = new RealTimeGraphHRValueThread(activity,
    graphConstructor.getlineGraph());

GraphConstructor类中的相关代码:

    public void initializeGraphs() {
    initializeGraphsDisplayDimensions();
    barGraphView = new BarGraphView(activity, "Calories expendeture(Kcal)"){
             @Override
             protected String formatLabel(double value, boolean isValueX) {
               if (isValueX) {
               return getTime((long) value);
               } else {
               return super.formatLabel(value, isValueX);
               }
               }
            };
    lineGraphView = new LineGraphView(activity, "Heart-rate"){
            @Override
             protected String formatLabel(double value, boolean isValueX) {
               if (isValueX) {
               return getTime((long) value);
               } else {
               return super.formatLabel(value, isValueX);
               }
               }
            };
    setUpGraph(barGraphView, 0);
    setUpGraph(lineGraphView, 1);
}

private void setUpGraph(GraphView graphView, int index){
    graphView.setViewPort(1, 4);
    graphView.setScrollable(true);
    if(displayHeight <= 854){
        graphView.getGraphViewStyle().setVerticalLabelsFontSize(18f);
        graphView.getGraphViewStyle().setHorizontalLabelsFontSize(18f);
    }
    else if(displayHeight > 854){
        graphView.getGraphViewStyle().setVerticalLabelsFontSize(24f);
        graphView.getGraphViewStyle().setHorizontalLabelsFontSize(24f);
    }
    layouts.get(index).addView(graphView);
}

    public GraphView getBarGraph(){
    return barGraphView;
}

public GraphView getlineGraph(){
    return lineGraphView;
}

RealTimeGraphKcalThread和RealTimeGraphHRValueThread扩展了realTimeGraphThread。这是创建系列和附加数据的地方。

    public abstract class RealTimeGraphThread extends TerminateableThread {

private GraphViewSeries series;
private boolean running = true;
private Activity activity;
private int delay;

protected abstract double getValue();

protected RealTimeGraphThread(Activity activity, GraphView graphView,
        int delay) {
    series = new GraphViewSeries(new GraphViewData[] {});
    graphView.addSeries(series);
    this.activity = activity;
    this.delay = delay;
}

@Override
public void run() {
    long timer, diff;
    timer = System.currentTimeMillis();
    while (running) {
        double value = getValue();
        if (String.valueOf(value) != null) {
            activity.runOnUiThread(new UpdateValue((double) System
                    .currentTimeMillis(), value));
        }
        timer += delay;
        diff = timer - System.currentTimeMillis();
        if (diff > 0)
            try {
                sleep(diff);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    }
}

@Override
public void terminate() {
    running = false;
}

private void resetSeries() {
    GraphViewData[] newData = new GraphViewData[10];
    int j = 0;
    for (int i = series.getSize() - 10; i < series.getSize(); i++) {
        newData[j] = series.getValues()[i];
        j++;
    }
    series.resetData(newData);
}

private class UpdateValue implements Runnable {

    private double timeStamp, value;

    public UpdateValue(double timeStamp, double value) {
        this.timeStamp = timeStamp;
        this.value = value;
    }

    @Override
    public void run() {
        series.appendData(new GraphViewData(timeStamp, value), true);
        if (series.getSize() >= 300) {
            resetSeries();
        }
    }
}

编辑:

如果我更改以下内容:

     while (running) {
    double value = getValue();
    if (String.valueOf(value) != null) {
        activity.runOnUiThread(new UpdateValue((double) System
                .currentTimeMillis(), value));
    }
    timer += delay;
    diff = timer - System.currentTimeMillis();
    if (diff > 0)
        try {
            sleep(diff);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
}

    double i = 1d;
while (running) {
    double value = getValue();
    if (String.valueOf(value) != null) {
        activity.runOnUiThread(new UpdateValue(i, value));
    }
    timer += delay;
    diff = timer - System.currentTimeMillis();
    if (diff > 0){
        try {
             sleep(diff);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }       
    i++;
}

     @Override
         protected String formatLabel(double value, boolean isValueX) {
           if (isValueX) {
           return getTime((long) value);

    @Override
         protected String formatLabel(double value, boolean isValueX) {
           if (isValueX) {
           return getTime(System.CurrentTimeMillis());

我得到一张工作图,但所有x轴值仍然设置为最新值。

1 个答案:

答案 0 :(得分:0)

可能是您以错误的方式对数据进行排序。尝试使用bubblesort来解决问题。数据数组(位置0)中的frist值必须是具有最低时间戳的对象。

如果从数据库中获取数据,则按时间顺序获取数据的DESC值是错误的,您必须先对数组进行排序,然后才能将其用于带有x轴时间戳的graphview。 / p>

我希望这是问题,我的回答会对你有帮助。