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 color lines and points individually.
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 Use Palette Provider 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
//
// UsePaletteProviderFragment.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 com.scichart.charting.visuals.SciChartSurface
import com.scichart.charting.visuals.annotations.AnnotationCoordinateMode.RelativeY
import com.scichart.charting.visuals.annotations.BoxAnnotation
import com.scichart.charting.visuals.annotations.IAnnotation
import com.scichart.charting.visuals.annotations.OnAnnotationDragListener
import com.scichart.charting.visuals.axes.AutoRange
import com.scichart.charting.visuals.renderableSeries.OhlcRenderableSeriesBase
import com.scichart.charting.visuals.renderableSeries.XyRenderableSeriesBase
import com.scichart.charting.visuals.renderableSeries.data.OhlcRenderPassData
import com.scichart.charting.visuals.renderableSeries.data.XyRenderPassData
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IFillPaletteProvider
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IPointMarkerPaletteProvider
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IStrokePaletteProvider
import com.scichart.charting.visuals.renderableSeries.paletteProviders.PaletteProviderBase
import com.scichart.core.model.IntegerValues
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.fragments.base.ExampleSingleChartBaseFragment
import com.scichart.examples.utils.ThousandsLabelProvider
import com.scichart.examples.utils.interpolator.ElasticOutInterpolator
import com.scichart.examples.utils.scichartExtensions.*
import kotlin.math.max
import kotlin.math.min
class UsePaletteProviderFragment : ExampleSingleChartBaseFragment() {
override fun showDefaultModifiersInToolbar(): Boolean = false
override fun initExample(surface: SciChartSurface) {
val dataManager = DataManager.getInstance()
val priceBars = dataManager.getPriceDataIndu(context)
val dataOffset = -1000.0
val boxAnnotation = BoxAnnotation(context).apply {
x1 = 152.0; y1 = 0.0; x2 = 158.0; y2 = 1.0
coordinateMode = RelativeY
setBackgroundResource(R.drawable.example_box_annotation_background_1)
setIsEditable(true)
setOnAnnotationDragListener(object : OnAnnotationDragListener {
override fun onDragStarted(annotation: IAnnotation) {
updateAnnotation(annotation)
}
fun updateAnnotation(annotation: IAnnotation) {
annotation.y1 = 0.0
annotation.y2 = 1.0
surface.invalidateElement()
}
override fun onDragEnded(annotation: IAnnotation) {
updateAnnotation(annotation)
}
override fun onDragDelta(annotation: IAnnotation, horizontalOffset: Float, verticalOffset: Float) {
updateAnnotation(annotation)
}
})
}
surface.suspendUpdates {
xAxes {
numericAxis { visibleRange = DoubleRange(150.0, 165.0) }
}
yAxes {
numericAxis {
autoRange = AutoRange.Always
growBy = DoubleRange(0.0, 0.1)
drawMajorTicks = false
drawMinorTicks = false
labelProvider = ThousandsLabelProvider()
}
}
renderableSeries {
fastMountainRenderableSeries {
xyDataSeries<Double, Double>("Mountain Series") {
append(priceBars.indexesAsDouble, dataManager.offset(priceBars.lowData, dataOffset * 2))
}
areaStyle = SolidBrushStyle(0x9787CEEB)
strokeStyle = SolidPenStyle(ColorUtil.Magenta)
paletteProvider = XyCustomPaletteProvider(ColorUtil.Red, boxAnnotation)
scaleAnimation { zeroLine = 6000.0; interpolator = ElasticOutInterpolator() }
}
fastLineRenderableSeries {
xyDataSeries<Double, Double>("Line Series") {
append(priceBars.indexesAsDouble, dataManager.offset(priceBars.closeData, -dataOffset))
}
strokeStyle = SolidPenStyle(ColorUtil.Blue)
ellipsePointMarker {
fillStyle = SolidBrushStyle(ColorUtil.Red)
strokeStyle = SolidPenStyle(ColorUtil.Orange, 2f)
setSize(10)
}
paletteProvider = XyCustomPaletteProvider(ColorUtil.Red, boxAnnotation)
scaleAnimation { zeroLine = 12250.0; interpolator = ElasticOutInterpolator() }
}
fastOhlcRenderableSeries {
ohlcDataSeries<Double, Double>("Candlestick Series") {
append(priceBars.indexesAsDouble, priceBars.openData, priceBars.highData, priceBars.lowData, priceBars.closeData)
}
paletteProvider = OhlcCustomPaletteProvider(ColorUtil.CornflowerBlue, boxAnnotation)
scaleAnimation { zeroLine = 11750.0; interpolator = ElasticOutInterpolator() }
}
fastCandlestickRenderableSeries {
ohlcDataSeries<Double, Double>("Candlestick Series") {
append(priceBars.indexesAsDouble,
dataManager.offset(priceBars.openData, dataOffset),
dataManager.offset(priceBars.highData, dataOffset),
dataManager.offset(priceBars.lowData, dataOffset),
dataManager.offset(priceBars.closeData, dataOffset)
)
}
paletteProvider = OhlcCustomPaletteProvider(ColorUtil.Green, boxAnnotation)
scaleAnimation { zeroLine = 10750.0; interpolator = ElasticOutInterpolator() }
}
fastColumnRenderableSeries {
xyDataSeries<Double, Double>("Column Series") {
append(priceBars.indexesAsDouble, dataManager.offset(priceBars.closeData, dataOffset * 3))
}
strokeStyle = SolidPenStyle(ColorUtil.Blue)
zeroLineY = 6000.0
dataPointWidth = 0.8
fillBrushStyle = SolidBrushStyle(ColorUtil.Blue)
paletteProvider = XyCustomPaletteProvider(ColorUtil.Purple, boxAnnotation)
scaleAnimation { zeroLine = 6000.0; interpolator = ElasticOutInterpolator() }
}
xyScatterRenderableSeries {
xyDataSeries<Double, Double>("Scatter Series") {
append(priceBars.indexesAsDouble, dataManager.offset(priceBars.closeData, dataOffset * 2.5))
}
squarePointMarker {
fillStyle = SolidBrushStyle(ColorUtil.Red)
strokeStyle = SolidPenStyle(ColorUtil.Orange, 2f)
setSize(7)
}
paletteProvider = XyCustomPaletteProvider(ColorUtil.LimeGreen, boxAnnotation)
scaleAnimation { zeroLine = 9000.0; interpolator = ElasticOutInterpolator() }
}
}
chartModifiers { defaultModifiers() }
annotations { annotation(boxAnnotation) }
}
}
private class XyCustomPaletteProvider(private val color: Int, private val annotation: BoxAnnotation) : PaletteProviderBase<XyRenderableSeriesBase>(XyRenderableSeriesBase::class.java), IFillPaletteProvider, IStrokePaletteProvider, IPointMarkerPaletteProvider {
private val colors = IntegerValues()
override fun update() {
val renderableSeries = renderableSeries
val currentRenderPassData = renderableSeries!!.currentRenderPassData as XyRenderPassData
val xValues = currentRenderPassData.xValues
val size = currentRenderPassData.pointsCount()
colors.setSize(size)
val x1 = annotation.x1 as Double
val x2 = annotation.x2 as Double
val min = min(x1, x2)
val max = max(x1, x2)
val colorsArray = colors.itemsArray
val valuesArray = xValues.itemsArray
for (i in 0 until size) {
val value = valuesArray[i]
if (value > min && value < max) colorsArray[i] = color else colorsArray[i] = DEFAULT_COLOR
}
}
override fun getFillColors(): IntegerValues = colors
override fun getPointMarkerColors(): IntegerValues = colors
override fun getStrokeColors(): IntegerValues = colors
}
private class OhlcCustomPaletteProvider(private val color: Int, private val annotation: BoxAnnotation) : PaletteProviderBase<OhlcRenderableSeriesBase>(OhlcRenderableSeriesBase::class.java), IFillPaletteProvider, IStrokePaletteProvider, IPointMarkerPaletteProvider {
private val colors = IntegerValues()
override fun update() {
val renderableSeries = renderableSeries
val currentRenderPassData = renderableSeries!!.currentRenderPassData as OhlcRenderPassData
val xValues = currentRenderPassData.xValues
val size = currentRenderPassData.pointsCount()
colors.setSize(size)
val x1 = annotation.x1 as Double
val x2 = annotation.x2 as Double
val min = min(x1, x2)
val max = max(x1, x2)
val colorsArray = colors.itemsArray
val valuesArray = xValues.itemsArray
for (i in 0 until size) {
val value = valuesArray[i]
if (value > min && value < max) {
colorsArray[i] = color
} else {
colorsArray[i] = DEFAULT_COLOR
}
}
}
override fun getFillColors(): IntegerValues = colors
override fun getPointMarkerColors(): IntegerValues = colors
override fun getStrokeColors(): IntegerValues = colors
}
}
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// UsePaletteProviderFragment.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 androidx.annotation.NonNull;
import com.scichart.charting.model.dataSeries.IOhlcDataSeries;
import com.scichart.charting.model.dataSeries.IXyDataSeries;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.annotations.AnnotationCoordinateMode;
import com.scichart.charting.visuals.annotations.BoxAnnotation;
import com.scichart.charting.visuals.annotations.IAnnotation;
import com.scichart.charting.visuals.annotations.OnAnnotationDragListener;
import com.scichart.charting.visuals.axes.AutoRange;
import com.scichart.charting.visuals.axes.IAxis;
import com.scichart.charting.visuals.pointmarkers.EllipsePointMarker;
import com.scichart.charting.visuals.pointmarkers.SquarePointMarker;
import com.scichart.charting.visuals.renderableSeries.FastCandlestickRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.FastColumnRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.FastLineRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.FastMountainRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.FastOhlcRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.OhlcRenderableSeriesBase;
import com.scichart.charting.visuals.renderableSeries.XyRenderableSeriesBase;
import com.scichart.charting.visuals.renderableSeries.XyScatterRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.data.OhlcRenderPassData;
import com.scichart.charting.visuals.renderableSeries.data.XyRenderPassData;
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IFillPaletteProvider;
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IPointMarkerPaletteProvider;
import com.scichart.charting.visuals.renderableSeries.paletteProviders.IStrokePaletteProvider;
import com.scichart.charting.visuals.renderableSeries.paletteProviders.PaletteProviderBase;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.core.model.DoubleValues;
import com.scichart.core.model.IntegerValues;
import com.scichart.drawing.utility.ColorUtil;
import com.scichart.examples.R;
import com.scichart.examples.data.DataManager;
import com.scichart.examples.data.PriceSeries;
import com.scichart.examples.fragments.base.ExampleSingleChartBaseFragment;
import com.scichart.examples.utils.ThousandsLabelProvider;
import com.scichart.examples.utils.interpolator.ElasticOutInterpolator;
import java.util.Collections;
public class UsePaletteProviderFragment extends ExampleSingleChartBaseFragment {
@Override
public boolean showDefaultModifiersInToolbar() {
return false;
}
@Override
protected void initExample(@NonNull SciChartSurface surface) {
final IAxis xAxis = sciChartBuilder.newNumericAxis().withVisibleRange(150d, 165d).build();
final IAxis yAxis = sciChartBuilder.newNumericAxis().withLabelProvider(new ThousandsLabelProvider()).withGrowBy(0, 0.1).withAutoRangeMode(AutoRange.Always).build();
final DataManager dataManager = DataManager.getInstance();
final PriceSeries priceBars = dataManager.getPriceDataIndu(getActivity());
final double dataOffset = -1000;
final IXyDataSeries<Double, Double> mountainDataSeries = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Mountain Series").build();
final IXyDataSeries<Double, Double> lineDataSeries = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Line Series").build();
final IXyDataSeries<Double, Double> columnDataSeries = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Column Series").build();
final IXyDataSeries<Double, Double> xyScatterDataSeries = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Scatter Series").build();
final IOhlcDataSeries<Double, Double> candlestickDataSeries = sciChartBuilder.newOhlcDataSeries(Double.class, Double.class).withSeriesName("Candlestick Series").build();
final IOhlcDataSeries<Double, Double> ohlcDataSeries = sciChartBuilder.newOhlcDataSeries(Double.class, Double.class).withSeriesName("OHLC Series").build();
mountainDataSeries.append(priceBars.getIndexesAsDouble(), dataManager.offset(priceBars.getLowData(), dataOffset * 2));
lineDataSeries.append(priceBars.getIndexesAsDouble(), dataManager.offset(priceBars.getCloseData(), -dataOffset));
ohlcDataSeries.append(priceBars.getIndexesAsDouble(), priceBars.getOpenData(), priceBars.getHighData(), priceBars.getLowData(), priceBars.getCloseData());
candlestickDataSeries.append(priceBars.getIndexesAsDouble(),
dataManager.offset(priceBars.getOpenData(), dataOffset),
dataManager.offset(priceBars.getHighData(), dataOffset),
dataManager.offset(priceBars.getLowData(), dataOffset),
dataManager.offset(priceBars.getCloseData(), dataOffset));
columnDataSeries.append(priceBars.getIndexesAsDouble(), dataManager.offset(priceBars.getCloseData(), dataOffset * 3));
xyScatterDataSeries.append(priceBars.getIndexesAsDouble(), dataManager.offset(priceBars.getOpenData(), dataOffset * 2.5));
final BoxAnnotation annotation = sciChartBuilder.newBoxAnnotation().withPosition(152d, 0, 158d, 1).withBackgroundDrawableId(R.drawable.example_box_annotation_background_1).withIsEditable(true).withCoordinateMode(AnnotationCoordinateMode.RelativeY).build();
annotation.setOnAnnotationDragListener(new OnAnnotationDragListener() {
@Override
public void onDragStarted(IAnnotation annotation) {
updateAnnotation(annotation);
}
protected void updateAnnotation(IAnnotation annotation) {
annotation.setY1(0);
annotation.setY2(1);
surface.invalidateElement();
}
@Override
public void onDragEnded(IAnnotation annotation) {
updateAnnotation(annotation);
}
@Override
public void onDragDelta(IAnnotation annotation, float horizontalOffset, float verticalOffset) {
updateAnnotation(annotation);
}
});
final FastMountainRenderableSeries mountainSeries = sciChartBuilder.newMountainSeries()
.withAreaFillColor(0x9787CEEB)
.withStrokeStyle(ColorUtil.Magenta)
.withDataSeries(mountainDataSeries)
.withPaletteProvider(new XyCustomPaletteProvider(ColorUtil.Red, annotation))
.build();
final FastLineRenderableSeries lineSeries = sciChartBuilder.newLineSeries()
.withStrokeStyle(ColorUtil.Blue)
.withPointMarker(sciChartBuilder.newPointMarker(new EllipsePointMarker()).withFill(ColorUtil.Red).withStroke(ColorUtil.Orange, 2f).withSize(10, 10).build())
.withDataSeries(lineDataSeries)
.withPaletteProvider(new XyCustomPaletteProvider(ColorUtil.Red, annotation))
.build();
final FastOhlcRenderableSeries ohlcSeries = sciChartBuilder.newOhlcSeries()
.withDataSeries(ohlcDataSeries)
.withPaletteProvider(new OhlcCustomPaletteProvider(ColorUtil.CornflowerBlue, annotation))
.build();
final FastCandlestickRenderableSeries candlestickSeries = sciChartBuilder.newCandlestickSeries()
.withDataSeries(candlestickDataSeries)
.withPaletteProvider(new OhlcCustomPaletteProvider(ColorUtil.Green, annotation))
.build();
final FastColumnRenderableSeries columnSeries = sciChartBuilder.newColumnSeries()
.withStrokeStyle(ColorUtil.Blue)
.withZeroLine(6000)
.withDataPointWidth(0.8d)
.withFillColor(ColorUtil.Blue)
.withDataSeries(columnDataSeries)
.withPaletteProvider(new XyCustomPaletteProvider(ColorUtil.Purple, annotation))
.build();
final XyScatterRenderableSeries xyScatterSeries = sciChartBuilder.newScatterSeries()
.withDataSeries(xyScatterDataSeries)
.withPointMarker(sciChartBuilder.newPointMarker(new SquarePointMarker()).withFill(ColorUtil.Red).withStroke(ColorUtil.Orange, 2f).withSize(7, 7).build())
.withPaletteProvider(new XyCustomPaletteProvider(ColorUtil.LimeGreen, annotation))
.build();
UpdateSuspender.using(surface, () -> {
Collections.addAll(surface.getXAxes(), xAxis);
Collections.addAll(surface.getYAxes(), yAxis);
Collections.addAll(surface.getRenderableSeries(), mountainSeries, lineSeries, ohlcSeries, candlestickSeries, columnSeries, xyScatterSeries);
Collections.addAll(surface.getChartModifiers(), sciChartBuilder.newModifierGroupWithDefaultModifiers().build());
Collections.addAll(surface.getAnnotations(), annotation);
sciChartBuilder.newAnimator(mountainSeries).withScaleTransformation(6000d).withInterpolator(new ElasticOutInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(lineSeries).withScaleTransformation(12500d).withInterpolator(new ElasticOutInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(ohlcSeries).withScaleTransformation(11750d).withInterpolator(new ElasticOutInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(candlestickSeries).withScaleTransformation(10750d).withInterpolator(new ElasticOutInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(columnSeries).withScaleTransformation(6000d).withInterpolator(new ElasticOutInterpolator()).withDuration(3000).withStartDelay(350).start();
sciChartBuilder.newAnimator(xyScatterSeries).withScaleTransformation(9000d).withInterpolator(new ElasticOutInterpolator()).withDuration(3000).withStartDelay(350).start();
});
}
private static final class XyCustomPaletteProvider extends PaletteProviderBase<XyRenderableSeriesBase> implements IFillPaletteProvider, IStrokePaletteProvider, IPointMarkerPaletteProvider {
private final IntegerValues colors = new IntegerValues();
private final int color;
private final BoxAnnotation annotation;
public XyCustomPaletteProvider(int color, BoxAnnotation annotation) {
super(XyRenderableSeriesBase.class);
this.color = color;
this.annotation = annotation;
}
@Override
public void update() {
final XyRenderableSeriesBase renderableSeries = this.renderableSeries;
final XyRenderPassData currentRenderPassData = (XyRenderPassData) renderableSeries.getCurrentRenderPassData();
final DoubleValues xValues = currentRenderPassData.xValues;
final int size = currentRenderPassData.pointsCount();
colors.setSize(size);
final double x1 = (Double) annotation.getX1();
final double x2 = (Double) annotation.getX2();
final double min = Math.min(x1, x2);
final double max = Math.max(x1, x2);
final int[] colorsArray = colors.getItemsArray();
final double[] valuesArray = xValues.getItemsArray();
for (int i = 0; i < size; i++) {
final double value = valuesArray[i];
if (value > min && value < max)
colorsArray[i] = color;
else
colorsArray[i] = PaletteProviderBase.DEFAULT_COLOR;
}
}
@Override
public IntegerValues getFillColors() {
return colors;
}
@Override
public IntegerValues getPointMarkerColors() {
return colors;
}
@Override
public IntegerValues getStrokeColors() {
return colors;
}
}
private static final class OhlcCustomPaletteProvider extends PaletteProviderBase<OhlcRenderableSeriesBase> implements IFillPaletteProvider, IStrokePaletteProvider, IPointMarkerPaletteProvider {
private final IntegerValues colors = new IntegerValues();
private final int color;
private final BoxAnnotation annotation;
public OhlcCustomPaletteProvider(int color, BoxAnnotation annotation) {
super(OhlcRenderableSeriesBase.class);
this.color = color;
this.annotation = annotation;
}
@Override
public void update() {
final OhlcRenderableSeriesBase renderableSeries = this.renderableSeries;
final OhlcRenderPassData currentRenderPassData = (OhlcRenderPassData) renderableSeries.getCurrentRenderPassData();
final DoubleValues xValues = currentRenderPassData.xValues;
final int size = currentRenderPassData.pointsCount();
colors.setSize(size);
final double x1 = (Double) annotation.getX1();
final double x2 = (Double) annotation.getX2();
final double min = Math.min(x1, x2);
final double max = Math.max(x1, x2);
final int[] colorsArray = colors.getItemsArray();
final double[] valuesArray = xValues.getItemsArray();
for (int i = 0; i < size; i++) {
final double value = valuesArray[i];
if (value > min && value < max) {
colorsArray[i] = color;
} else {
colorsArray[i] = PaletteProviderBase.DEFAULT_COLOR;
}
}
}
@Override
public IntegerValues getFillColors() {
return colors;
}
@Override
public IntegerValues getPointMarkerColors() {
return colors;
}
@Override
public IntegerValues getStrokeColors() {
return colors;
}
}
}