123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <!--********************************************************************
- * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
- *********************************************************************-->
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title data-i18n="resources.title_jingjinPopGraph"></title>
- <style type="text/css">
- .editPane {
- position: absolute;
- right: 60px;
- top: 50px;
- text-align: center;
- background: #FFF;
- z-index: 1000;
- }
- </style>
- </head>
- <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
- <div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
- <div class='panel-heading'>
- <h5 class='panel-title text-center' data-i18n="resources.title_jingjinPopGraph"></h5>
- </div>
- <div class='panel-body' id='params'>
- <p></p>
- <div align='right' class='button-group'>
- <input type='button' id='btn1' class='btn btn-primary' data-i18n="[value]resources.btn_addThemeLayer" onclick="addThemeLayer()"/>
- <input type='button' id='btn2' class='btn btn-primary' data-i18n="[value]resources.btn_switchBar" onclick="toBar()"/>
- <input type='button' id='btn3' class='btn btn-primary' data-i18n="[value]resources.btn_switchLine" onclick="toLine()"/>
- <input type='button' id='btn4' class='btn btn-primary' data-i18n="[value]resources.btn_switchPie" onclick="toPie()"/>
- <input type='button' id='btn5' class='btn btn-primary' data-i18n="[value]resources.btn_updateData" onclick="updateData()"/>
- <input type='button' id='btn6' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear" onclick="clearThemeLayer()"/>
- </div>
- </div>
- </div>
- <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
- <script type="text/javascript" exclude="iclient-classic" include="Bar,Line,Pie" src="../../dist/classic/include-classic.js"></script>
- <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
- <script type="text/javascript">
- var map, layer, themeLayer, infowin, infowinPosition;
- var host = window.isLocal ? window.server : "https://iserver.supermap.io",
- url1 = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
- url2 = host + "/iserver/services/data-jingjin/rest/data";
- // 柱状图表配置
- var settingForBar = {
- // width,height,codomain 分别表示图表宽、高、数据值域;此三项参数为必设参数
- width: 180,
- height: 100,
- // 允许图表展示的值域范围,此范围外的数据将不制作图表
- codomain: [0, 1000],
- // 自动处理小数位
- decimalNumber: 2,
- barStyle: {fillOpacity: 0.7}, // 柱状图中柱条的(表示字段值的图形)样式
- // 按字段设置柱条样式 (与 themeLayer.themeFields 中的字段一一对应) // '#ff9277', ' #dddd00', ' #ffc877', ' #bbe3ff', ' #d5ffbb',
- barStyleByFields: [{fillColor: "#ff9277"}, {fillColor: "#dddd00"}, {fillColor: "#ffc877"}],
- barHoverStyle: {fillOpacity: 1}, // 柱条 hover 样式
- xShapeBlank: [10, 10, 10], // 水平方向上的空白间距参数
- axisYTick: 4, // y 轴刻度数量
- // y 轴标签及其样式
- axisYLabels: ["1000", "750", "500", "250", "0"],
- axisYLabelsStyle: {fillColor: "#008ACD"},
- // x 轴标签及其样式
- axisXLabels: ["92年", "95年", "99年"],
- axisXLabelsStyle: {fillColor: "#008ACD"},
- backgroundRadius: [5, 5, 5, 5] // 背景框圆角参数
- };
- // 折线图表配置
- var settingForLine = {
- width: 180,
- height: 100,
- codomain: [0, 1000],
- decimalNumber: 2,
- // 折线 style
- lineStyle: {strokeColor: "#D8361B", strokeOpacity: 0.7},
- // 折线节点(表示字段值的图形)样式
- pointStyle: {
- stroke: true,
- strokeColor: "#D8361B",
- pointRadius: 3,
- strokeWidth: 1,
- fillColor: "#F3F3F3",
- fillOpacity: 1
- },
- // 折线节点 hover 样式
- pointHoverStyle: {
- fillColor: "#D8361B"
- },
- xShapeBlank: [10, 10],
- axisYTick: 4,
- axisYLabels: ["1000", "750", "500", "250", "0"],
- axisXLabels: ["92年", "95年", "99年"],
- backgroundRadius: [5, 5, 5, 5]
- };
- // 饼图配置
- var settingForPie = {
- width: 100,
- height: 100,
- codomain: [0, 1000],
- decimalNumber: 2,
- // 饼图扇形(表示字段值的图形)样式
- sectorStyle: {fillOpacity: 0.8},
- // 按字段设置饼图扇形 (样式与 themeLayer.themeFields 数组中的字段名称一一对应)
- sectorStyleByFields: [{fillColor: "#ff9277"}, {fillColor: "#dddd00"}, {fillColor: "#ffc877"}],
- // 饼图扇形 hover 样式
- sectorHoverStyle: {fillOpacity: 1}
- };
- // 统计图模块要求浏览器支持 Canvas 渲染
- if (!document.createElement('canvas').getContext) {
- widgets.alert.showAlert(resources.msg_supportCanvas, false);
- }
- // 创建地图
- map = new SuperMap.Map("map", {
- controls: [
- new SuperMap.Control.LayerSwitcher(),
- new SuperMap.Control.ScaleLine(),
- new SuperMap.Control.Zoom(),
- new SuperMap.Control.Navigation({
- dragPanOptions: {
- enableKinetic: true
- }
- })]
- });
- layer = new SuperMap.Layer.TiledDynamicRESTLayer("Jingjin", url1, {
- transparent: true,
- cacheEnabled: true
- }, {maxResolution: "auto"});
- layer.events.on({"layerInitialized": addLayer});
- // 创建一个柱状(Bar)统计专题图
- themeLayer = new SuperMap.Layer.Graph("ThemeLayer", "Bar");
- // 配置图表参数
- themeLayer.chartsSetting = settingForBar;
- themeLayer.setOpacity(0.9);
- // 注册专题图 mousemove, mouseout 事件 (注意:专题图图层对象自带 on 函数注册事件,专题图对象没有 events 属性)
- themeLayer.on("mousemove", showInfoWin);
- themeLayer.on("mouseout", closeInfoWin);
- // 注册地图 mousemove,用于获取当前鼠标的地理位置
- map.events.on({
- "mousemove": function (e) {
- infowinPosition = e.xy.clone();
- // 偏移显示弹出窗
- infowinPosition.x += 40;
- infowinPosition.y -= 20;
- }
- });
- function addLayer() {
- map.addLayers([layer, themeLayer]);
- map.setCenter(new SuperMap.LonLat(117, 40.004), 0);
- }
- //获取 feature 数据, 专题图的数据必须是 SuperMap.Feature.Vector
- function addThemeLayer() {
- clearThemeLayer();
- // 使用 GetFeaturesBySQLService 获取数据
- var getFeatureParam, getFeatureBySQLService, getFeatureBySQLParams;
- getFeatureParam = new SuperMap.REST.FilterParameter({
- name: "Jingjin",
- attributeFilter: "SMID > -1"
- });
- getFeatureBySQLParams = new SuperMap.REST.GetFeaturesBySQLParameters({
- queryParameter: getFeatureParam,
- toIndex: 500,
- datasetNames: ["Jingjin:BaseMap_R"]
- });
- getFeatureBySQLService = new SuperMap.REST.GetFeaturesBySQLService(url2, {
- eventListeners: {"processCompleted": processCompleted, "processFailed": processFailed}
- });
- getFeatureBySQLService.processAsync(getFeatureBySQLParams);
- }
- function processCompleted(getFeaturesEventArgs) {
- // 指定用于专题图制作的属性字段
- themeLayer.themeFields = ["POP_1992", "POP_1995", "POP_1999"];
- themeLayer.overlayWeightField = "POP_1999";
- // 改变x轴上的标签
- settingForBar.axisXLabels = settingForLine.axisXLabels = settingForPie.axisXLabels = ["92年", "95年", "99年"];
- // 图标宽度 180
- settingForBar.width = settingForLine.width = 180;
- var result = getFeaturesEventArgs.result;
- if (result && result.features) {
- // 向专题图层添加数据
- themeLayer.addFeatures(result.features);
- }
- }
- function processFailed(e) {
- widgets.alert.showAlert(e.error.errorMsg,false);
- }
- // 清除专题图层中的内容
- function clearThemeLayer() {
- themeLayer.clear();
- closeInfoWin();
- }
- // 显示地图弹窗
- function showInfoWin(e) {
- // e.target 是图形对象,即数据的可视化对象;
- // 图形对象的 refDataID 属性是数据(feature)的 id 属性,它指明图形对象是由那个数据制作而来;
- // 图形对象的 dataInfo 属性是图形对象表示的具体数据,他有两个属性,field 和 value;
- if (e.target && e.target.refDataID && e.target.dataInfo) {
- closeInfoWin();
- // 获取图形对应的数据 (feature)
- var fea = themeLayer.getFeatureById(e.target.refDataID);
- var info = e.target.dataInfo;
- // 弹窗内容
- var contentHTML = "<div style='color: yellow;'>";
- contentHTML += "城市:" + fea.attributes.NAME;
- contentHTML += "<hr style='margin: 3px'>";
- switch (info.field) {
- case "POP_1992":
- contentHTML += "92年人口 <br/>" + info.value + "(万)";
- break;
- case "POP_1995":
- contentHTML += "95年人口 <br/>" + info.value + "(万)";
- break;
- case "POP_1999":
- contentHTML += "99年人口 <br/>" + info.value + "(万)";
- break;
- case "POP_2003":
- contentHTML += "03年人口 <br/>" + info.value + "(万)";
- break;
- case "POP_2008":
- contentHTML += "08年人口 <br/>" + info.value + "(万)";
- break;
- default:
- contentHTML += "No Data";
- }
- contentHTML += "</div>";
- // 弹出框大小
- var infowinSize = (SuperMap.Browser.name == "firefox") ? new SuperMap.Size(110, 85) : new SuperMap.Size(100, 75);
- // 弹出窗地理位置
- var lonLat = map.getLonLatFromPixel(infowinPosition);
- infowin = new SuperMap.Popup(
- "infowin",
- lonLat,
- infowinSize,
- contentHTML,
- false,
- false,
- null);
- infowin.setBackgroundColor("#000000");
- infowin.setOpacity(0.6);
- if (infowin) map.removePopup(infowin);
- map.addPopup(infowin);
- }
- }
- // 移除和销毁地图弹窗
- function closeInfoWin() {
- if (infowin) {
- try {
- map.removePopup(infowin);
- }
- catch (e) {
- widgets.alert.showAlert(e.message,false);
- }
- }
- }
- // 更新数据
- function updateData() {
- var feas = themeLayer.features;
- for (var i = 0, len = feas.length; i < len; i++) {
- var fea = feas[i];
- fea.attributes.POP_1992 = getRandomNumber(50, 950, 2);
- fea.attributes.POP_1995 = getRandomNumber(50, 950, 2);
- fea.attributes.POP_1999 = getRandomNumber(50, 950, 2);
- fea.attributes.POP_2003 = getRandomNumber(50, 950, 2);
- fea.attributes.POP_2008 = getRandomNumber(50, 950, 2);
- }
- // 重新指定用于专题图制作的属性字段
- themeLayer.themeFields = ["POP_1992", "POP_1995", "POP_1999", "POP_2003", "POP_2008"];
- // 改变 x 轴上的标签
- settingForBar.axisXLabels = settingForLine.axisXLabels = settingForPie.axisXLabels = ["92年", "95年", "99年", "03年", "08年"];
- // 图标宽度改为 220
- settingForBar.width = settingForLine.width = 220;
- // 为 03、08年 数据增加 style
- settingForBar.barStyleByFields.push({fillColor: "#bbe3ff"});
- settingForBar.barStyleByFields.push({fillColor: "#d5ffbb"});
- settingForPie.sectorStyleByFields.push({fillColor: "#bbe3ff"});
- settingForPie.sectorStyleByFields.push({fillColor: "#d5ffbb"});
- // 重绘图层(图表)
- themeLayer.redraw();
- }
- // 获取指定范围内的随机数
- // min - 范围下限
- // max - 范围上限
- // decimalNum - 返回结果的小数位数。如果为 0,返回整数。
- function getRandomNumber(min, max, decimalNum) {
- var rNum = min + Math.random() * (max - min);
- if (decimalNum) {
- if (!isNaN(decimalNum)) {
- return rNum;
- }
- else {
- decimalNum = parseInt(decimalNum);
- }
- if (decimalNum === 0) {
- return Math.round(rNum);
- }
- else {
- return parseFloat(rNum).toFixed(decimalNum);
- }
- }
- else {
- return rNum;
- }
- }
- // 切换图表为柱状图
- function toBar() {
- themeLayer.chartsSetting = settingForBar;
- themeLayer.setOpacity(0.9);
- themeLayer.setChartsType("Bar");
- }
- // 切换图表为折线图
- function toLine() {
- themeLayer.chartsSetting = settingForLine;
- themeLayer.setOpacity(0.9);
- themeLayer.setChartsType("Line");
- }
- // 切换图表为饼图
- function toPie() {
- themeLayer.chartsSetting = settingForPie;
- themeLayer.setOpacity(1);
- themeLayer.setChartsType("Pie");
- }
- </script>
- </body>
- </html>
|