echarts 地圖顯示餅圖

地圖+餅圖

代碼下載地址:map_pie: 地圖上標(biāo)注餅圖

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="jquery-1.11.0.min.js"></script>
    <script src="echarts.min.js"></script>
    <script src="china.js"></script>
</head>
<body>
    <div class="wrap" style="position: relative;">
        <div id="map" style="width: 100%; height: 900px;"></div>
    </div>
    <script>
    var myChart = echarts.init(document.getElementById('map'));
    // 失去坐標(biāo)
    var geoCoordMap = {
        "北京": [116.41667,39.91667],
        "上海": [121.43333,34.50000],
        "廣州": [113.23333,23.16667],
        "杭州": [120.20000,30.26667],
        "重慶": [106.45000, 29.56667],
        "青島": [120.33333,36.06667],
        "廈門": [118.10000,24.46667],
        "福州": [119.30000,26.08333],
        "蘭州": [103.73333,36.03333],
        "長沙": [113.00000,28.21667],
        "南京": [118.78333,32.05000],
        "海外": [130.41667,36.91667],
    };
    //市區(qū)數(shù)據(jù)
    var rawData = [
        ["北京",5,20,30],
        ["上海",10,10,30],
        ["廣州",10,50,30],
        ["杭州",10,20,3],
        ["重慶",10,20,8],
        ["青島",10,20,10],
        ["廈門",10,20,4],
        ["福州",10,10,30],
        ["蘭州",10,15,30],
        ["長沙",10,25,30],
        ["南京",10,20,5],
        ["海外",10,5,10]
    ];


    function makeMapData(rawData) {
        var mapData = [];
        for (var i = 0; i < rawData.length; i++) {
            var geoCoord = geoCoordMap[rawData[i][0]];//某個市區(qū)得經(jīng)緯度
            if (geoCoord) {
                mapData.push({
                    name: rawData[i][0],//某個市區(qū)的名稱
                    value: geoCoord.concat(rawData[i].slice(1))
                });
            }
        }
        console.log(mapData);
        return mapData;
    };

    option = {
        animation: false,
        // 地圖背景顏色
        backgroundColor: new echarts.graphic.RadialGradient(0.5, 0.5, 0.4, [{
            offset: 0,
            color: '#fff'
        }, {
            offset: 1,
            color: '#fff'
        }]),
        tooltip: {
            trigger: 'axis'
        },
        geo: {
            map: 'china',
            // silent: true,
            roam: true,
            zoom: 0.9, // 地圖初始大小
            center: [116.366794, 40.400309], // 初始中心位置
            label: {
                normal:{
                    show:false
                },
                emphasis: {
                    show: false,
                    areaColor: '#eee'
                }
            },
            // 地區(qū)塊兒顏色
            itemStyle: {
                normal: {
                    areaColor: '#f3e5a1',
                    borderColor: '#877167'
                },
                emphasis: {
                    areaColor: '#21AEF8'
                }
            }
        },
        series: []
    };

    function renderEachCity() {
        var option = {
            title:[],
            grid: [],
            legend: {
                x : '64%',
                y : '30%',
                orient:'vertical',
                data:['乘用車','客車','專用車']
            },
            series: []
        };

        echarts.util.each(rawData, function(dataItem, idx) {
            var geoCoord = geoCoordMap[dataItem[0]];
            var coord = myChart.convertToPixel('geo', geoCoord);
            idx += '';
            inflationData = [
                { name: '乘用車', value: dataItem[1] },
                { name: '客車', value: dataItem[2] },
                { name: '專用車', value: dataItem[3] },
            ]
            var total =  dataItem[1]+dataItem[2]+dataItem[3];
            var title ={
                text: dataItem[0],
                textStyle:{
                    fontSize:10,
                    fontWeight:'bold',
                },
                x: coord[0]-15,
                y: coord[1]+15
            };
            option.title.push(title);
            option.grid.push({
                id: idx,
                gridId:idx,
                width: 30,
                height: 40,
                left: coord[0] - 15,
                top: coord[1] - 15,
                z: 100
            });
            option.series.push({
                id: idx,
                type: 'pie',
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                lableLine: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                radius:total>50 ? '4%' :( total > 40 ? '3%' :(total>30 ? '2%':'1%')) ,
                center:coord,
                data: inflationData,
                z: 100,
                itemStyle: {
                    normal: {
                        color: function(params){
                            // 柱狀圖每根柱子顏色
                            var colorList = ['#fcae91','#fb6a4a','#cb181d'];
                            return colorList[params.dataIndex];
                        }
                    }
                }
            });
        });
        myChart.setOption(option);
    }

    setTimeout(renderEachCity, 0);
    // 縮放和拖拽
    function throttle(fn, delay, debounce) {

        var currCall;
        var lastCall = 0;
        var lastExec = 0;
        var timer = null;
        var diff;
        var scope;
        var args;

        delay = delay || 0;

        function exec() {
            lastExec = (new Date()).getTime();
            timer = null;
            fn.apply(scope, args || []);
        }

        var cb = function() {
            currCall = (new Date()).getTime();
            scope = this;
            args = arguments;
            diff = currCall - (debounce ? lastCall : lastExec) - delay;

            clearTimeout(timer);

            if (debounce) {
                timer = setTimeout(exec, delay);
            } else {
                if (diff >= 0) {
                    exec();
                } else {
                    timer = setTimeout(exec, -diff);
                }
            }

            lastCall = currCall;
        };

        return cb;
    }

    var throttledRenderEachCity = throttle(renderEachCity, 0);
    myChart.on('geoRoam', throttledRenderEachCity);
    myChart.setOption(option);


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

我的這部分代碼也是從一個博客上修改后的,具體原博客的地址我找不到了,他的是顯示柱狀圖我給改成了餅圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,405評論 4 61
  • 定義: 由一個工廠對象決定創(chuàng)建出哪一種產(chǎn)品類的實例. 類型: 創(chuàng)建型(creational), 但不屬于GOF23...
    a_salt_fish閱讀 293評論 0 4
  • 不工作讓陷入了焦慮不安的狀態(tài),滿身的疲憊無法宣泄,無處訴說。告訴自己不要惶恐,可還是滿心糾結(jié)。既希望離開家去外面工...
    歐陽紫凌閱讀 151評論 0 0
  • 《如水赤子夢》 我們都有過——爛漫天真的童年,朝氣蓬勃的少年,因年少無知而輕狂妄為的青春…… 更亦有過, 想用一生...
    5ce43e45b388閱讀 382評論 0 0

友情鏈接更多精彩內(nèi)容