This is a migrated thread and some comments may be shown as answers.

how can I set time on x-axis

3 Answers 78 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
shamila
Top achievements
Rank 1
shamila asked on 11 Nov 2014, 11:36 AM
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











3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 12 Nov 2014, 09:50 AM
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.

 
0
shamila
Top achievements
Rank 1
answered on 14 Nov 2014, 05:12 AM
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]]];
        }

0
Jack
Telerik team
answered on 14 Nov 2014, 04:13 PM
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.

 
Tags
Chart
Asked by
shamila
Top achievements
Rank 1
Answers by
Jack
Telerik team
shamila
Top achievements
Rank 1
Share this question
or