注冊會員,創建你的web開發資料庫,以前用mapx,里面有專題圖,挺好。經過幾天的試驗,我把mapobjects中的各種渲染效果(類似mapx專題圖,但不一樣)都試了試,下面附上源代碼,如果那位有不懂的地方請自己琢磨,哈哈…
代碼如下:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace dbinmo
{
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class 渲染示例 : system.windows.forms.form
{
private axmapobjects2.axmap map;
private system.windows.forms.button 直方圖;
private system.windows.forms.button 餅圖;
private system.windows.forms.button 點密度;
private system.windows.forms.button 按值渲染;
private system.windows.forms.button 分類渲染;
private system.windows.forms.button z值渲染;
private system.windows.forms.button 組渲染;
private system.windows.forms.button 退出;
private system.windows.forms.panel 操作板;
private system.windows.forms.button 刪除所有渲染;
private system.windows.forms.panel panel1;
private system.windows.forms.button 全圖;
private system.windows.forms.button 拖動;
private system.windows.forms.button 縮小;
private system.windows.forms.button 放大;
private system.windows.forms.panel 分隔符;
private system.windows.forms.button 標注渲染;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private system.componentmodel.container components = null;
public 渲染示例()
{
//
// windows 窗體設計器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(渲染示例));
this.map = new axmapobjects2.axmap();
this.操作板 = new system.windows.forms.panel();
this.直方圖 = new system.windows.forms.button();
this.餅圖 = new system.windows.forms.button();
this.點密度 = new system.windows.forms.button();
this.按值渲染 = new system.windows.forms.button();
this.分類渲染 = new system.windows.forms.button();
this.z值渲染 = new system.windows.forms.button();
this.組渲染 = new system.windows.forms.button();
this.退出 = new system.windows.forms.button();
this.標注渲染 = new system.windows.forms.button();
this.刪除所有渲染 = new system.windows.forms.button();
this.panel1 = new system.windows.forms.panel();
this.全圖 = new system.windows.forms.button();
this.拖動 = new system.windows.forms.button();
this.縮小 = new system.windows.forms.button();
this.放大 = new system.windows.forms.button();
this.分隔符 = new system.windows.forms.panel();
((system.componentmodel.isupportinitialize)(this.map)).begininit();
this.操作板.suspendlayout();
this.suspendlayout();
//
// map
//
this.map.dock = system.windows.forms.dockstyle.fill;
this.map.location = new system.drawing.point(0, 0);
this.map.name = "map";
this.map.ocxstate = ((system.windows.forms.axhost.state)(resources.getobject("map.ocxstate")));
this.map.size = new system.drawing.size(616, 429);
this.map.tabindex = 0;
this.map.mousedownevent += new axmapobjects2._dmapevents_mousedowneventhandler(this.map_mousedownevent);
//
// 操作板
//
this.操作板.borderstyle = system.windows.forms.borderstyle.fixed3d;
this.操作板.controls.add(this.刪除所有渲染);
this.操作板.controls.add(this.panel1);
this.操作板.controls.add(this.全圖);
this.操作板.controls.add(this.拖動);
this.操作板.controls.add(this.縮小);
this.操作板.controls.add(this.放大);
this.操作板.controls.add(this.分隔符);
this.操作板.controls.add(this.標注渲染);
this.操作板.controls.add(this.退出);
this.操作板.controls.add(this.組渲染);
this.操作板.controls.add(this.z值渲染);
this.操作板.controls.add(this.分類渲染);
this.操作板.controls.add(this.按值渲染);
this.操作板.controls.add(this.點密度);
this.操作板.controls.add(this.餅圖);
this.操作板.controls.add(this.直方圖);
this.操作板.dock = system.windows.forms.dockstyle.right;
this.操作板.location = new system.drawing.point(496, 0);
this.操作板.name = "操作板";
this.操作板.size = new system.drawing.size(120, 429);
this.操作板.tabindex = 1;
//
// 直方圖
//
this.直方圖.dock = system.windows.forms.dockstyle.top;
this.直方圖.location = new system.drawing.point(0, 0);
this.直方圖.name = "直方圖";
this.直方圖.size = new system.drawing.size(116, 24);
this.直方圖.tabindex = 0;
this.直方圖.text = "直方圖";
this.直方圖.click += new system.eventhandler(this.直方圖_click);
//
// 餅圖
//
this.餅圖.dock = system.windows.forms.dockstyle.top;
this.餅圖.location = new system.drawing.point(0, 24);
this.餅圖.name = "餅圖";
this.餅圖.size = new system.drawing.size(116, 24);
this.餅圖.tabindex = 1;
this.餅圖.text = "餅圖";
this.餅圖.click += new system.eventhandler(this.餅圖_click);
//
// 點密度
//
this.點密度.dock = system.windows.forms.dockstyle.top;
this.點密度.location = new system.drawing.point(0, 48);
this.點密度.name = "點密度";
this.點密度.size = new system.drawing.size(116, 23);
this.點密度.tabindex = 2;
this.點密度.text = "點密度";
this.點密度.click += new system.eventhandler(this.點密度_click);
//
// 按值渲染
//
this.按值渲染.dock = system.windows.forms.dockstyle.top;
this.按值渲染.location = new system.drawing.point(0, 71);
this.按值渲染.name = "按值渲染";
this.按值渲染.size = new system.drawing.size(116, 23);
this.按值渲染.tabindex = 3;
this.按值渲染.text = "按值渲染";
this.按值渲染.click += new system.eventhandler(this.按值渲染_click);
//
// 分類渲染
//
this.分類渲染.dock = system.windows.forms.dockstyle.top;
this.分類渲染.location = new system.drawing.point(0, 94);
this.分類渲染.name = "分類渲染";
this.分類渲染.size = new system.drawing.size(116, 23);
this.分類渲染.tabindex = 4;
this.分類渲染.text = "分類渲染";
this.分類渲染.click += new system.eventhandler(this.分類渲染_click);
//
// z值渲染
//
this.z值渲染.dock = system.windows.forms.dockstyle.top;
this.z值渲染.location = new system.drawing.point(0, 117);
this.z值渲染.name = "z值渲染";
this.z值渲染.size = new system.drawing.size(116, 23);
this.z值渲染.tabindex = 5;
this.z值渲染.text = "z值渲染";
this.z值渲染.click += new system.eventhandler(this.z值渲染_click);
//
// 組渲染
//
this.組渲染.dock = system.windows.forms.dockstyle.top;
this.組渲染.location = new system.drawing.point(0, 140);
this.組渲染.name = "組渲染";
this.組渲染.size = new system.drawing.size(116, 23);
this.組渲染.tabindex = 6;
this.組渲染.text = "組渲染";
this.組渲染.click += new system.eventhandler(this.組渲染_click);
//
// 退出
//
this.退出.dock = system.windows.forms.dockstyle.bottom;
this.退出.location = new system.drawing.point(0, 402);
this.退出.name = "退出";
this.退出.size = new system.drawing.size(116, 23);
this.退出.tabindex = 7;
this.退出.text = "退出";
this.退出.click += new system.eventhandler(this.退出_click);
//
// 標注渲染
//
this.標注渲染.dock = system.windows.forms.dockstyle.top;
this.標注渲染.location = new system.drawing.point(0, 163);
this.標注渲染.name = "標注渲染";
this.標注渲染.size = new system.drawing.size(116, 23);
this.標注渲染.tabindex = 7;
this.標注渲染.text = "標注渲染";
this.標注渲染.click += new system.eventhandler(this.標注渲染_click);
//
// 刪除所有渲染
//
this.刪除所有渲染.dock = system.windows.forms.dockstyle.top;
this.刪除所有渲染.location = new system.drawing.point(0, 319);
this.刪除所有渲染.name = "刪除所有渲染";
this.刪除所有渲染.size = new system.drawing.size(116, 23);
this.刪除所有渲染.tabindex = 22;
this.刪除所有渲染.text = "刪除所有渲染";
this.刪除所有渲染.click += new system.eventhandler(this.刪除所有渲染_click);
//
// panel1
//
this.panel1.dock = system.windows.forms.dockstyle.top;
this.panel1.location = new system.drawing.point(0, 299);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(116, 20);
this.panel1.tabindex = 21;
//
// 全圖
//
this.全圖.dock = system.windows.forms.dockstyle.top;
this.全圖.location = new system.drawing.point(0, 276);
this.全圖.name = "全圖";
this.全圖.size = new system.drawing.size(116, 23);
this.全圖.tabindex = 20;
this.全圖.text = "全圖";
this.全圖.click += new system.eventhandler(this.全圖_click);
//
// 拖動
//
this.拖動.dock = system.windows.forms.dockstyle.top;
this.拖動.location = new system.drawing.point(0, 253);
this.拖動.name = "拖動";
this.拖動.size = new system.drawing.size(116, 23);
this.拖動.tabindex = 19;
this.拖動.text = "拖動";
this.拖動.click += new system.eventhandler(this.拖動_click);
//
// 縮小
//
this.縮小.dock = system.windows.forms.dockstyle.top;
this.縮小.location = new system.drawing.point(0, 230);
this.縮小.name = "縮小";
this.縮小.size = new system.drawing.size(116, 23);
this.縮小.tabindex = 18;
this.縮小.text = "縮小";
this.縮小.click += new system.eventhandler(this.縮小_click);
//
// 放大
//
this.放大.dock = system.windows.forms.dockstyle.top;
this.放大.location = new system.drawing.point(0, 207);
this.放大.name = "放大";
this.放大.size = new system.drawing.size(116, 23);
this.放大.tabindex = 17;
this.放大.text = "放大";
this.放大.click += new system.eventhandler(this.放大_click);
//
// 分隔符
//
this.分隔符.dock = system.windows.forms.dockstyle.top;
this.分隔符.location = new system.drawing.point(0, 186);
this.分隔符.name = "分隔符";
this.分隔符.size = new system.drawing.size(116, 21);
this.分隔符.tabindex = 16;
//
// 渲染示例
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(616, 429);
this.controls.add(this.操作板);
this.controls.add(this.map);
this.name = "渲染示例";
this.text = "渲染示例";
this.windowstate = system.windows.forms.formwindowstate.maximized;
((system.componentmodel.isupportinitialize)(this.map)).endinit();
this.操作板.resumelayout(false);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main()
{
application.run(new 渲染示例());
}
/// <summary>
/// 退出系統
/// </summary>
private void 退出_click(object sender, system.eventargs e)
{
this.close();
}
/// <summary>
/// 圖表渲染,值方圖
/// </summary>
private void 直方圖_click(object sender, system.eventargs e)
{
//直方圖的例子中使用lakers圖層
//
//定義圖標渲染變量
mapobjects2.chartrenderer cr = new mapobjects2.chartrendererclass();
//設置渲染類型為直方圖
cr.charttype = mapobjects2.charttypeconstants.mobar;
//設置直方圖顯示的字段個數為兩個,就是直方圖顯示兩個柱子
cr.fieldcount = 2;
//設置第一個字段,請參看lakes.dbf
cr.set_field(0,"surf_elev");
//設置這個直方圖的柱子顏色為紅色
cr.set_color(0,(uint)mapobjects2.colorconstants.mored);
//設置第二個柱子
cr.set_field(1,"depth");
cr.set_color(1,(uint)mapobjects2.colorconstants.mogreen);
mapobjects2.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("lakes"));
lyr.renderer = cr;
//使用refresh()刷新老也是刷新不好,正沒辦法。。。,設為全圖倒是不錯的注意,^_^
map.refresh();
}
/// <summary>
/// 全圖視野
/// </summary>
private void 全圖_click(object sender, system.eventargs e)
{
map.extent = map.fullextent;
}
/// <summary>
/// 拖動,只設置圖標
/// </summary>
private void 拖動_click(object sender, system.eventargs e)
{
map.mousepointer = mapobjects2.mousepointerconstants.mopan;
}
/// <summary>
/// 縮小,只設置圖標
/// </summary>
private void 縮小_click(object sender, system.eventargs e)
{
map.mousepointer = mapobjects2.mousepointerconstants.mozoomout;
}
/// <summary>
/// 放大,只設置圖標
/// </summary>
private void 放大_click(object sender, system.eventargs e)
{
map.mousepointer = mapobjects2.mousepointerconstants.mozoomin;
}
/// <summary>
/// 鼠標按下時間,處理地圖放大、縮小、拖動等。
/// </summary>
private void map_mousedownevent(object sender, axmapobjects2._dmapevents_mousedownevent e)
{
mapobjects2.rectangle rect;
mapobjects2.point pt = map.tomappoint(e.x,e.y);
if(e.button == 2)//右鍵點擊取消
{
map.mousepointer = mapobjects2.mousepointerconstants.modefault;
}
if(map.mousepointer == mapobjects2.mousepointerconstants.mopan)//拖動
{
map.pan();
}
else if(map.mousepointer == mapobjects2.mousepointerconstants.mozoomin)//放大
{
rect = map.trackrectangle();
if(rect == null|| (rect.width < 0.00005) || (rect.height < 0.00005))
{
rect = map.extent;
rect.scalerectangle(0.6667);
rect.offset(-(rect.center.x - pt.x),-(rect.center.y - pt.y));
}
map.extent = rect;
}
else if(map.mousepointer == mapobjects2.mousepointerconstants.mozoomout)//縮小
{
rect = map.trackrectangle();
if ((null == rect) || (rect.width < 0.00005) || (rect.height < 0.00005))
{
rect = map.extent;
rect.scalerectangle(1.5);
rect.offset(-(rect.center.x - pt.x),-(rect.center.y - pt.y));
}
else
{
double drate = map.extent.width / rect.width * 10;
rect.scalerectangle(drate);
}
map.extent = rect;
}
}
/// <summary>
/// 圖表渲染,使用餅圖
/// </summary>
private void 餅圖_click(object sender, system.eventargs e)
{
//餅圖的例子中使用lakers圖層
//過程和直方圖一樣
//
//定義圖標渲染變量
mapobjects2.chartrenderer cr = new mapobjects2.chartrendererclass();
//設置渲染類型為餅圖
cr.charttype = mapobjects2.charttypeconstants.mopie;
//設置餅圖顯示的字段個數為兩個,就是餅圖顯示兩半
cr.fieldcount = 2;
//設置第一個字段,請參看lakes.dbf
cr.set_field(0,"surf_elev");
//設置這個餅圖的一半顏色為紅色
cr.set_color(0,(uint)mapobjects2.colorconstants.mored);
//設置第二半
cr.set_field(1,"depth");
cr.set_color(1,(uint)mapobjects2.colorconstants.mogreen);
//添加到地圖上
mapobjects2.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("lakes"));
lyr.renderer = cr;
//刷新
map.refresh();
}
/// <summary>
/// 點密度渲染
/// </summary>
private void 點密度_click(object sender, system.eventargs e)
{
//點密度的例子中使用lakes圖層
//
//定義點密度渲染變量
mapobjects2.dotdensityrenderer ddr = new mapobjects2.dotdensityrendererclass();
//設置渲染的數據字段,請參看lakes.dbf
ddr.field = "depth";
//點大小
ddr.dotsize = 3;
//點顏色
ddr.dotcolor = (uint)mapobjects2.colorconstants.moblue;
ddr.dotvalue = 1;
ddr.drawbackground = true;
//添加到地圖上
mapobjects2.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("lakes"));
lyr.renderer = ddr;
//刷新
map.refresh();
}
/// <summary>
/// 按值渲染
/// </summary>
private void 按值渲染_click(object sender, system.eventargs e)
{
//按值渲染的例子中使用country圖層,這里利用按值渲染,將每個國家用不同的顏色顯示
//
//字符串s變量,很像delphi中的tstringlist,^_^
mapobjects2.strings strs = new mapobjects2.stringsclass();
//圖層變量,引用country圖層
mapobjects2.maplayer lyr = (mapobjects2.maplayer)map.layers.item("country");
//recordset對象,我將在以后的文章中陸續講解關于mapobjects2.2中使用數據集變量的內容!!!
//使用方法大致與vb中對數據表的操作差不多。
mapobjects2.recordset rc = lyr.records;
//這個循環獲取所有國家的簡稱代碼列表
while(!rc.eof)
{
strs.add(rc.fields.item("fips_code").valueasstring);
rc.movenext();
}
//定義按值渲染對象
mapobjects2.valuemaprenderer vmr = new mapobjects2.valuemaprendererclass();
//將其添加到圖層
lyr.renderer = vmr;
//設置渲染使用的字段,請參看country.dbf文件
vmr.field = "fips_code";
//渲染的個數
vmr.valuecount = strs.count;
//缺省值
vmr.usedefault = true;
mapobjects2.symbol sym = vmr.defaultsymbol;
sym.color = (uint)mapobjects2.colorconstants.moyellow;//缺省顏色為黃色
//渲染過程
for(int i=0;i<vmr.valuecount;i++)
{
vmr.set_value((short)i,strs.item((short)i));
}
//刷新
map.refresh();
}
/// <summary>
/// 分類渲染
/// </summary>
private void 分類渲染_click(object sender, system.eventargs e)
{
//分類渲染的例子使用country圖層
//
//圖層變量引用cities層
mapobjects2.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("cities"));
//定義分類渲染變量
mapobjects2.classbreaksrenderer cbr = new mapobjects2.classbreaksrendererclass();
//添加到地圖
lyr.renderer = cbr;
//設置符號類型為點,也可使用其他類型,請自己試驗,方法大概都差不多
cbr.symboltype = mapobjects2.symboltypeconstants.mopointsymbol;
//設置字段,請參看cities.dbf文件
cbr.field = "population";
//statistics對象經常配合分類渲染使用,他表示用一個recordset對象的calculatestatistics方法
// 對一個數值字段的統計計算結果
mapobjects2.statistics stat = lyr.records.calculatestatistics("population");
//mean——平均值,stddev——均方差
double fbv = stat.mean - (stat.stddev * 3);
for(int i=0;i<6;i++)
{
if((fbv >= stat.min)&&fbv <= stat.max)
{
//分類個數,依次加一
cbr.breakcount = (short)(cbr.breakcount + 1);
//設置分類
cbr.set_break((short)(cbr.breakcount - 1),fbv);
}
fbv += stat.stddev;
}
//sizesymbols方法制定頭尾的大小用來表現點特征大小漸進效果
cbr.sizesymbols(3,8);
//rampcolors方法設置顏色從白色到紅色的漸進顯示
cbr.rampcolors((uint)mapobjects2.colorconstants.mowhite,(uint)mapobjects2.colorconstants.mored);
//刷新
map.refresh();
}
/// <summary>
/// z值渲染
/// </summary>
private void z值渲染_click(object sender, system.eventargs e)
{
//本來想好好搞搞,可是我找不到帶有z值的地圖,哈哈,認了吧,下面給出基本的語句,有興趣的
//兄弟姐妹們可以自己試試
// mapobjects2.zrenderer zr = new mapobjects2.zrendererclass();
// //z值分成幾部分?
// zr.breakcount = 2;
// //設置各個部分的分界線
// zr.set_break(0,100);
// zr.set_break(1,1000);
// //設置特征類型
// zr.symboltype = mapobjects2.symboltypeconstants.mopointsymbol;
// mapobjects2.symbol sym;
// for(short i=0;i<zr.breakcount;i++)
// {
// //獲取特征
// sym = zr.get_symbol(i);
// //設置特征屬性,定義一個顏色數組,將不同特征設置不同顏色,效果會明顯些
// sym.color = (uint)mapobjects2.colorconstants.mored;
// sym.size = 6;
// sym.style = mapobjects2.markerstyleconstants.motrianglemarker;
// }
// //添加到地圖
// mapobjects2.maplayer lyr = (mapobjects2.maplayer)map.layers.item("帶有z值的圖層名稱");
// lyr.renderer = zr;
// //刷新
// map.refresh();
}
/// <summary>
/// 組渲染
/// </summary>
private void 組渲染_click(object sender, system.eventargs e)
{
//組渲染其實就是組合渲染,一般定義多個渲染方式,然后使用如下的語句進行組合
mapobjects2.maplayer lyr = (mapobjects2.maplayer)map.layers.item("country");
//標注渲染
mapobjects2.labelrenderer lr = new mapobjects2.labelrendererclass();
lr.field = "name";
lr.get_symbol(0).color = (uint)mapobjects2.colorconstants.moblack;
lr.get_symbol(0).font.size = 8;
//按值渲染
mapobjects2.strings strs = new mapobjects2.stringsclass();
mapobjects2.recordset rc = lyr.records;
while(!rc.eof)
{
strs.add(rc.fields.item("fips_code").valueasstring);
rc.movenext();
}
mapobjects2.valuemaprenderer vmr = new mapobjects2.valuemaprendererclass();
vmr.field = "fips_code";
vmr.valuecount = strs.count;
vmr.usedefault = true;
mapobjects2.symbol sym = vmr.defaultsymbol;
sym.color = (uint)mapobjects2.colorconstants.moyellow;
for(int i=0;i<vmr.valuecount;i++)
{
vmr.set_value((short)i,strs.item((short)i));
}
//將二者組合,添加到圖層
mapobjects2.grouprenderer gr = new mapobjects2.grouprendererclass();
gr.add(vmr);
gr.add(lr);
lyr.renderer = gr;
map.refresh();
}
/// <summary>
/// 刪除所有渲染
/// </summary>
private void 刪除所有渲染_click(object sender, system.eventargs e)
{
mapobjects2.maplayer lyr;
for(int i=0;i<map.layers.count;i++)
{
lyr = (mapobjects2.maplayer)map.layers.item(i);
lyr.renderer = new mapobjects2.labelrendererclass();
map.refresh();
}
}
/// <summary>
/// 標注渲染
/// </summary>
private void 標注渲染_click(object sender, system.eventargs e)
{
mapobjects2.maplayer lyr = (mapobjects2.maplayer)map.layers.item("cities");
mapobjects2.labelrenderer lr = new mapobjects2.labelrendererclass();
lr.field = "name";
lr.get_symbol(0).color = (uint)mapobjects2.colorconstants.moblack;
lr.get_symbol(0).font.size = 8;
lyr.renderer = lr;
map.refresh();
}
}
}