Customize the x-axis on the shinboni chart


#1

I have a graphing requirement as follows :

1)Y axis displays weight data of the person

2)X-axis displays the 7 days of the week and should be displayed as “Mon, tue wed thur fri sat sun”

The week is calculated as follows : If today is 26th of december , then a weeks data would be displayed as - Dec - 19th to Dec- 26th 

Right now this is what I have got working using the below code :

************************

   //Setting the Y-axis based on the min-max weight values
    SChartNumberRange *rangeY=[[SChartNumberRange alloc]initWithMinimum:minY.intValue<=19 ? @(0) : [NSNumber numberWithInt:minY.intValue-20] andMaximum:[NSNumber numberWithInt:maxY.intValue+20]];
     SChartNumberAxis *YAxis = [[SChartNumberAxisalloc] initWithRange:rangeY];
     YAxis.style.majorTickStyle.showTicks=YES;
     _shinobiChart.yAxis = YAxis;
 
    //Setting up the x-axis
     SChartNumberRange *range=[[SChartNumberRangealloc]initWithMinimum:[NSNumbernumberWithInt:[[ANDDateManagergettingOnlyDateFromDate:week.weekStartDate] intValue]-1] andMaximum:[NSNumbernumberWithInt:[[ANDDateManagergettingOnlyDateFromDate:week.weekEndDate] intValue]+1]];
    SChartNumberAxis *xAxis = [[SChartNumberAxisalloc] initWithRange:range];
    xAxis.style.titleStyle.position = SChartTitlePositionTopOrRight;
    xAxis.style.majorTickStyle.showTicks=YES;
  _shinobiChart.xAxis = xAxis;
 *********************************************************************************
 Using the above code, I get the days of the week on the x-axis . This is expected since I using the SChartNumberAxis for the x-axis. I tried using the SChartCategoryAxis just to get a dummy value of months of the x-axis but nothing changed 
This is what I tried :
    /* NSMutableArray * monthNames = [[NSMutableArray alloc] initWithObjects:@"Jan", @"Fev", @"Mar", @"Abr", @"Mai", @"Jun", @"Jul", @"Ago", @"Set", @"Out", @"Nov", @"Dez", nil];
    SChartNumberRange * numberRange = [[SChartNumberRange alloc] initWithMinimum:[NSNumber numberWithInt:1]andMaximum:[NSNumber numberWithInt:12]];
    SChartCategoryAxis *xAxis = [[SChartCategoryAxis alloc] initWithRange:numberRange];
 
    xAxis.categories = monthNames; */
**************************************
Is there any sample code or pointers which I can refer to and check how to customize the x-axis to display the days of the week instead of the days

Thanks


#2

Hi,

I’m not 100% sure I understand your exact use case, but I think you would be best off with a DateTime axis for your x-axis. Then you can use the NSDate values for the x-value in your data points.

To specify the format of the labels, you can provide a format string to the axis, which will in-turn, be provided to an NSDateFormatter:

axis.labelFormatString = @"EEE";

Note that you might also have to specify the tick interval to ensure that you override the automatic label calculator, and don’t get repeated day labels.

Hope that helps

sam


#3

Thank you for your post. 

I got it working using the SChartCategory axis and explicitly setting the datapoint.xvalue based on my requirements .

One quick question , where would I be able to get more information on the format strings that can ge given to the axis?


#4

Hi Sim777,

It seems like there was a mistake in our documentation which means that the information for labelFormatString isn’t included.

Number axis - The number axis passes the tick value as an NSNumber through the NSString method stringWithFormat: so set a number format string, ie @"%1.2f mm".

Date axis - The date axis passes the tick value as an NSDate through an NSDateFormatter, so setting the labelFormatString set’s the dateFormat property of the NSDateFormatter, so set the string as if you were setting a date formatter’s dateFormat property, i.e @"dd MMM".

Category axis - The category axis passes the tick value as an NSString through the NSString method stringWithFormat:  so set any suitable format string, i.e @"%@ District".

I hope this helps.

Thanks,
Jan