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.
Demonstrates how to enable freehand drawing directly on a SciChartSurface using touch gestures. This example shows how users can sketch arbitrary shapes, highlights, or markups on charts by capturing continuous touch input and converting it into a drawable path.
val freehandDrawingModifier = FreehandDrawingModifier().apply {
brushColor = Color.WHITE
brushThickness = 5f
setAnnotationCreationListener(listener)
}
// Create Freehand Drawing Annotation
val freehandDrawingAnnotation = FreehandDrawingAnnotation(this).apply {
initialBasePoints.add(ComparablePoint(140, 32.5))
initialBasePoints.add(ComparablePoint(150, 32.8))
initialBasePoints.add(ComparablePoint(160, 32.2))
initialBasePoints.add(ComparablePoint(170, 32.6))
brushColor = Color.YELLOW
brushThickness = 8f
setIsEditable(true)
}
surface.chartModifiers.add(freehandDrawingModifier)
The full source code for the Android Bubble Chart example is included below (Scroll down!).
Did you know you can also view the source code from one of the following sources as well?
- Clone the SciChart.Android.Examples from Github.
- Or, view source and export each example to an Android Studio project of the SciChart Android Examples app.
- Also the SciChart Android Trial contains the full source for the examples (link below).
FreehandDrawingAnnotationFragment.java
View source code//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2026. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// BrushAnnotationFragment.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.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import androidx.annotation.NonNull;
import com.scichart.charting.model.AnnotationCollection;
import com.scichart.charting.model.dataSeries.OhlcDataSeries;
import com.scichart.charting.modifiers.FreehandDrawingModifier;
import com.scichart.charting.modifiers.OnAnnotationCreatedListener;
import com.scichart.charting.modifiers.PinchZoomModifier;
import com.scichart.charting.modifiers.ZoomExtentsModifier;
import com.scichart.charting.modifiers.ZoomPanModifier;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.annotations.IAnnotation;
import com.scichart.examples.data.MarketDataService;
import com.scichart.examples.data.PriceSeries;
import com.scichart.examples.databinding.ExampleFreehandAnnotationFragmentBinding;
import com.scichart.examples.fragments.base.ExampleBaseFragment;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
public class FreehandDrawingAnnotationFragment extends ExampleBaseFragment<ExampleFreehandAnnotationFragmentBinding> implements OnAnnotationCreatedListener {
private final FreehandDrawingModifier freehandDrawingModifier = new FreehandDrawingModifier();
private final ZoomPanModifier zoomPanModifier = new ZoomPanModifier();
private final PinchZoomModifier pinchZoomModifier = new PinchZoomModifier();
private final ZoomExtentsModifier zoomExtentsModifier = new ZoomExtentsModifier();
private boolean isDrawingMode = true;
@NonNull
@Override
protected ExampleFreehandAnnotationFragmentBinding inflateBinding(@NonNull LayoutInflater inflater) {
return ExampleFreehandAnnotationFragmentBinding.inflate(inflater);
}
@Override
protected void initExample(ExampleFreehandAnnotationFragmentBinding binding) {
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());
final SciChartSurface surface = binding.surface;
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());
// Drawing modifier starts enabled
freehandDrawingModifier.setAnnotationCreationListener(this);
freehandDrawingModifier.setIsEnabled(true);
binding.thicknessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
freehandDrawingModifier.setBrushThickness(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
binding.colorWhite.setOnClickListener(v -> selectColor(v, Color.WHITE));
binding.colorYellow.setOnClickListener(v -> selectColor(v, Color.YELLOW));
binding.colorGreen.setOnClickListener(v -> selectColor(v, Color.GREEN));
binding.colorBlue.setOnClickListener(v -> selectColor(v, Color.BLUE));
// Set initial selection
selectColor(binding.colorWhite, Color.WHITE);
binding.drawingModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> setDrawingMode(isChecked, binding));
zoomPanModifier.setIsEnabled(!isDrawingMode);
pinchZoomModifier.setIsEnabled(!isDrawingMode);
zoomExtentsModifier.setIsEnabled(!isDrawingMode);
surface.getChartModifiers().add(sciChartBuilder.newModifierGroup()
.withModifier(freehandDrawingModifier)
.withModifier(zoomPanModifier)
.withModifier(pinchZoomModifier)
.withModifier(zoomExtentsModifier)
.build());
binding.deleteAnnotation.setOnClickListener(v -> {
final AnnotationCollection annotations = surface.getAnnotations();
for (int i = annotations.size() - 1; i >= 0; i--) {
final IAnnotation annotation = annotations.get(i);
if (annotation.isSelected()) {
annotations.remove(i);
}
}
});
}
@Override
public void onAnnotationCreated(@NonNull IAnnotation newAnnotation) {
newAnnotation.setIsEditable(!isDrawingMode);
}
private void selectColor(View view, int color) {
binding.colorWhite.setBackground(getColorDrawable(Color.WHITE, view == binding.colorWhite));
binding.colorYellow.setBackground(getColorDrawable(Color.YELLOW, view == binding.colorYellow));
binding.colorGreen.setBackground(getColorDrawable(Color.GREEN, view == binding.colorGreen));
binding.colorBlue.setBackground(getColorDrawable(Color.BLUE, view == binding.colorBlue));
freehandDrawingModifier.setBrushColor(color);
}
private Drawable getColorDrawable(int color, boolean isSelected) {
GradientDrawable gd = new GradientDrawable();
gd.setColor(color);
gd.setShape(GradientDrawable.RECTANGLE);
if (isSelected) {
gd.setStroke(4, Color.MAGENTA);
}
return gd;
}
/**
* Toggles between Drawing Mode and Selection Mode.
* Drawing Mode ON: drawing modifier is active, all existing annotations become non-editable/non-selectable.
* Drawing Mode OFF: drawing modifier is disabled, all existing annotations become editable/selectable.
*/
private void setDrawingMode(boolean drawingMode, ExampleFreehandAnnotationFragmentBinding binding) {
isDrawingMode = drawingMode;
freehandDrawingModifier.setIsEnabled(drawingMode);
zoomPanModifier.setIsEnabled(!drawingMode);
pinchZoomModifier.setIsEnabled(!drawingMode);
zoomExtentsModifier.setIsEnabled(!drawingMode);
final SciChartSurface surface = binding.surface;
final AnnotationCollection annotations = surface.getAnnotations();
for (int i = 0; i < annotations.size(); i++) {
annotations.get(i).setIsEditable(!drawingMode);
}
}
}
FreehandDrawingAnnotationFragment.kt
View source code//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2026. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// BrushAnnotationFragment.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.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.SeekBar
import com.scichart.charting.modifiers.FreehandDrawingModifier
import com.scichart.charting.modifiers.OnAnnotationCreatedListener
import com.scichart.charting.modifiers.PinchZoomModifier
import com.scichart.charting.modifiers.ZoomExtentsModifier
import com.scichart.charting.modifiers.ZoomPanModifier
import com.scichart.charting.visuals.annotations.IAnnotation
import com.scichart.data.model.DoubleRange
import com.scichart.examples.data.MarketDataService
import com.scichart.examples.databinding.ExampleFreehandAnnotationFragmentBinding
import com.scichart.examples.fragments.base.ExampleBaseFragment
import com.scichart.examples.utils.scichartExtensions.*
import java.util.*
class FreehandDrawingAnnotationFragment : ExampleBaseFragment<ExampleFreehandAnnotationFragmentBinding>(), OnAnnotationCreatedListener {
private val freehandDrawingModifier = FreehandDrawingModifier()
private val zoomPanModifier = ZoomPanModifier()
private val pinchZoomModifier = PinchZoomModifier()
private val zoomExtentsModifier = ZoomExtentsModifier()
private var isDrawingMode = true
override fun inflateBinding(inflater: LayoutInflater): ExampleFreehandAnnotationFragmentBinding {
return ExampleFreehandAnnotationFragmentBinding.inflate(inflater)
}
override fun initExample(binding: ExampleFreehandAnnotationFragmentBinding) {
// Drawing modifier starts enabled
freehandDrawingModifier.setAnnotationCreationListener(this)
freehandDrawingModifier.isEnabled = true
binding.thicknessSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
freehandDrawingModifier.brushThickness = progress.toFloat()
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})
binding.colorWhite.setOnClickListener { selectColor(it, Color.WHITE) }
binding.colorYellow.setOnClickListener { selectColor(it, Color.YELLOW) }
binding.colorGreen.setOnClickListener { selectColor(it, Color.GREEN) }
binding.colorBlue.setOnClickListener { selectColor(it, Color.BLUE) }
// Set initial selection
selectColor(binding.colorWhite, Color.WHITE)
binding.drawingModeSwitch.setOnCheckedChangeListener { _, isChecked -> setDrawingMode(isChecked, binding) }
val surface = binding.surface
surface.suspendUpdates {
xAxes { categoryDateAxis { } }
yAxes { numericAxis { visibleRange = DoubleRange(30.0, 37.0) } }
renderableSeries {
fastCandlestickRenderableSeries {
ohlcDataSeries<Date, Double> {
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 {
modifier(freehandDrawingModifier)
modifier(zoomPanModifier.apply { isEnabled = !isDrawingMode })
modifier(pinchZoomModifier.apply { isEnabled = !isDrawingMode })
modifier(zoomExtentsModifier.apply { isEnabled = !isDrawingMode })
}
}
binding.deleteAnnotation.setOnClickListener {
val annotations = surface.annotations
for (i in annotations.indices.reversed()) {
val annotation = annotations[i]
if (annotation.isSelected) {
annotations.removeAt(i)
}
}
}
}
override fun onAnnotationCreated(newAnnotation: IAnnotation) {
newAnnotation.setIsEditable(!isDrawingMode)
}
private fun selectColor(view: View, color: Int) {
binding.colorWhite.background = getColorDrawable(Color.WHITE, view === binding.colorWhite)
binding.colorYellow.background = getColorDrawable(Color.YELLOW, view === binding.colorYellow)
binding.colorGreen.background = getColorDrawable(Color.GREEN, view === binding.colorGreen)
binding.colorBlue.background = getColorDrawable(Color.BLUE, view === binding.colorBlue)
freehandDrawingModifier.brushColor = color
}
private fun getColorDrawable(color: Int, isSelected: Boolean): Drawable {
return GradientDrawable().apply {
setColor(color)
shape = GradientDrawable.RECTANGLE
if (isSelected) {
setStroke(4, Color.MAGENTA)
}
}
}
/**
* Toggles between Drawing Mode and Selection Mode.
* Drawing Mode ON: drawing modifier is active, all existing annotations become non-editable/non-selectable.
* Drawing Mode OFF: drawing modifier is disabled, all existing annotations become editable/selectable.
*/
private fun setDrawingMode(drawingMode: Boolean, binding: ExampleFreehandAnnotationFragmentBinding) {
isDrawingMode = drawingMode
freehandDrawingModifier.setIsEnabled(drawingMode)
zoomPanModifier.isEnabled = !drawingMode
pinchZoomModifier.isEnabled = !drawingMode
zoomExtentsModifier.isEnabled = !drawingMode
val annotations = binding.surface.annotations
for (i in 0 until annotations.size) {
annotations[i].setIsEditable(!drawingMode)
}
}
}
Back to Android Chart Examples


