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 how to use FastUniformHeatmapRenderableSeries and create a real-time Heatmap in SciChart for Android.
Android Heatmap Charts can be used to visualize a variety of scientific and statistical data. Display a real-time spectrogram (Fourier Transform plus Time) of audio data. To overlay areas of heat on a background image such as a map or image, or to quickly bring to life 2-dimensional data and show the relative intensities via the heat color.
The FastUniformHeatmapRenderableSeries requires an UniformHeatmapDataSeries, which contains 1D array with heatmap values and ColorMap instance which allows to map values to colors.
In this example you also can tap and drag on the chart to see the animated-zoom performance while the heatmap is updating.
Tip!
You can enable drawing of heatmap value inside each cell by calling setDrawTextInCell(True)
The full source code for the Android Heatmap 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?
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// HeatmapChartFragment.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.basicChartTypes.kt
import android.view.LayoutInflater
import com.scichart.charting.visuals.renderableSeries.ColorMap
import com.scichart.core.model.DoubleValues
import com.scichart.core.model.IValues
import com.scichart.drawing.utility.ColorUtil.*
import com.scichart.examples.databinding.ExampleHeatmapChartFragmentBinding
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
import kotlin.math.max
import kotlin.math.sin
import kotlin.math.sqrt
class HeatmapChartFragment : ExampleBaseFragment<ExampleHeatmapChartFragmentBinding>() {
private val scheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
private lateinit var schedule: ScheduledFuture<*>
private val dataSeries = UniformHeatmapDataSeries<Int, Int, Double>(WIDTH, HEIGHT)
private var timerIndex = 0
private val valuesList: MutableList<IValues<Double>> = ArrayList(SERIES_PER_PERIOD)
override fun inflateBinding(inflater: LayoutInflater): ExampleHeatmapChartFragmentBinding {
return ExampleHeatmapChartFragmentBinding.inflate(inflater)
}
override fun initExample(binding: ExampleHeatmapChartFragmentBinding) {
for (i in 0 until SERIES_PER_PERIOD) {
valuesList.add(createValues(i))
}
binding.surface.suspendUpdates {
xAxes { numericAxis() }
yAxes { numericAxis() }
renderableSeries {
fastUniformHeatmapRenderableSeries {
minimum = 0.0
maximum = 200.0
colorMap = ColorMap(
intArrayOf(DarkBlue, CornflowerBlue, DarkGreen, Chartreuse, Yellow, Red),
floatArrayOf(0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f)
)
dataSeries = this@HeatmapChartFragment.dataSeries
binding.heatmapColourMap.run {
minimum = this@fastUniformHeatmapRenderableSeries.minimum
maximum = this@fastUniformHeatmapRenderableSeries.maximum
colorMap = this@fastUniformHeatmapRenderableSeries.colorMap
}
}
}
chartModifiers {
defaultModifiers()
cursorModifier {
showTooltip = true
receiveHandledEvents = true
}
}
}
schedule = scheduledExecutorService.scheduleWithFixedDelay({
binding.surface.suspendUpdates {
val values = valuesList[timerIndex % SERIES_PER_PERIOD]
dataSeries.updateZValues(values)
timerIndex++
}
}, 0, TIME_INTERVAL, TimeUnit.MILLISECONDS)
}
private fun createValues(index: Int): IValues<Double> {
val values = DoubleValues(WIDTH * HEIGHT)
val random = Random()
val angle = Math.PI * 2 * index / SERIES_PER_PERIOD
val cx = 150.0; val cy = 100.0
for (y in 0 until HEIGHT) {
for (x in 0 until WIDTH) {
val v = (1 + sin(y * 0.04 + angle)) * 50 + (1 + sin(x * 0.1 + angle)) * 50 * (1 + sin(angle * 2))
val r = sqrt((y - cx) * (y - cx) + (x - cy) * (x - cy))
val exp = max(0.0, 1 - r * 0.008)
values.add(v * exp + random.nextDouble() * 50)
}
}
return values
}
override fun onDestroyView() {
super.onDestroyView()
schedule?.cancel(true)
}
companion object {
private const val WIDTH = 200
private const val HEIGHT = 300
private const val SERIES_PER_PERIOD = 30
private const val TIME_INTERVAL: Long = 40
}
}
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// HeatmapChartFragment.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.basicChartTypes;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import com.scichart.charting.model.dataSeries.UniformHeatmapDataSeries;
import com.scichart.charting.modifiers.ModifierGroup;
import com.scichart.charting.visuals.SciChartHeatmapColourMap;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.axes.NumericAxis;
import com.scichart.charting.visuals.renderableSeries.ColorMap;
import com.scichart.charting.visuals.renderableSeries.FastUniformHeatmapRenderableSeries;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.core.model.DoubleValues;
import com.scichart.core.model.IValues;
import com.scichart.examples.databinding.ExampleHeatmapChartFragmentBinding;
import com.scichart.examples.fragments.base.ExampleBaseFragment;
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;
import static com.scichart.drawing.utility.ColorUtil.*;
public class HeatmapChartFragment extends ExampleBaseFragment<ExampleHeatmapChartFragmentBinding> {
private static final int WIDTH = 200, HEIGHT = 300;
private static final int SERIES_PER_PERIOD = 30;
private static final long TIME_INTERVAL = 40;
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> schedule;
private final UniformHeatmapDataSeries<Integer, Integer, Double> dataSeries = new UniformHeatmapDataSeries<>(Integer.class, Integer.class, Double.class, WIDTH, HEIGHT);
private int timerIndex = 0;
private final List<IValues<Double>> valuesList = new ArrayList<>(SERIES_PER_PERIOD);
@NonNull
@Override
protected ExampleHeatmapChartFragmentBinding inflateBinding(@NonNull LayoutInflater inflater) {
return ExampleHeatmapChartFragmentBinding.inflate(inflater);
}
@Override
protected void initExample(@NonNull ExampleHeatmapChartFragmentBinding binding) {
final NumericAxis xAxis = sciChartBuilder.newNumericAxis().build();
final NumericAxis yAxis = sciChartBuilder.newNumericAxis().build();
for (int i = 0; i < SERIES_PER_PERIOD; i++) {
valuesList.add(createValues(i));
}
final FastUniformHeatmapRenderableSeries heatmapRenderableSeries = sciChartBuilder.newUniformHeatmap()
.withColorMap(new ColorMap(
new int[]{DarkBlue, CornflowerBlue, DarkGreen, Chartreuse, Yellow, Red},
new float[]{0f, 0.2f, 0.4f, 0.6f, 0.8f, 1})
)
.withMinimum(0)
.withMaximum(200)
.withDataSeries(dataSeries)
.build();
final SciChartHeatmapColourMap colourMap = binding.heatmapColourMap;
colourMap.setMinimum(heatmapRenderableSeries.getMinimum());
colourMap.setMaximum(heatmapRenderableSeries.getMaximum());
colourMap.setColorMap(heatmapRenderableSeries.getColorMap());
final SciChartSurface surface = binding.surface;
Collections.addAll(surface.getXAxes(), xAxis);
Collections.addAll(surface.getYAxes(), yAxis);
Collections.addAll(surface.getRenderableSeries(), heatmapRenderableSeries);
ModifierGroup modifiers = sciChartBuilder.newModifierGroupWithDefaultModifiers()
.withCursorModifier().withShowTooltip(true).withReceiveHandledEvents(true).build()
.build();
Collections.addAll(surface.getChartModifiers(), modifiers);
schedule = scheduledExecutorService.scheduleWithFixedDelay(() -> UpdateSuspender.using(surface, () -> {
final IValues<Double> values = valuesList.get(timerIndex % SERIES_PER_PERIOD);
dataSeries.updateZValues(values);
timerIndex++;
}), 0, TIME_INTERVAL, TimeUnit.MILLISECONDS);
}
private static IValues<Double> createValues(int index) {
final DoubleValues values = new DoubleValues(WIDTH * HEIGHT);
final Random random = new Random();
final double angle = Math.PI * 2 * index / SERIES_PER_PERIOD;
final double cx = 150, cy = 100;
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
final double v = (1 + Math.sin(y * 0.04 + angle)) * 50 + (1 + Math.sin(x * 0.1 + angle)) * 50 * (1 + Math.sin(angle * 2));
final double r = Math.sqrt((y - cx) * (y - cx) + (x - cy) * (x - cy));
final double exp = Math.max(0, 1 - r * 0.008);
values.add(v * exp + random.nextDouble() * 50);
}
}
return values;
}
@Override
public void onDestroyView() {
super.onDestroyView();
schedule.cancel(true);
}
}