美文网首页
地图插件的使用

地图插件的使用

作者: 上海_前端_求内推 | 来源:发表于2021-06-24 15:17 被阅读0次

1,首先引入三个插件分别是jq,echarts.js,china.js
2,相关代码如下


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>患者来源分布</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link href="/Content/themes/shwgk/ico/shwgk.ico" rel="shortcut icon">
    <!--  jquery -->
    <script src="/Scripts/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <!--  echarts -->
    <script src="/Scripts/plugins/echarts/echarts.js"></script>
    <script src="/Content/themes/shwgk/js/china.js"></script>
    <style type="text/css">
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
            background-color: #27293d;
        }

        /*定义滚动条轨道 内阴影+圆角*/
        ::-webkit-scrollbar-track {
            -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.1);
            border-radius: 10px;
            background-color: #27293d;
        }

        /*定义滑块 内阴影+圆角*/
        ::-webkit-scrollbar-thumb {
            border-radius: 10px;
            -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.1);
            background-color: #76767e;
        }
    </style>
    <style>
        body {
            background: linear-gradient(to right, #2e0d46 0%,#2a0940 50%,#2f0c46 100%);
            /*background: linear-gradient(to right, #000000 0%,#000000 50%,#000000 100%);*/
            margin: 0;
        }

        .map_box {
            width: 100%;
            position: relative;
        }

        #map {
            width: 100%;
            height: 100%;
        }

        .canvas {
            position: absolute;
            width: 100%;
            left: 0;
            top: 0;
            height: 99%;
            z-index: 0;
        }

        .logo {
            position: fixed;
            top: 2%;
            left: 10%;
            z-index: 99;
        }

        .home_button {
            position: fixed;
            right: 16%;
            top: 5.2%;
            color: #fdf003;
            cursor: pointer;
            font-size: 20px;
            z-index: 999;
            animation: notice_color 1s;
            animation-iteration-count: infinite;
        }

        @keyframes notice_color {
            0% {
                color: #ff0000;
                text-shadow: 0px 0px 5px rgba(0,0,0,.1);
            }

            50% {
                color: #fff;
                text-shadow: 0px 0px 5px rgba(0,0,0,.1);
            }

            100% {
                color: #ff0000;
                text-shadow: 0px 0px 5px rgba(0,0,0,.1);
            }
        }
    </style>
</head>
<body>
    <section>
        

<link href="/Content/bootstrap/css/font-awesome.min.css" rel="stylesheet" />
<style type="text/css">
    .home_button {
        right: 11%;
    }
</style>
<div class="map_box" style="">
    <div class="canvas" style="opacity: .5">
        <iframe frameborder="0" src="/Content/commom/html/canvas_black.html" style="width: 100%; height: 100%"></iframe>
    </div>
    <img src="/Content/themes/shwgk/img/title.png" class="logo" />
    <a class="home_button" href="/home/index"><i class="fa fa-home"></i></a>
    <div id="map"></div>
