1

我创建了一个实时图表,其中 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 轴值仍设置为最新值。

4

1 回答 1

0

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

如果您从数据库中获取数据,则在时间顺序错误时对值 DESC 进行排序以获取 lastet,并且您必须先对数组进行排序,然后才能将其用于带有 x 轴时间戳的图形视图。

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

于 2017-01-16T12:47:37.960 回答