Skip to content

Plotting JQPlot grpahs with interpolating the points that are zero

August 3, 2012

So the Olympics are well underway on Day 7 now, and too many events to watch! The highlights were for me this week were definitely Bradley Wiggins epic and historic on Wednesday 1st August as I watched most of the time trial race! Team GB now have 6 Gold, 6 Silver and 7 Bronze so pretty good in the last few days!

Over the last two and three days I had to find an efficient solution to interpolate points that are zero in the JQPlot charts without storing zero values on the table. The problem was that I had to find a good way of placing the zero values in between as well doing a check for points in the chart that do have values whilst keeping track of when to stop by today’s date so I had three variables to hand and sort of loop over. I laster found out that I had a bug on the static metods to get the next month in the Unix Timestamp which was the cause of why it took so long, but found a solution finally! That’s the Olympic spirit I suppose in programming!

Data = “[“;

IList<PortalStats> portalStats = PortalStats.GetPortalSessionsPerMonth();

string dateString = “”;
long currentdate, date, todaysdate;

// Set to first registered login date.

todaysdate = DateTimeHelpers.UnixTimeStampFromDateTime(DateTime.Today.Year, DateTime.Today.Month, 1);

if (portalStats.Count > 0)
{
currentdate = DateTimeHelpers.UnixTimeStampFromDateTime(portalStats[0].Year, portalStats[0].Month, 1);

date = DateTimeHelpers.UnixTimeStampFromDateTime(portalStats[0].Year, portalStats[0].Month, 1);

string pointString = portalStats[0].SessionCount.ToString() + “]”;

int i = 0;

while (currentdate < todaysdate)
{

if (currentdate == date)
{
dateString = “[” + portalStats[i].TimeStamp + “000” + “, “;

pointString = portalStats[i].SessionCount.ToString() + “]”;

Data += dateString + pointString;

if (currentdate < todaysdate)
{
Data += “, “;
}

i++;
}
else
{
dateString = “[” + currentdate.ToString() + “000” + “, “;

pointString = “0” + “]”;

Data += dateString + pointString;

if (currentdate < todaysdate)
{
Data += “, “;
}
}
currentdate = DateTimeHelpers.AddOneMonth(currentdate);

if (i < portalStats.Count)
{
date = DateTimeHelpers.UnixTimeStampFromDateTime(portalStats[i].Year, portalStats[i].Month, 1);
}
else
{
date = 0;
}

}

Data += “]”;
}

 

You can see in the above that the I had to use two variables to keep track of the currentdate in UNIX Timestamp to keep the current state of the time, and date to get the next UNIX TimeStamp form the PortalStats that has a value greater than zero which is the session count.

I then update the next date variable at the end of the statements in the loop, the problem was that I place this variable at the start so it got updated too quickly!

Finally I used this fundamental while loop while (currentdate < todaysdate), to always keep looping until the last point is todays date!

This is a useful method I used to convert from DateTime object to Unix TimeStamp:-

public static long UnixTimeStampFromDateTime(DateTime dateTime)
{
DateTime date = dateTime;
TimeSpan t = (date – new DateTime(1970, 1, 1).ToLocalTime());
long timestamp = (long)t.TotalSeconds;
return timestamp;
}

Also the offending method that I had trouble earlier was: –

public static long AddOneMonth(double timeStamp)
{
DateTime dt = ConvertFromUnixTimestamp(timeStamp);

dt.AddMonths(1);

return DateTimeHelpers.UnixTimeStampFromDateTime(dt);
}

This was later changed to: –

public static long AddOneMonth(double timeStamp)
{
DateTime dt = ConvertFromUnixTimestamp(timeStamp);

DateTime dtMonth = dt.AddMonths(1);

return DateTimeHelpers.UnixTimeStampFromDateTime(dtMonth);
}

 

You can see that the object is stored seperate into dtMonth rather than dt, which the earlier attempt didn’t get updated!!!!!

Any this problem is solved!

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: