Skip to content

Interaction with the Chart

Philipp Jahoda edited this page May 20, 2015 · 30 revisions

This library allows you to fully customize the possible touch (and gesture) interaction with the chart-view and react to the interaction via callback-methods.

Enabling / disabling interaction

  • setTouchEnabled(boolean enabled): Allows to enable/disable all possible touch-interactions with the chart.
  • setTouchEnabled(boolean enabled): Allows to enable/disable all possible touch-interactions with the chart.
  • setDragEnabled(boolean enabled): Enables/disables dragging (panning) for the chart.
  • setScaleEnabled(boolean enabled): Enables/disables scaling for the chart on both axes.
  • setScaleXEnabled(boolean enabled): Enables/disables scaling on the x-axis.
  • setScaleYEnabled(boolean enabled): Enables/disables scaling on the y-axis.
  • setPinchZoom(boolean enabled): If set to true, pinch-zooming is enabled. If disabled, x- and y-axis can be zoomed separately.
  • setDoubleTapToZoomEnabled(boolean enabled): Set this to false to disallow zooming the chart via double-tap on it.
  • setHighlightEnabled(boolean enabled): If set to true, highlighting/selecting values via touch is possible on the chart.
  • setHighlightPerDragEnabled(boolean enabled): Set this to true to allow highlighting per dragging over the chart surface when it is fully zoomed out. Default: true
  • setHighlightIndicatorEnabled(boolean enabled): If set to true, the indicator lines (or bars) that show which value has been selected are drawn.

Chart fling / deceleration

  • setDragDecelerationEnabled(boolean enabled): If set to true, chart continues to scroll after touch up. Default: true.
  • setDragDecelerationFrictionCoef(float coef): Deceleration friction coefficient in [0 ; 1] interval, higher values indicate that speed will decrease slowly, for example if it set to 0, it will stop immediately. 1 is an invalid value, and will be converted to 0.9999 automatically.

Highlighting programmatically

  • highlightValues(Highlight[] highs): Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting.
  • highlightValue(int xIndex, int dataSetIndex): Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting.
  • getHighlighted(): Returns an Highlight[] array that contains information about all highlighted entries, their x-index and dataset-index.

Highlighting values programmatically will not generate a callback to the OnChartValueSelectedListener.

Selection callbacks

This library provides a number of listeners for callbacks upon interaction. One of them is the OnChartValueSelectedListener, for callbacks when highlighting values via touch:

public interface OnChartValueSelectedListener {
    /**
    * Called when a value has been selected inside the chart.
    *
    * @param e The selected Entry.
    * @param dataSetIndex The index in the datasets array of the data object
    * the Entrys DataSet is in.
    * @param h the corresponding highlight object that contains information
    * about the highlighted position
    */
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h);
    /**
    * Called when nothing has been selected or an "un-select" has been made.
    */
    public void onNothingSelected();
}

Simply let your class that should receive the callbacks implement this interface and set it as a listener to the chart:

chart.setOnChartValueSelectedListener(this);

Gesture callbacks

The OnChartGestureListener will allow you to react to gestures made on the chart:

public interface OnChartGestureListener {

    /**
     * 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);
}

Simply let your class that should receive the callbacks implement this interface and set it as a listener to the chart:

chart.setOnChartGestureListener(this);

The documentation has moved.

Clone this wiki locally