Annotations
Annotations allow you to highlight particular parts of the data. There are two concepts in Chartreuse's annotation system:
- AnnotationType, which defines various types of annotations, each with its specific characteristics; and
- AnnotationPosition, which represents a specific positioning for annotations along with an arrow angle and placement logic.
Types of annotations
Annotation types are found within the chartreuse.component package.
Currently, there are four different types of annotations:
- Circle, a circle with user-specified radius;
- CircleWithText, text along with a circle with user-specified radius;
- Text, plain text; and
- TextWithBox, text in a box.
An example of creating an AnnotationType:
val annotationType = AnnotationType.CircleWithText(15, "Something interesting happened here")
Positioning the annotations
Each annotation in Chartreuse uses AnnotationPosition for positioning.
AnnotationPosition uses Landmark, Angle and (Point, Double) => Point to place an annotation:
- The
Landmarkis used as the reference point for annotation placement; - The
Angleis used to properly rotate the arrow indicating the annotation (More on the arrows in the following block); and - The
(Point, Double) => Pointis a function that takes a base point and a margin offset, and returns the final annotation point. For example,(pt, offset) => Point(pt.x - offset, pt.y + offset)will place the annotation diagonally above and to the left of the point of interest.
Most users will use predefined annotation positioning options for ease of use (e.g. AnnotationPosition.topLeft).
But for more precise positioning it is possible to define all the parameters manually.
Creating annotations
To create an annotation, it's enough to use the apply method of Annotation, which takes the point of interest and the AnnotationType:
Annotation(
Point(1950, 81651),
AnnotationType.Text("Rapid growth began here")
)
Annotations can be added to a plot with the addAnnotation method:
val annotatedPlot = plot
.addAnnotation(annotation)
By default, each annotation is placed in the center of the point of interest:
But the position can be adjusted using the withAnnotationPosition method:
Annotation(
Point(1950, 81651),
AnnotationType.Text("Rapid growth began here")
)
.withAnnotationPosition(AnnotationPosition.bottomRight)
Which will produce
In addition, it's possible to draw an arrow between the text and the point of interest. withArrow method is used for this.
Arrows are placed automatically:
Annotation(
Point(1950, 81651),
AnnotationType.Text("Rapid growth began here")
)
.withAnnotationPosition(AnnotationPosition.bottomRight)
.withArrow()
Which will produce