Pre loader

Android Chart Interactive Annotations

Android Chart - Examples

SciChart Android ships with ~90 Android Chart Examples which you can browse, play with, view the source-code and even export each SciChart Android Chart Example to a stand-alone Android Studio project. All of this is possible with the new and improved SciChart Android Examples Suite, which ships as part of our Android Charts SDK.

Download Scichart Android

Demonstrates how you may interact with annotations that have been placed on a chart, using the Annotations API.

Annotation Types Include

– AxisMarkerAnnotation
BoxAnnotation
– CustomAnnotation
– HorizontalLineAnnotation
– LineAnnotation
– LineArrowAnnotation
TextAnnotation
VerticalLineAnnotation

Tip!

To make an annotation Read-only, call the setIsEditable(false) on an Annotation. In this example, all the annotations have IsEditable=true. You can select them, drag and resize via the anchor-points.

The full source code for the Android Chart Interactive Annotations example is included below (Scroll down!).

Did you know you can also view the source code from one of the following sources as well?

  1. Clone the SciChart.Android.Examples from Github.
  2. Or, view source and export each example to an Android Studio project from the Java version of the SciChart Android Examples app.
  3. Also the SciChart Android Trial contains the full source for the examples (link below).

DOWNLOAD THE ANDROID CHART EXAMPLES

Kotlin: InteractionWithAnnotationsFragment.kt
View source code
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales:   sales@scichart.com
//
// InteractionWithAnnotationsFragment.kt is part of SCICHART®, High Performance Scientific Charts
// For full terms and conditions of the license, see http://www.scichart.com/scichart-eula/
//
// This source code is protected by international copyright law. Unauthorized
// reproduction, reverse-engineering, or distribution of all or any portion of
// this source code is strictly prohibited.
//
// This source code contains confidential and proprietary trade secrets of
// SciChart Ltd., and should at no time be copied, transferred, sold,
// distributed or made available without express written permission.
//******************************************************************************

package com.scichart.examples.fragments.examples2d.chartAnnotations.kt

import android.view.Gravity
import com.scichart.charting.visuals.SciChartSurface
import com.scichart.charting.visuals.annotations.AnnotationCoordinateMode
import com.scichart.charting.visuals.annotations.AnnotationSurfaceEnum
import com.scichart.charting.visuals.annotations.HorizontalAnchorPoint
import com.scichart.charting.visuals.annotations.LabelPlacement
import com.scichart.data.model.DoubleRange
import com.scichart.drawing.common.FontStyle
import com.scichart.drawing.utility.ColorUtil
import com.scichart.examples.R
import com.scichart.examples.data.MarketDataService
import com.scichart.examples.fragments.base.ExampleSingleChartBaseFragment
import com.scichart.examples.utils.scichartExtensions.*
import java.util.*

class InteractionWithAnnotationsFragment : ExampleSingleChartBaseFragment() {

    override fun showDefaultModifiersInToolbar(): Boolean = false

