SciChart Android 2D Charts API > Annotations API > HorizontalLineAnnotation
HorizontalLineAnnotation

The HorizontalLineAnnotation allows to draw a horizontal line between X1, X2 coordinates at Y1:

 

HorizontalLineAnnotation Features

Please refer to the Common Annotation Features to learn more about the ones inherited from the base class.  The HorizontalLineAnnotation class itself allows setting HorizontalGravity. More about it in the next paragraph.

Position HorizontalLineAnnotation

In general case, the position of an HorizontalLineAnnotation can be defined by the Y1 value. It can be set via the setY1() method. 

Additionaly, it is possible to specify X coordinates for the line ends. This requires setting HorizontalGravity to a corresponding value via the setHorizontalGravity() method. The recognizable values are Gravity.FILL_HORIZONTALGravity.CENTER_HORIZONTALGravity.RIGHTGravity.LEFT. In combination with the X1, X2 values they will end up in the following behavior:

The X1, X2 values can be set via the setX1() or setX2() methods.

In a multi-axis scenario, or if you changed the Ids of your axes, both XAxisId and YAxisId must be set. This can be done via the setAxisId(), setYAxisId() methods.

Create HorizontalLineAnnotation

HorizontalLineAnnotation can be added onto a chart using the following code:

Copy Code
// assume the surface has been created and configured before
ISciChartSurface surface;

// create a HorizontalLineAnnotation
HorizontalLineAnnotation horizontalLine = new HorizontalLineAnnotation(getActivity());

// allow to interact with the annotation in run-time
horizontalLine.setEditable(true);

// in a multi-axis scenario, specify the XAxisId and YAxisId
horizontalLine.setXAxisId("Top_X_Axis");
horizontalLine.setYAxisId("Left_Y_Axis");

// specify that the annotation should appear fully stretched
horizontalLine.setHorizontalGravity(Gravity.FILL_HORIZONTAL);

// set the Y1 coordinate
horizontalLine.setY1(400d);

// add the annotation to the Annotations collection of the surface
Collections.addAll(surface.getAnnotations(), horizontalLine);

Also, a HorizontalLineAnnotation can be created using Chart Builders:

Copy Code
HorizontalLineAnnotation horizontalLine = sciChartBuilder.newHorizontalLineAnnotation()
        .withPosition(5d, 3.2d)
        .withStroke(2, ColorUtil.Orange)
        .withHorizontalGravity(Gravity.RIGHT)
        .withXAxisId("Top_X_Axis")
        .withYAxisId("Left_Y_Axis")
        .withIsEditable(true)
        .build();

To learn more about other annotation types, available out of the box in SciChart, please find the comprehensive list in the What Is an Annotation article.

See Also