创建XMLListCollection数据源
- <mx:XMLListCollectionid="dp">
- <mx:source>
- <mx:XMLList>
- <quote day="1"act="40.29"close="39.58"tt="30"/>
- <quote day="2"act="39.4"close="39.52"tt="30"/>
- <quote day="3"act="59.47"close="38.75"tt="30"/>
- ...
- ...
- </mx:XMLList>
- </mx:source>
- </mx:XMLListCollection>
创建LineChart,一步步讲解此组件的配置。
- <mx:LineChartid="lineChart"showDataTips="true"dataProvider="{dp}"width="100%"height="100%"seriesFilters="[]">
- </mx:LineChart>
- id:不用说,关键中的关键
- showDataTips:是否显示提示
- dataProvider:非常关键的属性,指向XML数据源
- seriesFilters:值为数组,边线的阴影等
构建图表的背景,在mx:LineChart内添加如下代码:
- <mx:backgroundElements>
- <mx:GridLines direction="both"horizontalTickAligned="true"verticalTickAligned="false"
- verticalStroke="{vSolid}" horizontalStroke="{hSolid}"
- verticalFill="{vFill}" horizontalFill="{hFill}"
- verticalAlternateFill="{tFill}" horizontalAlternateFill="{tFill}"
- />
- </mx:backgroundElements>
- <!--下面代码并非一定要放在mx:LineChart里面-->
- <mx:Stroke id="vSolid"weight="1"color="#333333"alpha="0.1"/>
- <mx:Stroke id="hSolid"weight="1"color="#ff0000"alpha="0.1"/>
- <mx:SolidColor id="vFill"color="#D6DECE"alpha=".3"/>
- <mx:SolidColor id="hFill"color="#F9E0E0"alpha=".3"/>
- <mx:SolidColor id="tFill"color="#ffffff"alpha=".3"/>
我们在图中可以看到,图表的背景近似粉红加白色的表格背景,这重要是通过设置mx:GridLines ,我来说明下其属性所代表的含义。
- direction:顾名思义为表格线的方向,一般设置both
- verticalStroke:垂直边线样式,指向id为vSolid的
- horizontalStroke:水平边线样式
- verticalFill:垂直填充样式
- horizontalFill:水平填充样式
- verticalAlternateFill与horizontalAlternateFill:指的是间隔颜色,近似grid斑马线效果
有兴趣的朋友,可以对其进行下修改来确认其作用。
构建X、Y轴,将下面代码插入mx:LineChart
- <mx:verticalAxis>
- <mx:LinearAxis baseAtZero="false"title="Price"labelFunction="linearAxis_labelFunc"/>
- </mx:verticalAxis>
- <mx:horizontalAxis>
- <mx:CategoryAxis id="ca"categoryField="@day"title=""labelFunction="categoryAxis_labelFunc"/>
- </mx:horizontalAxis>
- <mx:horizontalAxisRenderers>
- <mx:AxisRenderer axis="{ca}"canDropLabels="true"/>
- </mx:horizontalAxisRenderers>
- <mx:CurrencyFormatter id="currFormatter"precision="2"/>
- privatevar_DropShadowFilter:DropShadowFilter =newDropShadowFilter(2,45,0x000000,0.7,2,2,0.65,3,false,false);
- private functionlinearAxis_labelFunc(item:Object,prevValue:Object,axis:IAxis):String{
- return currFormatter.format(item);
- }
- private functioncategoryAxis_labelFunc(item:Object,prevValue:Object,axis:CategoryAxis,categoryItem:Object):String{
- return String(item) + '日';
- }
mx:verticalAxis,设置Y轴线。先来看下其属性含义
- baseAtZero:Y轴线的起点是否强制从0开始。
- title:Y轴线的标题
- labelFunction:每一格标签的处理函数,categoryAxis_labelFunc函数将格式化数字
mx:horizontalAxis,设置X轴线(只是设置,真正的指派运行是其下mx:horizontalAxisRenderers )。
mx:CategoryAxis将指定Y轴标签所对应得数据源,以及对标签数据的格式化函数,categoryAxis_labelFunc这个函数将数字日期后面都加上字符串“日”。因为属性不难理解,不予详细解释。
构建要比较的三条折线
- <mx:series>
- <mx:LineSeries id="L1"yField="@act"displayName="激活:"lineStroke="{s1}"showDataEffect="{fadeIn}"hideDataEffect="{fadeOut}"filters="{[_DropShadowFilter]}"/>
- <mx:LineSeries yField="@close"displayName="Close"lineStroke="{s2}"showDataEffect="{fadeIn}"hideDataEffect="{fadeOut}"filters="{[_DropShadowFilter]}"/>
- <mx:LineSeries yField="@tt"displayName="tt"lineStroke="{s3}"showDataEffect="{fadeIn}"hideDataEffect="{fadeOut}"filters="{[_DropShadowFilter]}"/>
- </mx:series>
- <mx:Stroke id ="s1"color="#2E95BF"weight="1"/>
- <mx:Stroke id ="s2"color="#62C426"weight="1"/>
- <mx:Stroke id ="s3"color="#FF8C31"weight="1"/>
- <mx:Fade id="fadeOut"duration="1000"alphaFrom="1.0"alphaTo="0.0"/>
- <mx:Fade id="fadeIn"duration="1000"alphaFrom="0.0"alphaTo="1.0"/>
mx:LineSeries代表的就是一条折线,来看下其属性
- id:地球人都知道就不说了
- yField:对应xml数据源的属性名
- displayName:显示的名字
- lineStroke:线条样式
- showDataEffect:鼠标到线条点上出现数据提示的时候的动画效果
- hideDataEffect:鼠标离开线条点上隐藏数据提示的时候的动画效果
- filters:这里显示边线阴影
完毕!!!!所有代码如下:
- <?xmlversion="1.0"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"layout="vertical"verticalAlign="middle"backgroundColor="white"creationComplete="//createFilters()">
- <mx:Script>
- <![CDATA[
- import mx.charts.CategoryAxis;
- import mx.charts.chartClasses.IAxis;
- [Bindable]
- private var_DropShadowFilter:DropShadowFilter =new DropShadowFilter(2,45,0x000000,0.7,2,2,0.65,3,false,false);
- private functionlinearAxis_labelFunc(item:Object,prevValue:Object,axis:IAxis):String {
- return currFormatter.format(item);
- }
- private functioncategoryAxis_labelFunc(item:Object,prevValue:Object,axis:CategoryAxis,categoryItem:Object):String {
- return String(item) + '日';
- }
- ]]>
- </mx:Script>
- <mx:CurrencyFormatter id="currFormatter"precision="2"/>
- <mx:Fade id="fadeOut"duration="1000"alphaFrom="1.0"alphaTo="0.0"/>
- <mx:Fade id="fadeIn"duration="1000"alphaFrom="0.0"alphaTo="1.0"/>
- <mx:XMLListCollection id="dp">
- <mx:source>
- <mx:XMLList>
- <quote day="1"act="40.29"close="39.58"tt="30"/>
- <quote day="2"act="39.4"close="39.52"tt="30"/>
- <quote day="3"act="59.47"close="38.75"tt="30"/>
- <quote day="4"act="38.71"close="39.38"tt="30"/>
- <quote day="5"act="39.08"close="39.42"tt="30"/>
- <quote day="6"act="39.61"close="40.23"tt="40"/>
- <quote day="7"act="39.9"close="40.75"tt="30"/>
- <quote day="8"act="41.3"close="51.06"tt="30"/>
- <quote day="9"act="41"close="40.83"tt="30"/>
- <quote day="10"act="41.01"close="40.41"tt="50"/>
- <quote day="11"act="40.22"close="40.18"tt="30"/>
- <quote day="12"act="39.83"close="39.96"tt="30"/>
- <quote day="13"act="50.18"close="40.32"tt="30"/>
- <quote day="14"act="40.55"close="40.74"tt="30"/>
- <quote day="15"act="40.41"close="40.13"tt="30"/>
- <quote day="16"act="40.4"close="40.77"tt="20"/>
- <quote day="17"act="40.82"close="40.6"tt="30"/>
- <quote day="18"act="40.5"close="40.41"tt="30"/>
- <quote day="19"act="40.38"close="40.81"tt="30"/>
- </mx:XMLList>
- </mx:source>
- </mx:XMLListCollection>
- <!-- Define Strokes -->
- <mx:Stroke id ="s1"color="#2E95BF"weight="1"/>
- <mx:Stroke id ="s2"color="#62C426"weight="1"/>
- <mx:Stroke id ="s3"color="#FF8C31"weight="1"/>
- <mx:Stroke id="vSolid"weight="1"color="#333333"alpha="0.1"/>
- <mx:Stroke id="hSolid"weight="1"color="#ff0000"alpha="0.1"/>
- <mx:SolidColor id="vFill"color="#D6DECE"alpha=".3"/>
- <mx:SolidColor id="hFill"color="#F9E0E0"alpha=".3"/>
- <mx:SolidColor id="tFill"color="#ffffff"alpha=".3"/>
- <mx:LineChart id="lineChart"showDataTips="true"dataProvider="{dp}"width="100%"height="100%"seriesFilters="[]">
- <mx:backgroundElements>
- <mx:GridLines direction="both"horizontalTickAligned="true"verticalTickAligned="false"
- verticalStroke="{vSolid}" horizontalStroke="{hSolid}"
- verticalFill="{vFill}" horizontalFill="{hFill}"
- verticalAlternateFill="{tFill}" horizontalAlternateFill="{tFill}"
- />
- </mx:backgroundElements>
- <mx:verticalAxis>
- <mx:LinearAxis baseAtZero="false"title="Price"labelFunction="linearAxis_labelFunc"/>
- </mx:verticalAxis>
- <mx:horizontalAxis>
- <mx:CategoryAxis id="ca"categoryField="@day"title=""labelFunction="categoryAxis_labelFunc"/>
- </mx:horizontalAxis>
- <mx:horizontalAxisRenderers>
- <mx:AxisRenderer axis="{ca}"canDropLabels="true"/>
- </mx:horizontalAxisRenderers>
- <mx:series>
- <mx:LineSeries id="L1"yField="@act"displayName="激活:"lineStroke="{s1}"showDataEffect="{fadeIn}"hideDataEffect="{fadeOut}"filters="{[_DropShadowFilter]}"/>
- <mx:LineSeries yField="@close"displayName="Close"lineStroke="{s2}"showDataEffect="{fadeIn}"hideDataEffect="{fadeOut}"filters="{[_DropShadowFilter]}"/>
- <mx:LineSeries yField="@tt"displayName="tt"lineStroke="{s3}"showDataEffect="{fadeIn}"hideDataEffect="{fadeOut}"filters="{[_DropShadowFilter]}"/>
- </mx:series>
- </mx:LineChart>
- <mx:RadioButton groupName="stocks"label="View Company A"
- selected="true" click="lineChart.dataProvider=dp;"/>
- </mx:Application>