如何将第一个域标签从原点移动一步
How to move the first domain label one step from origin
我已经在我的应用程序中实现了 AndroidPlot,它工作正常,但是我需要在来源之后一步的第一个值,因为日期不明确。
我尝试了提出的问题 here 中建议的解决方案,他们在其中添加了 setDomainValueFormat 方法,但显示错误消息:
"method can't resolve "
关于如何在原点后一步开始 x 轴域的任何建议?
plot = (XYPlot) findViewById(R.id.plot);
XYSeries series = new SimpleXYSeries(Arrays.asList(dates_in_m_seconds), Arrays.asList(values_as_numbers), "BP Status");
LineAndPointRenderer and configure them
LineAndPointFormatter seriesFormat = new LineAndPointFormatter(Color.RED, Color.GREEN,null, null);
plot.addSeries(series, seriesFormat);
// Specify x and y axes labels amount
plot.setRangeStep(StepMode.SUBDIVIDE,3);
plot.setDomainStep(StepMode.SUBDIVIDE,dates.size());
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Date date_Label = new Date(Math.round(((Number) obj).doubleValue()));
return format.format(date_Label, toAppendTo, pos);
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.LEFT).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Number num = (Number) obj;
switch (num.intValue()) {
case 0:
toAppendTo.append("Low");
break;
case 1:
toAppendTo.append("Normal");
break;
case 2:
toAppendTo.append("High");
break;
default:
toAppendTo.append("Unknown");
break;
}
return toAppendTo;
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
}
有几种方法可以解决这个问题。从表面上看,在 xVals 之间保持严格均匀的间距似乎是可取的,但是因为您的 xVals 不仅是时间戳,而且是原始输入的四舍五入版本,您只会获得近似均匀的间距。如果您想完全解决该问题,您可以使用 Y_VALS_ONLY 实例化 XYSeries,然后使用 dates_as_m_seconds 作为查找 table 以在域格式实例中呈现标签。
话虽如此,我已经包含了一个应该可以与您现有代码一起使用的解决方案。我没有尝试编译或 运行 它所以可能会有拼写错误,但总体思路是可行的:
- 手动设置绘图的域边界以在时间戳数据的第一个元素之前包含一些额外的 space。
- 细分域区域时,添加一个额外的元素,为新的隐形步骤腾出空间
- 修改您的域标签格式实例以跳过<数据中第一个时间戳的值,有效地使新的第一个元素不可见。
代码:
// this value must be final in order to be visible to the anonymous inner class instance
// of Format below. Note that this will fail miserably if you try to plot a series of
// size < 2.
final double firstX = Math.round(((Number) series.getX(0)).doubleValue());
double secondX = Math.round(((Number) series.getX(1)).doubleValue());
double lastX = Math.round(((Number) series.getX(series.size() - 1)).doubleValue());
// assuming the step interval is uniform, this is the distance between each xVal:
double stepInterval = secondX - firstX;
// add in extra space for the first "invisible index" by setting the starting
// domain boundary exactly one interval before the first actual element:
plot.setDomainBoundaries(firstX - stepInterval, lastX, BoundaryMode.FIXED);
// add an extra "invisible index":
plot.setDomainStep(StepMode.SUBDIVIDE,dates.size() + 1);
...
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
double timestampMs = Math.round(((Number) obj).doubleValue());
// only render indices corresponding to values in the series:
if(timestampMs >= firstX) {
Date date_Label = new Date();
return format.format(date_Label, toAppendTo, pos);
}
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
我已经在我的应用程序中实现了 AndroidPlot,它工作正常,但是我需要在来源之后一步的第一个值,因为日期不明确。
我尝试了提出的问题 here 中建议的解决方案,他们在其中添加了 setDomainValueFormat 方法,但显示错误消息:
"method can't resolve "
关于如何在原点后一步开始 x 轴域的任何建议?
plot = (XYPlot) findViewById(R.id.plot);
XYSeries series = new SimpleXYSeries(Arrays.asList(dates_in_m_seconds), Arrays.asList(values_as_numbers), "BP Status");
LineAndPointRenderer and configure them
LineAndPointFormatter seriesFormat = new LineAndPointFormatter(Color.RED, Color.GREEN,null, null);
plot.addSeries(series, seriesFormat);
// Specify x and y axes labels amount
plot.setRangeStep(StepMode.SUBDIVIDE,3);
plot.setDomainStep(StepMode.SUBDIVIDE,dates.size());
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Date date_Label = new Date(Math.round(((Number) obj).doubleValue()));
return format.format(date_Label, toAppendTo, pos);
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.LEFT).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Number num = (Number) obj;
switch (num.intValue()) {
case 0:
toAppendTo.append("Low");
break;
case 1:
toAppendTo.append("Normal");
break;
case 2:
toAppendTo.append("High");
break;
default:
toAppendTo.append("Unknown");
break;
}
return toAppendTo;
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
}
有几种方法可以解决这个问题。从表面上看,在 xVals 之间保持严格均匀的间距似乎是可取的,但是因为您的 xVals 不仅是时间戳,而且是原始输入的四舍五入版本,您只会获得近似均匀的间距。如果您想完全解决该问题,您可以使用 Y_VALS_ONLY 实例化 XYSeries,然后使用 dates_as_m_seconds 作为查找 table 以在域格式实例中呈现标签。
话虽如此,我已经包含了一个应该可以与您现有代码一起使用的解决方案。我没有尝试编译或 运行 它所以可能会有拼写错误,但总体思路是可行的:
- 手动设置绘图的域边界以在时间戳数据的第一个元素之前包含一些额外的 space。
- 细分域区域时,添加一个额外的元素,为新的隐形步骤腾出空间
- 修改您的域标签格式实例以跳过<数据中第一个时间戳的值,有效地使新的第一个元素不可见。
代码:
// this value must be final in order to be visible to the anonymous inner class instance
// of Format below. Note that this will fail miserably if you try to plot a series of
// size < 2.
final double firstX = Math.round(((Number) series.getX(0)).doubleValue());
double secondX = Math.round(((Number) series.getX(1)).doubleValue());
double lastX = Math.round(((Number) series.getX(series.size() - 1)).doubleValue());
// assuming the step interval is uniform, this is the distance between each xVal:
double stepInterval = secondX - firstX;
// add in extra space for the first "invisible index" by setting the starting
// domain boundary exactly one interval before the first actual element:
plot.setDomainBoundaries(firstX - stepInterval, lastX, BoundaryMode.FIXED);
// add an extra "invisible index":
plot.setDomainStep(StepMode.SUBDIVIDE,dates.size() + 1);
...
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
double timestampMs = Math.round(((Number) obj).doubleValue());
// only render indices corresponding to values in the series:
if(timestampMs >= firstX) {
Date date_Label = new Date();
return format.format(date_Label, toAppendTo, pos);
}
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});