最近项目中用到了折线图,找了几个.最后还是选择了hellocharts.需求要加载八万个点,这么多直接就卡死了,所以做了修改,实现边滑动边加载点.
hellocharts里的ChartScroller.class里有用来计算滚动时x轴可见距离的方法,那么可以在这里设置回调函数,将当前屏幕可见的x轴位置获取出来,然后在list中取对应位置的点去加载.
1.在ChartScroller类中中加入回调函数,分别在测量距离和滑动的方法中,即computeScrollOffset和scroll方法中使用回调
private onScrollListener onScrollListener;
/**
* 设置回调接口
*/
public void setOnScrollListener(onScrollListener onScrollListener)
{
this.onScrollListener = onScrollListener;
}
public interface onScrollListener
{
/**
* 当进行滑动时,将x点坐标记录
*/
public void onScroll(float x,float y);
}//曲线滑动过程中
public boolean computeScrollOffset(ChartComputator computator) {
if (scroller.computeScrollOffset()) {
// The scroller isn't finished, meaning a fling or programmatic pan
// operation is
// currently active.
computator.computeScrollSurfaceSize(surfaceSizeBuffer);
scrollerStartViewport.set(computator.getCurrentViewport());
// Log.e("computeScrollOffset", "x--" + scrollerStartViewport.left
// + " :y--" + scrollerStartViewport.right);
//计算时回调
if(onScrollListener !=null){
onScrollListener.onScroll(scrollerStartViewport.right,scrollerStartViewport.left);
}
final Viewport maxViewport = computator.getMaximumViewport();
computator.computeScrollSurfaceSize(surfaceSizeBuffer);
final float currXRange = maxViewport.left + maxViewport.width()
* scroller.getCurrX() / surfaceSizeBuffer.x;
final float currYRange = maxViewport.top - maxViewport.height()
* scroller.getCurrY() / surfaceSizeBuffer.y;
computator.setViewportTopLeft(currXRange, currYRange);
return true;
}
return false;
}
2.在ChartTouchHandler类中同样设置回调,并在computeTouch方法中的move事件中接收回调
private onChartScrollListener onChartScrollListener;
/**
* 设置回调接口
*/
public void setOnChartScrollListener(onChartScrollListener onChartScrollListener)
{
this.onChartScrollListener = onChartScrollListener;
}
public interface onChartScrollListener
{
/**
* 当进行滑动时,将x点坐标记录
*/
public void onChartScroll(float x,float y);
}case MotionEvent.ACTION_MOVE:
// If value was touched and now touch point is outside of value area - clear touch and invalidate, user
// probably moved finger away from given chart value.
//滑动时设置回调
chartScroller.setOnScrollListener(new onScrollListener() {
@Override
public void onScroll(float right, float left) {
if(onChartScrollListener !=null){
onChartScrollListener.onChartScroll(right,left);
}
}
});
if (renderer.isTouched()) {
if (!checkTouch(event.getX(), event.getY())) {
renderer.clearTouch();
needInvalidate = true;
}
}
break;
3.在AbstractChartView类中也设置回调函数,并在onTouchEvent方法中接收回调
private onScrollListener onScrollListener;
/**
* 设置回调接口
*/
public void setOnScrollListener(onScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
public interface onScrollListener {
/**
* 当进行滑动时,将x点坐标记录
*/
public void onChartScroll(float x, float y);
}@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
touchHandler.setOnChartScrollListener(new onChartScrollListener() {
@Override
public void onChartScroll(float right, float left) {
if (onScrollListener != null) {
onScrollListener.onChartScroll(right, left);
}
}
});
if (isInteractive) {
boolean needInvalidate;
if (isContainerScrollEnabled) {
needInvalidate = touchHandler.handleTouchEvent(event,
getParent(), containerScrollType);
} else {
needInvalidate = touchHandler.handleTouchEvent(event);
}
if (needInvalidate) {
ViewCompat.postInvalidateOnAnimation(this);
}
return true;
} else {
return false;
}
}
4.然后就可以在我们的页面中接收回调了,实现滑动加载数据
//滑动回调
chart.setOnScrollListener(new onScrollListener() {
@Override
public void onChartScroll(float right, float left) {
// TODO Auto-generated method stub
Log.e("xy", right + ":" + left);
//是否滑动
isfill = true;
xList.clear();
labels.clear();
for (int i = (int) left; i < right; i++) {
//当前可见的点加入list
xList.add(dayList.get(i));
//同可见的点名称加入list
labels.add(dayList.get(i).getTime());
}
// List mList = new ArrayList();
// mList.clear();
// //特殊点的过滤算法
// ArrayList fList = new ArrayList();
// fList.clear();
// int len = xList.size();
// int i = 0;
// //加入可见范围的第一个点
// if (Collections.frequency(fList, xList.get(0)) < 1) {
// fList.add(xList.get(0));
// }
// for (i = 0; i < len; i++) {
// if (i > 0 && i < len - 2) {
// if ((Math.abs(xList.get(i).getPointY()
// - xList.get(i + 1).getPointY())) > 10
// && Collections.frequency(mList, xList.get(i)) < 1) {
// fList.add(xList.get(i));
// }
// // if(i!=0&&Collections.frequency(fList,
// // xList.get(len/(i*10)))<1){
// // fList.add(xList.get(len/(i*10)));
// // }
// }
// }
// //加入可见范围的最后一个点
// if (Collections.frequency(fList, xList.get(len - 1)) < 1) {
// fList.add(xList.get(len - 1));
// }
//加载曲线数据
generateData(xList);
}
});
mList = new ArrayList();
// mList.clear();
// //特殊点的过滤算法
// ArrayList fList = new ArrayList();
// fList.clear();
// int len = xList.size();
// int i = 0;
// //加入可见范围的第一个点
// if (Collections.frequency(fList, xList.get(0)) < 1) {
// fList.add(xList.get(0));
// }
// for (i = 0; i < len; i++) {
// if (i > 0 && i < len - 2) {
// if ((Math.abs(xList.get(i).getPointY()
// - xList.get(i + 1).getPointY())) > 10
// && Collections.frequency(mList, xList.get(i)) < 1) {
// fList.add(xList.get(i));
// }
// // if(i!=0&&Collections.frequency(fList,
// // xList.get(len/(i*10)))<1){
// // fList.add(xList.get(len/(i*10)));
// // }
// }
// }
// //加入可见范围的最后一个点
// if (Collections.frequency(fList, xList.get(len - 1)) < 1) {
// fList.add(xList.get(len - 1));
// }
//加载曲线数据
generateData(xList);
}
});