    override fun initExample(surface: SciChartSurface) {
        surface.theme = R.style.SciChart_NavyBlue

        surface.suspendUpdates {
            xAxes { categoryDateAxis { } }
            yAxes { numericAxis { visibleRange = DoubleRange(30.0, 37.0) } }
            renderableSeries {
                fastCandlestickRenderableSeries {
                    dataSeries = OhlcDataSeries<Date, Double>().apply {
                        val marketDataService = MarketDataService(Calendar.getInstance().time, 5, 5)
                        val data = marketDataService.getHistoricalData(200)

                        append(data.dateData, data.openData, data.highData, data.lowData, data.closeData)
                    }
                    opacity = 0.4f
                }
            }
            chartModifiers { zoomPanModifier() }

            annotations {
                textAnnotation {
                    x1 = 10; y1 = 30.5
                    setIsEditable(true)
                    text = "Buy!"
                    fontStyle = FontStyle(20f, ColorUtil.White)
                    zIndex = 1 // draw this annotation above other annotations
                }
                textAnnotation {
                    x1 = 50; y1 = 34.0
                    setBackgroundResource(R.drawable.example_text_annotation_background)
                    setIsEditable(true)
                    text = "Sell!"
                    fontStyle = FontStyle(20f, ColorUtil.White)
                    setPadding(8, 8, 8, 8)
                    zIndex = 1 // draw this annotation above other annotations
                }
                textAnnotation {
                    x1 = 80; y1 = 37
                    setIsEditable(true)
                    text = "Rotated text"
                    fontStyle = FontStyle(20f, ColorUtil.White)
                    rotationAngle = 30f
                    zIndex = 1 // draw this annotation above other annotations
                }
                boxAnnotation {
                    x1 = 50; y1 = 35.5; x2 = 120; y2 = 32
                    setIsEditable(true)
                    setBackgroundResource(R.drawable.example_box_annotation_background_4)
                }
                lineAnnotation {
                    x1 = 40; y1 = 30.5; x2 = 60; y2 = 33.5
                    setIsEditable(true)
                    stroke = SolidPenStyle(0xAAFF6600, 2f)
                }
                lineAnnotation {
                    x1 = 120; y1 = 30.5; x2 = 175; y2 = 36
                    setIsEditable(true)
                    stroke = SolidPenStyle(0xAAFF6600, 2f)
                }
                lineArrowAnnotation {
                    x1 = 50; y1 = 35; x2 = 80; y2 = 31.4
                    headLength = 8f
                    headWidth = 16f
                    setIsEditable(true)
                }
                axisMarkerAnnotation {
                    y1 = 32.7
                    annotationSurface = AnnotationSurfaceEnum.YAxis
                    setIsEditable(true)
                }
                axisMarkerAnnotation {
                    x1 = 100
                    annotationSurface = AnnotationSurfaceEnum.XAxis
                    formattedValue = "Horizontal"
                    setIsEditable(true)
                }
                horizontalLineAnnotation {
                    x1 = 150; y1 = 32.2
                    stroke = SolidPenStyle(ColorUtil.Red, 2f)
                    horizontalGravity = Gravity.END
                    setIsEditable(true)
                    annotationLabels {
                        annotationLabel { labelPlacement = LabelPlacement.Axis }
                    }
                }
                horizontalLineAnnotation {
                    x1 = 130; y1 = 33.9; x2 = 160
                    stroke = SolidPenStyle(ColorUtil.Blue, 2f)
                    horizontalGravity = Gravity.CENTER_HORIZONTAL
                    setIsEditable(true)
                    annotationLabels {
                        annotationLabel { labelPlacement = LabelPlacement.Left; labelValue = "Left" }
                        annotationLabel { labelPlacement = LabelPlacement.Top; labelValue = "Top" }
                        annotationLabel { labelPlacement = LabelPlacement.Right; labelValue = "Right" }
                    }
                }
                verticalLineAnnotation {
                    x1 = 20; y1 = 35; y2 = 33
                    stroke = SolidPenStyle(ColorUtil.DarkGreen, 2f)
                    verticalGravity = Gravity.CENTER_VERTICAL
                    setIsEditable(true)
                }
                verticalLineAnnotation {
                    x1 = 40; y1 = 34
                    stroke = SolidPenStyle(ColorUtil.Green, 2f)
                    verticalGravity = Gravity.TOP
                    setIsEditable(true)
                    annotationLabels {
                        annotationLabel { labelPlacement = LabelPlacement.Top; rotationAngle = 90f }
                    }
                }
                textAnnotation {
                    x1 = 0.5; y1 = 0.5
                    coordinateMode = AnnotationCoordinateMode.Relative
                    horizontalAnchorPoint = HorizontalAnchorPoint.Center
                    text = "EUR.USD"
                    fontStyle = FontStyle(72f, 0x77FFFFFF)
                    zIndex = -1 // draw this annotation below other annotations
                }
            }
        }
    }
}
Java: InteractionWithAnnotationsFragment.java
View source code
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales:   sales@scichart.com
//
// InteractionWithAnnotationsFragment.java is part of SCICHART®, High Performance Scientific Charts
// For full terms and conditions of the license, see http://www.scichart.com/scichart-eula/
//
// This source code is protected by international copyright law. Unauthorized
// reproduction, reverse-engineering, or distribution of all or any portion of
// this source code is strictly prohibited.
//
// This source code contains confidential and proprietary trade secrets of
// SciChart Ltd., and should at no time be copied, transferred, sold,
// distributed or made available without express written permission.
//******************************************************************************

package com.scichart.examples.fragments.examples2d.chartAnnotations;

import android.graphics.Typeface;
import android.view.Gravity;

import androidx.annotation.NonNull;

import com.scichart.charting.model.dataSeries.OhlcDataSeries;
import com.scichart.charting.modifiers.ZoomPanModifier;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.annotations.AnnotationCoordinateMode;
import com.scichart.charting.visuals.annotations.AnnotationSurfaceEnum;
import com.scichart.charting.visuals.annotations.HorizontalAnchorPoint;
import com.scichart.charting.visuals.annotations.LabelPlacement;
import com.scichart.drawing.utility.ColorUtil;
import com.scichart.examples.R;
import com.scichart.examples.data.MarketDataService;
import com.scichart.examples.data.PriceSeries;
import com.scichart.examples.fragments.base.ExampleSingleChartBaseFragment;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

public class InteractionWithAnnotationsFragment extends ExampleSingleChartBaseFragment {

    @Override
    public boolean showDefaultModifiersInToolbar() {
        return false;
    }

