arcgis api for flex之專題圖製做(餅狀圖,柱狀圖等)

近期公司給我一個任務,就是利用arcgis api for flex實現在地圖上點(業務數據)直接顯示餅狀圖以及柱狀圖的專題圖製做。而不是經過點擊點顯示氣泡窗體的形式來實現。這個公司已經實現了。html

      通過一段時間的摸索,參照一些網上資源,眼下大概弄出來了,裏面還有待無缺的地方的。git

     效果圖例如如下:github

 

      (1)Chart.mxml,基本的展現地圖專題圖效果的頁面 api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?xml version= "1.0"  encoding= "utf-8" ?

>ide

<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:esri= "http://www.esri.com/2008/ags"
                pageTitle= "Charts in infowindow"  xmlns:symbols= "com.esri.ags.symbols.*" >
 
     <fx:Style>
         .chartStyle
         {
             borderThickness:  0 ;
             infoPlacement: center;
             backgroundAlpha:  0 ;
             infoOffsetX:  0 ;
             infoOffsetY:  0 ;
             paddingLeft:  0 ;
             paddingRight:  0 ;
             paddingTop:  0 ;
             paddingBottom:  0 ;
         }
     </fx:Style>
     
     <fx:Script>
         <![CDATA[
             import  com.esri.ags.geometry.MapPoint;
             import  com.esri.ags.FeatureSet;
             import  com.esri.ags.Graphic;
             import  com.esri.ags.events.MapEvent;
             import  com.esri.ags.tasks.QueryTask;
             import  com.esri.ags.tasks.supportClasses.Query;
             
             import  mx.collections.ArrayCollection;
             import  mx.controls.Alert;
             import  mx.events.FlexEvent;
             import  mx.rpc.AsyncResponder;
             
             protected  function  myMap_initializeHandler(event:MapEvent): void
             {
                 var  pie:MapPoint =  new  MapPoint( 113.55185 , 22.82289 );
                 var  column:MapPoint =  new  MapPoint( 113.59637985600011 , 22.758225999000047 );
                 var  bar:MapPoint =  new  MapPoint( 113.52757794 , 22.84012158 );
                 var  gpie:Graphic =  new  Graphic(pie);
                 var  gcolumn:Graphic =  new  Graphic(column);
                 var  gbar:Graphic =  new  Graphic(bar);   
                 //g.attributes = new Object();                                             
                 var  thematic:ArrayCollection =  new  ArrayCollection(
                     [                  
                     { Name:  "危化品1" , Rate:  25  },                
                     { Name:  "危化品2" , Rate:  15  },                
                     { Name:  "危化品3" , Rate:  23  }
                     ]);
 
                 //g.attributes.thematic = thematic;
                 gpie.attributes = thematic;
                 gcolumn.attributes = thematic;
                 gbar.attributes = thematic;
                                 
                 this .myGraphicsLayerpie.add(gpie);
                 this .myGraphicsLayercolumn.add(gcolumn);
                 this .myGraphicsLayerbar.add(gbar);
             }
         ]]>
     </fx:Script>
     
     <fx:Declarations>
         <esri:InfoSymbol id= "infoSymbolpie"  infoRenderer= "InfoRendererPieChart"  containerStyleName= "chartStyle" >
         </esri:InfoSymbol>
         <esri:InfoSymbol id= "infoSymbolcolumn"  infoRenderer= "InfoRendererColumnChart"  containerStyleName= "chartStyle" >
         </esri:InfoSymbol>
         <esri:InfoSymbol id= "infoSymbolbar"  infoRenderer= "InfoRendererBarChart"  containerStyleName= "chartStylee" >
         </esri:InfoSymbol>
     </fx:Declarations>
     
     <esri:Map id= "myMap"  load= "myMap_initializeHandler(event)" >
         <esri:extent>
             <esri:Extent xmin= "113.284171273203"  ymin= "22.6348519473499"  xmax= "113.774816132605"  ymax= "22.9103935318251" >
                 <esri:spatialReference>
                     <esri:SpatialReference wkid= "4326" />
                 </esri:spatialReference>
             </esri:Extent>
         </esri:extent>
         <esri:ArcGISTiledMapServiceLayer url= "http://localhost:6080/ArcGIS/rest/services/ns_new/MapServer" />
         <esri:GraphicsLayer id= "myGraphicsLayercolumn"  symbol= "{infoSymbolcolumn}" >
         </esri:GraphicsLayer>
         <esri:GraphicsLayer id= "myGraphicsLayerpie"  symbol= "{infoSymbolpie}" >
         </esri:GraphicsLayer>
         <esri:GraphicsLayer id= "myGraphicsLayerbar"  symbol= "{infoSymbolbar}" >
         </esri:GraphicsLayer>
     </esri:Map>
     
