1,利用combobox创建色带,首先draw
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle rec= e.Bounds;
rec.Width = rec.Width /array.Length;
string fromColor = comboBox1.Items[e.Index].ToString().Split('|')[0];
string ToColor = comboBox1.Items[e.Index].ToString().Split('|')[1];
_FromColor = Color.FromName(fromColor);
_ToColor = Color.FromName(ToColor);
IAlgorithmicColorRamp ramp = new AlgorithmicColorRampClass();//创建色带,也可以创建渐变色带用LinearGradientBrush类
ramp.FromColor = ColorToIColor(_FromColor);
ramp.ToColor = ColorToIColor(_ToColor);
ramp.Size =array.Length ;
bool ok=true;
ramp.CreateRamp(out ok);
IEnumColors EnumColor = ramp.Colors;
IColor color = null;
SolidBrush sb=null;
for (int i = 0; i < array.Length; i++)
{
color = EnumColor.Next();
sb=new SolidBrush(IColorToColor(color));
e.Graphics.FillRectangle(sb, rec);
rec.Offset(rec.Width, 0);//对小矩形进行偏移
}
}
2.在Load中
comboBox1.DrawMode = DrawMode.OwnerDrawFixed; //使draw_Item有效
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.Clear();
comboBox1.BeginUpdate();
foreach (string oneColor in colorList)
{
comboBox1.Items.Add(oneColor);
}
comboBox1.EndUpdate();
3、定义颜色,色带的起始与终止色彩
private static string[] colorList = { "Black|White", "White|Black", "Red|Blue","Green|Black" };
4、根据获取的字段值,对每一个值进行渲染
首先获取字段值(不重复)
private double[] array;
private void button1_Click(object sender, EventArgs e)
{
string layername = comboBox2.SelectedItem.ToString();
string fieldname = comboBox3.SelectedItem.ToString();
SelectLayer sl = new SelectLayer(mapControl);
IFeatureLayer layer= sl.GetLayerByName(layername) as IFeatureLayer;
ITableHistogram histogram = new BasicTableHistogramClass();
histogram.Field = fieldname;
histogram.Table = layer as ITable;
IBasicHistogram BasicH = histogram as IBasicHistogram;
object value=null;
object frequece=null;
BasicH.GetHistogram(out value, out frequece);
array = value as double[];
for (int i = 0; i < array.Length; i++)
{
listView1.Items.Add(array[i].ToString());
}
}
最后进行渲染:
private void button2_Click(object sender, EventArgs e)
{
SelectLayer sl = new SelectLayer(mapControl);
IGeoFeatureLayer layer = sl.GetLayerByName(comboBox2.SelectedItem.ToString()) as IGeoFeatureLayer;
string fieldname = comboBox3.SelectedItem.ToString();
string item = comboBox1.SelectedItem.ToString();
string beginColor = item.Split('|')[0];
string finishColor = item.Split('|')[1];
Color _beginColor = Color.FromName(beginColor);
Color _finishColor = Color.FromName(finishColor);
IAlgorithmicColorRamp ramp = new AlgorithmicColorRampClass();
ramp.FromColor = ColorToIColor(_beginColor);
ramp.ToColor = ColorToIColor(_finishColor);
ramp.Size = array.Length;
bool ok = true;
ramp.CreateRamp(out ok);
IEnumColors enumColor = ramp.Colors;
IColor color = null;
ISimpleFillSymbol fillSymbol = null;
IUniqueValueRenderer valueRender = new UniqueValueRendererClass();
valueRender.FieldCount = 1;
valueRender.set_Field(0, fieldname);
for (int i = 0; i < array.Length; i++)
{
color = enumColor.Next();
fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = color;
valueRender.AddValue(array[i].ToString(), fieldname, fillSymbol as ISymbol);
}
MessageBox.Show("成功!");
layer.Renderer = valueRender as IFeatureRenderer;
mapControl.Refresh();
}
最后渲染结果如上图。可能方法比较笨拙,代码重复较多,只是限于水平有限,并是自己所想出来的结果,所以和各位Giser一起分享。