Pre loader

Android Chart Realtime Ghosted Traces

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

This example shows persistence of old traces giving a ‘ghosted’ effect.

Tip!

As new series are drawn older series are made increasingly transparent until they become invisible.

The full source code for the Android Chart Realtime Ghosted Traces 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: RealTimeGhostTracesFragment.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
//
// RealTimeGhostTracesFragment.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.view.LayoutInflater
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import com.scichart.charting.model.dataSeries.XyDataSeries
import com.scichart.charting.visuals.SciChartSurface
import com.scichart.charting.visuals.axes.AutoRange
import com.scichart.core.utility.NumberUtil
import com.scichart.data.model.DoubleRange
import com.scichart.drawing.utility.ColorUtil
import com.scichart.examples.R
import com.scichart.examples.data.DataManager
import com.scichart.examples.databinding.ExampleRealTimeGhostTracesFragmentBinding
import com.scichart.examples.fragments.base.ExampleBaseFragment
import com.scichart.examples.utils.scichartExtensions.*
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit

class RealTimeGhostTracesFragment : ExampleBaseFragment<ExampleRealTimeGhostTracesFragmentBinding>(), OnSeekBarChangeListener {
    private val scheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
    private var schedule: ScheduledFuture<*>? = null

    override fun showDefaultModifiersInToolbar(): Boolean = false

    override fun inflateBinding(inflater: LayoutInflater): ExampleRealTimeGhostTracesFragmentBinding {
        return ExampleRealTimeGhostTracesFragmentBinding.inflate(inflater)
    }

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

        binding.surface.run {
            xAxes { numericAxis  { autoRange = AutoRange.Always } }
            yAxes { numericAxis  {
                growBy = DoubleRange(0.1, 0.1)
                autoRange = AutoRange.Never
                visibleRange = DoubleRange(-2.0, 2.0)
            }}
            renderableSeries {
                val seriesColor = (0xff68bcae).toInt();
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(seriesColor) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.9f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.8f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.7f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.62f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.55f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.45f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.35f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.25f)) }
                fastLineRenderableSeries { strokeStyle = SolidPenStyle(ColorUtil.argb(seriesColor, 0.15f)) }
            }
        }

        binding.seekBar.run {
            setOnSeekBarChangeListener(this@RealTimeGhostTracesFragment)
            onProgressChanged(this, this.progress, false)
        }
    }

    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        if (progress > 0) {
            binding.speedValue.text = String.format("%d ms", progress)

            schedule?.cancel(true)
            schedule = scheduledExecutorService.scheduleWithFixedDelay(scheduledRunnable, 0, progress.toLong(), TimeUnit.MILLISECONDS)
        }
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) { }
    override fun onStopTrackingTouch(seekBar: SeekBar?) { }

    private val scheduledRunnable: Runnable = object : Runnable {
        private var lastAmplitude = 1.0
        private val phase = 0.0
        private val random = Random()
        override fun run() {
            val surface = binding.surface
            surface.suspendUpdates {
                val dataSeries = XyDataSeries<Double, Double>()
                val randomAmplitude = NumberUtil.constrain(lastAmplitude + (random.nextDouble() - 0.5), -2.0, 2.0)
                val noisySinewave = DataManager.getInstance().getNoisySinewave(randomAmplitude, phase, 1000, 0.25)
                lastAmplitude = randomAmplitude

                dataSeries.append(noisySinewave.xValues, noisySinewave.yValues)

                reassignRenderableSeries(surface, dataSeries)
            }
        }
    }

    private fun reassignRenderableSeries(surface: SciChartSurface, dataSeries: XyDataSeries<Double, Double>) {
        surface.suspendUpdates {
            val rs = surface.renderableSeries

            // shift old data series
            rs[9].dataSeries = rs[8].dataSeries
            rs[8].dataSeries = rs[7].dataSeries
            rs[7].dataSeries = rs[6].dataSeries
            rs[6].dataSeries = rs[5].dataSeries
            rs[5].dataSeries = rs[4].dataSeries
            rs[4].dataSeries = rs[3].dataSeries
            rs[3].dataSeries = rs[2].dataSeries
            rs[2].dataSeries = rs[1].dataSeries
            rs[1].dataSeries = rs[0].dataSeries

            // use new data series to draw first renderable series
            rs[0].dataSeries = dataSeries
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()

        schedule?.cancel(true)
    }
}
Java: RealTimeGhostTracesFragment.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
//
// RealTimeGhostTracesFragment.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.view.LayoutInflater;
import android.widget.SeekBar;

import androidx.annotation.NonNull;

