在界面中拖入一个ChartControl控件,然后添加一个simplebutton控件。在simplebutton控件的click事件中加入如下代码:
private void button1_Click(object sender, EventArgs e)
{
//数据源
chartControl1.DataSource = CreateChartData();
//数据成员
chartControl1.SeriesDataMember = "DWMC";
//下面显示
chartControl1.SeriesTemplate.ArgumentDataMember = "DWMC";
chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] {"Value"});
chartControl1.SeriesTemplate.View = new StackedBarSeriesView();
chartControl1.Titles.Add(new ChartTitle());
chartControl1.Titles[0].Text = "道路类型统计图";
}
private DataTable CreateChartData()
{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();//此处打开的shp 格式的数据
//IWorkspaceFactory pWorkspaceFactory = new OleDbFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(@"D:\我的文档\道路边线", 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
int ZCityRoadNum = 0, CCityRoadNum = 0, NRoadNum = 0, DXBGNum = 0, NullNum = 0, CDNum = 0;//城市道路(主干道)、城市道路(次干道)、内部道路、单线标准轨、空、车档
if (pFeatureWorkspace != null)
{
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass("道路边线");
IQueryFilter filter = new QueryFilterClass();
filter.WhereClause = null;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(filter,false);
IFeature pFeature = pFeatureCursor.NextFeature();
string value = null;
while (pFeature != null)
{
int index = pFeatureClass.FindField("DWMC");
if (index != -1)
{
value = pFeature.get_Value(index).ToString();
}
if (value.Trim() == "城市道路(主干道)")
{
ZCityRoadNum++;
}
if (value.Trim() == "城市道路(次干道)")
{
CCityRoadNum++;
}
if (value.Trim() == "内部道路")
{
NRoadNum++;
}
if (value.Trim() == "单线标准轨")
{
DXBGNum++;
}
if (value.Trim() == "车挡")
{
CDNum++;
}
else
{
NullNum++;
}
pFeature = pFeatureCursor.NextFeature();
}
}
//建一个空表
DataTable table = new DataTable("DLBX_DWMC");
//列
table.Columns.Add("DWMC",typeof(string));
table.Columns.Add("Value",typeof(Decimal));
//行
table.Rows.Add(new object[]{"城市主干道",ZCityRoadNum});
table.Rows.Add(new object[]{"城市次干道",CCityRoadNum});
table.Rows.Add(new object[]{"内部道路", NRoadNum });
table.Rows.Add(new object[]{"单线标准轨",DXBGNum });
table.Rows.Add(new object[]{"车挡",CDNum});
table.Rows.Add(new object[]{"无",NullNum});
return table;
}
}