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 the PaletteProvider API to change Heatmap colors in real-time. The SeekBar is used to provide a threshold value to the IUniformHeatmapPaletteProvider instance, which updates the Heatmap accordingly.
Tips!
Using this API you can color individual data points of the following RenderableSeries:
– FastLineRenderableSeries
– FastMountainRenderableSeries
– FastBandRenderableSeries
– FastBubbleRenderableSeries
– FastCandlestickRenderableSeries
– FastOhlcRenderableSeries
– FastImpulseRenderableSeries
– FastColumnRenderableSeries
– XyScatterRenderableSeries
– FastErrorBarsRenderableSeries and FastFixedErrorBarsRenderableSeries
– FastUniformHeatmapRenderableSeries
The full source code for the Android Chart Heatmap Custom Palette 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
//
// HeatmapPaletteProviderFragment.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.stylingAndTheming.kt
import android.graphics.Color
import android.view.LayoutInflater
import android.widget.SeekBar
import com.scichart.charting.visuals.axes.AxisAlignment.Bottom
import com.scichart.charting.visuals.axes.AxisAlignment.Right
import com.scichart.charting.visuals.renderableSeries.FastUniformHeatmapRenderableSeries
import com.scichart.charting.visuals.renderableSeries.data.UniformHeatmapRenderPassData
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IUniformHeatmapPaletteProvider
import com.scichart.charting.visuals.renderableSeries.paletteProviders.PaletteProviderBase
import com.scichart.core.model.DoubleValues
import com.scichart.core.model.IValues
import com.scichart.examples.databinding.ExampleHeatmapPaletteFragmentBinding
import com.scichart.examples.fragments.base.ExampleBaseFragment
import com.scichart.examples.utils.scichartExtensions.*
import java.util.*
import kotlin.math.sin
import kotlin.math.sqrt
class HeatmapPaletteProviderFragment : ExampleBaseFragment<ExampleHeatmapPaletteFragmentBinding>(), SeekBar.OnSeekBarChangeListener {
private val heatmapPaletteProvider = CustomUniformHeatMapProvider()
override fun inflateBinding(inflater: LayoutInflater): ExampleHeatmapPaletteFragmentBinding {
return ExampleHeatmapPaletteFragmentBinding.inflate(inflater)
}
override fun initExample(binding: ExampleHeatmapPaletteFragmentBinding) {
binding.seekBar.run {
setOnSeekBarChangeListener(this@HeatmapPaletteProviderFragment)
heatmapPaletteProvider.setThresholdValue(progress.toDouble())
}
binding.surface.suspendUpdates {
xAxes { numericAxis { axisAlignment = Bottom } }
yAxes { numericAxis { axisAlignment = Right } }
renderableSeries {
fastUniformHeatmapRenderableSeries {
uniformHeatmapDataSeries<Int, Int, Double>(WIDTH, HEIGHT) {
updateZValues(createValues())
}
minimum = 0.0
maximum = 200.0
this.paletteProvider = heatmapPaletteProvider
}
}
chartModifiers { defaultModifiers() }
}
}
private fun createValues(): IValues<Double> {
val values = DoubleValues(WIDTH * HEIGHT)
val random = Random()
val angle = Math.PI * 2
val cx = 150.0
val cy = 100.0
for (x in 0 until WIDTH) {
for (y in 0 until HEIGHT) {
val v = (1 + sin(x * 0.04 + angle)) * 50 + (1 + sin(y * 0.1 + angle)) * 50 * (1 + sin(angle * 2))
val r = sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy))
val exp = 0.0.coerceAtLeast(1 - r * 0.008)
values.add(v * exp + random.nextDouble() * 50)
}
}
return values
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
heatmapPaletteProvider.setThresholdValue(progress.toDouble())
binding.thresholdValue.text = String.format("%d", progress)
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
private class CustomUniformHeatMapProvider : PaletteProviderBase<FastUniformHeatmapRenderableSeries>(FastUniformHeatmapRenderableSeries::class.java), IUniformHeatmapPaletteProvider {
private var thresholdValue = 0.0
fun setThresholdValue(thresholdValue: Double) {
this.thresholdValue = thresholdValue
renderableSeries?.invalidateElement()
}
override fun shouldSetColors(): Boolean = false
override fun update() {
val renderableSeries = renderableSeries
val currentRenderPassData = renderableSeries!!.currentRenderPassData as UniformHeatmapRenderPassData
val zValues = currentRenderPassData.zValues
val zColors = currentRenderPassData.zColors
val size = zValues.size()
zColors.setSize(size)
// working with array is much faster than calling set() many times
val zValuesArray = zValues.itemsArray
val zColorsArray = zColors.itemsArray
for (zIndex in 0 until size) {
val value = zValuesArray[zIndex]
zColorsArray[zIndex] = if (value < thresholdValue) Color.BLACK else Color.WHITE
}
}
}
companion object {
private const val WIDTH = 300
private const val HEIGHT = 200
}
}
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// HeatmapPaletteProviderFragment.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.stylingAndTheming;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.widget.SeekBar;
import androidx.annotation.NonNull;
import com.scichart.charting.model.dataSeries.UniformHeatmapDataSeries;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.axes.AxisAlignment;
import com.scichart.charting.visuals.axes.NumericAxis;
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IUniformHeatmapPaletteProvider;
import com.scichart.charting.visuals.renderableSeries.paletteProviders.PaletteProviderBase;
import com.scichart.charting.visuals.renderableSeries.FastUniformHeatmapRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.data.UniformHeatmapRenderPassData;
import com.scichart.core.model.DoubleValues;
import com.scichart.core.model.IValues;
import com.scichart.core.model.IntegerValues;
import com.scichart.examples.databinding.ExampleHeatmapPaletteFragmentBinding;
import com.scichart.examples.fragments.base.ExampleBaseFragment;
import java.util.Collections;
import java.util.Random;
public class HeatmapPaletteProviderFragment extends ExampleBaseFragment<ExampleHeatmapPaletteFragmentBinding> implements SeekBar.OnSeekBarChangeListener {
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
private final CustomUniformHeatMapProvider paletteProvider = new CustomUniformHeatMapProvider();
@NonNull
@Override
protected ExampleHeatmapPaletteFragmentBinding inflateBinding(@NonNull LayoutInflater inflater) {
return ExampleHeatmapPaletteFragmentBinding.inflate(inflater);
}
@Override
protected void initExample(ExampleHeatmapPaletteFragmentBinding binding) {
binding.seekBar.setOnSeekBarChangeListener(this);
final NumericAxis xAxis = sciChartBuilder.newNumericAxis().withAxisAlignment(AxisAlignment.Bottom).build();
final NumericAxis yAxis = sciChartBuilder.newNumericAxis().withAxisAlignment(AxisAlignment.Right).build();
final UniformHeatmapDataSeries<Integer, Integer, Double> dataSeries = new UniformHeatmapDataSeries<>(Integer.class, Integer.class, Double.class, WIDTH, HEIGHT);
paletteProvider.setThresholdValue(binding.seekBar.getProgress());
dataSeries.updateZValues(createValues());
final FastUniformHeatmapRenderableSeries heatmapRenderableSeries = sciChartBuilder.newUniformHeatmap()
.withDataSeries(dataSeries)
.withMinimum(0)
.withMaximum(200)
.withPaletteProvider(paletteProvider)
.build();
final SciChartSurface surface = binding.surface;
Collections.addAll(surface.getXAxes(), xAxis);
Collections.addAll(surface.getYAxes(), yAxis);
Collections.addAll(surface.getRenderableSeries(), heatmapRenderableSeries);
Collections.addAll(surface.getChartModifiers(), sciChartBuilder.newModifierGroupWithDefaultModifiers().build());
}
private static IValues<Double> createValues() {
final DoubleValues values = new DoubleValues(WIDTH * HEIGHT);
final Random random = new Random();
final double angle = Math.PI * 2;
final double cx =150, cy = 100;
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
final double v = (1 + Math.sin(x * 0.04 + angle)) * 50 + (1 + Math.sin(y * 0.1 + angle)) * 50 * (1 + Math.sin(angle * 2));
final double r = Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
final double exp = Math.max(0, 1 - r * 0.008);
values.add(v * exp + random.nextDouble() * 50);
}
}
return values;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
paletteProvider.setThresholdValue(progress);
binding.thresholdValue.setText(String.format("%d", progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
private static class CustomUniformHeatMapProvider extends PaletteProviderBase<FastUniformHeatmapRenderableSeries> implements IUniformHeatmapPaletteProvider {
private double thresholdValue;
public CustomUniformHeatMapProvider() {
super(FastUniformHeatmapRenderableSeries.class);
}
public void setThresholdValue(double thresholdValue) {
this.thresholdValue = thresholdValue;
if (renderableSeries != null) {
renderableSeries.invalidateElement();
}
}
@Override
public boolean shouldSetColors() {
return false;
}
@Override
public void update() {
final FastUniformHeatmapRenderableSeries renderableSeries = this.renderableSeries;
final UniformHeatmapRenderPassData currentRenderPassData = (UniformHeatmapRenderPassData) renderableSeries.getCurrentRenderPassData();
final DoubleValues zValues = currentRenderPassData.zValues;
final IntegerValues zColors = currentRenderPassData.zColors;
final int size = zValues.size();
zColors.setSize(size);
// working with array is much faster than calling set() many times
final double[] zValuesArray = zValues.getItemsArray();
final int[] zColorsArray = zColors.getItemsArray();
for (int zIndex = 0; zIndex < size; zIndex++) {
final double value = zValuesArray[zIndex];
zColorsArray[zIndex] = value < thresholdValue ? Color.BLACK : Color.WHITE;
}
}
}
}