how can I set time on x-axis

4 posts, 0 answers
  1. shamila
    shamila avatar
    4 posts
    Member since:
    Oct 2014

    Posted 11 Nov 2014 Link to this post

    Hi,
      How can i give time values as the y values   in [TKChartDataPoint dataPointWithX: Y:?];
     Please give some  sample code.
    I cannot plot the graph with time data
    My code
     NSDate *dateFrom = [self dateWithYear:2014 month:11 day:11 hour:0 minutes:00];   
     NSDate *dateTo = [self dateWithYear:2014 month:11 day:11 hour:23 minutes:59];  
     periodXAxis.range = [TKRange rangeWithMinimum:dateFrom andMaximum:dateTo];
     periodXAxis.majorTickIntervalUnit = TKChartDateTimeAxisIntervalUnitHours;
     NSMutableArray *impressionData = [[NSMutableArray alloc] init];    
        for (int i = 0; i < xAxisValues.count ; i++)
        {
            [impressionData addObject:[TKChartDataPoint dataPointWithX:xAxisValues[i] Y:yValues[i]]];// y values like @05:00,@06:00 
        }
           TKChartSplineAreaSeries *seriesForImpression = [[TKChartSplineAreaSeries alloc] initWithItems:impressionData];
        [_chart addSeries:seriesForImpression];
    please help me.
    Shamila











  2. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 12 Nov 2014 Link to this post

    Hello Shamila,

    Thank you for your interest in our components.

    I assume that your data is in string format. If this is the case, you should convert the string to NSDate. Here is how I modified your code:
    NSDateComponents *components = [NSDateComponents new];
    components.year = 2014;
    components.month = 11;
    components.day = 11;
     
    for (int i = 0; i < xAxisValues.count ; i++)
    {
        NSDateFormatter *formatter = [NSDateFormatter new];
        formatter.dateFormat = @"hh:mm";
        NSDate *date = [formatter dateFromString:yValues[i]];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        date = [calendar dateByAddingComponents:components toDate:date options:0];
        [impressionData addObject:[TKChartDataPoint dataPointWithX:xAxisValues[i] Y:date]];
    }

    I hope this helps. Do not hesitate to contact us if you have further questions.

    Regards,
    Jack
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft banner
  4. shamila
    shamila avatar
    4 posts
    Member since:
    Oct 2014

    Posted 13 Nov 2014 in reply to Jack Link to this post

    Hi jack ,
    Thanks for the answer.
    Now I have to set the time on x-axis and points on y axis. but when i use this code i have get a straight line (y =13). startDate  = '14/11/2014 00:00:01' and endDate = '14/11/2014 23:59:59'
    please help me i cannot find what is the problem. My code is like
    startDate = //todays date 
     endDate =  // today night 23:59 
            
            NSDateComponents *components = [NSDateComponents new];
            components.year = 2014;
            components.month = 11;
            components.day = 14;
            periodXAxis.range = [TKRange rangeWithMinimum:startDate andMaximum:endDate];
            periodXAxis.majorTickIntervalUnit = TKChartDateTimeAxisIntervalUnitHours;
            xValues  = @[@"01:12",@"02:12",@"03:12",@"04:12",@"05:12", @"06:12",@"07:12",@"08:12",@"09:12",@"10:12",@"11:12",@"12:12",@"13:12",@"14:12",@"15:12",@"16:12",@"17:12" @"18:12",@"19:12",@"20:12",@"21:12"@"22:12" @"23:12"];
           
     yValues =  @[@1, @2, @3, @0, @6,@9, @0, @4, @8, @2,@9, @10,@13,@14,@15,@0,@0,@8,@0,@5,@0,@3,@0];
           
     for (int i = 0; i < xValues.count ; i++)
            {
                
                NSDateFormatter *formatter = [NSDateFormatter new];
                formatter.dateFormat = @"hh:mm";
                NSDate *date = [formatter dateFromString:xValues[i]];
                NSCalendar *calendar = [NSCalendar currentCalendar];
                date = [calendar dateByAddingComponents:components toDate:date options:0];
                [pointsWithCategoriesAndValues addObject:[TKChartDataPoint dataPointWithX:date Y:yValues[i]]];
            }

  5. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 14 Nov 2014 Link to this post

    Hi Shamila,

    The issue is caused by the dateFormat value of NSDataFormatter class. In this form it will not work with 24h time. You can solve the issue by changing the format to "H:M". Here is the code:
    for (int i = 0; i < xValues.count ; i++)
        {
            NSDateFormatter *formatter = [NSDateFormatter new];
            formatter.dateFormat = @"H:M";
            NSDate *date = [formatter dateFromString:xValues[i]];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            date = [calendar dateByAddingComponents:components toDate:date options:0];
            [pointsWithCategoriesAndValues addObject:[TKChartDataPoint dataPointWithX:date Y:yValues[i]]];
        }

    You can find detailed information about date formatting in Apple online documentation.
    https://developer.apple.com/Library/mac/documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html#//apple_ref/doc/uid/10000029i

    Regards,
    Jack
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
DevCraft banner