Gradient in jbchartview

121 Views Asked by At

Trying to make the bars a gradient color and cannot find any documentation on how to do this. Looks like this is the right func. Just need help on what to type in the brackets. Not concerned about what color really either.

func barGradientForBarChartView(barChartView: JBBarChartView!) -> CAGradientLayer! {


}

Thanks for the help!

2

There are 2 best solutions below

0
On BEST ANSWER

Managed to figure this one out. Figured I would post the code incase anyone else needs. Just remember that it covers all of the bars. You can't separate them by index like with solid bars.

func barGradientForBarChartView(barChartView: JBBarChartView) -> CAGradientLayer {
    let topColor = UIColor.orangeColor()
    let bottomColor = UIColor.blueColor()

    let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor]
    let gradientLocations: [Float] = [0.0, 1.0]

    let gradientLayer: CAGradientLayer = CAGradientLayer()
    gradientLayer.colors = gradientColors
    gradientLayer.locations = gradientLocations

    return gradientLayer
}
0
On

Just an FYI that the header contains the documentation necessary. As well, the README is a great place to start.

/**
 *  If you already implement barChartView:barViewAtIndex: delegate - this   method has no effect.
 *  If a custom UIView isn't supplied and barChartView:colorForBarViewAtIndex:  isn't implemented, then
 *  a gradient layer may be supplied to be used across all bars within the chart.
 *
 *  Default: black color.
 *
 *  @param barChartView     The bar chart object requesting this information.
 *
 *  @return The gradient layer to be used as a mask over all bars within the chart.
 */

Lastly, you can create a gradient on a per bar basis by utilizing barViewAtIndex: and returning a custom view with a gradient.

Sample:

- (UIView *)barChartView:(JBBarChartView *)barChartView barViewAtIndex:(NSUInteger)index
{
    UIView *customBarView = [[UIView alloc] init];
    CAGradientLayer *gradient = [CAGradientLayer new];
    gradient.startPoint = CGPointMake(0.0, 0.0);
    gradient.endPoint = CGPointMake(1.0, 0.0);
    gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor];
    [customBarView.layer insertSublayer:gradient atIndex:0];
    return customBarView;
}

Of course, the customView should be a subclass of UIView and you should implement layoutSubviews to set the gradient's frame properly, but you get the idea.