//获取当前图层,并把它设置成IGeoFeatureLayer的实例
IMap pMap = axMapControl1.Map;
ILayer pLayer = pMap.get_Layer(0) as IFeatureLayer;
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer;
pGeoFeatureLayer.ScaleSymbols = false ;
//获取图层上的feature
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
//定义柱状图渲染组建对象
IChartRenderer pChartRenderer = new ChartRendererClass();
//定义渲染字段对象并给字段对象实例化为pChartRenderer
IRendererFields pRendererFields;
pRendererFields = (IRendererFields)pChartRenderer;
//向渲染字段对象中添加字段--- 待补充自定义添加
pRendererFields.AddField("SHAPE_Area", "SHAPE_Area");
pRendererFields.AddField("OBJECTID", "OBJECTID");
// 通过查找features的所用字段的值,计算出数据字段的最大值,作为设置柱状图的比例大小的依据
ITable pTable;
int fieldNumber;
pTable = pFeatureLayer as ITable;
// 查找出geoFeatureLayer的属性表中的字段个数
fieldNumber = pTable.FindField("SHAPE_Area");
if (fieldNumber == -1)
{
MessageBox.Show("Can't find field called ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
const int numFields = 2;// 设置bars的个数
int[] fieldIndecies = new int[2];
//long fieldIndex;
double maxValue;
bool firstValue;
//double[] fieldValue=new double[5];
double fieldValue;
fieldIndecies[0] = pTable.FindField("SHAPE_Area");
fieldIndecies[1] = pTable.FindField("OBJECTID");
firstValue = true;
maxValue = 0;
int n = pFeatureClass.FeatureCount(null);
for (int i = 0; i < numFields; i++)
{
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
for (int j = 0; j < n; j++)
{
IFeature pFeature = pFeatureCursor.NextFeature();
fieldValue = Convert.ToDouble(pFeature.get_Value(fieldIndecies[i]));
if (firstValue)
{
//给maxValue赋初值
maxValue = fieldValue;
firstValue = false;
}
else if (fieldValue > maxValue)
{
maxValue = fieldValue;
}
}
}
if (maxValue <= 0)
{
MessageBox.Show("Failed to calculate the maximum value or maxvalue is 0.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//创建累积排列符号
IStackedChartSymbol stackedChartSymbol = new StackedChartSymbolClass();
stackedChartSymbol.Width = 10;
//stackedChartSymbol.UseOutline = true;
IMarkerSymbol pMarkerSymbol = stackedChartSymbol as IMarkerSymbol;
pMarkerSymbol.Size = 50;
IChartSymbol pChartSymbol = stackedChartSymbol as IChartSymbol;
pChartSymbol.MaxValue = maxValue;
//添加渲染符号
ISymbolArray pSymbolArray = stackedChartSymbol as ISymbolArray;
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = getRGB(255, 0, 0);
pSymbolArray.AddSymbol(pFillSymbol as ISymbol );
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = getRGB(0, 255, 0);
pSymbolArray.AddSymbol(pFillSymbol as ISymbol);
//设置柱状图符号
pChartRenderer.ChartSymbol = stackedChartSymbol as IChartSymbol;
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = getRGB(0, 0,255);
pChartRenderer.BaseSymbol = pFillSymbol as ISymbol;
pChartRenderer.UseOverposter = false ;
//创建图例
pChartRenderer.CreateLegend();
pGeoFeatureLayer.Renderer = pChartRenderer as IFeatureRenderer;
axMapControl1.Refresh();