您的位置:首页 > 其它

使用饼图返回查询结果

2008-11-08 09:35 330 查看

Description

This example queries the map for census blocks within the current map extent. The user can then click on a census block to see an info window with a chart of the demographic makeup of the census block. The chart is created with the Google Chart API.这是个有关查询当前地图范围的街区人口普查数据的例子。我们可以通过在查询所得到的图层上点击相应的街区来查看利用这个街区的人口数据生成的饼图。这个饼图是通过 Google Chart API生成的。To accomplish the query, the example uses QueryTask, a class specific to the ArcGIS JavaScript extension. The URL of the map layer to be queried is passed to the QueryTask constructor:
qtask = new esri.arcgis.gmaps.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1");
为了完成这个查询,该例子使用了QueryTask对象,这个对象是通过 ArcGIS JavaScript extension定义的。生成一个QueryTask对象需要使用一个指向地图地址的url,将这个url作为参数传入construter,就可以了。
Notice the 1 at the end of the URL. This means that the layer with index number 1 in the ESRI_Census_USA map service will be queried. To find out the layer index numbers and URLs for a given map service, use the Services Directory.在url中最后有一个index number,不同的index number有不同的作用,The QueryTask requires a Query. The code specifies the bounding box of the area to be queried (query.queryGeometry) and the fields to return (query.outFields). The query is also told to return the geometry information of the features (query.returnGeometry).在构造器中生成的QueryTask只是一个可供查询的图层,要真正地执行查询,还需要一个Query。Query具有以下几个参数: 所查询区域的boundingbox(query.queryGeometry) 查询结果的返回字段(query.outFields) 是否返回查询结果的图形(query.returnGeometry = true;)
This line runs the task, thereby executing the query:
qtask.execute(query, false, mycallback);
The three arguments represent 1) the query conditions, 2) whether to return KML, and 3) a callback function that runs immediately after the query is executed.执行查询的语句:qtask.execute(query, false, mycallback);这三个参数分别是指:1)查询条件 2)是否返回kml 3)一个在查询执行后马上执行的callback函数The callback function mycallback does the work of setting up the chart in the info window. The function takes as input the features returned from the query (fset). It loops through each feature and does the following things:Parses the information from the age fields
Calculates the total population for the block group
Calculates a percentage of the population for each age group
Creates the text and images for the info window that will appear when someone clicks the feature. In doing so, the function passes the information on age distribution to the Google Chart API to create a chart. Look closely in the code for http://chart.apis.google.com/chart? and you can see where the Google Chart API is being referenced. See the Google Chart API Developer's Guide to learn about constructing charts in the Google Chart API.
Adds the feature and its associated info window information to the map.
mycallback函数执行在info窗口中创建饼图的功能。这个函数所需的参数是Query所得到的结果(fset数组),然后使用一个循环对数组中的每个元素做下面的事情        1.从年龄字段里面读取数据,并将其转为数字        2. 计算所有的人口        3.计算每个组中人口的比例        4.创建在信息窗口中显示的文字和饼图(饼图是通过将各个比例尺数据作为参数传给Google Chart API得到)        5. 将features和相应的信息窗口加入地图
示例源代码:                       

var gmap = null;
var qtask = null;
var query = null;
var mapExtension = null;

function initialize() {
// GMap construction
gmap = new GMap2(document.getElementById('gmap'));
gmap.addMapType(G_NORMAL_MAP);
gmap.addMapType(G_SATELLITE_MAP);
gmap.addControl(new GLargeMapControl());
gmap.addControl(new GMapTypeControl());
gmap.setCenter(new GLatLng(33.97142760360439, -117.3805046081543), 15); // RIVERSIDE (Polyline, Polygon)
gmap.enableScrollWheelZoom();

//Create MapExtension utility class
mapExtension = new esri.arcgis.gmaps.MapExtension(gmap);

// Query Task
qtask = new esri.arcgis.gmaps.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1");

// Query
query = new esri.arcgis.gmaps.Query();

}

