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