MPAndroidChart图形联动

MPAndroidChart图形联动

本篇基于博客MPAndroidChart的K线图上添加均线,两个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}));

posted on 2016-01-05 19:04  封起De日子  阅读(676)  评论(0编辑  收藏  举报

导航