Using AdvancedColumnSeriesStyle with SeriesStyleProvider


#1

Hello,

I’m attempting to customize the style of a ColumnSeries via SeriesStyleProvider. My goal is to have a chart that shows a single series with each column displayed as different colored vertical gradients. I’ve created my style provider class that implements SeriesStyleProvider<AdvancedColumnSeriesStyle>, however there doesn’t seem to be a method on my ColumnSeries for setting the style provider with the AdvancedColumnSeriesStyle type. Is there a way to use AdvancedColumnSeriesStyle with SeriesStyleProvider?


#2

Hi,
As AdvancedColumnSeriesStyle is an extension of ColumnSeriesStyle, you should be able to declare your SeriesStyleProvider with a ColumnSeriesStyle type parameter. Within the provide method you should be able to grab the ColumnSeriesStyle object (which will in this case be an AdvancedColumnSeriesStyle) from your ColumnSeries and modify the gradient colours. I’ve tried this approach and I’ve been able to successfully display a column chart with different coloured columns and a vertical gradient. I hope this helps, thanks,
Kai.


#3

Hi Kai,
Thanks for the response. I’ve tried your suggestion of having my SeriesStyleProvider implement ColumnSeriesStyle which allows me to set the provider on my column series via setSeriesStyleProvider(SeriesStyleProvider<ColumnSeriesStyle>), however it doesn’t seem to change my gradient direction. The gradients are still horizontal rather than vertical which is what I would expect with this approach (unless I’m misunderstanding it) as my AdvancedColumnSeriesStyle is cast back into a normal ColumnSeriesStyle as a result of my implementation of the provide method. I’ve attached my SeriesStyleProvider implementation for reference.

This code builds and runs but seems to ignore setting the gradient direction to VERTICAL.

public class ColumnSeriesStyleProvider implements SeriesStyleProvider<ColumnSeriesStyle> {

public LinkedHashMap<Integer, ChartColumnGradient> gradientStylePerPointIndex;
private WeakReference<Context> contextWeakReference;

public ColumnSeriesStyleProvider(WeakReference<Context> context) {
    super();
    contextWeakReference = context;
}

@Override
public <S extends Series<ColumnSeriesStyle>> ColumnSeriesStyle provide(Data<?, ?> data, int index, S series) {
    AdvancedColumnSeriesStyle providedStyle = (AdvancedColumnSeriesStyle) series.getStyle(); //This returns ColumnSeriesStyle by default since that's what we declared in the implementation
    if (gradientStylePerPointIndex != null) {
        ChartColumnGradient gradient = gradientStylePerPointIndex.get(index);
        providedStyle = advancedColumnSeriesStyleWithGradient(gradient);
    }

    return providedStyle;
}

private AdvancedColumnSeriesStyle advancedColumnSeriesStyleWithGradient(ChartColumnGradient gradient) {
    AdvancedColumnSeriesStyle style = new AdvancedColumnSeriesStyle();
    style.setFillStyle(SeriesStyle.FillStyle.GRADIENT);
    for (Map.Entry<Integer, Float> color : gradient.colorStops().entrySet()) {
        style.addGradientStop(GradientStop.create(ContextCompat.getColor(contextWeakReference.get(), color.getKey()), color.getValue()));
    }
    style.setGradientDirection(Series.Orientation.VERTICAL);
    style.setLineShown(false);
    return style;
}

}

Setting it here:

ColumnSeriesStyleProvider styleProvider = new ColumnSeriesStyleProvider(new WeakReference<Context>(this));
columnSeries.setSeriesStyleProvider(styleProvider);

#4

Hi,
The gradientDirection property is ignored for individual point styling as it applies to the entire series. As such, setting it inside your SeriesStyleProvider will have no effect. If you set an AdvancedColumnSeriesStyle on your ColumnSeries and then set the gradientDirection here your vertical gradient should work as required.
I hope this helps,
Kai.


#5

Hi Kai,
That makes sense. I’ve tested this and it works. Thanks for the help!


#6

You’re welcome. Glad your issue is resolved.