Skip to main content
A newer version of this page is available.

Selection and Hit Testing

  • 4 minutes to read

The Selection allows end-users to select single or multiple points or series:

Selection  example

How to: Handle chart selection changes

The code snippet below shows how to handle chart selection changes:

chart.setSelectionMode(SelectionMode.SINGLE);
chart.setSelectionKind(SelectionKind.POINT);
chart.setSelectionChangedListener(new SelectionChangedListener() {
    @Override
    public void onSelectionChanged(SelectionChangedInfo selectionChangedInfo) {
        SelectionAction currentAction = selectionChangedInfo.getAction();
        if (currentAction == SelectionAction.RESET || currentAction == SelectionAction.NONE)
            return;
        if (selectionChangedInfo.getSelectedInfo().getDataPointIndices().length > 0) {
            String toastText = String.format(
                    "The point with index %d of the '%s' series was selected.",
                    selectionChangedInfo.getSelectedInfo().getDataPointIndices()[0],
                    chart.getSeries()[selectionChangedInfo.getSelectedInfo().getSeriesIndex()].getDisplayName());
            Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT)
                    .show();
        }
    }
});

The following symbols allow you to configure the selection:

Symbol Description
ChartBase.getSelectionMode() Returns the value indicating whether single or multiple chart items can be selected.
ChartBase.setSelectionMode(SelectionMode) Specifies the value indicating whether single or multiple chart items can be selected.
ChartBase.getSelectionKind() Returns the value indicating whether series or points should be selected.
ChartBase.setSelectionKind(SelectionKind) Specifies the value indicating whether series or points should be selected.
ChartBase.setSelectionChangedListener(SelectionChangedListener) Specifies the listener handling the event when series or their point is selected.
SelectionChangedListener The interface to implement a listener receiving information about changing selected/deselected chart points and series.
SelectionChangedInfo The information that is received by SelectionChangedListener.

How to: Select individual point or whole series

The Charts’ selection mechanism allows you to specify a type of chart elements which should be selected - a series or a point. The following code demonstrates how to do this:

   chart.setSelectionKind(SelectionKind.SERIES);

The following classes and methods allow you to specify what chart element should be selected:

Symbol Description
ChartBase.getSelectionKind() Returns the value indicating whether series or points should be selected.
ChartBase.setSelectionKind(SelectionKind) Specifies the value indicating whether series or points should be selected.
SelectionKind Lists the values indicating the chart element can be selected.

How to: Select several points (series) simultaneously

The Chart selection mechanism allows you to select one or more chart elements simultaneously. The following code demonstrates how to configure the selection mechanism to accomplish this:

   chart.setSelectionMode(SelectionMode.MULTIPLE);

The following classes and methods allow you to specify the selection mode:

Symbols Description
ChartBase.getSelectionMode Returns the value indicating whether single or multiple chart items can be selected.
ChartBase.setSelectionMode(SelectionMode) Specifies the value indicating whether single or multiple chart items can be selected.
SelectionMode Lists the values used to specify the selection mode of chart elements.

How to: Get information about chart elements under the specified point

The Hit testing allows you to recognize which element is at the specified screen coordinates. For instance, determining which chart element has been tapped by an end-user. The following example shows how to perform hit testing:

chart.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && event.getPointerCount() == 1){
        StringBuilder builder = new StringBuilder("You tap");
        boolean useComma = false;
        HitTestInfo info = chart.calcHitInfo(event.getX(), event.getY());
        if (info.isInLegend()) {
            builder.append(" in the legend");
            useComma = true;
        }
        if (info.isInSeries()) {
            if (useComma) builder.append(",");
            builder.append(" in a series");
            useComma = true;
        }
        if (info.isInPoint()) {
            if (useComma) builder.append(",");
            builder.append(" in a point");
        }
        builder.append(".");
        Toast.makeText(getApplicationContext(), builder.toString(), Toast.LENGTH_SHORT)
                .show();
        }
        return false;
    }
});

The following methods and classes allows you to perform hit testing:

Symbols Description
ChartBase.calcHitInfo(float,float) Returns information about chart elements at a specified point.
HitTestInfo Information about chart elements under a hit-test point.