gpt4 book ai didi

Android GraphView调整整数水平标签的位置

转载 作者:行者123 更新时间:2023-11-30 01:54:10 25 4
gpt4 key购买 nike

我正在使用 Android Graph View显示折线图。我需要更改水平标签以获得漂亮的四舍五入数字。在附加的屏幕截图中,值是

1
11:15
22:29
33:44

我需要调整水平标签和垂直线的位置,以便标签阅读

0
11:00
23:00
33:44

上面屏幕截图的代码使用了这个基类

protected TextView text;
protected GraphView graph;

@Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.ride_chart_fragment, null, false);

graph = (GraphView) view.findViewById(R.id.graph);
if (graph==null){
Log.e("Rides","Graph should not ne null");
return view;
}
Series series = linePoints();
if (series==null) return view;


graph.getGridLabelRenderer().setNumHorizontalLabels(4);

graph.addSeries(series);
setViewport(graph, series);

text = (TextView)view.findViewById(R.id.textView);
text.setVisibility(View.INVISIBLE);


graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
@Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
// show normal x values
return UnitConverter.secondsToInterval((float) value);
} else {

return super.formatLabel(value, isValueX) + units(new Settings(inflater.getContext()));
}
}
});

graph.getGridLabelRenderer().setLabelVerticalWidth((int)getResources().getDimension(R.dimen.graph_label_vertical_width));
graph.getGridLabelRenderer().setLabelHorizontalHeight((int)getResources().getDimension(R.dimen.graph_label_vertical_height));



graph.getGridLabelRenderer().setGridColor(Color.parseColor("#20000000"));
graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.BOTH);
//graph.getGridLabelRenderer().
return view;
}

和这个子类来渲染图形

protected Series linePoints() {

//RideDetailActivity activity = (RideDetailActivity)getActivity();
if (activity==null || activity.isFinishing() || activity.ride==null) return null;
Settings settings = new Settings(activity);

long rideId = activity.ride.sqlRide.getId();
LezyneLinkApplication application = (LezyneLinkApplication)activity.getApplicationContext();
DaoSession session = application.getDaoSession();
RideElevationDao table = session.getRideElevationDao();
List<RideElevation> elevations = table
.queryBuilder()
.where(RideElevationDao.Properties.RideId.eq(rideId))
.orderAsc(RideElevationDao.Properties.Index)
.list();
DataPoint dataPoints[] = new DataPoint[elevations.size()];
int index=0;
boolean isMetric = settings.isMetric();
for (RideElevation elevation : elevations){
double y = elevation.getY();
if (!settings.isMetric()){
y = UnitConverter.convertMetersToFeet(y);
}
DataPoint point = new DataPoint(elevation.getX(),y);
dataPoints[index] = point;
index++;
}

LineGraphSeries series = new LineGraphSeries<DataPoint>(dataPoints);
series.setDrawBackground(true);
series.setColor(Color.argb(0xFF, 0x8e, 0x00, 0xe8));
series.setBackgroundColor(Color.argb(0x3F, 0x47, 0x2c, 0x17));
series.setThickness(6);
return series;
}

@Override
protected void setViewport(GraphView graph,Series series) {

graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMaxX(series.getHighestValueX());
graph.getViewport().setYAxisBoundsManual(true);
double lowest = series.getLowestValueY();
double highest = series.getHighestValueY();
graph.getViewport().setMinY(lowest);
Settings settings = new Settings(context);
if (settings.isMetric()){
if (highest<lowest+121) highest = lowest+121;
}
else {
if (highest<lowest+400) highest = lowest+400;
}
graph.getViewport().setMaxY(highest);
}

我没有看到任何方法可以按原样使用库执行此操作,因此我正在考虑更改源代码。我错过了 API 中的某些内容吗?任何人对在代码中的何处添加此功能有任何建议。

Graph Screen Shot

最佳答案

有两种方法:

1) (困难)使用动态视口(viewport)查看源代码并找到完成 humanRound 的点 (GridLabelRenderer.java)

理解,修改;)

2) 使用固定视口(viewport)并自行计算最小和最大边界,您可以更改 numberOfHorizo​​ntalLabels 以获得最佳匹配。

关于Android GraphView调整整数水平标签的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32531586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com