core-plot and NSDate (iPhone) - iphone

I wish to plot a line graph where the x-axis is defined as a number of days between two dates and the y-axis is a value that varies on each of the days.
I can plot the y values as an NSNumber but I have no idea how to set the ranges and the markup on the x-axis. I have looked at the date example in the "examples" directory of the core-plot distribution but have found it a little confusing.
Does anyone know of a tutorial, or code sample, which might asist me in this regard?
Thank you in advance.

There are two main concepts you need to be aware of: how to format the dates and how to convert the dates into numbers for the axis ranges and data values.
Core Plot provides a CPTimeFormatter class that takes care of the formatting. You provide an NSDateFormatter set to whatever format you want to use and a reference date to define the origin of the numeric scale. Set the labelFormatter property on the axis to your initialized CPTimeFormatter and it will take care of converting the numeric data to dates and applying the desired format.
The key to calculating the numeric values is that you need to find the difference between your reference date and the date value of interest in seconds. That's why the sample program defined oneDay = 24 * 60 * 60.
24 hrs/day * 60 min/hr * 60 sec/min = 86400 sec/day.

Related

Converting Date Time into standard format

I have data (240 x 9 dimensions) in which a DATETIME column has entries as shown below. I want to separate the date and time parts. Also, I need to calculate the time difference between an entry and a previous one. The date is formatted as yyyymmdd followed by the hhmmsssss. However, the last three entries of the seconds represent the decimal seconds i.e., 36200 means 36.2 seconds
Using MATLAB I have separated the date and time entries as character arrays as the original data was in the double format as below:
datestr: 240 x8 char e.g., '20190101'
timestr: 240 x9 char e.g., '001634200'
I want to convert the date into the standard format. I used
formatOut = 'yyyy/mm/dd';
date=datestr(datenum('20190101','yyyymmdd',1900),formatOut);
But getting this error : Index in position 1 exceeds array bounds (must not exceed 240).
Additionally, I need to convert the time into hh:mm:ss.sss format and then calculate the difference between a time and its previous entry.
Any help will be appreciated.

MATLAB Change numbers to date

I have time set up as serial dates. Each number corresponds to a day, in order, from 20100101 to 20130611. How do I convert the serial date to a date in the format month-year? I need this because I want to plot data and need the x axis to show the date.
Thanks!
The first step is to convert your date-format into one of the standard Matlab date formats. The best format to use for plots is the "serial date format". The numbers itself are a bit awkward, since they represent the "amount of time after 0/0/0000, in days", which is a huge number. Also, this date actually never existed, making it really weird when you want to work with dates that are BC.
However, the conversion is easy, since your format also counts the days, but you count after 31st of December, 2009. You can convert this using
numeric_date_vec = datenum(2009, 12, 31) + x;
You then plot your data using
plot(numeric_date_vec, y)
and you let Matlab add the date-ticks automatically by calling
datetick('mmm yyyy')
The problem is, the ticks do not update after zooming in. You can either call
datetick('mmm yyyy','keeplimits')
again, after each zooming or panning, or you download datetickzoom from the Matlab file exchange. It takes the same arguments as datetick, but it hooks into the zoom function and updates the ticks automatically.
Edit:
Sometimes, the dateticks are not spaced in any sensible way, then you can either try to zoom in and out a little until it snaps to something good, or you have to set the ticks manually:
% Set ticks to first day of the months in 2010
tick_locations = datenum(2012,[1:12],1);
% Set ticks on x-axis
set(gca, 'XTick', tick_locations)
% Call datetick again to get the right date labels, use option "keepticks"
datetick('mmm yyyy','keeplimits', 'keepticks')
You might have to modify the tick_locations = datenum(2012,[1:12],1) a bit to get the ticks that you want. For instance, you can use
tick_locations = datenum(2012,[1:2:25],1)
to get every second month between Jan 2012 and Jan 2013.
For day number n use
datestr(datenum(2009, 12, 31) + n, 'yyyy-mm')
for example
>> datestr(datenum(2009, 12, 31)+365, 'yyyy-mm')
ans =
2010-12
>> datestr(datenum(2009, 12, 31)+366, 'yyyy-mm')
ans =
2011-01

