MPAndroidChart 教程:与图表的交互 Interaction with the Chart

该库允许您完全自定义与图表视图的可能触摸(和手势)交互,并通过回调方法对交互作出反应。

启用/禁用交互

  • setTouchEnabled(boolean enabled):启用/禁用与图表的所有可能的触摸交互。
  • setDragEnabled(boolean enabled):启用/禁用图表的拖动(平移)。
  • setScaleEnabled(boolean enabled):启用/禁用缩放图表上的两个轴。
  • setScaleXEnabled(boolean enabled):启用/禁用x轴上的缩放。
  • setScaleYEnabled(boolean enabled):启用/禁用y轴缩放。
  • setPinchZoom(boolean enabled):如果设置为true,则启用缩放缩放。如果禁用,则可以单独缩放x轴和y轴。
  • setDoubleTapToZoomEnabled(boolean enabled):将此设置为false以禁止通过双击来缩放图表。

图表抛掷/减速

  • setDragDecelerationEnabled(boolean enabled):如果设置为true,图表会在触摸后继续滚动,默认值:true。
  • setDragDecelerationFrictionCoef(float coef):减速摩擦系数[0; 1]间隔,较高的值表示速度将缓慢下降,例如,如果设置为0,它将立即停止。1是无效值,将自动转换为0.9999。

突出显示值

highlightning section,对如何通过点击手势和编程方式突出显示条目进行了描述。

手势回调

OnChartGestureListener将允许您对图表上的手势做出反应:

public interface OnChartGestureListener {

    /**
     * Callbacks when a touch-gesture has started on the chart (ACTION_DOWN)
     *
     * @param me
     * @param lastPerformedGesture
     */
    void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);

    /**
     * Callbacks when a touch-gesture has ended on the chart (ACTION_UP, ACTION_CANCEL)
     *
     * @param me
     * @param lastPerformedGesture
     */
    void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);

    /**
     * Callbacks when the chart is longpressed.
     * 
     * @param me
     */
    public void onChartLongPressed(MotionEvent me);

    /**
     * Callbacks when the chart is double-tapped.
     * 
     * @param me
     */
    public void onChartDoubleTapped(MotionEvent me);

    /**
     * Callbacks when the chart is single-tapped.
     * 
     * @param me
     */
    public void onChartSingleTapped(MotionEvent me);

    /**
     * Callbacks then a fling gesture is made on the chart.
     * 
     * @param me1
     * @param me2
     * @param velocityX
     * @param velocityY
     */
    public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY);

   /**
     * Callbacks when the chart is scaled / zoomed via pinch zoom gesture.
     * 
     * @param me
     * @param scaleX scalefactor on the x-axis
     * @param scaleY scalefactor on the y-axis
     */
    public void onChartScale(MotionEvent me, float scaleX, float scaleY);

   /**
    * Callbacks when the chart is moved / translated via drag gesture.
    *
    * @param me
    * @param dX translation distance on the x-axis
    * @param dY translation distance on the y-axis
    */
    public void onChartTranslate(MotionEvent me, float dX, float dY);
}

只需让你的接收回调的类实现这个接口并将其设置为图表的监听器:

chart.setOnChartGestureListener(this);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值