import com.scichart.charting.model.RenderableSeriesCollection;
import com.scichart.charting.model.dataSeries.XyDataSeries;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.axes.AutoRange;
import com.scichart.charting.visuals.axes.NumericAxis;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.core.utility.NumberUtil;
import com.scichart.data.model.DoubleRange;
import com.scichart.drawing.utility.ColorUtil;
import com.scichart.examples.R;
import com.scichart.examples.data.DataManager;
import com.scichart.examples.data.DoubleSeries;
import com.scichart.examples.databinding.ExampleRealTimeGhostTracesFragmentBinding;
import com.scichart.examples.fragments.base.ExampleBaseFragment;

import java.util.Collections;
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 RealTimeGhostTracesFragment extends ExampleBaseFragment<ExampleRealTimeGhostTracesFragmentBinding> implements SeekBar.OnSeekBarChangeListener {
    private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    private ScheduledFuture<?> schedule;

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

    @NonNull
    @Override
    protected ExampleRealTimeGhostTracesFragmentBinding inflateBinding(@NonNull LayoutInflater inflater) {
        return ExampleRealTimeGhostTracesFragmentBinding.inflate(inflater);
    }

    @Override
    protected void initExample(ExampleRealTimeGhostTracesFragmentBinding binding) {
        binding.surface.setTheme(R.style.SciChart_NavyBlue);

        final NumericAxis xAxis = sciChartBuilder.newNumericAxis().withAutoRangeMode(AutoRange.Always) .build();
        final NumericAxis yAxis = sciChartBuilder.newNumericAxis()
                .withGrowBy(new DoubleRange(0.1d, 0.1d))
                .withAutoRangeMode(AutoRange.Never)
                .withVisibleRange(new DoubleRange(-2d, 2d))
                .build();

        final SciChartSurface surface = binding.surface;
        Collections.addAll(surface.getXAxes(), xAxis);
        Collections.addAll(surface.getYAxes(), yAxis);

        final int seriesColor = 0xFF68bcae;
        Collections.addAll(surface.getRenderableSeries(),
                sciChartBuilder.newLineSeries().withStrokeStyle(seriesColor).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.9f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.8f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.7f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.62f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.55f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.45f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.35f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.25f)).build(),
                sciChartBuilder.newLineSeries().withStrokeStyle(ColorUtil.argb(seriesColor, 0.15f)).build()
        );

        final SeekBar seekBar = binding.seekBar;
        seekBar.setOnSeekBarChangeListener(this);
        onProgressChanged(seekBar, seekBar.getProgress(), false);
    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (progress > 0) {
            binding.speedValue.setText(String.format("%d ms", progress));

            if (schedule != null) {
                schedule.cancel(true);
            }

            schedule = scheduledExecutorService.scheduleWithFixedDelay(scheduledRunnable, 0, progress, TimeUnit.MILLISECONDS);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }

    private final Runnable scheduledRunnable = new Runnable() {
        private double lastAmplitude = 1.0;
        private double phase = 0;
        private final Random random = new Random();

        @Override
        public void run() {
            final SciChartSurface surface = binding.surface;
            UpdateSuspender.using(surface, () -> {
                final XyDataSeries<Double, Double> dataSeries = new XyDataSeries<>(Double.class, Double.class);

                final double randomAmplitude = NumberUtil.constrain(lastAmplitude + (random.nextDouble() - 0.5), -2d, 2);
                final DoubleSeries noisySinewave = DataManager.getInstance().getNoisySinewave(randomAmplitude, phase, 1000, 0.25);
                lastAmplitude = randomAmplitude;

                dataSeries.append(noisySinewave.xValues, noisySinewave.yValues);

                reassignRenderableSeries(surface, dataSeries);
            });
        }
    };

    private void reassignRenderableSeries(final SciChartSurface surface, final XyDataSeries<Double, Double> dataSeries) {
        UpdateSuspender.using(surface, () -> {
            final RenderableSeriesCollection rs = surface.getRenderableSeries();

            // shift old data series
            rs.get(9).setDataSeries(rs.get(8).getDataSeries());
            rs.get(8).setDataSeries(rs.get(7).getDataSeries());
            rs.get(7).setDataSeries(rs.get(6).getDataSeries());
            rs.get(6).setDataSeries(rs.get(5).getDataSeries());
            rs.get(5).setDataSeries(rs.get(4).getDataSeries());
            rs.get(4).setDataSeries(rs.get(3).getDataSeries());
            rs.get(3).setDataSeries(rs.get(2).getDataSeries());
            rs.get(2).setDataSeries(rs.get(1).getDataSeries());
            rs.get(1).setDataSeries(rs.get(0).getDataSeries());

            // use new data series to draw first renderable series
            rs.get(0).setDataSeries(dataSeries);
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (schedule != null) {
            schedule.cancel(true);
        }
    }
}
Back to Android Chart Examples