Flip the label position above and below datapoint


#1

Hi,

I have a chart rendered in a view. I am using a default yRange. Some times the data points are very close to the upper and lower edges of the view in which the chart is. In those situations, the labels for those datapoints are cut off.

My question is:

Is there a way to detect that the datapoint is close to the edges and then depending on the upper edge or lower edge, I want to flip the label position to belowData or aboveData so that I can see the labels all the time


#2

Hi Veerun,

I’m assuming this is for iOS?

If so, it’s fairly simple to ensure the labels are positioned within the chart’s plot area using the alterDataPointLabel delegate method:

- (void)sChart:(ShinobiChart *)chart alterDataPointLabel:(SChartDataPointLabel *)label
  forDataPoint:(SChartDataPoint *)dataPoint
      inSeries:(SChartSeries *)series
{
    if (label.frame.origin.y < 0)
    {
        // If the label is beyond the top of the plot area
        CGRect offsetFrame = CGRectOffset(label.frame, 0, -label.frame.origin.y);
        label.frame = offsetFrame;
    }
    else if (label.frame.origin.y > chart.plotAreaFrame.size.height)
    {
        // The label is beyond the bottom of the plot area
        CGRect offsetFrame = CGRectOffset(label.frame, 0, - (label.frame.origin.y - chart.plotAreaFrame.size.height) - label.frame.size.height);
        label.frame = offsetFrame;
    }
}

Alternatively, if you wish to flip the label to the other side of the rendered data point, you should be able to use the same delegate method to position the labels, however you may need to make use of SChartAxis’ ’ pixelValueForDataValue:’ to find the y-value of the data point and then offset the label in the direction you desire (e.g. + or - 50 points).

I hopet that’s of some help.

Sam


#3

perfect. the sample code helps. More than that your comment at the end actually hepled more. I need to get the pixelValueForDataValue as well in order to correctly position the label frame