PDA

View Full Version : Help with arrayCollection.additem()


maasss18
11-16-2008, 06:13 AM
Can someone give me a sample on creating an array collection programically with the additem()

Sly_cardinal
11-16-2008, 08:24 AM
var ac:ArrayCollection = new ArrayCollection(new Array());

var newItem:Object;
for (var i:int = 0; i < 10; i++)
{
newItem = new Object();
ac.addItem(newItem);
}

maasss18
11-16-2008, 08:36 AM
and what if i want to add stuff into the object. Such as maybe a name and value.

is it alright if i do not put the "new Array()" in the
new ArrayCollection(new Array());

Sly_cardinal
11-16-2008, 08:52 AM
and what if i want to add stuff into the object. Such as maybe a name and value.

is it alright if i do not put the "new Array()" in the
new ArrayCollection(new Array());

[url="http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html#ArrayCollection()"]As the docs point out[url], if you don't pass an array to the ArrayCollection constructor then a new Array is created behind the scenes.

It's up to you to decide what to put into the objects inside the ArrayCollection - my initial example was just showing how to put stuff in with the addItem function (the new Object() stuff was incidental).

Here is another simple example showing how to create objects and add properties to them:


// Assumes that length of nameList and valueList is the same.
function createArrayCollection(nameList:Array, valueList:Array):ArrayCollection
{
var ac:ArrayCollection = new ArrayCollection();

var newItem:Object;
for (var i:int = 0; i < nameList.length; i++)
{
newItem = new Object();
newItem.name = nameList[i];
newItem.value = valueList[i];

ac.addItem(newItem);
}

return ac;
}


Note that the previous function is equivalent to the one below:


// Assumes that length of nameList and valueList is the same.
function createArrayCollection(nameList:Array, valueList:Array):ArrayCollection
{
var objectList:Array = new Array();

var newItem:Object;
for (var i:int = 0; i < nameList.length; i++)
{
newItem = new Object();
newItem.name = nameList[i];
newItem.value = valueList[i];

objectList.push(newItem);
}

return new ArrayCollection(objectList);
}


Also, once you start passing objects around with specific properties on them, then you'd probably want to create a class for them (so instead of going new Object() and then adding properties, you might call new MyObjectClass(name, value)).

Hopefully I've not muddied the waters too much...

maasss18
11-16-2008, 09:05 AM
Let me show u my codes


for each(var variableItem2:TestVarItemRenderer in _varRendererCollection)
{
var newItemName:Object=variableItem2._data.varName;
var newItemValue:Object=variableItem2.HS.value;

newDataProviderAC.addItem({(newItemName):(newItemV alue)});

}

The newDataProviderAC is declared elsewhere.
var newDataProviderAC:ArrayCollection = new ArrayCollection;

Sly_cardinal
11-16-2008, 09:24 AM
Which part of that isn't working?

maasss18
11-16-2008, 09:31 AM
The newDataProviderAC is the dataProvider used for a Linechart.
The line does not appear when run.

Sly_cardinal
11-16-2008, 12:27 PM
If you're using the value of an object as a property name then I think you have to use array notation:


var newItem:Object = new Object();
newItem[newItemName] = newItemValue;
newDataProviderAC.addItem(newItem);

maasss18
11-18-2008, 10:12 AM
I am trying to add a timer function to the linechart to make it a real-time line chart.
This is the part of my code concerning.

public var r:Timer;
[Bindable]
public var newDataProviderAC:ArrayCollection = new ArrayCollection();
[Bindable]
public var newSeriesArray:Array = new Array();
[Bindable]
public var newItemName:String;
[Bindable]
public var newItemValue:Number;
[Bindable]
public var newSeries:LineSeries;
[Bindable]
public var newItem:Object = new Object();


private function initApp(e:FlexEvent):void
{

r = new Timer(1000); <---- Creates new timer
btnPlay.addEventListener(MouseEvent.CLICK,addData) ;
btnStop.addEventListener(MouseEvent.CLICK,stopTime r);
btnRev.addEventListener(MouseEvent.CLICK,rewindTim er);
}