function executeQuery() {
var cent = gmap.getCenter();
var bounds = gmap.getBounds();

// set query parameters
query.queryGeometry = bounds;
query.returnGeometry = true;
query.outFields = ["STATE_FIPS","CNTY_FIPS","STCOFIPS","TRACT","BLKGRP","FIPS","POP2000","POP2007","POP00_SQMI","POP07_SQMI","WHITE","BLACK","AMERI_ES","ASIAN","HAWN_PI","OTHER","MULT_RACE","HISPANIC","MALES","FEMALES","AGE_UNDER5","AGE_5_17","AGE_18_21","AGE_22_29","AGE_30_39","AGE_40_49","AGE_50_64","AGE_65_UP","MED_AGE","MED_AGE_M","MED_AGE_F","HOUSEHOLDS","***E_HH_SZ","HSEHLD_1_M","HSEHLD_1_F","MARHH_CHD","MARHH_NO_C","MHH_CHILD","FHH_CHILD","FAMILIES","***E_FAM_SZ","HSE_UNITS","VACANT","OWNER_OCC","RENTER_OCC","SQMI"];

// execute query task
qtask.execute(query, false, mycallback);

}

function mycallback(fset) {
var fe = fset.features;
for (var x = 0; x < fe.length; x++) {
var feature = fe[x];
var att = feature.attributes;

var age_under5 = parseInt(att.AGE_UNDER5);
var age_5_17 = parseInt(att.AGE_5_17);
var age_18_21 = parseInt(att.AGE_18_21);
var age_22_29 = parseInt(att.AGE_22_29);
var age_30_39 = parseInt(att.AGE_30_39);
var age_40_49 = parseInt(att.AGE_40_49);
var age_50_64 = parseInt(att.AGE_50_64);
var age_65_up = parseInt(att.AGE_65_UP);

total = age_under5 + age_5_17 + age_18_21 + age_22_29 + age_30_39 + age_40_49 + age_50_64 + age_65_up;

age_under5 = (age_under5 / total) * 100;
age_5_17 = (age_5_17 / total) * 100;
age_18_21 = (age_18_21 / total) * 100;
age_22_29 = (age_22_29 / total) * 100;
age_30_39 = (age_30_39 / total) * 100;
age_40_49 = (age_40_49 / total) * 100;
age_50_64 = (age_50_64 / total) * 100;
age_65_up = (age_65_up / total) * 100;

var infoWindowOption = {
content:"Total 2007 Census Block Population is " + att.POP2007 + ".
The age breakdown is as follows.
"
+ ""
};

mapExtension.addToMap(feature, null, infoWindowOption);
}// end for loop
}



var gmap = null;
var qtask = null;
var query = null;
var mapExtension = null;

function initialize() {
// GMap construction
gmap = new GMap2(document.getElementById('gmap'));
gmap.addMapType(G_NORMAL_MAP);
gmap.addMapType(G_SATELLITE_MAP);
gmap.addControl(new GLargeMapControl());
gmap.addControl(new GMapTypeControl());
gmap.setCenter(new GLatLng(33.97142760360439, -117.3805046081543), 15); // RIVERSIDE (Polyline, Polygon)
gmap.enableScrollWheelZoom();

//Create MapExtension utility class
mapExtension = new esri.arcgis.gmaps.MapExtension(gmap);

// Query Task
qtask = new esri.arcgis.gmaps.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1");

// Query
query = new esri.arcgis.gmaps.Query();

}

function executeQuery() {
var cent = gmap.getCenter();
var bounds = gmap.getBounds();

// set query parameters
query.queryGeometry = bounds;
query.returnGeometry = true;
query.outFields = ["STATE_FIPS","CNTY_FIPS","STCOFIPS","TRACT","BLKGRP","FIPS","POP2000","POP2007","POP00_SQMI","POP07_SQMI","WHITE","BLACK","AMERI_ES","ASIAN","HAWN_PI","OTHER","MULT_RACE","HISPANIC","MALES","FEMALES","AGE_UNDER5","AGE_5_17","AGE_18_21","AGE_22_29","AGE_30_39","AGE_40_49","AGE_50_64","AGE_65_UP","MED_AGE","MED_AGE_M","MED_AGE_F","HOUSEHOLDS","***E_HH_SZ","HSEHLD_1_M","HSEHLD_1_F","MARHH_CHD","MARHH_NO_C","MHH_CHILD","FHH_CHILD","FAMILIES","***E_FAM_SZ","HSE_UNITS","VACANT","OWNER_OCC","RENTER_OCC","SQMI"];

// execute query task
qtask.execute(query, false, mycallback);

}

