SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
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 the difference between FIFO DataSeries and standard DataSeries. A FIFO series discards old data-points once a predefined number of points is met. This type of data-series is extremely efficient for scrolling charts, where you do not care about discarded (off-screen) data.
FIFO Series can be used in more ways than just scrolling a chart. For instance, reload the entire FIFO buffer each frame to get a Spectrum Analyzer style chart. Or, use FIFO series and call setVisibleRange() on X-Axis to achieve an ECG style chart.
Tip!
To switch to FIFO series just need to set FifoCapacity for DataSeries
The full source code for the Android Realtime Scrolling Charts example is included below (Scroll down!).
Did you know you can also view the source code from one of the following sources as well?
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// FifoChartsFragment.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.createRealtimeCharts.kt
import android.os.Bundle
import com.scichart.charting.visuals.SciChartSurface
import com.scichart.charting.visuals.axes.AutoRange
import com.scichart.data.model.DoubleRange
import com.scichart.data.model.ISciList
import com.scichart.examples.R
import com.scichart.examples.fragments.base.ExampleSingleChartBaseFragment
import com.scichart.examples.utils.scichartExtensions.*
import com.scichart.examples.utils.widgetgeneration.ImageViewWidget
import com.scichart.examples.utils.widgetgeneration.Widget
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import kotlin.math.cos
import kotlin.math.sin
class FifoChartsFragment : ExampleSingleChartBaseFragment() {
private val random = Random()
private val ds1 = XyDataSeries<Double, Double>().apply { fifoCapacity = FIFO_CAPACITY }
private val ds2 = XyDataSeries<Double, Double>().apply { fifoCapacity = FIFO_CAPACITY }
private val ds3 = XyDataSeries<Double, Double>().apply { fifoCapacity = FIFO_CAPACITY }
private val xVisibleRange = DoubleRange(GROW_BY, VISIBLE_RANGE_MAX + GROW_BY)
private val scheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
private lateinit var schedule: ScheduledFuture<*>
@Volatile
private var isRunning = true
override fun getToolbarItems(): List<Widget> = ArrayList<Widget>().apply {
add(ImageViewWidget.Builder().setId(R.drawable.example_toolbar_play).setListener { isRunning = true }.build())
add(ImageViewWidget.Builder().setId(R.drawable.example_toolbar_pause).setListener { isRunning = false }.build())
add(ImageViewWidget.Builder().setId(R.drawable.example_toolbar_stop).setListener {
isRunning = false
resetChart()
}.build())
}
override fun initExample(surface: SciChartSurface) {
surface.suspendUpdates {
xAxes { numericAxis {
visibleRange = xVisibleRange
autoRange = AutoRange.Never }
}
yAxes { numericAxis {
growBy = DoubleRange(0.1, 0.1)
autoRange = AutoRange.Always }
}
renderableSeries {
fastLineRenderableSeries { dataSeries = ds1; strokeStyle = SolidPenStyle(0xFF4083B7, 2f) }
fastLineRenderableSeries { dataSeries = ds2; strokeStyle = SolidPenStyle(0xFFFFA500, 2f) }
fastLineRenderableSeries { dataSeries = ds3; strokeStyle = SolidPenStyle(0xFFE13219, 2f) }
}
}
schedule = scheduledExecutorService.scheduleWithFixedDelay(insertRunnable, 0, TIME_INTERVAL, TimeUnit.MILLISECONDS)
}
var t = 0.0
private val insertRunnable = Runnable {
if (!isRunning) return@Runnable
binding.surface.suspendUpdates {
val y1 = 3.0 * sin(2 * Math.PI * 1.4 * t) + random.nextDouble() * 0.5
val y2 = 2.0 * cos(2 * Math.PI * 0.8 * t) + random.nextDouble() * 0.5
val y3 = sin(2 * Math.PI * 2.2 * t) + random.nextDouble() * 0.5
ds1.append(t, y1)
ds2.append(t, y2)
ds3.append(t, y3)
t += ONE_OVER_TIME_INTERVAL
if (t > VISIBLE_RANGE_MAX) {
xVisibleRange.setMinMax(xVisibleRange.min + ONE_OVER_TIME_INTERVAL, xVisibleRange.max + ONE_OVER_TIME_INTERVAL)
}
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
isRunning = false
outState.run {
putDouble("time", t)
putParcelable("xValues1", ds1.xValues)
putParcelable("yValues1", ds1.yValues)
putParcelable("xValues2", ds2.xValues)
putParcelable("yValues2", ds2.yValues)
putParcelable("xValues3", ds3.xValues)
putParcelable("yValues3", ds3.yValues)
}
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
savedInstanceState?.run {
t = getDouble("time")
val xValues1: ISciList<Double> = getParcelable("xValues1")!!
val yValues1: ISciList<Double> = getParcelable("yValues1")!!
val xValues2: ISciList<Double> = getParcelable("xValues2")!!
val yValues2: ISciList<Double> = getParcelable("yValues2")!!
val xValues3: ISciList<Double> = getParcelable("xValues3")!!
val yValues3: ISciList<Double> = getParcelable("yValues3")!!
ds1.append(xValues1, yValues1)
ds2.append(xValues2, yValues2)
ds3.append(xValues3, yValues3)
}
}
override fun onDestroyView() {
super.onDestroyView()
schedule.cancel(true)
}
private fun resetChart() {
binding.surface.suspendUpdates {
ds1.clear()
ds2.clear()
ds3.clear()
}
}
companion object {
private const val FIFO_CAPACITY = 50
private const val TIME_INTERVAL: Long = 30
private const val ONE_OVER_TIME_INTERVAL = 1.0 / TIME_INTERVAL
private const val VISIBLE_RANGE_MAX = FIFO_CAPACITY * ONE_OVER_TIME_INTERVAL
private const val GROW_BY = VISIBLE_RANGE_MAX * 0.1
}
}
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// FifoChartsFragment.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.createRealtimeCharts;
import android.os.Bundle;
import androidx.annotation.NonNull;
import com.scichart.charting.model.dataSeries.IXyDataSeries;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.axes.AutoRange;
import com.scichart.charting.visuals.axes.NumericAxis;
import com.scichart.charting.visuals.renderableSeries.IRenderableSeries;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.data.model.DoubleRange;
import com.scichart.data.model.ISciList;
import com.scichart.examples.R;
import com.scichart.examples.fragments.base.ExampleSingleChartBaseFragment;
import com.scichart.examples.utils.widgetgeneration.ImageViewWidget;
import com.scichart.examples.utils.widgetgeneration.Widget;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class FifoChartsFragment extends ExampleSingleChartBaseFragment {
private final static int FIFO_CAPACITY = 50;
private final static long TIME_INTERVAL = 30;
private final static double ONE_OVER_TIME_INTERVAL = 1.0 / TIME_INTERVAL;
private final static double VISIBLE_RANGE_MAX = FIFO_CAPACITY * ONE_OVER_TIME_INTERVAL;
private final static double GROW_BY = VISIBLE_RANGE_MAX * 0.1;
private final Random random = new Random();
private final IXyDataSeries<Double, Double> ds1 = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withFifoCapacity(FIFO_CAPACITY).build();
private final IXyDataSeries<Double, Double> ds2 = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withFifoCapacity(FIFO_CAPACITY).build();
private final IXyDataSeries<Double, Double> ds3 = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withFifoCapacity(FIFO_CAPACITY).build();
private final DoubleRange xVisibleRange = new DoubleRange(-GROW_BY, VISIBLE_RANGE_MAX + GROW_BY);
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> schedule;
private volatile boolean isRunning = true;
@NonNull
@Override
public List<Widget> getToolbarItems() {
return new ArrayList<Widget>() {
{
add(new ImageViewWidget.Builder().setId(R.drawable.example_toolbar_play).setListener(v -> isRunning = true).build());
add(new ImageViewWidget.Builder().setId(R.drawable.example_toolbar_pause).setListener(v -> isRunning = false).build());
add(new ImageViewWidget.Builder().setId(R.drawable.example_toolbar_stop).setListener(v -> {
isRunning = false;
resetChart();
}).build());
}
};
}
@Override
protected void initExample(@NonNull SciChartSurface surface) {
final NumericAxis xAxis = sciChartBuilder.newNumericAxis().withVisibleRange(xVisibleRange).withAutoRangeMode(AutoRange.Never).build();
final NumericAxis yAxis = sciChartBuilder.newNumericAxis().withGrowBy(0.1d, 0.1d).withAutoRangeMode(AutoRange.Always).build();
final IRenderableSeries rs1 = sciChartBuilder.newLineSeries().withDataSeries(ds1).withStrokeStyle(0xFF4083B7, 2f, true).build();
final IRenderableSeries rs2 = sciChartBuilder.newLineSeries().withDataSeries(ds2).withStrokeStyle(0xFFFFA500, 2f, true).build();
final IRenderableSeries rs3 = sciChartBuilder.newLineSeries().withDataSeries(ds3).withStrokeStyle(0xFFE13219, 2f, true).build();
UpdateSuspender.using(surface, () -> {
Collections.addAll(surface.getXAxes(), xAxis);
Collections.addAll(surface.getYAxes(), yAxis);
Collections.addAll(surface.getRenderableSeries(), rs1, rs2, rs3);
});
schedule = scheduledExecutorService.scheduleWithFixedDelay(() -> {
if (!isRunning) {
return;
}
UpdateSuspender.using(surface, insertRunnable);
}, 0, TIME_INTERVAL, TimeUnit.MILLISECONDS);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
isRunning = false;
outState.putDouble("time", t);
outState.putParcelable("xValues1", ds1.getXValues());
outState.putParcelable("yValues1", ds1.getYValues());
outState.putParcelable("xValues2", ds2.getXValues());
outState.putParcelable("yValues2", ds2.getYValues());
outState.putParcelable("xValues3", ds3.getXValues());
outState.putParcelable("yValues3", ds3.getYValues());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
t = savedInstanceState.getDouble("time");
final ISciList<Double> xValues1 = savedInstanceState.getParcelable("xValues1");
final ISciList<Double> yValues1 = savedInstanceState.getParcelable("yValues1");
final ISciList<Double> xValues2 = savedInstanceState.getParcelable("xValues2");
final ISciList<Double> yValues2 = savedInstanceState.getParcelable("yValues2");
final ISciList<Double> xValues3 = savedInstanceState.getParcelable("xValues3");
final ISciList<Double> yValues3 = savedInstanceState.getParcelable("yValues3");
ds1.append(xValues1, yValues1);
ds2.append(xValues2, yValues2);
ds3.append(xValues3, yValues3);
}
}
double t = 0;
private final Runnable insertRunnable = () -> {
double y1 = 3.0 * Math.sin(((2 * Math.PI) * 1.4) * t) + random.nextDouble() * 0.5;
double y2 = 2.0 * Math.cos(((2 * Math.PI) * 0.8) * t) + random.nextDouble() * 0.5;
double y3 = Math.sin(((2 * Math.PI) * 2.2) * t) + random.nextDouble() * 0.5;
ds1.append(t, y1);
ds2.append(t, y2);
ds3.append(t, y3);
t += ONE_OVER_TIME_INTERVAL;
if (t > VISIBLE_RANGE_MAX) {
xVisibleRange.setMinMax(xVisibleRange.getMin() + ONE_OVER_TIME_INTERVAL, xVisibleRange.getMax() + ONE_OVER_TIME_INTERVAL);
}
};
@Override
public void onDestroyView() {
super.onDestroyView();
if (schedule != null) {
schedule.cancel(true);
}
}
private void resetChart() {
UpdateSuspender.using(binding.surface, () -> {
ds1.clear();
ds2.clear();
ds3.clear();
});
}
}