基于MPAndroidChart库制作K线图(三) —— 手势高亮联动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31743309/article/details/82466078

目录
基于MPAndroidChart库制作K线图(一) ­­­­­­­­­­­­—— 基础图
基于MPAndroidChart库制作K线图(二) ­­­­­­­­­­­­—— 自定义x、y轴
基于MPAndroidChart库制作K线图(三) ­­­­­­­­­­­­—— 手势高亮联动


一、效果图

二、高亮联动
图表联动高亮显示时,通过OnChartValueSelectedListener()可以监听到图表的Entry值,通过k线图高亮来处理柱状图的高亮联动就需要重写onValueSelected方法

@Override
public void onValueSelected(Entry e, Highlight h) {
    if (dstCharts != null) {
        for (BarLineChartBase chart : dstCharts) {
            float touchY = h.getDrawY();//手指接触点在srcChart上的Y坐标,即手势监听器中保存数据
            float y = h.getY();
            if (chart instanceof BarChart) {
                y = touchY - srcChart.getHeight();
            } else if (chart instanceof CombinedChart) {
                y = touchY + chart.getHeight();
            }
            Highlight hl = new Highlight(h.getX(), Float.NaN, h.getDataSetIndex());
            hl.setDraw(h.getX(), y);
            chart.highlightValues(new Highlight[]{hl});
        }
    }
    if (mListener != null) {
        mListener.valueSelected(e);
    }
}

然后给CombinedChart和BarChart分别设置高亮监听

因为k线图本身有左右滑动的监听,如果再滑动高亮线的话就会冲突,而在触发k线图的监听OnChartGestureListener之前通过view的onTouch来更改手势变化

@Override
public boolean onTouch(View v, MotionEvent event) {
    mDetector.onTouchEvent(event);
    if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
        mIsLongPress = false;
        mChart.highlightValue(null, true);
        if (mListener != null) {
            mListener.disableHighlight();
        }
        mChart.enableScroll();
    }
    if (mIsLongPress && event.getAction() == MotionEvent.ACTION_MOVE) {
        if (mListener != null) {
            mListener.enableHighlight();
        }
        Highlight h = mChart.getHighlightByTouchPoint(event.getX(), event.getY());
        if (h != null) {
            h.setDraw(event.getX(), event.getY());
            mChart.highlightValue(h, true);
            mChart.disableScroll();
        }
        return true;
    }
    return false;
}

三、源码下载

github: https://github.com/xkdaq/KoinChart
coding: https://coding.net/u/xkdaq/p/KoinChart/git

猜你喜欢

转载自blog.csdn.net/qq_31743309/article/details/82466078