这一节我们会介绍K线面板中其它细节的实现方式和可能遇到的问题。
1.刷新当前展示的数据
2.自定义y轴的样式
3.拉伸和压缩K线图
源码下载地址:https://github.com/Lonely7th/TsAndroidClient
1.刷新当前展示的数据
上一节我们谈到了如何处理用户的手势操作,处理的结果最终都要体现在K线图中,CandleStickChart已经为我们实现了很多功能,我们只需要更新List
刷新数据的起点和终点,每次执行滑动操作时都会调用该方法:
private boolean updateDataIndex(int direction) {
if (direction < 0 && currentEndIndex >= tkData.size() - 1) {//页面不能向左滑动
return false;
}
if (direction > 0 && currentEndIndex <= currentShowCount) {//页面不能向右滑动
return false;
}
currentEndIndex -= direction;
if (currentShowCount < currentEndIndex) {
currentStartIndex = currentEndIndex - currentShowCount;
} else {
int length = tkData.size() - 1;
currentEndIndex = currentShowCount > length ? length : currentShowCount;
}
return true;
}
更新数据列表,数据起点、数据终点和数据展示量有变动时会调用该方法:
private float[] updateDrawData() {
if (xVals != null && yVals != null) {
xVals.clear();
yVals.clear();
}
float yAxisMax = Float.MIN_VALUE;
float yAxisMin = Float.MAX_VALUE;
for (int i = currentStartIndex; i <= currentEndIndex; i++) {
TkDetailsBean bean = tkData.get(i);
float open = Float.parseFloat(bean.getCur_open_price());
float close = Float.parseFloat(bean.getCur_close_price());
float shadowH = Float.parseFloat(bean.getCur_max_price());
float shadowL = Float.parseFloat(bean.getCur_min_price());
CandleEntry ce = new CandleEntry(i - currentStartIndex, shadowH, shadowL, open, close);
yVals.add(ce);
xVals.add("" + (i - currentStartIndex));
if (yAxisMax < shadowH) {
yAxisMax = shadowH;
}
if (yAxisMin > shadowL) {
yAxisMin = shadowL;
}
}
return new float[]{yAxisMax, yAxisMin};
}
2.自定义y轴的样式
CandleStickChart提供了很多方法用于定义y轴的样式,包括自定义网格线、文字颜色、文字格式等等。
CandleStickChart会自动计算当前数据的最大值和最小值作为绘图的上下边界,但是这个边界并不会随着展示数据的变动而改变,所以这里我们需要每次都重新设置y轴的边界值:
float[] yAxisArray = updateDrawData();
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMaxValue(yAxisArray[0]);
leftAxis.setAxisMinValue(yAxisArray[1]);
CandleStickChart会根据数据的最大值和最小值计算坐标点,这里我们自定义坐标点的文字样式,让这些文字以浮点数展示:
leftAxis.setValueFormatter(new MyYAxisValueFormatter());
public class MyYAxisValueFormatter implements YAxisValueFormatter {
private DecimalFormat mFormat;
public MyYAxisValueFormatter () {
mFormat = new DecimalFormat("###,###,##0.00");
}
@Override
public String getFormattedValue(float value, YAxis yAxis) {
return mFormat.format(value);
}
}
3.拉伸和压缩K线图
拉伸和压缩K线图也是K线面板中不可缺少的功能,这里我们通过修改当前的数据展示量来实现拉伸和压缩的效果:
switch (view.getId()) {
case R.id.btn_bar_min://页面压缩
if (currentShowCount > SCREEN_DATA_MIN) {
currentShowCount -= SCREEN_DATA_CHANGE;
onScaleUI();
}
break;
case R.id.btn_bar_max://页面拉伸
if (currentShowCount < SCREEN_DATA_MAX) {
currentShowCount += SCREEN_DATA_CHANGE;
onScaleUI();
}
break;
}
private void onScaleUI() {
updateDataIndex(0);
...
mChart.highlightValues(null);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
缓存当前展示的股票代码,我们将当前展示的股票代码存入缓存,便于下次打开时使用:
//缓存当前展示的证券代码
SharedPreferencesUtils.setCurrentTkCode(tkCode);
public static void setCurrentTkCode(String tkCode){
SharedPreferences.Editor editor = TsApplication.sf.edit();
editor.putString("cur_tk_code", tkCode);
editor.commit();
}
如果文章中有描述错误的地方,请与我联系:[email protected]