function mycallback(fset) {
var fe = fset.features;
for (var x = 0; x < fe.length; x++) {
var feature = fe[x];
var att = feature.attributes;

var age_under5 = parseInt(att.AGE_UNDER5);
var age_5_17 = parseInt(att.AGE_5_17);
var age_18_21 = parseInt(att.AGE_18_21);
var age_22_29 = parseInt(att.AGE_22_29);
var age_30_39 = parseInt(att.AGE_30_39);
var age_40_49 = parseInt(att.AGE_40_49);
var age_50_64 = parseInt(att.AGE_50_64);
var age_65_up = parseInt(att.AGE_65_UP);

total = age_under5 + age_5_17 + age_18_21 + age_22_29 + age_30_39 + age_40_49 + age_50_64 + age_65_up;

age_under5 = (age_under5 / total) * 100;
age_5_17 = (age_5_17 / total) * 100;
age_18_21 = (age_18_21 / total) * 100;
age_22_29 = (age_22_29 / total) * 100;
age_30_39 = (age_30_39 / total) * 100;
age_40_49 = (age_40_49 / total) * 100;
age_50_64 = (age_50_64 / total) * 100;
age_65_up = (age_65_up / total) * 100;

var infoWindowOption = {
content:"Total 2007 Census Block Population is " + att.POP2007 + ".
The age breakdown is as follows.
"
+ ""
};

mapExtension.addToMap(feature, null, infoWindowOption);
}// end for loop
}



var gmap = null;
var qtask = null;
var query = null;
var mapExtension = null;

function initialize() {
// GMap construction
gmap = new GMap2(document.getElementById('gmap'));
gmap.addMapType(G_NORMAL_MAP);
gmap.addMapType(G_SATELLITE_MAP);
gmap.addControl(new GLargeMapControl());
gmap.addControl(new GMapTypeControl());
gmap.setCenter(new GLatLng(33.97142760360439, -117.3805046081543), 15); // RIVERSIDE (Polyline, Polygon)
gmap.enableScrollWheelZoom();

//Create MapExtension utility class
mapExtension = new esri.arcgis.gmaps.MapExtension(gmap);

// Query Task
qtask = new esri.arcgis.gmaps.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1");

// Query
query = new esri.arcgis.gmaps.Query();

}

function executeQuery() {
var cent = gmap.getCenter();
var bounds = gmap.getBounds();

// set query parameters
query.queryGeometry = bounds;
query.returnGeometry = true;
query.outFields = ["STATE_FIPS","CNTY_FIPS","STCOFIPS","TRACT","BLKGRP","FIPS","POP2000","POP2007","POP00_SQMI","POP07_SQMI","WHITE","BLACK","AMERI_ES","ASIAN","HAWN_PI","OTHER","MULT_RACE","HISPANIC","MALES","FEMALES","AGE_UNDER5","AGE_5_17","AGE_18_21","AGE_22_29","AGE_30_39","AGE_40_49","AGE_50_64","AGE_65_UP","MED_AGE","MED_AGE_M","MED_AGE_F","HOUSEHOLDS","***E_HH_SZ","HSEHLD_1_M","HSEHLD_1_F","MARHH_CHD","MARHH_NO_C","MHH_CHILD","FHH_CHILD","FAMILIES","***E_FAM_SZ","HSE_UNITS","VACANT","OWNER_OCC","RENTER_OCC","SQMI"];

// execute query task
qtask.execute(query, false, mycallback);

}

