This is a migrated thread and some comments may be shown as answers.

Down stroke color for Android candlestick charts

1 Answer 35 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Thean Hoo
Top achievements
Rank 1
Thean Hoo asked on 25 Jun 2015, 11:08 AM
Is there a way to set a different (down stroke) color for Android candlestick charts, similar to OHLC charts?

1 Answer, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 29 Jun 2015, 08:03 AM
Hello Thean,

Thanks for writing.
You can currently specify a down stroke by overriding the candle stick renderer. For example:
public class CandlestickPointRenderer extends OhlcPointRendererBase {
    private Paint bodyPaint = new Paint();
    /**
     * Creates a new instance of the {@link CandlestickPointRenderer} class.
     *
     * @param series owner series.
     */
    public CandlestickPointRenderer(CandlestickSeries series) {
        super(series);
        this.bodyPaint.setColor(series.getStrokeColor());
        this.bodyPaint.setStrokeWidth(series.getStrokeWidth());
    }
    /**
     * Gets the current body paint.
     *
     * @return the current body paint.
     */
    public Paint getBodyPaint() {
        return this.bodyPaint;
    }
    /**
     * Sets the current body paint.
     *
     * @param paint the new body paint.
     */
    public void setBodyPaint(Paint paint) {
        if (paint == null)
            throw new NullPointerException("paint");
        this.bodyPaint = paint;
    }
    @Override
    protected void renderPointCore(Canvas canvas, DataPoint point) {
        OhlcDataPoint ohlcPoint = (OhlcDataPoint) point;
        NumericalAxisOhlcPlotInfo plotInfo = ohlcPoint.getNumericalPlot();
        RectF pointSlot = Util.convertToRectF(point.getLayoutSlot());
        Paint strokePaint;
        Paint bodyPaint;
        if (getSeries().isPaletteApplied() && this.pointColors().containsKey(ohlcPoint)) {
            PaletteEntry entry = this.pointColors().get(ohlcPoint);
            int color = entry.getStroke();
            float strokeWidth = entry.getStrokeWidth();
            bodyPaint = new Paint();
            bodyPaint.setColor(color);
            bodyPaint.setStrokeWidth(strokeWidth);
            strokePaint = new Paint();
            strokePaint.setColor(color);
            strokePaint.setStrokeWidth(strokeWidth);
        } else {
            strokePaint = this.upStrokePaint;
            bodyPaint = this.bodyPaint;
        }
        if (ohlcPoint.isFalling()) {
            bodyPaint.setStyle(Paint.Style.STROKE);
        } else {
            bodyPaint.setStyle(Paint.Style.FILL);
        }
        float stickUpperMiddle = (float) (Math.min(plotInfo.physicalOpen, plotInfo.physicalClose));
        float stickLowerMiddle = (float) (Math.max(plotInfo.physicalOpen, plotInfo.physicalClose));
        canvas.drawLine(pointSlot.centerX(), pointSlot.top, pointSlot.centerX(), stickUpperMiddle, strokePaint);
        canvas.drawLine(pointSlot.centerX(), stickLowerMiddle, pointSlot.centerX(), pointSlot.bottom, strokePaint);
        canvas.drawRect(pointSlot.left, stickUpperMiddle, pointSlot.right, stickLowerMiddle, bodyPaint);
    }
}

This is the base implementation you can simply rename the class to CustomCandlestickRenderer for example and tell your series to use it like this:
candleStickSeries.setDataPointRenderer(new CandlestickCustomRenderer(candleStickSeries));

Please write again if you need further assistance.

Regards,
Victor
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Chart
Asked by
Thean Hoo
Top achievements
Rank 1
Answers by
Victor
Telerik team
Share this question
or