Pre loader

Android Chart Sync Multiple Charts

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

Showcases how to synchronize axis ranges across multiple charts by sharing IRange instances. Two SciChartSurfaces are created and arranged vertically. This example shows how to synchronize Zoom, Pan, Axis Scale operations.

Tip!

The full source code for the Android Chart Sync Multiple 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?

  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: SyncMultipleChartsFragment.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
//
// SyncMultipleChartsFragment.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.multiChart.kt

import android.view.LayoutInflater
import android.view.animation.DecelerateInterpolator
import com.scichart.charting.visuals.SciChartSurface
import com.scichart.data.model.DoubleRange
import com.scichart.examples.R
import com.scichart.examples.databinding.ExampleSyncMultipleChartsFragmentBinding
import com.scichart.examples.fragments.base.ExampleBaseFragment
import com.scichart.examples.utils.Constant
import com.scichart.examples.utils.interpolator.DefaultInterpolator
import com.scichart.examples.utils.scichartExtensions.*
import kotlin.math.sin

class SyncMultipleChartsFragment: ExampleBaseFragment<ExampleSyncMultipleChartsFragmentBinding>() {
    private val sharedXRange = DoubleRange()
    private val sharedYRange = DoubleRange()

    override fun showDefaultModifiersInToolbar(): Boolean = false

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

    override fun initExample(binding: ExampleSyncMultipleChartsFragmentBinding) {
        initChart(binding.chart0)
        initChart(binding.chart1)
    }

    private fun initChart(surface: SciChartSurface) {
        surface.theme = R.style.SciChart_NavyBlue

        surface.suspendUpdates {
            xAxes { numericAxis { growBy = DoubleRange(0.1, 0.1); visibleRange = sharedXRange} }
            yAxes { numericAxis { growBy = DoubleRange(0.1, 0.1); visibleRange = sharedYRange} }
            renderableSeries {
                fastLineRenderableSeries {
                    xyDataSeries<Double, Double> {
                        for (i in 1 until POINTS_COUNT) {
                            append(i.toDouble(), POINTS_COUNT * sin(i * Math.PI * 0.1) / i)
                        }
                    }
                    strokeStyle = SolidPenStyle(0xFF47bde6)

                    sweepAnimation {
                        duration = Constant.ANIMATION_DURATION
                        startDelay = Constant.ANIMATION_START_DELAY
                        interpolator = DefaultInterpolator.getInterpolator()
                    }
                }
            }

            chartModifiers {
                modifierGroup(context) {
                    zoomExtentsModifier()
                    pinchZoomModifier()
                    rolloverModifier { receiveHandledEvents = true }
                    xAxisDragModifier { receiveHandledEvents = true }
                    yAxisDragModifier { receiveHandledEvents = true }
                }.run {
                    motionEventGroup = "ModifiersSharedEventsGroup"
                    receiveHandledEvents = true
                }
            }
        }

        surface.zoomExtents()
    }

    companion object {
        private const val POINTS_COUNT = 500
    }
}
Java: SyncMultipleChartsFragment.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
//
// SyncMultipleChartsFragment.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.multiChart;

import android.view.LayoutInflater;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.NonNull;

import com.scichart.charting.model.dataSeries.IXyDataSeries;
import com.scichart.charting.model.dataSeries.XyDataSeries;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.axes.IAxis;
import com.scichart.charting.visuals.renderableSeries.FastLineRenderableSeries;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.data.model.DoubleRange;
import com.scichart.data.model.IRange;
import com.scichart.examples.R;
import com.scichart.examples.databinding.ExampleSyncMultipleChartsFragmentBinding;
import com.scichart.examples.fragments.base.ExampleBaseFragment;
import com.scichart.examples.utils.Constant;
import com.scichart.examples.utils.interpolator.DefaultInterpolator;

import java.util.Collections;

public class SyncMultipleChartsFragment extends ExampleBaseFragment<ExampleSyncMultipleChartsFragmentBinding> {
    private final static int POINTS_COUNT = 500;

    private final IRange<?> sharedXRange = new DoubleRange();
    private final IRange<?> sharedYRange = new DoubleRange();

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

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

    @Override
    protected void initExample(ExampleSyncMultipleChartsFragmentBinding binding) {
        initChart(binding.chart0);
        initChart(binding.chart1);
    }

    private void initChart(final SciChartSurface surface) {
        surface.setTheme(R.style.SciChart_NavyBlue);

        final IAxis xAxis = sciChartBuilder.newNumericAxis().withGrowBy(0.1d, 0.1d).withVisibleRange(sharedXRange).build();
        final IAxis yAxis = sciChartBuilder.newNumericAxis().withGrowBy(0.1d, 0.1d).withVisibleRange(sharedYRange).build();

        IXyDataSeries<Double, Double> dataSeries = new XyDataSeries<>(Double.class, Double.class);
        for (int i = 1; i < POINTS_COUNT; i++) {
            dataSeries.append((double) i, POINTS_COUNT * Math.sin(i * Math.PI * 0.1) / i);
        }

        final FastLineRenderableSeries line = sciChartBuilder.newLineSeries().withDataSeries(dataSeries).withStrokeStyle(0xFF47bde6, 1f, true).build();

        UpdateSuspender.using(surface, () -> {
            Collections.addAll(surface.getXAxes(), xAxis);
            Collections.addAll(surface.getYAxes(), yAxis);
            Collections.addAll(surface.getRenderableSeries(), line);
            Collections.addAll(surface.getChartModifiers(), sciChartBuilder.newModifierGroup()
                    .withMotionEventsGroup("ModifiersSharedEventsGroup").withReceiveHandledEvents(true)
                    .withZoomExtentsModifier().build()
                    .withPinchZoomModifier().build()
                    .withRolloverModifier().withReceiveHandledEvents(true).build()
                    .withXAxisDragModifier().withReceiveHandledEvents(true).build()
                    .withYAxisDragModifier().withReceiveHandledEvents(true).build()
                    .build());

            surface.zoomExtents();

            sciChartBuilder.newAnimator(line).withSweepTransformation().withInterpolator(DefaultInterpolator.getInterpolator()).withDuration(Constant.ANIMATION_DURATION).withStartDelay(Constant.ANIMATION_START_DELAY).start();
        });
    }
}
Back to Android Chart Examples