function mycallback(fset) {
var fe = fset.features;
for (var x = 0; x < fe.length; x++) {
var feature = fe[x];
var att = feature.attributes;

var age_under5 = parseInt(att.AGE_UNDER5);
var age_5_17 = parseInt(att.AGE_5_17);
var age_18_21 = parseInt(att.AGE_18_21);
var age_22_29 = parseInt(att.AGE_22_29);
var age_30_39 = parseInt(att.AGE_30_39);
var age_40_49 = parseInt(att.AGE_40_49);
var age_50_64 = parseInt(att.AGE_50_64);
var age_65_up = parseInt(att.AGE_65_UP);

total = age_under5 + age_5_17 + age_18_21 + age_22_29 + age_30_39 + age_40_49 + age_50_64 + age_65_up;

age_under5 = (age_under5 / total) * 100;
age_5_17 = (age_5_17 / total) * 100;
age_18_21 = (age_18_21 / total) * 100;
age_22_29 = (age_22_29 / total) * 100;
age_30_39 = (age_30_39 / total) * 100;
age_40_49 = (age_40_49 / total) * 100;
age_50_64 = (age_50_64 / total) * 100;
age_65_up = (age_65_up / total) * 100;

var infoWindowOption = {
content:"Total 2007 Census Block Population is " + att.POP2007 + ".
The age breakdown is as follows.
"
+ ""
};

mapExtension.addToMap(feature, null, infoWindowOption);
}// end for loop
}



var gmap = null;
var qtask = null;
var query = null;
var mapExtension = null;

function initialize() {
// GMap construction
gmap = new GMap2(document.getElementById('gmap'));
gmap.addMapType(G_NORMAL_MAP);
gmap.addMapType(G_SATELLITE_MAP);
gmap.addControl(new GLargeMapControl());
gmap.addControl(new GMapTypeControl());
gmap.setCenter(new GLatLng(33.97142760360439, -117.3805046081543), 15); // RIVERSIDE (Polyline, Polygon)
gmap.enableScrollWheelZoom();

//Create MapExtension utility class
mapExtension = new esri.arcgis.gmaps.MapExtension(gmap);

// Query Task
qtask = new esri.arcgis.gmaps.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1");

// Query
query = new esri.arcgis.gmaps.Query();

}

function executeQuery() {
var cent = gmap.getCenter();
var bounds = gmap.getBounds();

// set query parameters
query.queryGeometry = bounds;
query.returnGeometry = true;
query.outFields = ["STATE_FIPS","CNTY_FIPS","STCOFIPS","TRACT","BLKGRP","FIPS","POP2000","POP2007","POP00_SQMI","POP07_SQMI","WHITE","BLACK","AMERI_ES","ASIAN","HAWN_PI","OTHER","MULT_RACE","HISPANIC","MALES","FEMALES","AGE_UNDER5","AGE_5_17","AGE_18_21","AGE_22_29","AGE_30_39","AGE_40_49","AGE_50_64","AGE_65_UP","MED_AGE","MED_AGE_M","MED_AGE_F","HOUSEHOLDS","***E_HH_SZ","HSEHLD_1_M","HSEHLD_1_F","MARHH_CHD","MARHH_NO_C","MHH_CHILD","FHH_CHILD","FAMILIES","***E_FAM_SZ","HSE_UNITS","VACANT","OWNER_OCC","RENTER_OCC","SQMI"];

// execute query task
qtask.execute(query, false, mycallback);

}

function mycallback(fset) {
var fe = fset.features;
for (var x = 0; x < fe.length; x++) {
var feature = fe[x];
var att = feature.attributes;

var age_under5 = parseInt(att.AGE_UNDER5);
var age_5_17 = parseInt(att.AGE_5_17);
var age_18_21 = parseInt(att.AGE_18_21);
var age_22_29 = parseInt(att.AGE_22_29);
var age_30_39 = parseInt(att.AGE_30_39);
var age_40_49 = parseInt(att.AGE_40_49);
var age_50_64 = parseInt(att.AGE_50_64);
var age_65_up = parseInt(att.AGE_65_UP);

total = age_under5 + age_5_17 + age_18_21 + age_22_29 + age_30_39 + age_40_49 + age_50_64 + age_65_up;

age_under5 = (age_under5 / total) * 100;
age_5_17 = (age_5_17 / total) * 100;
age_18_21 = (age_18_21 / total) * 100;
age_22_29 = (age_22_29 / total) * 100;
age_30_39 = (age_30_39 / total) * 100;
age_40_49 = (age_40_49 / total) * 100;
age_50_64 = (age_50_64 / total) * 100;
age_65_up = (age_65_up / total) * 100;

var infoWindowOption = {
content:"Total 2007 Census Block Population is " + att.POP2007 + ".
The age breakdown is as follows.
"
+ ""
};

mapExtension.addToMap(feature, null, infoWindowOption);
}// end for loop
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