0
votes

I have flex 4, zend & php. I am trying to generate a chart w/ 5 yrs of data & then let the user (without making another call to the server) get a 1 yr chart w/ the same data. (In other words, the 1 yr's worth of data is a subset of the 5 yrs). I can get the initial 5 yr chart but when I try to apply a filter, as below, I get no data. What am I doing wrong?

I have the following code:

`

 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           xmlns:datadata="services.datadata.*">
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;

        protected function getData(event:FlexEvent):void
        {                               
            getDataResult.token = dataData.getData();

        }

        public function filterByRange(item:Object):Boolean 
        {
            var result:Boolean = true;

            if (item.date.time > 2007-01-01 && item.date.time < 2008-12-31 ){
                result = false;
            }
            return result;
            dg.refresh();
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <s:ArrayCollection id="dg" filterFunction="filterByRange" list="{getDataResult.lastResult}"  />
    <s:CallResponder id="getDataResult"/>
    <datadata:DataData id="dataData" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
</fx:Declarations>



<mx:AreaChart id="areachart1" width="100%" height="100%" creationComplete="getData(event)" dataProvider="{dg}" showDataTips="true">
    <mx:series>
        <mx:AreaSeries yField="price" id="areaSeries" xField="date"/>
    </mx:series>
    <mx:horizontalAxis>
        <mx:DateTimeAxis/>
    </mx:horizontalAxis>
</mx:AreaChart>


`

This is what my PHP code looks like. Remember, I am using Zend.

`

           public function getData() {

    $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename");        
    $this->throwExceptionOnError();

    mysqli_stmt_execute($stmt);
    $this->throwExceptionOnError();

    $rows = array();

    mysqli_stmt_bind_result($stmt, $row->date, $row->price);

    while (mysqli_stmt_fetch($stmt)) {
      $row->date = new DateTime($row->date);
      $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->date, $row->price);
    }

    mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);

    return $rows;
}

`

1
Are you not getting any data to display in the chart, even without the filter? If you set a breakpoint on a result handler for getDataResult do you see the data? Do the objects in the ArrayCollection have correct properties for price and date? - James Ward
Try binding the ArrayCollection list property instead of source to getDataResult.lastResult (or source to lastResult.source). It sounds like the result is already an ArrayCollection and source only takes an Array. - dchang
Flex returns a datetime variable formatted like: "Mon Sep 5 20:00:00 GMT-0400 2005".....how in the world do I get a date > or < something like that? - kristen
I've tried many variations w/ no luck....YYYY-MM-DD, mm/dd/yy, etc - kristen
Do I have to somehow filter on the timestamp that flex adds when it converts it to datetime or does the filtering happen before flex converts it to datetime? - kristen

1 Answers

1
votes

For your date range case, you can try something like:

private var startDate:Date = new Date(0); // epoch
private var endDate:Date = new Date(); // now

public function filterByRange(item:Object):Boolean 
{
    var itemDate:Date = item.date;
    return (itemDate.time > startDate.time) && (itemDate.time < endDate.time);
}

private function onClick():void
{
    startDate = new Date("2007/01/01");
    endDate = new Date("2008/12/31");
    dg.refresh();
}

...

<s:Button click="onClick()"/>

You'll need to make sure to call refresh on your ArrayCollection to apply the filter again. Just threw in a button click action for an example user interaction.