-------------does not use timer yet. Is a Click event-------------
private function addData(e:Event):void {

-------------TestVarItemRenderer is each Object with a HSlider------------
for each(var variableItem:TestVarItemRenderer in _varRendererCollection)
{
newItemName=variableItem._data.varName;
newItemValue=variableItem.HS.value;
newItem[newItemName] = newItemValue;
newDataProviderAC.addItem(newItem);
newSeries = new LineSeries();
newSeries.yField=newItemName;
newSeries.displayName=newItemName;
newSeriesArray.push(newSeries);
}
legend.dataProvider=myChart;
myChart.dataProvider=newDataProviderAC;
myChart.series= newSeriesArray;

--------------Calls the startTimer function with a timer----------------
btnPlay.addEventListener(MouseEvent.CLICK,startTim er);
r.addEventListener(TimerEvent.TIMER, startTimer);
}

private function startTimer(e:Event):void{
r.start();
timeLabel.text=""+q
q++;

for each(var variableItem:TestVarItemRenderer in _varRendererCollection)
{
newItemName=variableItem._data.varName;
newItemValue=variableItem.HS.value;
newItem[newItemName] = newItemValue;
}
newDataProviderAC.addItem(newItem);
}//end of startTimer



When the program runs, the lines do appear per tick in a straight line. but when each individual HSlider is adjusted, it does not plot to the next point, it however draws a new straight line at the HSlider.value.

Sly_cardinal
11-18-2008, 01:09 PM
Try this (no guarantees that it will compile - it's a bit late at the moment, but the logic should hopefully be right):


public var r:Timer;
[Bindable]
public var newDataProviderAC:ArrayCollection = new ArrayCollection();
[Bindable]
public var newSeriesArray:Array = new Array();
[Bindable]
public var newSeries:LineSeries;


private function initApp(e:FlexEvent):void
{
r = new Timer(1000); <---- Creates new timer
btnPlay.addEventListener(MouseEvent.CLICK,addData) ;
btnStop.addEventListener(MouseEvent.CLICK,stopTime r);
btnRev.addEventListener(MouseEvent.CLICK,rewindTim er);
}

-------------does not use timer yet. Is a Click event-------------
private function addData(e:Event):void
{
//-------------TestVarItemRenderer is each Object with a HSlider------------

var newItemName:String;
var newItemValue:Number;

// Create an object that will have the value of each slider.
var newItem:Object = new Object();
for each(var variableItem:TestVarItemRenderer in _varRendererCollection)
{
newItemName=variableItem._data.varName;
newItemValue=variableItem.HS.value;
newItem[newItemName] = newItemValue;

// Create a new LineSeries for each slider.
newSeries = new LineSeries();
newSeries.yField = newItemName;
newSeries.displayName = newItemName;

newSeriesArray.push(newSeries);
}
newDataProviderAC.addItem(newItem);

legend.dataProvider=myChart;
myChart.dataProvider=newDataProviderAC;
myChart.series = newSeriesArray;

--------------Calls the startTimer function with a timer----------------
btnPlay.addEventListener(MouseEvent.CLICK,startTim er);
r.addEventListener(TimerEvent.TIMER, startTimer);
}

private function startTimer(e:Event):void
{
r.start();
timeLabel.text=""+q
q++;

var newItemName:String;
var newItemValue:Number;
var newItem:Object = new Object();
for each(var variableItem:TestVarItemRenderer in _varRendererCollection)
{
newItemName=variableItem._data.varName;
newItemValue=variableItem.HS.value;
newItem[newItemName] = newItemValue;
}
newDataProviderAC.addItem(newItem);
}//end of startTimer


A couple of things: don't be afraid of local variables. You had a couple of member variables ('newItem', 'newItemValue' and 'newItemName') that are more appropriate as function-level variables.

In your 'startTimer' you needed to instantiate a new 'newItem' object - previously you were just setting the same values on the same object instance so that is why it wasn't plotting more points, there was only one unique object in the data provider.