Custom y-axis values


#1

Hi,

I have y-axis values with wide ranges (50 --> 20 million), therefore would like to just create my y-axis with a 0% --> 100% and display my line chart like that.I don’t want to mess with the data (as I have multiple series + tooltip).  Is it possible to create a custom y axis with ticks from 0–>100 ?


#2

Hi Steven,

Yes, creating a custom y axis to display percentages should be no problem. I’ve created a basic implementation at this end, and I’ve added the code below.

The first step to do is to subclass one of the default axes. I’ve subclassed SChartNumberAxis, as the rest of the behaviour of our axis will be as for a normal numeric one.

#import <ShinobiCharts/ShinobiChart.h>

@interface SChartPercentageAxis : SChartNumberAxis

@end

In our implementation, we can then override the stringForId: axis method. The chart uses this when it is rendering labels for tick marks on the axis.

#import "SChartPercentageAxis.h"

@implementation SChartPercentageAxis

- (NSString *)stringForId:(id)obj {
    double axisMax = [self.axisRange.maximum doubleValue];
    double value = [obj doubleValue];
    
    double percentage = (value / axisMax) * 100.0;
    return [NSString stringWithFormat:@"%.0f%%", percentage];
}

@end

In this implementation, we just work out the percentage of the axis max which is being displayed, then return a string to represent that. Now, when we create the chart, we set its y axis to be an instance of SChartPercentageAxis.

self.chart.yAxis = [SChartPercentageAxis new];

I hope this helps, let me know how things go. One thing you might need to be aware of is the tick frequency on your y axis. You might have to set this so that your tick marks are displayed at regular percentage intervals.

Regards,

Dan


#3

Thanks a lot for your detailed answer.  does the job nicely!  :grin: