ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  ChartTime Problem
Posted by Greg Fair on Sep-08-2010 00:47
Hi

I am trying to use the scrolling feature of CD. Looking at the example code I am modifying the the AJAX function processPartialUpdate().

I have my own function to generate the new data that the graph will display and am calling this within the processPartialUpdate() function. The problem I am having is that my function requires a start and an end datetime in unix timestamp format and it appears that processPartialUpdate() only deals with x-axis datetimes that have been converted using the ChartTime function. Is there any way to convert a ChartTime value back to something that I can extract a unix timestamp from.

Code below. My function is called aSpeedData() and the the last two parameters are currently hard coded as unix tiimes (1283156452, 1283356452). this is just to test that data is being returned. What I need is to replace those hard coded values the new start and end times.

-------------------------
-------------------------
function processPartialUpdate(&$viewer) {

    global $startDate, $endDate, $viewPortStartDate, $viewPortEndDate, $aSpeedData;

    $startDate = $viewer->getCustomAttr("startDate");
    $endDate = $viewer->getCustomAttr("endDate");

    if ($viewer->isViewPortChangedEvent()) {
        $duration = $endDate - $startDate;
        $viewPortStartDate = $startDate + (int)(0.5 + $viewer->getViewPortLeft() * $duration);
        $viewPortEndDate = $viewPortStartDate + (int)(0.5 + $viewer->getViewPortWidth() * $duration)
            ;
    } else {
        # The user has changed the selected range by using the start date / end date select boxes.
        # We need to retrieve the selected dates from those boxes. For partial updates, the select
        # box values are sent in as Javascript ChartViewer custom attributes.
        $startYear = (int)($viewer->getCustomAttr("StartYear"));
        $startMonth = (int)($viewer->getCustomAttr("StartMonth"));
        $startDay = (int)($viewer->getCustomAttr("StartDay"));
        $endYear = (int)($viewer->getCustomAttr("EndYear"));
        $endMonth = (int)($viewer->getCustomAttr("EndMonth"));
        $endDay = (int)($viewer->getCustomAttr("EndDay"));

        $viewPortStartDate = chartTime($startYear, $startMonth, 1) + ($startDay - 1) * 86400;
        $viewPortEndDate = chartTime($endYear, $endMonth, 1) + ($endDay - 1) * 86400;
    }

    $id = (int)($viewer->getCustomAttr("id"));
    $cg = (int)($viewer->getCustomAttr("cg"));

    # Draw the chart

    $aSpeedData = getSpeedData($id, $cg, 1283156452, 1283356452, 30);

    drawChart($viewer);



}
-------------------------
-------------------------

Thanks very much for any help you may be able to give me.

Kind Regards

Greg.

  Re: ChartTime Problem
Posted by Peter Kwan on Sep-08-2010 03:47
Hi Greg,

Actually, in the original sample code, there are additional code that shows how to get the year, month and day from the chartTime. For your case, you may use code like:

$startYMD = getChartYMD($viewPortStartDate);
$startYear = (int)($startYMD / 10000);
$startMonth = (int)($startYMD / 100) % 100;
$startDay = $startYMD % 100;

$endYMD = getChartYMD($viewPortEndDate);
$endYear = (int)($endYMD / 10000);
$endMonth = (int)($endYMD / 100) % 100;
$endDay = $endYMD % 100;

$startUnixTimeStamp = mktime(0, 0, 0, $startDay, $startMonth, $startYear);
$endUnixTimeStamp = mktime(23, 59, 59, $endDay, $endMonth, $endYear );

Hope this can help.

Regards
Peter Kwan

  Re: ChartTime Problem
Posted by Greg Fair on Sep-08-2010 21:56
Thanks Peter - I didn't look hard enough in the example code obviously. Just one correction if anyone else uses this info, the order of vars in the mktime should be.....

$startUnixTimeStamp = mktime(0, 0, 0, $startMonth, $startDay, $startYear);
$endUnixTimeStamp = mktime(23, 59, 59, $endMonth, $endDay, $endYear );

Had me searching for an hour or so :)

I wonder if you could tell me how I would extract the hh:mm:ss values from the $startYMD variable. CD seems to ignore the extra data generated by setting the start and end dates to their 'maximum' values but we deal with large datasets sometimes and if someone only needs data for half a day I'd rather not generate a days worth if I dont have to.

Many thanks again for the solution you gave.

  Re: ChartTime Problem
Posted by Peter Kwan on Sep-08-2010 22:29
Hi Greg,

Sorry for the incorrect code, and thanks a lot for posting the corrected code, so that other people can benefit.

For the hour, minutes and day, because the chartTime is the calendar seconds elapsed since 1-1-0000 12:00am, so you just need to compute the modules 86400 (86400 is the number of seconds in a day) to get the seconds elapsed since midnight. Therefore a fast method is:

$startUnixTimeStamp = mktime(0, 0, 0, $startMonth, $startDay, $startYear) + floor(fmod($viewPortStartDate, 86400));
$endUnixTimeStamp = mktime(0, 0, 0, $endMonth, $endDay, $endYear ) +  ceil(fmod($viewPortEndDate, 86400));

A more accurate method is:

$clockSecondsElapsedSinceMidnight = fmod($viewPortStartDate, 86400);
$startHour = floor(clockSecondsElapsedSinceMidnight / 3600);
$startMinute = floor(clockSecondsElapsedSinceMidnight / 60) % 60;
$startSecond = floor(clockSecondsElapsedSinceMidnight % 60);

$startUnixTimeStamp = mktime($startHour, $startMinute, $startSecond, $startMonth, $startDay, $startYear);

and similarly for the end date.

Hope this can help.

Regards
Peter Kwan