在 "graphView" 标签不从原点开始
In "graphView" labels does not start from the origin
我创建了 60 个数据点(每小时中的每一分钟)并将它们显示在图表上。为 x 轴设置 7 个标签。带有日期的标签。
最左边和最右边的标签与x轴的起点和终点不重合。此屏幕截图显示不匹配:
代码:
private void updateGraph(){
DataPoint[] dataPoints = new DataPoint[mCurrencyStampList.size()];
int i = 0;
Double minY = null;
Double maxY = 0D;
for(CurrencyStamp stamp : mCurrencyStampList){
dataPoints[i] = new DataPoint(stamp.getDate(), stamp.getClose());
if(maxY < stamp.getClose()){
maxY = stamp.getClose().doubleValue();
}
if(minY == null || minY > stamp.getClose()){
minY = stamp.getClose().doubleValue();
}
i++;
}
LineGraphSeries<DataPoint> points = new LineGraphSeries<>(dataPoints);
mGraphView.addSeries(points);
DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getContext());
mGraphView.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity(), dateFormat));
mGraphView.getGridLabelRenderer().setTextSize(32);
mGraphView.getGridLabelRenderer().setNumHorizontalLabels(7);
double minX = mCurrencyStampList.get(0).getDate().getTime();
double maxX = mCurrencyStampList.get(mCurrencyStampList.size()-1).getDate().getTime();
mGraphView.getViewport().setMinimalViewport(minX, maxX, minY == null ? 0 : minY, maxY);
mGraphView.getViewport().setXAxisBoundsManual(true);
mGraphView.getViewport().setYAxisBoundsManual(true);
}
图表的划分在时间上不重合。如何解决这个问题?
更新:
我想我明白为什么极端线正在向中心移动。 unix 时间戳在转换为 double 时失去精度。我不知道如何解决这个问题。 Mpandroidchart 库上有同样的问题。我尝试 AnyChart 的试用版,对我来说很好用(在点的构造函数中使用自己的格式。不是 float 和 double)但是这个试用版
我迁移到 MPAndroid 并应用了系数。只需沿 x 轴设置最小值和最大值:
xAxis.setAxisMinimum(0);
xAxis.setAxisMaximum(100000);
创建系数:
long xMin = mCurrencyStampList.get(0).getDate().getTime();
long xMax = mCurrencyStampList.get(mCurrencyStampList.size() - 1).getDate().getTime();
float xCoefficient = (xMax - xMin) / 100000;
多个 x 值不同:
long currentTS = s.getDate().getTime();
Float x = (currentTS - xMin) / xCoefficient;
并在小部件中显示 return 值:
XAxis xAxis = mLineChart.getXAxis();
xAxis.setValueFormatter((value, axis) -> {
Long time = (long) (value * xCoefficient + xMin);
Date date = new Date(time);
DateFormat format;
if(mInterval == Interval.OneHour || mInterval == Interval.SixHours || mInterval == Interval.OneDay){
format = android.text.format.DateFormat.getTimeFormat(getContext());
}
else {
format = android.text.format.DateFormat.getDateFormat(getContext());
}
return format.format(date);
});
不是完美的解决方案,但适用于 unix 时间戳
我创建了 60 个数据点(每小时中的每一分钟)并将它们显示在图表上。为 x 轴设置 7 个标签。带有日期的标签。
最左边和最右边的标签与x轴的起点和终点不重合。此屏幕截图显示不匹配:
代码:
private void updateGraph(){
DataPoint[] dataPoints = new DataPoint[mCurrencyStampList.size()];
int i = 0;
Double minY = null;
Double maxY = 0D;
for(CurrencyStamp stamp : mCurrencyStampList){
dataPoints[i] = new DataPoint(stamp.getDate(), stamp.getClose());
if(maxY < stamp.getClose()){
maxY = stamp.getClose().doubleValue();
}
if(minY == null || minY > stamp.getClose()){
minY = stamp.getClose().doubleValue();
}
i++;
}
LineGraphSeries<DataPoint> points = new LineGraphSeries<>(dataPoints);
mGraphView.addSeries(points);
DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getContext());
mGraphView.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity(), dateFormat));
mGraphView.getGridLabelRenderer().setTextSize(32);
mGraphView.getGridLabelRenderer().setNumHorizontalLabels(7);
double minX = mCurrencyStampList.get(0).getDate().getTime();
double maxX = mCurrencyStampList.get(mCurrencyStampList.size()-1).getDate().getTime();
mGraphView.getViewport().setMinimalViewport(minX, maxX, minY == null ? 0 : minY, maxY);
mGraphView.getViewport().setXAxisBoundsManual(true);
mGraphView.getViewport().setYAxisBoundsManual(true);
}
图表的划分在时间上不重合。如何解决这个问题?
更新: 我想我明白为什么极端线正在向中心移动。 unix 时间戳在转换为 double 时失去精度。我不知道如何解决这个问题。 Mpandroidchart 库上有同样的问题。我尝试 AnyChart 的试用版,对我来说很好用(在点的构造函数中使用自己的格式。不是 float 和 double)但是这个试用版
我迁移到 MPAndroid 并应用了系数。只需沿 x 轴设置最小值和最大值:
xAxis.setAxisMinimum(0);
xAxis.setAxisMaximum(100000);
创建系数:
long xMin = mCurrencyStampList.get(0).getDate().getTime();
long xMax = mCurrencyStampList.get(mCurrencyStampList.size() - 1).getDate().getTime();
float xCoefficient = (xMax - xMin) / 100000;
多个 x 值不同:
long currentTS = s.getDate().getTime();
Float x = (currentTS - xMin) / xCoefficient;
并在小部件中显示 return 值:
XAxis xAxis = mLineChart.getXAxis();
xAxis.setValueFormatter((value, axis) -> {
Long time = (long) (value * xCoefficient + xMin);
Date date = new Date(time);
DateFormat format;
if(mInterval == Interval.OneHour || mInterval == Interval.SixHours || mInterval == Interval.OneDay){
format = android.text.format.DateFormat.getTimeFormat(getContext());
}
else {
format = android.text.format.DateFormat.getDateFormat(getContext());
}
return format.format(date);
});
不是完美的解决方案,但适用于 unix 时间戳