Use NSDate for x-axis


#1

Is there a way to use the NSDate to chart data and use date labels for the x-axis?


#2

Yes there is. Simply create a chart with a date x-axis:

[[ShinobiChart alloc] initWithFrame:frame
                 withPrimaryXAxisType:SChartAxisTypeDateTime
                 withPrimaryYAxisType:SChartAxisTypeNumber];

Then within your data-source return the required NSDate values:

- (NSArray *)sChart:(ShinobiChart *)chart dataPointsForSeriesAtIndex:(int)seriesIndex
{
    NSMutableArray *datapointArray = [NSMutableArray array];

    //iterate over your data
    for (MyDataPointType *tdp in _yData) {
        SChartDataPoint *dp = [SChartDataPoint new];
        dp.xValue = // your NSDate goes here
        dp.yValue = // your number that relates to the date goes here
        [datapointArray addObject:dp];
    }
    return [NSArray arrayWithArray:datapointArray];
}

#3

Hi Colin and anyone who can help me.

Problem: I’m trying to get my xAxis to show on the month from the NSdate that I obtained.

I read throught serveral thread and tried the methods but none is of any help. I understand that I should post a question when I hit the wall and I have just hit it :frowning: been trying out this for a few days.

Here are the resources I tried.

This is my current code for the chart

intraix%20%E2%80%94%20UtilitiesDashboardTableViewController.m%20%E2%80%94%20Edited

The chart itself is also set to the follow:

utilitiesBarChart = [[ShinobiChartalloc] initWithFrame:CGRectInset(cell.bounds, margin, margin) withPrimaryXAxisType: SChartAxisTypeDateTime withPrimaryYAxisType:SChartAxisTypeNumber];

And this is my code for xvalue

intraix%20%E2%80%94%20UtilitiesDashboardTableViewController.m%20%E2%80%94%20Edited

This is the error

intraix%20%E2%80%94%20UtilitiesDashboardTableViewController.m

My chart continue to show in epoch time :frowning:

intraix%20%E2%80%94%20UtilitiesDashboardTableViewController.m%20and%20iOS%20Simulator%20-%20iPhone%20Retina%20(4-inch)%20/%20iOS%207.1%20(11D167)

any help would be awesome :smiley:

Thank you people


#4

Hi bosslee,

It looks like the xAxis you are creating is an SChartNumberAxis. This axis type isn’t designed to show/format dates or times, so it’s likely you’re ending up in a situation where the chart shows the number of seconds since 1970. This probably explains the error message too - the number axis is looking for a number formatter, not a date formatter.

Try using an SChartDateTimeAxis in place of you number axis and let us know how you get on.  :laughing: