//用来判断所选择的行政区级别
//0全国1省2市3县
public int qssx = 0;
private void button1_Click(object sender, EventArgs e)
{
System.Windows.MessageBox.Show("数据这个在加载,请稍候......");
//新建一个QueryTask
QueryTask queryTask = new QueryTask("http://********.155:6080/arcgis/rest/services/20150507newlocustarea/MapServer/0");
// Query对象
Query query = new Query();
if (comboBox32.SelectedIndex == 0 && comboBox30.SelectedIndex == 0)
{
//System.Windows.MessageBox.Show("请选择省");
query.Where = "NAME_1 = NAME_1 ";
qssx = 0;
}
if (comboBox32.SelectedIndex != 0 && comboBox30.SelectedIndex == 0 && comboBox29.SelectedIndex ==0)
{
query.Where = "NAME_1 ='" + comboBox32.SelectedItem.ToString() + "'";
qssx = 1;
}
if (comboBox32.SelectedIndex != 0 && comboBox30.SelectedIndex != 0 && comboBox29.SelectedIndex == 0)
{
query.Where = "NAME_12 ='" + comboBox30.SelectedItem.ToString() + "'";
qssx = 2;
}
if (comboBox32.SelectedIndex != 0 && comboBox30.SelectedIndex != 0 && comboBox29.SelectedIndex != 0)
{
query.Where = "NAME_12_13 ='" + comboBox29.SelectedItem.ToString() + "'";
qssx = 3;
}
query.OutFields.Add("*");
//异步查询,需要绑定queryTask的两个事件,通过ExecuteCompleted得到查询结果
queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
queryTask.Failed += QueryTask_Failed;
queryTask.ExecuteAsync(query);
//同步查询,不需要绑定事件,直接返回查询结果
//FeatureSet featureSet = queryTask.Execute(query);
if (comboBox13.SelectedItem.ToString() != "柱状图")
{
System.Windows.MessageBox.Show("请选择柱状图,饼状图尚未实现!");
return;
}
}
void QueryTask_ExecuteCompleted(object sender, QueryEventArgs e)
{
FeatureSet featureSet = e.FeatureSet;
System.Data.DataTable dt = new System.Data.DataTable();
chart2.Show();
label12.Visible = true;
label16.Visible = true;
//当选择为全国时
if (qssx == 0)
{
var result = featureSet.GroupBy(a => a.Attributes["NAME_1"])
.Select(group => new
{
Name = group.Key,
Count = group.Count(),
Area = group.Sum(x => Convert.ToDecimal(x.Attributes["面积"]))
});
if (featureSet != null && featureSet.Features.Count > 0)
{
DataColumn dc1 = new DataColumn("省份");//2
dt.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("蝗区总个数");
dt.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("蝗区总面积");
dt.Columns.Add(dc3);
int i1 = 0;
foreach (var groap in result)
{
DataRow newrow = dt.NewRow();
newrow[0] = groap.Name.ToString();
newrow[1] = groap.Count.ToString();
newrow[2] = groap.Area.ToString();
dt.Rows.Add(newrow);
i1++;
}
dataGridView2.DataSource = dt.DefaultView;
dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
label17.Text = dataGridView2.Rows.Count.ToString();
label14.Text = featureSet.Sum(x => Convert.ToDecimal(x.Attributes["面积"])).ToString("0.00");
label17.Visible = true;
label14.Visible = true;
}
chart2.ChartAreas[0].BackColor = Color.AliceBlue;
chart2.Legends[0].Enabled = true;
DataTable dtchart = default(DataTable);
dtchart = Datatable.CreateDataTable("省份", "蝗区总个数", 0, 1, dataGridView2);
chart2.DataSource = dtchart;
chart2.Series[0].YValueMembers = "蝗区总个数";
chart2.Series[0].XValueMember = "省份";
chart2.Series[0].Legend = "Legend1";
chart2.Series[0].LegendText = "蝗区总个数";
chart2.ChartAreas[0].AxisX.Interval = 1; //设置X轴坐标的间隔为1
chart2.ChartAreas[0].AxisX.IntervalOffset = 1; //设置X轴坐标偏移为1
chart2.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false; //设置是否交错显示,比如数据多的时间分成两行来显示
chart2.ChartAreas[0].AxisY.Title = "个";
chart2.DataBind();
}
if (qssx == 1)
{
//当选择为省时
var result = featureSet.GroupBy(a => a.Attributes["NAME_12"])
.Select(group => new
{
Name = group.Key,
Count = group.Count(),
Area = group.Sum(x => Convert.ToDecimal(x.Attributes["面积"]))
});
if (featureSet != null && featureSet.Features.Count > 0)
{
DataColumn dc1 = new DataColumn("市");//2
dt.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("蝗区总个数");
dt.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("蝗区总面积");
dt.Columns.Add(dc3);
int i1 = 0;
foreach (var groap in result)
{
DataRow newrow = dt.NewRow();
newrow[0] = groap.Name.ToString();
newrow[1] = groap.Count.ToString();
newrow[2] = groap.Area.ToString();
dt.Rows.Add(newrow);
i1++;
}
dataGridView2.DataSource = dt.DefaultView;
dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
label17.Text = dataGridView2.Rows.Count.ToString();
label14.Text = featureSet.Sum(x => Convert.ToDecimal(x.Attributes["面积"])).ToString("0.00");
label17.Visible = true;
label14.Visible = true;
}
chart2.ChartAreas[0].BackColor = Color.AliceBlue;
chart2.Legends[0].Enabled = true;
DataTable dtchart = default(DataTable);
dtchart = Datatable.CreateDataTable("市", "蝗区总个数", 0, 1, dataGridView2);
chart2.DataSource = dtchart;
chart2.Series[0].YValueMembers = "蝗区总个数";
chart2.Series[0].XValueMember = "市";
chart2.Series[0].Legend = "Legend1";
chart2.Series[0].LegendText = "蝗区总个数";
chart2.ChartAreas[0].AxisX.Interval = 1; //设置X轴坐标的间隔为1
chart2.ChartAreas[0].AxisX.IntervalOffset = 1; //设置X轴坐标偏移为1
chart2.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false; //设置是否交错显示,比如数据多的时间分成两行来显示
chart2.ChartAreas[0].AxisY.Title = "个";
chart2.DataBind();
}
if (qssx == 2)
{
//当选择为市时
var result = featureSet.GroupBy(a => a.Attributes["NAME_12_13"])
.Select(group => new
{
Name = group.Key,
Count = group.Count(),
Area = group.Sum(x => Convert.ToDecimal(x.Attributes["面积"]))
});
if (featureSet != null && featureSet.Features.Count > 0)
{
DataColumn dc1 = new DataColumn("县");//2
dt.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("蝗区总个数");
dt.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("蝗区总面积");
dt.Columns.Add(dc3);
int i1 = 0;
foreach (var groap in result)
{
DataRow newrow = dt.NewRow();
newrow[0] = groap.Name.ToString();
newrow[1] = groap.Count.ToString();
newrow[2] = groap.Area.ToString();
dt.Rows.Add(newrow);
i1++;
}
dataGridView2.DataSource = dt.DefaultView;
dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
label17.Text = dataGridView2.Rows.Count.ToString();
label14.Text = featureSet.Sum(x => Convert.ToDecimal(x.Attributes["面积"])).ToString("0.00");
label17.Visible = true;
label14.Visible = true;
}
chart2.ChartAreas[0].BackColor = Color.AliceBlue;
chart2.Legends[0].Enabled = true;
DataTable dtchart = default(DataTable);
dtchart = Datatable.CreateDataTable("县", "蝗区总个数", 0, 1, dataGridView2);
chart2.DataSource = dtchart;
chart2.Series[0].YValueMembers = "蝗区总个数";
chart2.Series[0].XValueMember = "县";
chart2.Series[0].Legend = "Legend1";
chart2.Series[0].LegendText = "蝗区总个数";
chart2.ChartAreas[0].AxisX.Interval = 1; //设置X轴坐标的间隔为1
chart2.ChartAreas[0].AxisX.IntervalOffset = 1; //设置X轴坐标偏移为1
chart2.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false; //设置是否交错显示,比如数据多的时间分成两行来显示
chart2.ChartAreas[0].AxisY.Title = "个";
chart2.DataBind();
}
if (qssx == 3)
{
//当选择为县时
//var result = featureSet.GroupBy(a => a.Attributes["NAME_12_13"])
// .Select(group => new
// {
// Name = group.Key,
// Count = group.Count(),
// Area = group.Sum(x => Convert.ToDecimal(x.Attributes["面积"]))
// });
if (featureSet != null && featureSet.Features.Count > 0)
{
DataColumn dc1 = new DataColumn("蝗区名称");//2
dt.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("蝗区面积");
dt.Columns.Add(dc2);
int i1 = 0;
foreach (var groap in featureSet)
{
DataRow newrow = dt.NewRow();
newrow[0] = groap.Attributes["蝗区名称"].ToString();
newrow[1] = groap.Attributes["面积"].ToString();
dt.Rows.Add(newrow);
i1++;
}
dataGridView2.DataSource = dt.DefaultView;
dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
label17.Text = dataGridView2.Rows.Count.ToString();
label14.Text = featureSet.Sum(x => Convert.ToDecimal(x.Attributes["面积"])).ToString("0.00");
label17.Visible = true;
label14.Visible = true;
}
chart2.ChartAreas[0].BackColor = Color.AliceBlue;
chart2.Legends[0].Enabled = true;
DataTable dtchart = default(DataTable);
dtchart = Datatable.CreateDataTable("蝗区名称", "蝗区面积", 0, 1, dataGridView2);
chart2.DataSource = dtchart;
chart2.Series[0].YValueMembers = "蝗区面积";
chart2.Series[0].XValueMember = "蝗区名称";
chart2.Series[0].Legend = "Legend1";
chart2.Series[0].LegendText = "蝗区面积";
chart2.ChartAreas[0].AxisX.Interval = 1; //设置X轴坐标的间隔为1
chart2.ChartAreas[0].AxisX.IntervalOffset = 1; //设置X轴坐标偏移为1
chart2.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false; //设置是否交错显示,比如数据多的时间分成两行来显示
chart2.ChartAreas[0].AxisY.Title = "平方千米";
chart2.DataBind();
}
}
void QueryTask_Failed(object sender, TaskFailedEventArgs e)
{
System.Windows.MessageBox.Show(e.Error.Message.ToString());
}