Updating date format on an axis fails except on chart creation


If I use the following code when I create a chart with a SChartAxisTypeDateTime as it’s x axis:

[_chart.xAxis.labelFormatter.dateFormatter setDateFormat:@“w”]

The X axis labels will correctly be formatted as week numbers instead of the default dates.  However, if I try to do that instead from sChartRenderStarted (or ANY delegate method) the chart never updates the x axis.  I have an app where there are different time modes, one of which is weekly, in this mode I need to change the formatting of the x axis as above.  Where is the proper place to set this?


Here’s some sample code.  Note that the date formatting works correctly the first time it’s set - it just can’t be updated again once the chart is created.  The work around I am using for now is to destroy the chart and create a new one every time I need to update the x-axis date formatting.  Whereas, logically the date formatting SHOULD work (but doesn’t) by doing it in the delegate method, -(void)sChartRenderStarted:(ShinobiChart *)chart withFullRedraw:(BOOL)fullRedraw

    if (!_chart) {

         _chart = [[ShinobiChartalloc] initWithFrame:self.view.boundswithPrimaryXAxisType:SChartAxisTypeDateTimewithPrimaryYAxisType:SChartAxisTypeNumber];



        _chart = [[ShinobiChartalloc] initWithFrame:self.view.boundswithPrimaryXAxisType:SChartAxisTypeDateTimewithPrimaryYAxisType:SChartAxisTypeNumber];


    if (self.currentTime.type == timeTypeWeek){


    }else if (self.currentTime.type == timeTypeDay){

        [_chart.xAxis.labelFormatter.dateFormattersetDateFormat:@“dd MMM”];


        [_chart.xAxis.labelFormatter.dateFormattersetDateFormat:@“dd MMM HH:mm”];



The “dateFormatter” property returns the “formatter” property, cast to an NSDateFormatter. Check that it is initialised, or potentially set it yourself with

NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = dateFormatString;

_chart.xAxis.labelFormatter.formatter = dateFormatter;



As Tom has said, the main issue here is the method being called on the axis. The ‘dateFormatter’ method on the SChartTickLabelFormatter class is actually a factory method to create an instance of SChartTickLabelFormatter which uses date/time formatting. As Tom points out above, the property you should be using is the ‘formatter’ property. This references the NSFormatter object which SChartTickLabelFormatter holds.

So, if you update your delegate method to the following:

- (void)sChartRenderStarted:(ShinobiChart *)chart withFullRedraw:(BOOL)fullRedraw {
    NSDateFormatter *dateFormatter = (NSDateFormatter*)_chart.xAxis.labelFormatter.formatter;

    if (self.currentTime.type == timeTypeWeek){
        [dateFormatter setDateFormat:@"w"];
    }else if (self.currentTime.type == timeTypeDay){
        [dateFormatter setDateFormat:@"dd MMM"];
        [dateFormatter setDateFormat:@"dd MMM HH:mm"];

then you should find that this will work for you without requiring you to replace the chart.

Hope this helps, let me know how things go!



Thanks Dan, that does indeed solve the problem!  Incidentally, I had also tried setting the labelFormatter property itself, but that would throw a warning saying that there was no DateFormatter found.  But passing a reference to the formatter property as you showed does indeed do the trick.  :laughing: