主要代码示例
require( [ 'echarts', 'echarts/chart/map' ], function(ec) { var result = new Object(); var myChart = ec.init(document.getElementById(type + "View")); data: (function() { $.ajax({ async : false, url : basePath + 'search-data-chronic-outer!getDisbetesOuterDataEveryYear.action', type : 'post', dataType : 'json', data: { "year" : year, "type" : params }, success: function(data) { result = data; console.log(result); } }); })(); // 导入美国地图 require('echarts/util/mapData/params').params.USA = { getGeoJson: function (callback) { $.getJSON('js/echarts/util/mapData/geoJson/USA_geo.json', callback); }, specialArea : { Alaska : { left : -127, top : 30, width : 15, height : 15 }, Hawaii : { left : -110, top : 28, width : 5, height : 5 }, 'Puerto Rico' : { left : -76, top : 26, width : 2, height : 2 } } }; option = { title : { text: year + " 年 ", x:'right', textStyle : { fontSize : 15 } }, tooltip : { trigger: 'item', showDelay: 0, transitionDuration: 0.2, formatter : function (params) { var maxValue = ""; var minValue =""; var stateName = params.name; for(var sName in result){ if(stateName == sName){ maxValue = result[sName].maxValue; minValue = result[sName].minValue; } } // 回填函数 var str = getZHName(params.name) + ''; str += '平均值:' + params.value + ''; str += '最大值:' + maxValue + ''; str += '最小值:' + minValue; return str; } }, // 拉动条 dataRange: { x : 'right', max : result.max, min : result.min, // 精度 precision : 0, color: ['orangered','yellow','lightskyblue'], text:['高','低'], calculable : true }, toolbox: { show : true, x: 'left', y: 'top', feature : { dataView : {show: true, readOnly: false}, restore : {show: true}, saveAsImage : {show: true} } }, series : [ { name: year + " 年 ", type: 'map', roam: true, mapType: 'USA', itemStyle:{ normal:{ label:{ show:true, formatter : function(s) { return getShortName(s); } } }, emphasis:{ label:{ show:true, formatter : function(s) { return getZHName(s); } } } }, // 文本位置修正 textFixed : { Alaska : [20, -20] }, data : result.data == null ? [] : result.data } ] }; myChart.setOption(option); } );
美国各州中文名称转换
/** * 根据英文州名获取中文州名 * @param name * @returns {String} */function getZHName(name) { var nameZH = name; if(name=='Alabama'){ nameZH = '亚拉巴马州'; }else if(name=='Alaska'){ nameZH = '阿拉斯加州'; }else if(name=='Arizona'){ nameZH = '亚利桑那州'; }else if(name=='California'){ nameZH = '加利福尼亚州'; }else if(name=="Arkansas"){ nameZH = '阿肯色州'; }else if(name=='Colorado'){ nameZH = '科罗拉多州'; }else if(name=='Connecticut'){ nameZH = '康涅狄格州'; }else if(name=='Delaware'){ nameZH = '特拉华州'; }else if(name.toUpperCase()=='District of Columbia'.toUpperCase()){ nameZH = '哥伦比亚特区'; }else if(name=='Florida'){ nameZH = '佛罗里达州'; }else if(name=='Georgia'){ nameZH = '佐治亚州'; }else if(name=='Hawaii'){ nameZH = '夏威夷州'; }else if(name=='Idaho'){ nameZH = '爱达荷州'; }else if(name=='Illinois'){ nameZH = '伊利诺伊州'; }else if(name=='Indiana'){ nameZH = '印第安纳州'; }else if(name=='Iowa'){ nameZH = '艾奥瓦州'; }else if(name=='Kansas'){ nameZH = '堪萨斯州'; }else if(name=='Kentucky'){ nameZH = '肯塔基州'; }else if(name=='Louisiana'){ nameZH = '路易斯安那州'; }else if(name=='Maine'){ nameZH = '缅因州'; }else if(name=='Maryland'){ nameZH = '马里兰州'; }else if(name=='Massachusetts'){ nameZH = '马萨诸塞州'; }else if(name=='Michigan'){ nameZH = '密歇根州'; }else if(name=='Minnesota'){ nameZH = '明尼苏达州'; }else if(name=='Mississippi'){ nameZH = '密西西比州'; }else if(name=='Missouri'){ nameZH = '密苏里州'; }else if(name=='Montana'){ nameZH = '蒙大拿州'; }else if(name=='Nebraska'){ nameZH = '内布拉斯加州'; }else if(name=='Nevada'){ nameZH = '内华达州'; }else if(name=='New Hampshire'){ nameZH = '新罕布什尔州'; }else if(name=='New Jersey'){ nameZH = '新泽西州'; }else if(name=='New Mexico'){ nameZH = '新墨西哥州'; }else if(name=='New York'){ nameZH = '纽约州'; }else if(name=='North Carolina'){ nameZH = '北卡罗来纳州'; }else if(name=='North Dakota'){ nameZH = '北达科他州'; }else if(name=='Ohio'){ nameZH = '俄亥俄州'; }else if(name=='Oklahoma'){ nameZH = '奥克拉荷马州'; }else if(name=='Oregon'){ nameZH = '俄勒冈州'; }else if(name=='Pennsylvania'){ nameZH = '宾夕法尼亚州'; }else if(name=='Rhode Island'){ nameZH = '罗得岛州'; }else if(name=='South Carolina'){ nameZH = '南卡罗来纳州'; }else if(name=='Tennessee'){ nameZH = '田纳西州'; }else if(name=='Texas'){ nameZH = '得克萨斯州'; }else if(name=='Utah'){ nameZH = '犹他州'; }else if(name=='Vermont'){ nameZH = '佛蒙特州'; }else if(name=='Virginia'){ nameZH = '弗吉尼亚州'; }else if(name=='Washington'){ nameZH = '华盛顿州'; }else if(name=='West Virginia'){ nameZH = '西弗吉尼亚州'; }else if(name=='Wisconsin'){ nameZH = '威斯康星州'; }else if(name=='Puerto Rico'){ nameZH = '波多黎各'; }else if(name=='Country Of Mexico'){ nameZH = '墨西哥'; }else if(name=='Arkansas'){ nameZH = '阿肯色州'; }else if(name=='Virgin Islands'){ nameZH = '美属维尔京群岛'; }else if(name=='South Dakota'){ nameZH = '南达科他州'; }else if(name=='Wyoming'){ nameZH = '怀俄明州'; }else if(name=='Guam'){ nameZH = '关岛'; }else if(name=='Canada'){ nameZH = '加拿大'; } return nameZH;}function getShortName(name) { var shortName = name; if(name=='Alabama'){ shortName = 'AL'; }else if(name=='Alaska'){ shortName = 'AK'; }else if(name=='Arizona'){ shortName = 'AZ'; }else if(name=='California'){ shortName = 'CA'; }else if(name=='Colorado'){ shortName = 'CO'; }else if(name=='Connecticut'){ shortName = 'CT'; }else if(name=='Delaware'){ shortName = 'DE'; }else if(name=='District of Columbia'){ shortName = 'DC'; }else if(name=='Florida'){ shortName = 'FL'; }else if(name=='Georgia'){ shortName = 'GA'; }else if(name=='Hawaii'){ shortName = 'HI'; }else if(name=='Idaho'){ shortName = 'ID'; }else if(name=='Illinois'){ shortName = 'IL'; }else if(name=='Indiana'){ shortName = 'IN'; }else if(name=='Iowa'){ shortName = 'IA'; }else if(name=='Kansas'){ shortName = 'KS'; }else if(name=='Kentucky'){ shortName = 'KY'; }else if(name=='Louisiana'){ shortName = 'LA'; }else if(name=='Maine'){ shortName = 'ME'; }else if(name=='Maryland'){ shortName = 'MD'; }else if(name=='Massachusetts'){ shortName = 'MA'; }else if(name=='Michigan'){ shortName = 'MI'; }else if(name=='Minnesota'){ shortName = 'MN'; }else if(name=='Mississippi'){ shortName = 'MS'; }else if(name=='Missouri'){ shortName = 'MO'; }else if(name=='Montana'){ shortName = 'MT'; }else if(name=='Nebraska'){ shortName = 'NE'; }else if(name=='Nevada'){ shortName = 'NV'; }else if(name=='New Hampshire'){ shortName = 'NH'; }else if(name=='New Jersey'){ shortName = 'NJ'; }else if(name=='New Mexico'){ shortName = 'NM'; }else if(name=='New York'){ shortName = 'NY'; }else if(name=='North Carolina'){ shortName = 'NC'; }else if(name=='North Dakota'){ shortName = 'ND'; }else if(name=='Ohio'){ shortName = 'OH'; }else if(name=='Oklahoma'){ shortName = 'OK'; }else if(name=='Oregon'){ shortName = 'OR'; }else if(name=='Pennsylvania'){ shortName = 'PA'; }else if(name=='Rhode Island'){ shortName = 'RI'; }else if(name=='South Carolina'){ shortName = 'SC'; }else if(name=='Tennessee'){ shortName = 'TN'; }else if(name=='Texas'){ shortName = 'TX'; }else if(name=='Utah'){ shortName = 'UT'; }else if(name=='Vermont'){ shortName = 'VT'; }else if(name=='Virginia'){ shortName = 'VA'; }else if(name=='Washington'){ shortName = 'WA'; }else if(name=='West Virginia'){ shortName = 'WV'; }else if(name=='Wisconsin'){ shortName = 'WI'; }else if(name=='Puerto Rico'){ shortName = 'PR'; }else if(name=='Country Of Mexico'){ shortName = '墨西哥'; }else if(name=='Arkansas'){ shortName = 'AR'; }else if(name=='Virgin Islands'){ shortName = 'VI'; }else if(name=='South Dakota'){ shortName = 'SD'; }else if(name=='Wyoming'){ shortName = 'WY'; }else if(name=='Guam'){ shortName = 'GU'; }else if(name=='Canada'){ shortName = '加拿大'; } return shortName;}
运行结果图
Remark:菜鸟交流群(260052172)