Code
public static int getLayerId(string name, ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc)
{
string[] lids;
string[] lnames;
qfunc.GetQueryableLayers(null, out lids, out lnames);
int layerid = 0;
for (int i = 0; i < lnames.Length; i++)
{
if (lnames[i] == name)
{
int.TryParse((string)lids[i], out layerid);
}
}
return layerid;
}
protected void Button1_Click(object sender, EventArgs e)
{
string resultstr = "";
int gisresource_index = 1;
string queryLayername = "usa_way";
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality(gisresource_index);
ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality ags_mf = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)mf;
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
int layerid = 1;
if (supported)
{
ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceBase ags_mr = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceBase)qfunc.Resource;
layerid = MapUtil.getLayerId(queryLayername, qfunc);
}
ChartRender c = new ChartRender(this.Map1, this.Toc1);
c.CreatePieRenderer(layerid, new string[] { "记录号"});
using System;
using System.Data;
using System.Web;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.ADF.Connection.AGS;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
/// BarChartRender 的摘要说明
public class ChartRender
{
private ESRI.ArcGIS.ADF.Web.UI.WebControls.Map pMap;
private ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc pToc;
private IServerContext pServerContext;
private MapResourceLocal pMapResLocal;
private IGeoFeatureLayer pGeoFeatureLyer;
public ChartRender()
{
}
public ChartRender(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map map, Toc toc)
{
pMap = map;
pToc = toc;
}
/// <summary>
/// 获取当前map的servercontext
/// </summary>
/// <returns></returns>
IServerContext GetServetContext()
{
try
{
MapFunctionality pMapFunc = pMap.GetFunctionality(pMap.MapResourceManagerInstance.ResourceItems.Count - 1) as MapFunctionality;
MapDescription pMapDesc = pMapFunc.MapDescription as MapDescription;
pMapResLocal = pMapFunc.MapResource as MapResourceLocal;
return pMapResLocal.ServerContextInfo.ServerContext;
}
catch (Exception ex)
{
throw new Exception("Fail to get the servercontext!");
}
}
/// <summary>
/// 创建柱状专题图表
/// </summary>
/// <param name="layerID"></param>
/// <param name="fields"></param>
public void CreateBarRenderer(int layerID, string[] fields)
{
try
{
pServerContext = GetServetContext();
MapServer pMapServer = pMapResLocal.MapServer as MapServer;
IMapServerObjects pMapServerObj = pMapServer as IMapServerObjects;
string mapName = pMapServer.get_MapName(0);
ILayer pLayer = pMapServerObj.get_Layer(mapName, layerID);
pGeoFeatureLyer = pLayer as IGeoFeatureLayer;
//设置专题图元素的属性名称列表
IChartRenderer pChartRender = pServerContext.CreateObject("esriCarto.ChartRenderer") as IChartRenderer;
IRendererFields pRenderFields = pChartRender as IRendererFields;
foreach (string var in fields)
{
pRenderFields.AddField(var, var);
}
//实例化图表对象并取得元素指定属性的最大值
IBarChartSymbol pBarChartSymbol = pServerContext.CreateObject("esriDisplay.BarChartSymbol") as IBarChartSymbol;
IChartSymbol pChartSymbol = pBarChartSymbol as IChartSymbol;
pChartSymbol.MaxValue = GetStaMaxMin(fields)[0];
pBarChartSymbol.Width = 8;
IMarkerSymbol pMarkerSymbol = pBarChartSymbol as IMarkerSymbol;
pMarkerSymbol.Size = 50;
//设置柱状图每列填充效果
ISymbolArray pSymbolArray = pBarChartSymbol as ISymbolArray;
Random ranColor = new Random();
for (int i = 0; i < fields.Length; i++)
{
IFillSymbol pFillSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as IFillSymbol;
pFillSymbol.Color = GetRGB(ranColor.Next(255), ranColor.Next(255), ranColor.Next(255));
pSymbolArray.AddSymbol((ISymbol)pFillSymbol);
}
//设置地图图层背景
ISimpleFillSymbol pFSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as SimpleFillSymbol;
pFSymbol.Color = GetRGB(239, 228, 249);
pChartRender.BaseSymbol = pFSymbol as ISymbol;
//应用柱状专题到指定图层
pChartRender.ChartSymbol = pBarChartSymbol as IChartSymbol;
pChartRender.Label = "Test";
pChartRender.UseOverposter = false;
pChartRender.CreateLegend();
pGeoFeatureLyer.Renderer = pChartRender as IFeatureRenderer;
//刷新地图显示图表及图例
pMap.Refresh();
pToc.Refresh();
pToc.ExpandDepth = 2;
}
catch (Exception ex)
{
throw new Exception("Fail to create bar chart!");
}
}
/// <summary>
/// 创建饼状专题地图
/// </summary>
/// <param name="layerID"></param>
/// <param name="fields"></param>
public void CreatePieRenderer(int layerID, string[] fields)
{
try
{
pServerContext = GetServetContext();
MapServer pMapServer = pMapResLocal.MapServer as MapServer;
IMapServerObjects pMapServerObj = pMapServer as IMapServerObjects;
string mapName = pMapServer.get_MapName(0);
ILayer pLayer = pMapServerObj.get_Layer(mapName, layerID);
pGeoFeatureLyer = pLayer as IGeoFeatureLayer;
//设置专题图元素的属性名称列表
IChartRenderer pChartRender = pServerContext.CreateObject("esriCarto.ChartRenderer") as IChartRenderer;
IRendererFields pRenderFields = pChartRender as IRendererFields;
foreach (string var in fields)
{
pRenderFields.AddField(var, var);
}
//实例化图表对象并取得元素指定属性的最大值
IPieChartSymbol pPieChartSymbol = pServerContext.CreateObject("esriDisplay.PieChartSymbol") as IPieChartSymbol;
IChartSymbol pChartSymbol = pPieChartSymbol as IChartSymbol;
pPieChartSymbol.Clockwise = true;
pPieChartSymbol.UseOutline = true;
pChartSymbol.MaxValue = GetStaMaxMin(fields)[0];
//设置饼图外围线
ISimpleLineSymbol pOutLine = pServerContext.CreateObject("esriDisplay.SimpleLineSymbol") as ISimpleLineSymbol;
pOutLine.Color = GetRGB(255, 0, 255);
pOutLine.Style = esriSimpleLineStyle.esriSLSSolid;
pOutLine.Width = 1;
pPieChartSymbol.Outline = pOutLine;
IMarkerSymbol pMarkerSymbol = pPieChartSymbol as IMarkerSymbol;
pMarkerSymbol.Size = 25;
//设置饼状图外围填充效果
ISymbolArray pSymbolArray = pPieChartSymbol as ISymbolArray;
ISimpleFillSymbol pFSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as SimpleFillSymbol;
pFSymbol.Color = GetRGB(213, 212, 252);
pFSymbol.Outline = pOutLine;
//设置饼状图每列填充效果
Random ranColor = new Random();
for (int i = 0; i < fields.Length; i++)
{
IFillSymbol pFillSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as IFillSymbol;
pFillSymbol.Color = GetRGB(ranColor.Next(255), ranColor.Next(255), ranColor.Next(255));
pSymbolArray.AddSymbol((ISymbol)pFillSymbol);
}
//设置地图图层背景
pFSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as SimpleFillSymbol;
pFSymbol.Color = GetRGB(239, 228, 249);
pChartRender.BaseSymbol = pFSymbol as ISymbol;
//设置饼状图表属性
IPieChartRenderer pPieChartRenderer = pChartRender as IPieChartRenderer;
pPieChartRenderer.MinValue = 453588;
pPieChartRenderer.MinSize = 10;
pPieChartRenderer.FlanneryCompensation = false;
pPieChartRenderer.ProportionalBySum = true;
pPieChartRenderer.ProportionalField = fields[0];
pChartRender.ChartSymbol = pPieChartSymbol as IChartSymbol;
pChartRender.Label = "Population";
//应用饼状专题到指定图层
pChartRender.UseOverposter = false;
pChartRender.CreateLegend();
pGeoFeatureLyer.Renderer = pChartRender as IFeatureRenderer;
//刷新地图显示图表及图例
pMap.Refresh();
pToc.Refresh();
}
catch (Exception ex)
{
throw new Exception("Fail to create bar chart!");
}
}
/// <summary>
/// 计算元素指定属性的最大值
/// </summary>
/// <param name="fields">元素的属性名称列表</param>
/// <returns></returns>
double[] GetStaMaxMin(string[] fields)
{
double pMaxValue = 0;
double pMinValue = 0;
double pStaMax;
double pStaMin;
double[] PMaxMin = new double[2];
for (int i = 0; i < fields.Length; i++)
{
ICursor pCursor = pGeoFeatureLyer.Search(null, true) as ICursor;
IDataStatistics pDataSta = new DataStatisticsClass();
pDataSta.Cursor = pCursor;
pDataSta.Field = fields[i];
pStaMax = pDataSta.Statistics.Maximum;
pStaMin = pDataSta.Statistics.Minimum;
if (pMaxValue < pStaMax)
{
pMaxValue = pStaMax;
}
if (pMinValue > pStaMin)
{
pMinValue = pStaMin;
}
}
PMaxMin[0] = pMaxValue;
PMaxMin[1] = pMinValue;
return PMaxMin;
}
/// <summary>
/// 获取GRB颜色
/// </summary>
/// <param name="red"></param>
/// <param name="green"></param>
/// <param name="blue"></param>
/// <returns></returns>
IColor GetRGB(int red, int green, int blue)
{
IRgbColor rgbColor = pServerContext.CreateObject("esriDisplay.RGBColor") as IRgbColor;
IColor color = rgbColor as IColor;
rgbColor.Red = red;
rgbColor.Green = green;
rgbColor.Blue = blue;
return color;
}
}
public static int getLayerId(string name, ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc)
{
string[] lids;
string[] lnames;
qfunc.GetQueryableLayers(null, out lids, out lnames);
int layerid = 0;
for (int i = 0; i < lnames.Length; i++)
{
if (lnames[i] == name)
{
int.TryParse((string)lids[i], out layerid);
}
}
return layerid;
}
protected void Button1_Click(object sender, EventArgs e)
{
string resultstr = "";
int gisresource_index = 1;
string queryLayername = "usa_way";
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality(gisresource_index);
ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality ags_mf = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)mf;
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
int layerid = 1;
if (supported)
{
ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceBase ags_mr = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceBase)qfunc.Resource;
layerid = MapUtil.getLayerId(queryLayername, qfunc);
}
ChartRender c = new ChartRender(this.Map1, this.Toc1);
c.CreatePieRenderer(layerid, new string[] { "记录号"});
using System;
using System.Data;
using System.Web;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.ADF.Connection.AGS;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
/// BarChartRender 的摘要说明
public class ChartRender
{
private ESRI.ArcGIS.ADF.Web.UI.WebControls.Map pMap;
private ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc pToc;
private IServerContext pServerContext;
private MapResourceLocal pMapResLocal;
private IGeoFeatureLayer pGeoFeatureLyer;
public ChartRender()
{
}
public ChartRender(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map map, Toc toc)
{
pMap = map;
pToc = toc;
}
/// <summary>
/// 获取当前map的servercontext
/// </summary>
/// <returns></returns>
IServerContext GetServetContext()
{
try
{
MapFunctionality pMapFunc = pMap.GetFunctionality(pMap.MapResourceManagerInstance.ResourceItems.Count - 1) as MapFunctionality;
MapDescription pMapDesc = pMapFunc.MapDescription as MapDescription;
pMapResLocal = pMapFunc.MapResource as MapResourceLocal;
return pMapResLocal.ServerContextInfo.ServerContext;
}
catch (Exception ex)
{
throw new Exception("Fail to get the servercontext!");
}
}
/// <summary>
/// 创建柱状专题图表
/// </summary>
/// <param name="layerID"></param>
/// <param name="fields"></param>
public void CreateBarRenderer(int layerID, string[] fields)
{
try
{
pServerContext = GetServetContext();
MapServer pMapServer = pMapResLocal.MapServer as MapServer;
IMapServerObjects pMapServerObj = pMapServer as IMapServerObjects;
string mapName = pMapServer.get_MapName(0);
ILayer pLayer = pMapServerObj.get_Layer(mapName, layerID);
pGeoFeatureLyer = pLayer as IGeoFeatureLayer;
//设置专题图元素的属性名称列表
IChartRenderer pChartRender = pServerContext.CreateObject("esriCarto.ChartRenderer") as IChartRenderer;
IRendererFields pRenderFields = pChartRender as IRendererFields;
foreach (string var in fields)
{
pRenderFields.AddField(var, var);
}
//实例化图表对象并取得元素指定属性的最大值
IBarChartSymbol pBarChartSymbol = pServerContext.CreateObject("esriDisplay.BarChartSymbol") as IBarChartSymbol;
IChartSymbol pChartSymbol = pBarChartSymbol as IChartSymbol;
pChartSymbol.MaxValue = GetStaMaxMin(fields)[0];
pBarChartSymbol.Width = 8;
IMarkerSymbol pMarkerSymbol = pBarChartSymbol as IMarkerSymbol;
pMarkerSymbol.Size = 50;
//设置柱状图每列填充效果
ISymbolArray pSymbolArray = pBarChartSymbol as ISymbolArray;
Random ranColor = new Random();
for (int i = 0; i < fields.Length; i++)
{
IFillSymbol pFillSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as IFillSymbol;
pFillSymbol.Color = GetRGB(ranColor.Next(255), ranColor.Next(255), ranColor.Next(255));
pSymbolArray.AddSymbol((ISymbol)pFillSymbol);
}
//设置地图图层背景
ISimpleFillSymbol pFSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as SimpleFillSymbol;
pFSymbol.Color = GetRGB(239, 228, 249);
pChartRender.BaseSymbol = pFSymbol as ISymbol;
//应用柱状专题到指定图层
pChartRender.ChartSymbol = pBarChartSymbol as IChartSymbol;
pChartRender.Label = "Test";
pChartRender.UseOverposter = false;
pChartRender.CreateLegend();
pGeoFeatureLyer.Renderer = pChartRender as IFeatureRenderer;
//刷新地图显示图表及图例
pMap.Refresh();
pToc.Refresh();
pToc.ExpandDepth = 2;
}
catch (Exception ex)
{
throw new Exception("Fail to create bar chart!");
}
}
/// <summary>
/// 创建饼状专题地图
/// </summary>
/// <param name="layerID"></param>
/// <param name="fields"></param>
public void CreatePieRenderer(int layerID, string[] fields)
{
try
{
pServerContext = GetServetContext();
MapServer pMapServer = pMapResLocal.MapServer as MapServer;
IMapServerObjects pMapServerObj = pMapServer as IMapServerObjects;
string mapName = pMapServer.get_MapName(0);
ILayer pLayer = pMapServerObj.get_Layer(mapName, layerID);
pGeoFeatureLyer = pLayer as IGeoFeatureLayer;
//设置专题图元素的属性名称列表
IChartRenderer pChartRender = pServerContext.CreateObject("esriCarto.ChartRenderer") as IChartRenderer;
IRendererFields pRenderFields = pChartRender as IRendererFields;
foreach (string var in fields)
{
pRenderFields.AddField(var, var);
}
//实例化图表对象并取得元素指定属性的最大值
IPieChartSymbol pPieChartSymbol = pServerContext.CreateObject("esriDisplay.PieChartSymbol") as IPieChartSymbol;
IChartSymbol pChartSymbol = pPieChartSymbol as IChartSymbol;
pPieChartSymbol.Clockwise = true;
pPieChartSymbol.UseOutline = true;
pChartSymbol.MaxValue = GetStaMaxMin(fields)[0];
//设置饼图外围线
ISimpleLineSymbol pOutLine = pServerContext.CreateObject("esriDisplay.SimpleLineSymbol") as ISimpleLineSymbol;
pOutLine.Color = GetRGB(255, 0, 255);
pOutLine.Style = esriSimpleLineStyle.esriSLSSolid;
pOutLine.Width = 1;
pPieChartSymbol.Outline = pOutLine;
IMarkerSymbol pMarkerSymbol = pPieChartSymbol as IMarkerSymbol;
pMarkerSymbol.Size = 25;
//设置饼状图外围填充效果
ISymbolArray pSymbolArray = pPieChartSymbol as ISymbolArray;
ISimpleFillSymbol pFSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as SimpleFillSymbol;
pFSymbol.Color = GetRGB(213, 212, 252);
pFSymbol.Outline = pOutLine;
//设置饼状图每列填充效果
Random ranColor = new Random();
for (int i = 0; i < fields.Length; i++)
{
IFillSymbol pFillSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as IFillSymbol;
pFillSymbol.Color = GetRGB(ranColor.Next(255), ranColor.Next(255), ranColor.Next(255));
pSymbolArray.AddSymbol((ISymbol)pFillSymbol);
}
//设置地图图层背景
pFSymbol = pServerContext.CreateObject("esriDisplay.SimpleFillSymbol") as SimpleFillSymbol;
pFSymbol.Color = GetRGB(239, 228, 249);
pChartRender.BaseSymbol = pFSymbol as ISymbol;
//设置饼状图表属性
IPieChartRenderer pPieChartRenderer = pChartRender as IPieChartRenderer;
pPieChartRenderer.MinValue = 453588;
pPieChartRenderer.MinSize = 10;
pPieChartRenderer.FlanneryCompensation = false;
pPieChartRenderer.ProportionalBySum = true;
pPieChartRenderer.ProportionalField = fields[0];
pChartRender.ChartSymbol = pPieChartSymbol as IChartSymbol;
pChartRender.Label = "Population";
//应用饼状专题到指定图层
pChartRender.UseOverposter = false;
pChartRender.CreateLegend();
pGeoFeatureLyer.Renderer = pChartRender as IFeatureRenderer;
//刷新地图显示图表及图例
pMap.Refresh();
pToc.Refresh();
}
catch (Exception ex)
{
throw new Exception("Fail to create bar chart!");
}
}
/// <summary>
/// 计算元素指定属性的最大值
/// </summary>
/// <param name="fields">元素的属性名称列表</param>
/// <returns></returns>
double[] GetStaMaxMin(string[] fields)
{
double pMaxValue = 0;
double pMinValue = 0;
double pStaMax;
double pStaMin;
double[] PMaxMin = new double[2];
for (int i = 0; i < fields.Length; i++)
{
ICursor pCursor = pGeoFeatureLyer.Search(null, true) as ICursor;
IDataStatistics pDataSta = new DataStatisticsClass();
pDataSta.Cursor = pCursor;
pDataSta.Field = fields[i];
pStaMax = pDataSta.Statistics.Maximum;
pStaMin = pDataSta.Statistics.Minimum;
if (pMaxValue < pStaMax)
{
pMaxValue = pStaMax;
}
if (pMinValue > pStaMin)
{
pMinValue = pStaMin;
}
}
PMaxMin[0] = pMaxValue;
PMaxMin[1] = pMinValue;
return PMaxMin;
}
/// <summary>
/// 获取GRB颜色
/// </summary>
/// <param name="red"></param>
/// <param name="green"></param>
/// <param name="blue"></param>
/// <returns></returns>
IColor GetRGB(int red, int green, int blue)
{
IRgbColor rgbColor = pServerContext.CreateObject("esriDisplay.RGBColor") as IRgbColor;
IColor color = rgbColor as IColor;
rgbColor.Red = red;
rgbColor.Green = green;
rgbColor.Blue = blue;
return color;
}
}
以上代码也是来自社区里的代码,测试可以运行。
但是有个严重问题,
1、饼状图绘完后不能清除(除非重启服务)
2、不能再次设置新的render(出现ComException),尝试release com对象,出现com 与RCW不能分开的提示,也就是不能手动release com
这么多问题存在,这个chartRender基本还是不敢使用,大家研究研究。
分类:
ArcGIS Server
Post Comment
公告
本文转自
c.CreatePieRenderer(layerid, new string[] { "记录号"});还有这个"记录号"是什么?