    @Override
    protected void initExample(@NonNull SciChartSurface surface) {
        surface.setTheme(R.style.SciChart_NavyBlue);

        final OhlcDataSeries<Date, Double> dataSeries = sciChartBuilder.newOhlcDataSeries(Date.class, Double.class).build();

        final MarketDataService marketDataService = new MarketDataService(Calendar.getInstance().getTime(), 5, 5);
        final PriceSeries data = marketDataService.getHistoricalData(200);

        dataSeries.append(data.getDateData(), data.getOpenData(), data.getHighData(), data.getLowData(), data.getCloseData());

        Collections.addAll(surface.getRenderableSeries(), sciChartBuilder.newCandlestickSeries().withDataSeries(dataSeries).withOpacity(0.4f).build());
        Collections.addAll(surface.getXAxes(), sciChartBuilder.newCategoryDateAxis().build());
        Collections.addAll(surface.getYAxes(), sciChartBuilder.newNumericAxis().withVisibleRange(30d, 37d).build());
        Collections.addAll(surface.getChartModifiers(), new ZoomPanModifier());

        Collections.addAll(surface.getAnnotations(),
                sciChartBuilder.newTextAnnotation()
                        .withPosition(10, 30.5d)
                        .withIsEditable(true)
                        .withText("Buy!")
                        .withFontStyle(20, ColorUtil.White)
                        .withZIndex(1) // draw this annotation above other annotations
                        .build(),
                sciChartBuilder.newTextAnnotation()
                        .withPosition(50, 34d)
                        .withBackgroundDrawableId(R.drawable.example_text_annotation_background)
                        .withIsEditable(true)
                        .withText("Sell!")
                        .withFontStyle(20, ColorUtil.White)
                        .withPadding(8)
                        .withZIndex(1) // draw this annotation above other annotations
                        .build(),
                sciChartBuilder.newTextAnnotation()
                        .withX1(80d).withY1(37d)
                        .withIsEditable(true)
                        .withText("Rotated text")
                        .withFontStyle(20, ColorUtil.White)
                        .withRotationAngle(30)
                        .withZIndex(1) // draw this annotation above other annotations
                        .build(),
                sciChartBuilder.newBoxAnnotation()
                        .withPosition(50, 35.5, 120, 32)
                        .withIsEditable(true)
                        .withBackgroundDrawableId(R.drawable.example_box_annotation_background_4)
                        .build(),
                sciChartBuilder.newLineAnnotation()
                        .withPosition(40, 30.5d, 60, 33.5d)
                        .withIsEditable(true)
                        .withStroke(2f, 0xAAFF6600)
                        .build(),
                sciChartBuilder.newLineAnnotation()
                        .withPosition(120, 30.5, 175, 36)
                        .withIsEditable(true)
                        .withStroke(2f, 0xAAFF6600)
                        .build(),
                sciChartBuilder.newLineArrowAnnotation()
                        .withPosition(50, 35d, 80, 31.4d)
                        .withArrowHeadLength(8f)
                        .withArrowHeadWidth(16f)
                        .withIsEditable(true)
                        .build(),
                sciChartBuilder.newAxisMarkerAnnotation()
                        .withIsEditable(true)
                        .withY1(32.7d)
                        .build(),
                sciChartBuilder.newAxisMarkerAnnotation()
                        .withAnnotationSurface(AnnotationSurfaceEnum.XAxis)
                        .withFormattedValue("Horizontal")
                        .withIsEditable(true)
                        .withX1(100)
                        .build(),
                sciChartBuilder.newHorizontalLineAnnotation()
                        .withPosition(150d, 32.2d)
                        .withStroke(2, ColorUtil.Red)
                        .withHorizontalGravity(Gravity.END)
                        .withIsEditable(true)
                        .withAnnotationLabel(LabelPlacement.Axis)
                        .build(),
                sciChartBuilder.newHorizontalLineAnnotation()
                        .withX1(130).withY1(33.9d).withX2(160)
                        .withStroke(2, ColorUtil.Blue)
                        .withHorizontalGravity(Gravity.CENTER_HORIZONTAL)
                        .withIsEditable(true)
                        .withAnnotationLabel(LabelPlacement.Left, "Left")
                        .withAnnotationLabel(LabelPlacement.Top, "Top")
                        .withAnnotationLabel(LabelPlacement.Right, "Right")
                        .build(),
                sciChartBuilder.newVerticalLineAnnotation()
                        .withX1(20).withY1(35d).withY2(33d)
                        .withStroke(2, ColorUtil.DarkGreen)
                        .withVerticalGravity(Gravity.CENTER_VERTICAL)
                        .withIsEditable(true)
                        .build(),
                sciChartBuilder.newVerticalLineAnnotation()
                        .withPosition(40, 34)
                        .withStroke(2, ColorUtil.Green)
                        .withVerticalGravity(Gravity.TOP)
                        .withIsEditable(true)
                        .withAnnotationLabel(LabelPlacement.Top, null, 90)
                        .build(),
                sciChartBuilder.newTextAnnotation()
                        .withPosition(0.5, 0.5)
                        .withCoordinateMode(AnnotationCoordinateMode.Relative)
                        .withHorizontalAnchorPoint(HorizontalAnchorPoint.Center)
                        .withText("EUR.USD")
                        .withFontStyle(Typeface.DEFAULT_BOLD, 72, 0x77FFFFFF)
                        .withZIndex(-1) // draw this annotation below other annotations
                        .build()
        );
    }
}
Back to Android Chart Examples