</div>
<script type="text/javascript">
    var chinaGeoCoordMap = {
        '上海': [121.4648, 31.2891],
        '黑龙江': [127.9688, 45.368],
        '大庆': [126.9688, 45.868],
        '内蒙古': [110.3467, 41.4899],
        "吉林": [125.8154, 44.2584],
        '北京': [116.4551, 40.2539],
        "辽宁": [123.1238, 42.1216],
        "河北": [114.4995, 38.1006],
        "天津": [117.4219, 39.4189],
        "山西": [112.3352, 37.9413],
        "陕西": [109.1162, 34.2004],
        "甘肃": [103.5901, 36.3043],
        "宁夏": [106.3586, 38.1775],
        "青海": [101.4038, 36.8207],
        "新疆": [87.9236, 43.5883],
        "西藏": [91.11, 29.97],
        "四川": [103.9526, 30.7617],
        "重庆": [108.384366, 30.439702],
        "山东": [117.1582, 36.8701],
        "河南": [113.4668, 34.6234],
        "江苏": [118.8062, 31.9208],
        "安徽": [117.29, 32.0581],
        "湖北": [114.3896, 30.6628],
        "浙江": [119.5313, 29.8773],
        "福建": [119.4543, 25.9222],
        "江西": [116.0046, 28.6633],
        "湖南": [113.0823, 28.2568],
        "贵州": [106.6992, 26.7682],
        "云南": [102.9199, 25.4663],
        "广东": [113.12244, 23.009505],
        "广西": [108.479, 23.1152],
        "海南": [110.3893, 19.8516]
    };

    $(document).ready(function () {
        //定时2分钟刷新
        //setInterval("RefreshMap_Init()", 3 * 60 * 1000);
        getMap_Init("01", "001101");
    });

    var RefreshMap_Init = function () {
        getMap_Init("01", "001101");
    }
    var getMap_Init = function (hospitalCode, dataCode) {
        console.log("001101")
        // $.ajax({
        //     url: "/AjaxData/GetRealTimeTableData",
        //     async: false,
        //     type: "get",
        //     dataType: 'json',
        //     data: { HospitalCode: hospitalCode, DataCode: dataCode },
        //     success: function (jsondata) {
        //         if (jsondata.success) {
                 
        //         }
        //     }
        // });
           RenderMap({"dataList":"[{\"name\":\"上海\",\"value\":2830},{\"name\":\"江苏\",\"value\":1040},{\"name\":\"安徽\",\"value\":500},{\"name\":\"浙江\",\"value\":446},{\"name\":\"江西\",\"value\":206},{\"name\":\"河南\",\"value\":161},{\"name\":\"湖北\",\"value\":112},{\"name\":\"山东\",\"value\":96},{\"name\":\"四川\",\"value\":96},{\"name\":\"黑龙江\",\"value\":87},{\"name\":\"福建\",\"value\":79},{\"name\":\"湖南\",\"value\":43},{\"name\":\"辽宁\",\"value\":38},{\"name\":\"贵州\",\"value\":38},{\"name\":\"吉林\",\"value\":37},{\"name\":\"陕西\",\"value\":34},{\"name\":\"甘肃\",\"value\":31},{\"name\":\"新疆\",\"value\":31},{\"name\":\"河北\",\"value\":30},{\"name\":\"广东\",\"value\":23},{\"name\":\"山西\",\"value\":22},{\"name\":\"内蒙古\",\"value\":20},{\"name\":\"重庆\",\"value\":18},{\"name\":\"北京\",\"value\":13},{\"name\":\"广西\",\"value\":13},{\"name\":\"云南\",\"value\":11},{\"name\":\"天津\",\"value\":9},{\"name\":\"青海\",\"value\":5},{\"name\":\"宁夏\",\"value\":2}]","DataCode":"001101","SqlStr":"SELECT PName as name,PCount AS value FROM OP_Encounter_ChinaMap  WHERE HospitalCode =\u002701\u0027  order by PCount desc","success":true,"msg":"获取成功"});
    }
    var RenderMap = function (jsondata) {
        var yData = [];
        var barData = [];
        var map_height = $(window).height();
        $('.map_box').height(map_height);

        var context = {
            DataCode: jsondata.DataCode,
            SqlStr: jsondata.SqlStr,
            dataList: JSON.parse(jsondata.dataList)   // XAxis ,r2
        };
        chinaDatas = context.dataList;

        var csjcount = 0;
        var totalcount = 0;
        var csjzb = "";

        for (var i = 0; i < context.dataList.length; i++) {
            barData.push(context.dataList[i]);
            yData.push(context.dataList[i].name);
            if (context.dataList[i].name == "上海" || context.dataList[i].name == "浙江" || context.dataList[i].name == "江苏" || context.dataList[i].name == "安徽") {
                csjcount += parseInt(context.dataList[i].value);
            }
            totalcount += parseInt(context.dataList[i].value);
        }
        csjzb = GetPercent(csjcount, totalcount);

        var convertData = function (data) {
            var res = [];
            for (var i = 0; i < data.length; i++) {
                var dataItem = data[i];
                var fromCoord = chinaGeoCoordMap[dataItem.name];
                var toCoord = [121.4648, 31.2891];
                if (fromCoord && toCoord) {
                    res.push([{
                        coord: fromCoord,
                        value: dataItem.value
                    }, {
                        coord: toCoord,
                    }]);
                }
            }
            return res;
        };

        var series = [{
            type: 'lines',
            zlevel: 2,
            effect: {
                show: true,
                period: 4, //箭头指向速度,值越小速度越快
                trailLength: 0.02, //特效尾迹长度[0,1]值越大,尾迹越长重
                symbol: 'arrow', //箭头图标
                symbolSize: 5, //图标大小
            },
            lineStyle: {
                normal: {
                    width: 1, //尾迹线条宽度
                    opacity: 1, //尾迹线条透明度
                    curveness: .3 //尾迹线条曲直度
                }
            },
            data: convertData(chinaDatas)
        }, {
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: { //涟漪特效
                period: 4, //动画时间,值越小速度越快
                brushType: 'stroke', //波纹绘制方式 stroke, fill
                scale: 4 //波纹圆环最大限制,值越大波纹越大
            },
            label: {
                normal: {
                    show: true,
                    position: 'right', //显示位置
                    offset: [5, 0], //偏移设置
                    formatter: function (params) {//圆环显示文字
                        return params.data.name;
                    },
                    fontSize: 13
                },
                emphasis: {
                    show: true
                }
            },
            symbol: 'circle',
            symbolSize: function (val) {
                if (parseInt(val[2]) > 1000) {
                    return 12;
                } else if (parseInt(val[2]) > 500) {
                    return 10;
                } else if (parseInt(val[2]) > 200) {
                    return 8;
                }
                return 6;
                //return 6; //5 + val[2] * 1; //圆环大小
            },
            itemStyle: {
                normal: {
                    show: false,
                    color: '#f00'
                }
            },
            data: chinaDatas.map(function (dataItem) {
                return {
                    name: dataItem.name,
                    value: chinaGeoCoordMap[dataItem.name].concat([dataItem.value])
                };
            }),
        },
        //被攻击点
        {
            type: 'scatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
                period: 4,
                brushType: 'stroke',
                scale: 4
            },
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    //offset:[5, 0],
                    color: '#0f0',
                    formatter: '{b}',
                    textStyle: {
                        color: "#0f0"
                    }
                },
                emphasis: {
                    show: true,
                    color: "#f60"
                }
            },
            symbol: 'pin',
            symbolSize: 50,
            data: [{
                name: '上海',
                value: chinaGeoCoordMap['上海'].concat([10]),
            }],
        },
        //横向柱状图
        {
            name: 'barSer',
            type: 'bar',
            roam: false,
            visualMap: false,
            zlevel: 2,
            barMaxWidth: 10,
            barGap: 0,
            label: {
                normal: {
                    show: true,
                      formatter:  function (params) {

                        params.data.value=params.data.value

                       
                    },
                    position: "right",
                    textStyle: {
                        color: "#fff",
                        fontSize: 16,
                        fontWeight: 'normal'
                    }
                }
            },
            data: barData
        }
        ]


        var option = {
            title: [{
                show: true,
                text: '排名情况 - 长三角占比 :' + csjzb,
                textStyle: {
                    color: '#f5f5f5',
                    fontSize: 20
                },
                right: '13%',
                top: '5%'
            }],
            tooltip: {
                show: true,
                formatter: function (params) {
                    debugger
                    var value;
                    chinaDatas.forEach(function (item, i) {
                        if (params.name == item.name) {
                            value = item.value;
                        }
                    })
                    if (params.name != 'undefined' && params.name != '') {
                        return params.name + ':' + value + '人'
                    }
                    return "";
                }
            },
            visualMap: { //图例值控制
                min: 0,
                max: 100,
                calculable: true,
                show: true,
                color: ['#f44336', '#fc9700', '#ffde00', '#ffde00', '#00eaff'],
                //color: ['#00eaff', '#ffde00', '#ffde00', '#fc9700', '#f44336'],
                textStyle: {
                    color: '#fff'
                }
            },
            grid: {
                right: '10%',
                top: '10%',
                bottom: '10%',
                width: '20%'
            },
            xAxis: {
                show: false
            },
            yAxis: {
                type: 'category',
                inverse: true,
                nameGap: 16,
                axisLine: {
                    show: false,
                    lineStyle: {
                        color: '#ddd'
                    }
                },
                axisTick: {
                    show: false,
                    lineStyle: {
                        color: '#ddd'
                    }
                },
                axisLabel: {
                    interval: 0,
                    margin: 25,
                    textStyle: {
                        color: '#f5f5f5',
                        align: 'right',
                        fontSize: 14
                    },
                    rich: {
                        a: {
                            color: '#fff',
                            backgroundColor: '#FAAA39',
                            width: 20,
                            height: 20,
                            align: 'center',
                            borderRadius: 2
                        },
                        b: {
                            color: '#fff',
                            backgroundColor: '#4197FD',
                            width: 20,
                            height: 20,
                            align: 'center',
                            borderRadius: 2
                        }
                    },
                    formatter: function (params) {
                        return params;
                    }
                },
                data: yData
            },
            geo: {
                roam: true,
                map: 'china',
                zoom: 0.9, //地图的大小(百分比)
                left: '3%',
                top: '10%',
                label: {
                    normal: { //控制地图中文本
                        show: false,
                        textStyle: {
                            color: '#fff'
                        }
                    },
                    emphasis: {
                        textStyle: {
                            color: '#fff'
                        }
                    }
                },
                itemStyle: {
                    normal: {
                        borderColor: 'rgba(147, 235, 248, 1)',
                        borderWidth: 1,
                        areaColor: {
                            type: 'radial',
                            x: 0.5,
                            y: 0.5,
                            r: 0.8,
                            colorStops: [{
                                offset: 0,
                                color: 'rgba(147, 235, 248, 0)' // 0% 处的颜色
                                // color: 'red' // 0% 处的颜色
                            }, {
                                offset: 1,
                                color: 'rgba(147, 235, 248, .2)' // 100% 处的颜色
                            }],
                            globalCoord: false // 缺省为 false
                        },
                        shadowColor: 'rgba(128, 217, 248, 1)',
                        shadowOffsetX: -2,
                        shadowOffsetY: 2,
                        shadowBlur: 10
                    },
                    emphasis: {
                        areaColor: '#389BB7',
                        borderWidth: 0
                    }
                }
            },
            series: series
        };
        var myChart = echarts.init(document.getElementById("map"));
        myChart.setOption(option);
    }

    var GetPercent = function (num, total) {

        num = parseFloat(num);
        total = parseFloat(total);
        if (isNaN(num) || isNaN(total)) {
            return "-";
        }
        return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00) + '%';
    }



    const data1 = [
  { year: '1951 年', sales: 38 },
  { year: '1952 年', sales: 52 },
  { year: '1956 年', sales: 61 },
  { year: '1957 年', sales: 145 },
  { year: '1958 年', sales: 48 },
  { year: '1959 年', sales: 38 },
  { year: '1960 年', sales: 38 },
  { year: '1962 年', sales: 38 },
];
const chart = new G2.Chart({
  container: 'container',
  autoFit: true,
  height: 500,
});

chart.data(data1);
chart.scale('sales', {
  nice: true,
});

chart.tooltip({
  showMarkers: false
});
chart.interaction('active-region');

chart.interval().position('year*sales');

chart.render();

</script>
    </section>
</body>
</html>

相关文章

网友评论

      本文标题:地图插件的使用

      本文链接:https://www.haomeiwen.com/subject/xfawyltx.html