Image instead of label on PieChart possible?


#1

Hi!

Is it possible to place a image on a pie chart instead of text labels? 

Thanks!


#2

Hi Dalibor,

Pie charts will support you adding icons to each slice. The SChartDelegate class provides a method which will allow you to do this - sChart:alterLabel:forDatapoint:atSliceIndex:inRadialSeries. In order to add icons to your pie slices, you could set your class as a delegate of your chart, and then add your icons as subviews of the pie labels. I’ve given some code below which shows the kind of thing which I mean:

- (void)sChart:(ShinobiChart *)chart alterLabel:(UILabel *)label forDatapoint:(SChartRadialDataPoint *)datapoint atSliceIndex:(NSInteger)index inRadialSeries:(SChartRadialSeries *)series {
    label.text = @"";
    UIImage *image = [UIImage imageNamed:@"shinobi.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [label addSubview:imageView];
    
    CGPoint oldLabelCenter = label.center;
    // Resize the view to match the icons
    label.bounds = imageView.bounds;
    label.center = oldLabelCenter;
}

In the example above, I clear the label text, then add the icon to the label. I resize the label so it doesn’t clip the icon, and ensure that the label is still centred on the pie slice. In this example I’ve just added the same icon to each slice - you would probably want to add different icons depending on the slice.

I hope this helps, let me know how things go!

Regards,

Dan