highcharts x axis date variable range

I have difficulty to make date range in x-axis for highcharts.
It based on user input that can have various date range, can be a month, 14 days, 2 months, etc.
Is it possible to give minimum and maximum date, and let the highcharts arrange the scaling between min and max date?
And how will be the correct syntax?
appreciate for the help.
You can set min/max values for xAxis, but dynamic version can be achived by using variables defiend by user.
http://api.highcharts.com/highcharts#xAxis.min
http://api.highcharts.com/highcharts#xAxis.max

How do you use time as an axis in a Core Plot graph?

I have two arrays arr1 and arr2 containing datetime in dd/MM/yyyy HH:mm:ss and HH:mm:ss respectively.
For eg. arr1[0] contains 23/12/2011 09:15:30 while arr2[0] contains 09:15:30.
Note that arr1 will always contain today's date, which is exactly what I want for my iPad app which I'm preparing using Xcode 4.2.
I want to plot specific time (either from arr1 or from arr2 whichever is best suitable) on the X axis and corresponding float value contained in an array arr3 on Y-axis.
Now I'm stuck at plotting time on X axis as I can't pass time in dd/MM/yyyy HH:mm:ss or HH:mm:ss.
I googled about it and got the suggestion to use epoch but I'm unable to implement it for my app.
Other than using epoch, how can I pass a time value to Core Plot for the X axis?
Core Plot requires numeric plot data. You'll have to find a way to convert the dates to a number. One way is to convert each one to an epoch value as you mentioned. If the dates are not evenly spaced, that may be your only option.
If you know that the dates are spaced at regular intervals (e.g., hourly, daily, monthly, etc.) you could just figure an index that corresponds to each date. Make custom labels to display the dates and set the plot space range to cover the correct range of indices.

Generate a periodic value based on dates

I was hoping someone that is good with math and loops could help me out. I'm writing a program in Objective C where I need to come up with a way to do a cycle. If you don't know Objective C I would appreciate any help in pseudo code just to help me figure this out.
What I need is a scale that is based on two dates. I know this will be some sort of loop but not sure how to figure it out.
For instance, lets say that the first date is 5/25/1976 and the second date is 9/25/2009. Every 25 days there will be a "peak" so it's value will be 100. If I divide 23 in half I get 12 (rounded) so it would be the opposite or "valley" so it's numerical value would be 0. In other words on the 23rd day it would be at 100 but then on the 24th day it would start going back down and then bottom out 12 days later and then start the cycle back up and top out again at 23 days.
What I need to be able to do is find the numerical value for any given date in between any two given dates.
Thanks for any help you can offer!
value = 100*cos(2*pi*(numDays/25))
Or something like that.
Calculate the difference in days (optionally in fractional days too) between the starting point and the day you want the value for.
Divide by the cycle period (could be 23 or 25 according to the question).
Take the fractional part.
Apply the correct periodic function - for example, either sin() or cos(), appropriately scaled for the trigonometric functions (multiply the fraction by 2π).
You could simulate the shape by values out of a table describing the values indexed on days into the period (so you would use waveform[Δt mod period] to determine the value).
The NSDate class has a method timeIntervalSinceDate that will give you then number of seconds between two dates. You could calculate the number of days between two dates like this:
- (double) daysBetweenStart:(NSDate*)start end:(NSDate*)end
{
return [start timeIntervalSinceDate:end] / 86400.0; // seconds in a day
}
You could use this to compute a step function based on that:
- (double) someDescriptiveFunctionName:(NSDate*)date fromDate:(NSDate*)start
{
double days = [self daysBetweenStart:start end:date];
if ((int) days % 23 == 0)
return 100.0;
else
return 0.0;
}
This function returns 100.0 if the given date is between 23 and 24 days from the start, and 0.0 otherwise. You could substitute 23 for whatever period you like. I'm not sure if this is what you wanted, so clarify your question if it wasn't.
Disclaimer: This is Cocoa. Hopefully it's the same as iPhone Cocoa?