DataPointLabel bar chart show at the end of the bar


#1

I’m trying to show a datapoint label on a bar chart at the end of the bar. All the way to the right of the bar but not bleeding onto the bar. The default settings seems to center the label at the end of the bar so half of the label is on the bar, and half falls off the bar.

So I’m overriding:

func sChart(_ chart: ShinobiChart, alter label: SChartDataPointLabel, for dataPoint: SChartDataPoint, in series: SChartSeries) {

And setting the frame of the label:

label.sizeToFit()
var frame = label.frame
frame.origin.x += frame.size.width / 2.0
label.frame = frame

But I’m getting strange and inconsistent results. Wondering if there is a better way to do this?


#2

Hi rches,

sChart:alterLabel:forDataPoint:inSeries: may be called more than once on a given label, so I guess what’s happening is that your label is being moved to the right multiple times.

You can avoid this by tagging the label when you style it (using UIView's tag property):

// The label identifier is used to prevent already customized labels getting customized again.
if (label.tag != LABEL_IDENTIFIER) {
    label.tag = LABEL_IDENTIFIER;
    // INSERT CUSTOM LABEL STYLE HERE
    ...
}

For more information, take a look at our Customizable DataPoint Labels blog post.

I hope that helps.

Kind regards,

Alison