//获取颜色对象
static public IRgbColor getRGB(int r, int g, int b)
{
IRgbColor pColor;
pColor = new RgbColorClass();
pColor.Red = r;
pColor.Green = g;
pColor.Blue = b;
return pColor;
}
static public IGeoFeatureLayer GetLevelMap(IGeoFeatureLayer geoFeatureLayer, string field, int classCount)
{
ITableHistogram tableHistogram;
IBasicHistogram basicHistogram;
ITable table;
ILayer layer = geoFeatureLayer as ILayer;
table = layer as ITable;
tableHistogram = new BasicTableHistogramClass();
//按照 数值字段分级
tableHistogram.Table = table;
tableHistogram.Field = field;
basicHistogram = tableHistogram as IBasicHistogram;
object values;
object frequencys;
//先统计每个值和各个值出现的次数
basicHistogram.GetHistogram(out values, out frequencys);
//创建平均分级对象
IClassifyGEN classifyGEN = new QuantileClass();
//用统计结果进行分级 ,级别数目为classCount
classifyGEN.Classify(values, frequencys, ref classCount);
//获得分级结果,是个 双精度类型数组
double[] classes;
classes = classifyGEN.ClassBreaks as double[];
IEnumColors enumColors = CreateAlgorithmicColorRamp(classes.Length).Colors;
IColor color;
IClassBreaksRenderer classBreaksRenderer = new ClassBreaksRendererClass();
classBreaksRenderer.Field = field;
classBreaksRenderer.BreakCount = classCount;
classBreaksRenderer.SortClassesAscending = true;
ISimpleFillSymbol simpleFillSymbol;
for (int i = 0; i < classes.Length - 1; i++)
{
color = enumColors.Next();
simpleFillSymbol = new SimpleFillSymbolClass();
simpleFillSymbol.Color = color;
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
classBreaksRenderer.set_Symbol(i, simpleFillSymbol as ISymbol);
classBreaksRenderer.set_Break(i, classes[i + 1]);
//构造显示的文字
classBreaksRenderer.set_Label(i, classes[i].ToString() + "——" + classes[i + 1].ToString());
classBreaksRenderer.set_Description(i, "sfhskdfks");
//着色对象的断点
}
if (geoFeatureLayer != null)
{
geoFeatureLayer.Renderer = classBreaksRenderer as IFeatureRenderer;
}
return geoFeatureLayer;
}
static public IGeoFeatureLayer GetChartMap(IGeoFeatureLayer geofeatureLayer, List<string> fields, List<string> alias)
{
// IGeoFeatureLayer geofeatureLayer = Lib.Layer.getShpLayer(path, layerName) as IGeoFeatureLayer;
ITable table = geofeatureLayer as ITable;
geofeatureLayer.ScaleSymbols = true;
IChartRenderer chartRenderer = new ChartRendererClass();
IRendererFields rendererFields = chartRenderer as IRendererFields;
for (int i = 0; i < fields.Count; i++)
{
rendererFields.AddField(fields[i], alias[i]);
}
double maxValue = 0;
ICursor cursor = table.Search(null, true);
IRowBuffer rowBuffer = cursor.NextRow();
while (rowBuffer != null)
{
for (int i = 0; i < fields.Count; i++)
{
double fieldValue;
try
{
fieldValue = double.Parse(rowBuffer.get_Value(table.FindField(fields[i])).ToString());
}
catch (Exception ex)
{
fieldValue = 0;
}
maxValue = Math.Max(maxValue, fieldValue);
}
rowBuffer = cursor.NextRow();
}
IBarChartSymbol barChartSymbol = new BarChartSymbolClass();
barChartSymbol.Width = 5;
IMarkerSymbol markerSymbol = barChartSymbol as IMarkerSymbol;
markerSymbol.Size = 50;
IChartSymbol chartSymbol = barChartSymbol as IChartSymbol;
chartSymbol.MaxValue = maxValue;
//添加渲染符号
ISymbolArray symbolArray = barChartSymbol as ISymbolArray;
IFillSymbol fillSymbol;
IColorRamp colors = CreateRandomColorRamp(fields.Count);
for (int i = 0; i < fields.Count; i++)
{
fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = colors.get_Color(i);
symbolArray.AddSymbol(fillSymbol as ISymbol);
}
//设置柱状图符号
chartRenderer.ChartSymbol = barChartSymbol as IChartSymbol;
fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = getRGB(0, 255, 255);
fillSymbol.Color.Transparency = 2;
chartRenderer.BaseSymbol = fillSymbol as ISymbol;
chartRenderer.UseOverposter = false;
//创建图例
chartRenderer.CreateLegend();
geofeatureLayer.Renderer = chartRenderer as IFeatureRenderer;
return geofeatureLayer;
}
////生成颜色带
static public IColorRamp CreateAlgorithmicColorRamp(int count)
{
//IUniqueValueRenderer pUniqueValueR;
IEnumColors pEnumRamp;
AlgorithmicColorRamp pColorRamp;
//pUniqueValueR = new UniqueValueRendererClass();
//pUniqueValueR.FieldCount = 1;
//pUniqueValueR.set_Field(0, FielName);
pColorRamp = new AlgorithmicColorRampClass();
pColorRamp.FromColor = getRGB(255, 255, 255);
pColorRamp.ToColor = getRGB(32, 200, 150);
pColorRamp.Size = count;
bool ok = true;
pColorRamp.CreateRamp(out ok);
pEnumRamp = pColorRamp.Colors;
return pColorRamp;
}
//生成颜色带
static public IColorRamp CreateRandomColorRamp(int count)
{
//IUniqueValueRenderer pUniqueValueR;
IEnumColors pEnumRamp;
IRandomColorRamp pColorRamp;
//pUniqueValueR = new UniqueValueRendererClass();
//pUniqueValueR.FieldCount = 1;
//pUniqueValueR.set_Field(0, FielName);
pColorRamp = new RandomColorRampClass();
pColorRamp.StartHue = 0;
pColorRamp.MinValue = 99;
pColorRamp.MinSaturation = 15;
pColorRamp.EndHue = 360;
pColorRamp.MaxValue = 100;
pColorRamp.MaxSaturation = 30;
pColorRamp.Size = count * 2;
bool ok = true;
pColorRamp.CreateRamp(out ok);
pEnumRamp = pColorRamp.Colors;
return pColorRamp;
}
static public IGeoFeatureLayer GetSimpleSymbolLayer(IGeoFeatureLayer geoFeatureLayer, string field)
{
// getGeoLayer("sichuan_county_landuse");
IUniqueValueRenderer uniqueValueRenderer = new UniqueValueRendererClass();
uniqueValueRenderer.FieldCount = 1;
uniqueValueRenderer.set_Field(0, field);
//简单填充符号
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
int areaNum = geoFeatureLayer.FeatureClass.FeatureCount(null);
IFeatureCursor featureCursor = geoFeatureLayer.FeatureClass.Search(null, false);
IFeature feature;
if (featureCursor != null)
{
IEnumColors enumColors = CreateRandomColorRamp(areaNum).Colors;
int fieldIndex = geoFeatureLayer.FeatureClass.Fields.FindField(field);
feature = featureCursor.NextFeature();
while (feature != null)
{
string nameValue = feature.get_Value(fieldIndex).ToString();
simpleFillSymbol = new SimpleFillSymbolClass();
simpleFillSymbol.Color = enumColors.Next();
uniqueValueRenderer.AddValue(nameValue, field, simpleFillSymbol as ISymbol);
feature = featureCursor.NextFeature();
}
}
geoFeatureLayer.Renderer = uniqueValueRenderer as IFeatureRenderer;
return geoFeatureLayer;
}