在Flex中,制作一个折线图

来源:互联网 发布:反转故事知乎神经病 编辑:程序博客网 时间:2024/04/27 15:58

1、问题背景

     在Flex中,制作一个折线图,并且给折线图的横轴和纵轴进行样式设置,具体实现步骤如下:


2、实现实例

(1)设置横轴样式和数据绑定

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <mx:horizontalAxis>  
  2.     <mx:CategoryAxis categoryField="quarter" displayName="季度"/>  
  3. </mx:horizontalAxis>  
  4.               
  5. <mx:horizontalAxisRenderers>  
  6.     <mx:AxisRenderer placement="bottom" tickLength="1" tickStroke="{new Stroke(0xFF0000,1)}"  
  7.                                  axisStroke="{new Stroke(0xFF0000,1)}">  
  8.         <mx:axis>  
  9.             <mx:LinearAxis id="bottomAxis"/>  
  10.         </mx:axis>  
  11.     </mx:AxisRenderer>  
  12. </mx:horizontalAxisRenderers>  

(2)设置纵轴样式和数据绑定

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <mx:verticalAxisRenderers>  
  2.     <mx:AxisRenderer placement="left" tickLength="1" tickStroke="{new Stroke(0xFF0000,1)}"  
  3.                                  axisStroke="{new Stroke(0xFF0000,1)}">  
  4.           <mx:axis>  
  5.            <mx:LinearAxis id="leftAxis"/>  
  6.           </mx:axis>  
  7.     </mx:AxisRenderer>  
  8. </mx:verticalAxisRenderers>  

(3)在图上绑定横轴和纵轴

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <mx:series>  
  2.     <mx:LineSeries verticalAxis="{leftAxis}" displayName="兔子" xField="quarter" yField="rabbit"/>  
  3. </mx:series>  

3、实现结果



4、附录

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx"  
  5.                width="100%" height="100%" fontSize="12"  
  6.                fontFamily="微软雅黑">  
  7.     <s:layout>  
  8.         <s:BasicLayout/>  
  9.     </s:layout>  
  10.     <fx:Script>  
  11.         <![CDATA[  
  12.             import mx.graphics.Stroke;  
  13.         ]]>  
  14.     </fx:Script>  
  15.     <fx:Script>  
  16.         <![CDATA[  
  17.             import mx.collections.ArrayCollection;  
  18.             import mx.events.FlexEvent;  
  19.   
  20.             [Bindable]  
  21.             //折线图数据绑定  
  22.             private var chartArray:ArrayCollection = new ArrayCollection([  
  23.                 {quarter:"第一季度",rabbit:"342",birthRate:"0.78677"},  
  24.                 {quarter:"第二季度",rabbit:"457",birthRate:"0.322343232"},  
  25.                 {quarter:"第三季度",rabbit:"786",birthRate:"0.457645"},  
  26.                 {quarter:"第四季度",rabbit:"654",birthRate:"0.454848"}  
  27.             ]);  
  28.   
  29.         ]]>  
  30.     </fx:Script>  
  31.     <fx:Declarations>  
  32.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  33.     </fx:Declarations>  
  34.       
  35.     <mx:VBox width="100%" height="100%" paddingBottom="100" paddingLeft="100" paddingRight="150"  
  36.              paddingTop="100" horizontalAlign="center">  
  37.         <mx:LineChart id="line" width="100%" height="90%" dataProvider="{chartArray}"  
  38.                       showAllDataTips="true">  
  39.             <mx:horizontalAxis>  
  40.                 <mx:CategoryAxis categoryField="quarter" displayName="季度"/>  
  41.             </mx:horizontalAxis>  
  42.               
  43.             <mx:horizontalAxisRenderers>  
  44.                 <mx:AxisRenderer placement="bottom" tickLength="1" tickStroke="{new Stroke(0xFF0000,1)}"  
  45.                                  axisStroke="{new Stroke(0xFF0000,1)}">  
  46.                     <mx:axis>  
  47.                         <mx:LinearAxis id="bottomAxis"/>  
  48.                     </mx:axis>  
  49.                 </mx:AxisRenderer>  
  50.             </mx:horizontalAxisRenderers>  
  51.               
  52.             <mx:verticalAxisRenderers>  
  53.                 <mx:AxisRenderer placement="left" tickLength="1" tickStroke="{new Stroke(0xFF0000,1)}"  
  54.                                  axisStroke="{new Stroke(0xFF0000,1)}">  
  55.                     <mx:axis>  
  56.                         <mx:LinearAxis id="leftAxis"/>  
  57.                     </mx:axis>  
  58.                 </mx:AxisRenderer>  
  59.             </mx:verticalAxisRenderers>  
  60.               
  61.             <mx:series>  
  62.                 <mx:LineSeries verticalAxis="{leftAxis}" displayName="兔子" xField="quarter" yField="rabbit"/>  
  63.             </mx:series>  
  64.               
  65.         </mx:LineChart>  
  66.         <mx:Legend dataProvider="{line}"/>  
  67.           
  68.     </mx:VBox>  
  69. </s:Application>  
0 0