描述
本例展示了如何配置分级渲染使用一个相等间隔分类。在这个分类类型中,断点被设置为相等的距离。
可以手工添加相等距离的断点;然而,如果数据被修改了,那些断点就会是不合理的。本例自动地计算断点,因此相同的代码可以用于不同的数据集。
在本例中,断点用相同颜色的逐渐增大的圆符号。如果想要应用某种颜色渐变到断点,需要手工重定义一个颜色数组,然后在循环中给断点加入颜色。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"> <html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>Class Breaks Renderer</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.5/js/dojo/dijit/themes/tundra/tundra.css">
<script type="text/javascript">var djConfig = {parseOnLoad: true }</script>
<script type="text/javascript" src= "http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.5"></script> <script type="text/javascript" charset="utf-8">
dojo.require("esri.map");
dojo.require("esri.tasks.query"); var map;
var stateName = "Florida";
var attributeField = "POP1990"; function init() {
map = new esri.Map("map", {
extent: new esri.geometry.Extent({xmin:-88.06640625,ymin:24.45556640625,xmax:-79.27734375,ymax:31.04736328125,spatialReference:{wkid:4326}}),
slider: false
});
dojo.connect(map, "onLoad", doQuery);
map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"));
} //查询所有国家 in Kansas
function doQuery() {
var queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");
var query = new esri.tasks.Query();
query.where = "STATE_NAME = '" + stateName + "'";
query.outFields = [attributeField];
query.returnGeometry = true;
queryTask.execute(query, addFeatureSetToMap);
} //添加国家到地图并且应用渲染
function addFeatureSetToMap(featureSet) {
var features = featureSet.features; var min = max = parseFloat(features[0].attributes[attributeField]); //找到Florida 城市中的最少人数和最多人数 dojo.forEach(features,function(feature) {
min = Math.min(min,feature.attributes[attributeField]);
max = Math.max(max,feature.attributes[attributeField]); } ); var numRanges = 7;
var breaks = (max-min) / numRanges; var outline = new esri.symbol.SimpleLineSymbol().setWidth(1);//轮廓
var fillColor = new dojo.Color([240,150,240,0.75]);
var defaultSymbol = new esri.symbol.SimpleMarkerSymbol().setSize(1).setOutline(outline); var renderer = new esri.renderer.ClassBreaksRenderer(defaultSymbol,attributeField); for (var i=0;i<numRanges;i++) {
renderer.addBreak(parseFloat(min + (i*breaks)),
parseFloat(min + ((i+1)*breaks)),
new esri.symbol.SimpleMarkerSymbol().setSize((i+1)*6).setColor(fillColor).setOutline(outline)); } map.graphics.setRenderer(renderer); //添加 features 到地图
dojo.forEach(featureSet.features,function(feature) {
map.graphics.add(feature); });
} dojo.addOnLoad(init);
</script> </head>
<body>
<div id="map" class="tundra" style="width:800px; height:400px; border:1px solid #000;"></div>
</body>
</html>