Chart does not render points after pushing the second data point when using SChartDateTimeAxis


#1

I’m trying this for the last weeks and can’t get the chart to work with SChartDateTimeAxis. Whenever I push the second datapoint and reload the chart, the information does not render and the axis is scrolled to 1970. Find my code below:

#import "KDChartViewController.h"
#import "KDDataPoint.h"

@interface KDChartViewController () <SChartDatasource, SChartDelegate>
- (void)setup;
- (NSDate *)addMinutes:(NSInteger)minutes toDate:(NSDate *)date;
@end

@implementation KDChartViewController {
    NSDate* _startDate;
    SChartLineSeries* _lineSeries;
    NSMutableArray* _dataPoints;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    _startDate = [NSDate date];
    _dataPoints = [[NSMutableArray alloc] init];
    [self setup];
}

- (void)setup
{
    _chart.datasource = self;
    _chart.delegate = self;
    _chart.backgroundColor = [UIColor clearColor];
    
    SChartDateRange* dateRange = [[SChartDateRange alloc] initWithDateMinimum:_startDate andDateMaximum:[self addMinutes:120 toDate:_startDate]];
    SChartDateTimeAxis* xAxis = [[SChartDateTimeAxis alloc] initWithRange:dateRange];
    
    xAxis.title = @"Time";
    xAxis.minorTickFrequency = @1;
    xAxis.majorTickFrequency = @5;
    xAxis.enableGesturePanning = YES;
    xAxis.enableGestureZooming = YES;
    _chart.xAxis = xAxis;
    
    SChartNumberRange* valueRange = [[SChartNumberRange alloc] initWithMinimum:@(-1) andMaximum:@5];
    SChartCategoryAxis* yAxis = [[SChartCategoryAxis alloc] initWithRange:valueRange];
    
    yAxis.enableGesturePanning = YES;
    yAxis.enableGestureZooming = YES;
    _chart.yAxis = yAxis;
    
    _lineSeries = [[SChartLineSeries alloc] init];
    _lineSeries.title = @"My Custom Title";
    _lineSeries.style.lineWidth = @2;
    _lineSeries.style.lineColor = [UIColor redColor];
    _lineSeries.style.pointStyle.showPoints = YES;
    _lineSeries.style.pointStyle.radius = @12;
    _lineSeries.style.pointStyle.color = [UIColor redColor];
}

- (NSDate *)addMinutes:(NSInteger)minutes toDate:(NSDate *)date
{
    NSDate* newDate = [NSDate dateWithTimeInterval:minutes * 60 sinceDate:date];
    return newDate;
}

- (IBAction)addPoint:(id)sender {
    int seconds = [[NSDate date] timeIntervalSinceDate:_startDate];
    
    KDDataPoint* dataPoint = [[KDDataPoint alloc] init];
    dataPoint.value = [NSNumber numberWithInt:seconds];
    dataPoint.minutes = [NSNumber numberWithInt:seconds];
    [_dataPoints addObject:dataPoint];
    
    [_chart reloadData];
    [_chart redrawChart];
}

#pragma mark - SChartDatasource methods
- (int)numberOfSeriesInSChart:(ShinobiChart *)chart {
    return 1;
}

-(SChartSeries*)sChart:(ShinobiChart *)chart seriesAtIndex:(int)index {
    return _lineSeries;
}

- (int)sChart:(ShinobiChart *)chart numberOfDataPointsForSeriesAtIndex:(int)seriesIndex {
    return _dataPoints.count;
}

- (id<SChartData>)sChart:(ShinobiChart *)chart dataPointAtIndex:(int)dataIndex forSeriesAtIndex:(int)seriesIndex {
    KDDataPoint* myData = [_dataPoints objectAtIndex:dataIndex];
    
    SChartDataPoint* datapoint = [[SChartDataPoint alloc] init];
    datapoint.xValue = [self addMinutes:myData.minutes.integerValue toDate:_startDate];
    datapoint.yValue = myData.value;
    return datapoint;
}
@end

#2

Hi,

Thank you for raising this issue with the date/time axis. We’ll investigate the problem at this end, and find out what is going on.

Many thanks,

Dan


#3

Dan,

Any idea about a patch? I’m working on a project that depends on it…

Thanks!


#4

Hi,

I’ve had a look at this issue, and it looks like you’re encountering a known bug with our date/time axis class. If we reload data on a date/time axis which has its default range set, the data loading process isn’t correct. We shall address this bug in the near future, and we shall be releasing a version with the bug fix in.

Until that bug fix release is available, could I suggest the following workaround? The bug presents when we reload data on an existing axis with a default range. If we instead create a new axis on the chart, with the desired range, we can get around the bug.

I pulled out the code you were using to create your x axis into a factory method, like so:

- (SChartDateTimeAxis *)createXAxis
{
    SChartDateRange* dateRange = [[SChartDateRange alloc] initWithDateMinimum:_startDate andDateMaximum:[self addMinutes:120 toDate:_startDate]];
    SChartDateTimeAxis* xAxis = [[SChartDateTimeAxis alloc] initWithRange:dateRange];
    
    xAxis.title = @"Time";
    xAxis.minorTickFrequency = @1;
    xAxis.majorTickFrequency = @5;
    xAxis.enableGesturePanning = YES;
    xAxis.enableGestureZooming = YES;
    return xAxis;
}

Then, when we data points to the chart, we call that factory method to reset the x axis:

- (IBAction)addPoint:(id)sender {
    int seconds = [[NSDate date] timeIntervalSinceDate:_startDate];
    
    // Create and add data point here...
    
    _chart.xAxis = [self createXAxis];
    
    [_chart reloadData];
    [_chart redrawChart];
}

I’ve tried this code at our end, and that seems to work around the issue for now. Could you give that a go, and let me know how things go?

Many thanks,

Dan


#5

Hi,

We’ve now released version 2.5.6 of charts, which fixes this bug. If you download that, you should be able to get rid of the workaround which I posted earlier.

I hope that helps, let us know if there are any issues.

Many thanks,

Dan


#6

Dan,

I’m tryind to find the most recent non-trial version of Shinobi Charts for download but with no luck. I can’t find it even in the Customer Portal. Would you mind helping me?

Regards,

Teoni