</s:Application>

(2)InfoRendererBarChart.mxml、InfoRendererColumnChart.mxml、InfoRendererPieChart.mxml。各自是柱狀圖以及餅狀圖實現的頁面post

1.InfoRendererBarChart.mxmlflex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version= "1.0"  encoding= "utf-8" ?>
<s:VGroup xmlns:fx= "http://ns.adobe.com/mxml/2009"
           xmlns:s= "library://ns.adobe.com/flex/spark"
           xmlns:mx= "library://ns.adobe.com/flex/mx"
           clipAndEnableScrolling= "true"
           creationComplete= "creationCompleteHandler()"
           implements = "mx.core.IDataRenderer"  width= "100"  height= "100" >
     <!--
          This  is  used by the QueryResultsWithChart sample.
     -->
 
     <fx:Script>
         <![CDATA[
             private  var  _data: Object ;
 
             [Bindable]
             // implement IDataRenderer
             public  function  get  data(): Object
             {
                 return  _data;
             }
 
             public  function  set  data(value: Object ): void
             {
                 _data = value;
             }
 
             private  function  creationCompleteHandler(): void
             {
 
             }
 
         ]]>
     </fx:Script>
     <mx:BarChart id= "columnChart"   width= "100%"  height= "100%"
                     dataProvider= "{data}"
                     showDataTips= "true" >
         <mx:series>
             <mx:BarSeries id= "barSeries"  xField= "Rate" />
         </mx:series>
         <mx:verticalAxis>
             <mx:CategoryAxis id= "barAxis"  categoryField= "Name" />
         </mx:verticalAxis>
         <mx:verticalAxisRenderers>
             <mx:AxisRenderer axis= "{barAxis}"  showLabels= "false" />
         </mx:verticalAxisRenderers>
     </mx:BarChart>
 
</s:VGroup>

2.InfoRendererColumnChart.mxml 優化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version= "1.0"  encoding= "utf-8" ?>
<s:VGroup xmlns:fx= "http://ns.adobe.com/mxml/2009"
           xmlns:s= "library://ns.adobe.com/flex/spark"
           xmlns:mx= "library://ns.adobe.com/flex/mx"
           clipAndEnableScrolling= "true"
           creationComplete= "creationCompleteHandler()"
           implements = "mx.core.IDataRenderer"  width= "100"  height= "100" >
     <!--
          This  is  used by the QueryResultsWithChart sample.
     -->
 
     <fx:Script>
         <![CDATA[
             private  var  _data: Object ;
 
             [Bindable]
             // implement IDataRenderer
             public  function  get  data(): Object
             {
                 return  _data;
             }
 
             public  function  set  data(value: Object ): void
             {
                 _data = value;
             }
 
             private  function  creationCompleteHandler(): void
             {
 
             }
         ]]>
     </fx:Script> 
     <mx:ColumnChart id= "columnChart"   width= "100%"  height= "100%"
                     dataProvider= "{data}"
                     showDataTips= "true" >
         <mx:series>
             <mx:ColumnSeries id= "columnSeries"  yField= "Rate" />
         </mx:series>
         <mx:horizontalAxis>
             <mx:CategoryAxis id= "columnAxis"  categoryField= "Name" />
         </mx:horizontalAxis>
         <mx:horizontalAxisRenderers>
             <mx:AxisRenderer axis= "{columnAxis}"  showLabels= "false" />
         </mx:horizontalAxisRenderers>
     </mx:ColumnChart>
 
</s:VGroup>

3.InfoRendererPieChart.mxmlthis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version= "1.0"  encoding= "utf-8" ?>
<s:VGroup xmlns:fx= "http://ns.adobe.com/mxml/2009"
           xmlns:s= "library://ns.adobe.com/flex/spark"
           xmlns:mx= "library://ns.adobe.com/flex/mx"
           clipAndEnableScrolling= "true"
           creationComplete= "creationCompleteHandler()"
           implements = "mx.core.IDataRenderer"  width= "100"  height= "100" >
     <!--
          This  is  used by the QueryResultsWithChart sample.
     -->
 
     <fx:Script>
         <![CDATA[
             private  var  _data: Object ;
 
             [Bindable]
             // implement IDataRenderer
             public  function  get  data(): Object
             {
                 return  _data;
             }
 
             public  function  set  data(value: Object ): void
             {
                 _data = value;
             }
 
             private  function  creationCompleteHandler(): void
             {
 
             }
 
         ]]>
     </fx:Script>
 
     <mx:PieChart id= "pieChart"
                  width= "100%"  height= "100%"
                  dataProvider= "{data}"
                  showDataTips= "true"  >
         <mx:series>
             <mx:PieSeries field= "Rate"
                           labelPosition= "callout"
                           nameField= "Name" >
             </mx:PieSeries>
         </mx:series>
     </mx:PieChart>
     
</s:VGroup>

上述的總體實現思路是這種:核心是InfoSymbol。InfoSymbol本身定義infoRenderer綁定專題圖的模版。比方InfoRendererBarChart.mxml、InfoRendererColumnChart.mxml、InfoRendererPieChart.mxml;程序初始化的時候生成了一些帶有統計信息的Graphic加入到地圖上。這些Graphic對象的attributes屬性集合來保存各個統計的對象,每個統計的對象包括兩個字段:Name表示危化品名稱,Rate表示佔有比重,如下咱們會在InfoSymbol的定義中再次看到這兩個字段。當定義好這些Graphic對象之後,咱們就可以把它們加入到設置了InfoSymbol符號的GraphicLayer上了。在InfoSymbol的定義中,咱們可以看到,在這個InfoSymbol中加入了一個餅圖組件PieChart,這個餅圖的dataProvider屬性綁定的是{data},它表明的事實上就是Graphic對象的attributes屬性。你可以簡單地這樣以爲:InfoSymbol中的data表明的就是其相應的Graphic對象的attributes屬性。url

其它的柱狀圖也是同理的。

      既然在InfoSymbol中可以得到Graphic的屬性信息,那麼依據Graphic的屬性信息來繪製不一樣的專題圖就是水到渠成的事情了。

      樣式代碼解析:     

1
2
3
4
5
6
7
8
9
10
11
12
.chartStyle
{          
     borderThickness:  0 /*顯示專題圖的邊框寬度*/
     infoPlacement: center; /*顯示專題圖的位置,這裏是中心*/
     backgroundAlpha:  0 ; /*顯示專題圖的背景透明度。這裏設置爲0。是爲了隱藏背景*/
     infoOffsetX:  0 ; /*顯示專題圖的X偏移,設置0。否則會偏離原始點位置*/
     infoOffsetY:  0 ; /*顯示專題圖的Y偏移,設置0,否則會偏離原始點位置*/
     paddingLeft:  0 ; /*顯示專題圖的位置偏移,設置0,否則會偏離原始點位置*/
     paddingRight:  0 ; /*顯示專題圖的位置偏移,設置0。否則會偏離原始點位置*/
     paddingTop:  0 ; /*顯示專題圖的位置偏移。設置0,否則會偏離原始點位置*/
     paddingBottom:  0 ; /*顯示專題圖的位置偏移,設置0,否則會偏離原始點位置*/
}

需要無缺優化之處:眼下GraphicsLayer定義了三個(pie,bar。column),而後各自綁定不一樣的infoSymbol(pie,bar,column)。這樣顯的有點冗餘了,事實上僅僅要定義一個GraphicsLayer,而後動態的推斷綁定的是哪一個infoSymbol。

備註:

GIS之家論壇(推薦):http://www.gishome.online

GIS之家GitHub:https://github.com/gishome/arcgis-for-js

GIS之家做品:https://shop116521643.taobao.com/shop/view_shop.htm

GIS之家興趣部落:http://buluo.qq.com/p/barindex.html?bid=327395

GIS項目交流羣:238339408

GIS之家交流羣一:432512093

相關文章
相關標籤/搜索