this linechart was generated programmatically from adobe flex

with this code: (in Flash Builder create new Flex Project

of Type Air)

Copy paste this:

<?xml version=”1.0″?>
<!– charts/VariableSeries.mxml –>
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml”
                creationComplete=”initApp();”
                width=”800″
                height=”600″
                >
    <mx:Script><![CDATA[
        import mx.charts.DateTimeAxis;
        import mx.charts.series.LineSeries;
       
        [Bindable]
        private var myXML:XML =
            <dataset>
            <item>
                <who>Tom</who>
                <when>08/22/2006</when>
                <hours>5.5</hours>
            </item>
            <item>
                <who>Tom</who>
                <when>08/23/2006</when>
                <hours>6</hours>
            </item>
            <item>
                <who>Tom</who>
                <when>08/24/2006</when>
                <hours>4.75</hours>
            </item>
            <item>
                <who>Dick</who>
                <when>08/22/2006</when>
                <hours>6</hours>
            </item>
            <item>
                <who>Dick</who>
                <when>08/23/2006</when>
                <hours>8</hours>
            </item>
            <item>
                <who>Dick</who>
                <when>08/24/2006</when>
                <hours>7.25</hours>
            </item>
            <item>
                <who>Jane</who>
                <when>08/22/2006</when>
                <hours>6.5</hours>
            </item>
            <item>
                <who>Jane</who>
                <when>08/23/2006</when>
                <hours>9</hours>
            </item>
            <item>
                <who>Jane</who>
                <when>08/24/2006</when>
                <hours>3.75</hours>
            </item>
            </dataset>;
       
        public function initApp():void
        {
            // Back up the current series on the chart.
            var currentSeries:Array = myChart.series;
           
            function newLineXML(NAME:String):LineSeries
            {
                // Create the new series and set its properties.
                var LINE:LineSeries = new LineSeries();
                LINE.yField = “hours”;
                LINE.xField = “when”;
               
                // get all items that have to do with who (Tom,Jane,Dick)
                LINE.displayName = NAME;
                LINE.dataProvider = myXML.item.(who==NAME);
                // ganz krass strange notation,
                // welche bewirkt dass nur noch die items who==”Tom” in localXML vorhanden sind.
               
               
                return LINE;
            }
           
         &n

bsp;  function newLineArrayObject(NAME:String):LineSeries
            {
                // Create the new series and set its properties.
                var LINE:LineSeries = new LineSeries();
                LINE.yField = “hours”;
                LINE.xField = “when”;
               
                // get all items that have to do with who (Tom,Jane,Dick)
                LINE.displayName = NAME;
               
                var DATAPROVIDER:Array = new Array();
                var DATE:String = “”;
                for(var counter:int = 1;(counter < 10);counter++)
                {
                    DATE = “08/23/”+(2000+counter).toString();
                    DATAPROVIDER.push({who:NAME,when:DATE,hours:Math.floor(Math.random()*100)});
                }
                LINE.dataProvider = DATAPROVIDER;
                /** instead of xml
                 * an array with objects with those properties would also be okay
                 localSeries.dataProvider = [{who:”Tom”,when:”08/23/2006″,hours:6},{who:”Tom”,when:”09/23/2006″,hours:19}];
                 * **/

                // Add the new series to the current Array of series.
               
                return LINE;
            }
            // generate Lines from XML dataProvider, Add the new Lines/Series to the current Array of series.
            currentSeries.push(newLineXML(“Tom”));
            currentSeries.push(newLineXML(“Jane”));
            currentSeries.push(newLineXML(“Dick”));
           
            // generate Lines from Array->Object dataProvider
            currentSeries.push(newLineArrayObject(“Simon”));
            currentSeries.push(newLineArrayObject(“Smith”));
            currentSeries.push(newLineArrayObject(“Allan”));
            currentSeries.push(newLineArrayObject(“Alf”));
            // Add the new Array of series to the chart.
            myChart.series = currentSeries;
           
            // Create a DateTimeAxis horizontal axis.
            var hAxis:DateTimeAxis = new DateTimeAxis();
            hAxis.dataUnits = “days”;
            // Set this to false to display the leftmost label.
            hAxis.alignLabelsToUnits = false;
            // Take the date in its current format and create a Date
            // object from it.
            hAxis.parseFunction = createDate;
            myChart.horizontalAxis = hAxis;
        }
       
        public function createDate(s:String):Date {
            // Reformat the date input to create Date objects
            // for the axis.
            var a:Array = s.split(“/”);
           
            // The existing String s is in the format “MM/DD/YYYY”.
            // To create a Date object, you pass “YYYY,MM,DD”,
            // where MM is zero-based, to the Date() constructor.
            var newDate:Date = new Date(a[2],a[0]-1,a[1]);
            return newDate;
        }
       
        public function generateDataprovider():Array
        {
            var RESULT:Array = new Array();
            var TRUE:Boolean = true;
            // generate dataProvider
            for(var counter:int = 1;(counter < 100);counter++)
            {
                var NUMBERX:Number = Math.random()*1000;
                var NUMBERY:Number = Math.random()*1000;
                if(TRUE)
                {
                    /*                     NUMBERX = Math.ceil(NUMBERX); // Returns the ceiling of the specified number or expression. The ceiling of a number is the closest integer that is greater than or equal to the number.
                    NUMBERY = Math.floor(NUMBERY); // Returns the floor of the number or expression specified in the parameter val. The floor is the closest integer that is less than or equal to the specified number or expression.
                    */   
                    RESULT.push({Y

ear:1970+counter,Profit:NUMBERX,Expenses:NUMBERY});
                }
            }
           
            return RESULT;
        }
       
        public function generateDataproviderXML():XMLList
        {
            var RESULT:XMLList = new XMLList();
            var wholist:Array = new Array();
            for each(var property:XML in myXML.item.who)
            {
                // Create an Array of unique names.
                if (wholist[property] != property)
                {
                    wholist[property] = property;
                }
            }
           
            // Iterate over names and create a new series
            // for each one.
            for (var s:String in wholist)
            {
                // Use all items whose name matches s.
                /** RESULT looks like this:
                    RESULT = XMLList (@1aadac49)   
                        [0] = XML   
                            <item>   
                                <who>   
                                    “Tom”   
                                <when>   
                                    “08/22/2006”   
                                <hours>   
                                    “5.5”   
                        [1] = XML   
                            <item>   
                                <who>   
                                    “Tom”   
                                <when>   
                                    “08/23/2006”   
                                <hours>   
                                    “6”   
                        [2] = XML   
                            <item>   
                                <who>   
                                    “Tom”   
                                <when>   
                                    “08/24/2006”   
                                <hours>   
                                    “4.75”   
                 **/
            }
       
            return RESULT;
        }
       
    ]]></mx:Script>
   
    <mx:Panel title=”Line Chart with Variable Number of Series”>
        <mx:LineChart id=”myChart” showDataTips=”true”/>
        <mx:Legend dataProvider=”{myChart}”/>
    </mx:Panel>
</mx:WindowedApplication>

Powered by ScribeFire.

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin