ArcGIS API for Silverlight 定义专题图

ArcGIS API for Silverlight 中提供的内置专题渲染类型包括简单符号化、唯一值符号化和分级符号化,如果想要实现其他的专题类型,如饼图、柱状图、仪表盘等,则需要

通过自定义开发的方式进行扩展。

自定义专题图类型的基本思路是使用ElementLayer与图表控件(如开源的 Visifire)相结合,在 ElementLayer 的 Children集合中添加图表对象(UI元素)。

实现一个专题图类型的基本思路:

1) 获取专题数据(例如通过 QueryTask从服务器获取) ;

2) 计算要素的标注点(GeometryService 的 LablePoints 方法) ;

 

3) 根据专题数据生成图表(调用图表控件) ;

4) 将图表添加到 ElementLayer 中(计算图表的大小、位置) 。

下面将通过示例展示如何创建自定义专题图。

核心代码

 

 

[csharp] view plaincopy
 
  1. 处理专题数据查询结果   
  2. void queryTask_ExecuteCompleted(object sender, QueryEventArgs e)  
  3. {  
  4.     // 遍历查询结果(待绘制的专题数据)      
  5.     foreach (Graphic feature in e.FeatureSet.Features)       
  6.     {           
  7.         // 计算要素的标注点          
  8.         GeometryService gpLabelPoints=newGeometryService(_geoServiceUrl);   
  9.         IList<Graphic> lstGraphics= new List<Graphic>();          
  10.         lstGraphics.Add(feature);          
  11.         gpLabelPoints.LabelPointsAsync(lstGraphics);   
  12.         // 记录要素的属性(计算标注点后将失去属性)          
  13.         IDictionary<string, double> dicGraphicAttr = newDictionary<string, double>();          
  14.         foreach (KeyValuePair<string, object> item in feature.Attributes)           
  15.         {       
  16.             //不包含比例字段               
  17.             if (!_isIncludeScaleField && _scaleField != "")              
  18.             {                    
  19.                 if(_scaleField.ToLower() == item.Key.ToLower()) continue;               
  20.             }   
  21.             string value = item.Value.ToString();               
  22.             double dValue;               
  23.             if (double.TryParse(value, outdValue))               
  24.             {                   
  25.                 dicGraphicAttr.Add(item.Key,dValue);               
  26.             }          
  27.         }   
  28.        // 计算图表的半径           
  29.        double dRadius = _radius;           
  30.        if (_isUseChartScale == true)           
  31.        {               
  32.            if (_scaleField != "" && feature.Attributes.ContainsKey(_scaleField))  
  33.            {                   
  34.                object oValue =feature.Attributes[_scaleField];                   
  35.                if (oValue != null)                   
  36.                {                       
  37.                    double dScaleValue =Convert.ToDouble(oValue);                      
  38.                    dRadius = CalcRadius(dScaleValue);                   
  39.                }               
  40.             }           
  41.         }   
  42.         //通过Lamda表达式实现对属性的访问(LabelPoint得到的Graphics没有属性)          
  43.         gpLabelPoints.LabelPointsCompleted += (object sender2, GraphicsEventArgsargs) =>           
  44.         {               
  45.             // 获取要素标注点               
  46.             MapPoint mapPoint = args.Results[0].Geometry as MapPoint;                
  47.             // 计算图表的外包矩形               
  48.             Envelope extent = newEnvelope(mapPoint.X - dRadius, mapPoint.Y - dRadius, mapPoint.X + dRadius,mapPoint.Y + dRadius);   
  49.             // 创建图表               
  50.             Chart chart = newChart();               
  51.             chart.View3D =_isView3D;   
  52.             DataSeries dataSeries = new DataSeries();               
  53.             dataSeries.RenderAs =_chartType;  
  54.             // 图表类型(饼图、柱状图等)              
  55.             dataSeries.LabelEnabled = false;               
  56.             dataSeries.LabelLineEnabled =false;   
  57.             // 添加图表数据(如饼图的饼块、柱状图的柱条等)               
  58.             foreach(KeyValuePair<string, double> dataItem in dicGraphicAttr)               
  59.             {    
  60.                 DataPoint dataPoint = newDataPoint();     
  61.                 dataPoint.AxisXLabel = dataItem.Key;     
  62.                 dataPoint.YValue =dataItem.Value;     
  63.                 dataSeries.DataPoints.Add(dataPoint);     
  64.             }   
  65.             chart.Series.Add(dataSeries);      
  66.             // 设置其他属性      
  67.             chart.Background= null;      
  68.             chart.BorderBrush =null;      
  69.             chart.IndicatorEnabled= false;      
  70.             chart.LightingEnabled = false;    
  71.             ElementLayer.SetEnvelope(chart, extent);// 指定图表的外包矩形    
  72.             // 将图表添加到ElementLayer中      
  73.             _themeLayer.Children.Add(chart);      
  74.         };      
  75.     }   
  76. }  

 

下面是效果图

注:本例是使用的全国各省市的GDP产业数据以及能耗数据。

饼的大小一样(GDP产业结构)

饼的大小分级(大小表示GDP总量)

仪表盘专题(单位GDP能耗)

posted @ 2015-03-20 16:17  Peter.Pu  阅读(148)  评论(0)    收藏  举报