MPAndroidChart图形联动
2016-01-05 19:04
381 查看
MPAndroidChart图形联动
原理
获取正在滑动的Chart的触摸事件,将事件同步给想要联动的Chart
实现
添加事件处理的类
package ……; import android.graphics.Matrix; import android.util.Log; import android.view.MotionEvent; import android.view.View; import com.github.mikephil.charting.charts.BarLineChartBase; import com.github.mikephil.charting.charts.Chart; import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.listener.ChartTouchListener; import com.github.mikephil.charting.listener.OnChartGestureListener; /** * Created by Android on 2015/12/10. */ public class CoupleChartGestureListener implements OnChartGestureListener { private Chart srcChart; private Chart[] dstCharts; public CoupleChartGestureListener(Chart srcChart, Chart[] dstCharts) { this.srcChart = srcChart; this.dstCharts = dstCharts; } @Override public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { } @Override public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { } @Override public void onChartLongPressed(MotionEvent me) { } @Override public void onChartDoubleTapped(MotionEvent me) { } @Override public void onChartSingleTapped(MotionEvent me) { for (Chart dstChart : dstCharts) { if (dstChart.getVisibility() == View.VISIBLE) { Highlight h = ((BarLineChartBase)dstChart).getHighlightByTouchPoint(me.getX(), me.getY()); ((BarLineChartBase)dstChart).highlightTouch(h); } } } @Override public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { } @Override public void onChartScale(MotionEvent me, float scaleX, float scaleY) { syncCharts(); } @Override public void onChartTranslate(MotionEvent me, float dX, float dY) { syncCharts(); } public void syncCharts() { Matrix srcMatrix; float[] srcVals = new float[9]; Matrix dstMatrix; float[] dstVals = new float[9]; // get src chart translation matrix: srcMatrix = srcChart.getViewPortHandler().getMatrixTouch(); srcMatrix.getValues(srcVals); // apply X axis scaling and position to dst charts: for (Chart dstChart : dstCharts) { if (dstChart.getVisibility() == View.VISIBLE) { dstMatrix = dstChart.getViewPortHandler().getMatrixTouch(); dstMatrix.getValues(dstVals); dstVals[Matrix.MSCALE_X] = srcVals[Matrix.MSCALE_X]; dstVals[Matrix.MTRANS_X] = srcVals[Matrix.MTRANS_X]; dstMatrix.setValues(dstVals); dstChart.getViewPortHandler().refresh(dstMatrix, dstChart, true); } } } }
事件传递
// 获取K线控件 KLineChart mKLine = (KLineChart) findViewById(R.id.kLineView); // 获取交易量控件 TradingVolumeChart mTradingVolumeView = (TradingVolumeChart) findViewById(R.id.tradingVolumeView); // 将K线控的滑动事件传递给交易量控件 mKLine.setOnChartGestureListener(new CoupleChartGestureListener(mKLine, new Chart[]{mTradingVolumeView})); // 将交易量控件的滑动事件传递给K线控件 mTradingVolumeView.setOnChartGestureListener(new CoupleChartGestureListener(mTradingVolumeView, new Chart[]{mKLine}));
相关文章推荐
- Android调用发送短信程序
- android 自定义相机
- android 系统相机
- 在Android程序中使用已有的SQLite数据库
- Android - JNI静态(static)载入OpenCV
- android解析xml简单分析
- Android中捕获TTextView文本中的链接点击事件方法
- android中Rect类的使用
- Android中AsyncTask的简单入手方式
- Android 系统应用调用,intent 的使用方法总结
- Android高效显示图片详解(三)
- Android高效显示图片详解(二)
- Android studio jni编写步骤,流程
- Android高效显示图片详解(一)
- android 设置头像并保存到本地
- Android 9-patch 九图的制作与使用
- Android TextView前加图标垂直居中第一行(仿大众点评购买须知/提示语)
- Android 应用程序升级到 5.0 需要注意的问题
- android中的文件操作详解以及内部存储和外部存储
- Android 使用动态加载框架DL进行插件化开发