BIGEMPA Js API示例中心

交互式饼状图源代码展示

代码编辑区 运行 下载 还原
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!--
            以下CSS地址请在安装软件了替换成本地的地址
            CSS地址请使用:
            http://localhost:9000/bigemap.js/v2.1.0/bigemap.css
            软件下载地址 http://www.bigemap.com/reader/download/detail201802017.html
        -->
    <link href='http://www.bigemap.com:9000/bigemap.js/v2.1.0/bigemap.css' rel='stylesheet' />
    <!--
            JS地址请使用:
            http://localhost:9000/bigemap.js/v2.1.0/bigemap.js
        -->
    <script src='http://www.bigemap.com:9000/bigemap.js/v2.1.0/bigemap.js'></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/d3/5.16.0/d3.min.js"></script>
</head>
<style>
    body {
        margin: 0;
        padding: 0;
    }

    #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
    }

    .left {
        position: absolute;
        z-index: 10;
        height: 100%;
          width: 600px;
        background-color: black;
        opacity: .5;
    }

    img {
        position: absolute;
        width: 26px;
        right: 2px;
    }

    .d3 {
        position: absolute;
        z-index: 10;
        top: 30px;
    }

    .svg1 {
        /* background: white; */
        border-radius: 50%;
        margin-top: 30px;
    }

    .toptitle {
        color: snow;
        font-size: 27px;
        text-align: center;
    }
</style>

<body>
    <div class="left">
        <span>
            <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIIAAACCCAMAAAC93eDPAAAAPFBMVEX///8AAAD4+Pjy8vJbW1v19fVYWFhERERVVVU9PT1SUlJgYGBPT09HR0dLS0vHx8eqqqrT09N2dnZwcHCtXMiCAAADmElEQVR4nO1b25aCMAwUar2vrvr//7q7R9BEm5ArZx+YV2sZ6DRhGFitFixYsGDBJK6Xkn2Icrkyv353v7j3mQT6+98xvlkGXXdKvBDl9DgGxaEbsE7jUNbjMdq/X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33A7Yby2D1Dz7jW7BgwYIFA34AFNccG0zpySgAAAAASUVORK5CYII="
                alt="">
        </span>
        <div class="toptitle">

        </div>
    </div>
    <div class="d3">

    </div>
    <div id='map'></div>
</body>
<script>
    //软件配置信息地址,软件安装完成之后使用本地地址,如:http://localhost:9000
    BM.Config.HTTP_URL = 'http://www.bigemap.com:9000';
    // 在ID为map的元素中实例化一个地图,并设置地图的ID号为 bigemap.googlemap-streets,ID号程序自动生成,无需手动配置,并设置地图的投影为百度地图 ,中心点,默认的级别和显示级别控件
    var map = BM.map('map', 'bigemap.googlemap-streets', { center: [30, 104], zoom: 4, zoomControl: true });
    $.get('/Public/offline/bounds/100000.geojson', function (data) {
        data = JSON.parse(data);
        var all = ["#f7acbc", "#deab8a", "#817936", "#444693", "#ef5b9c", "#fedcbd", "#7f7522", "#2b4490", "#feeeed", "#f47920", "#80752c", "#2a5caa", "#f05b72", "#905a3d", "#87843b", "#224b8f", "#f15b6c", "#8f4b2e", "#726930", "#003a6c", "#f8aba6", "#87481f", "#454926", "#102b6a", "#f69c9f", "#5f3c23", "#2e3a1f", "#426ab3", "#f58f98", "#6b473c", "#4d4f36", "#46485f", "#ca8687", "#faa755", "#b7ba6b", "#4e72b8", "#f391a9", "#fab27b", "#b2d235", "#181d4b", "#bd6758", "#f58220", "#5c7a29", "#1a2933", "#d71345", "#843900", "#bed742", "#121a2a", "#d64f44", "#905d1d", "#7fb80e", "#0c212b", "#d93a49", "#8a5d19", "#a3cf62", "#6a6da9", "#b3424a", "#8c531b", "#769149", "#585eaa", "#c76968", "#826858", "#6d8346", "#494e8f", "#bb505d", "#64492b", "#78a355", "#afb4db", "#987165", "#ae6642", "#abc88b", "#9b95c9", "#ac6767", "#56452d", "#74905d", "#6950a1", "#973c3f", "#96582a", "#cde6c7", "#6f60aa", "#b22c46", "#705628", "#1d953f", "#867892", "#a7324a", "#4a3113", "#77ac98", "#918597", "#aa363d", "#412f1f", "#007d65", "#6f6d85", "#ed1941", "#845538", "#84bf96", "#594c6d", "#f26522", "#8e7437", "#45b97c", "#694d9f", "#d2553d", "#69541b", "#225a1f", "#6f599c", "#b4534b", "#d5c59f", "#367459", "#8552a1", "#ef4136", "#cd9a5b", "#007947", "#543044", "#c63c26", "#cd9a5b", "#40835e", "#63434f", "#f3715c", "#b36d41", "#2b6447", "#7d5886", "#a7573b", "#df9464", "#005831", "#401c44", "#aa2116", "#b76f40", "#006c54", "#472d56", "#b64533", "#ad8b3d", "#375830", "#45224a", "#b54334", "#dea32c", "#274d3d", "#411445", "#853f04", "#d1923f", "#375830", "#4b2f3d", "#840228", "#c88400", "#27342b", "#402e4c", "#7a1723", "#c37e00", "#65c294", "#c77eb5", "#a03939", "#c37e00", "#73b9a2", "#ea66a6", "#8a2e3b", "#e0861a", "#72baa7", "#f173ac", "#8e453f", "#ffce7b", "#005344", "#fffffb", "#8f4b4a", "#fcaf17", "#122e29", "#fffef9", "#892f1b", "#ba8448", "#293047", "#f6f5ec", "#6b2c25", "#896a45", "#00ae9d", "#d9d6c3", "#733a31", "#76624c", "#508a88", "#d1c7b7", "#54211d", "#6d5826", "#70a19f", "#f2eada", "#78331e", "#ffc20e", "#50b7c1", "#d3d7d4", "#53261f", "#fdb933", "#00a6ac", "#999d9c", "#f15a22", "#d3c6a6", "#78cdd1", "#a1a3a6", "#b4533c", "#c7a252", "#008792", "#9d9087", "#84331f", "#dec674", "#94d6da", "#8a8c8e", "#f47a55", "#b69968", "#afdfe4", "#74787c", "#f15a22", "#c1a173", "#5e7c85", "#7c8577", "#f3704b", "#dbce8f", "#76becc", "#72777b", "#da765b", "#ffd400", "#90d7ec", "#77787b", "#c85d44", "#ffd400", "#009ad6", "#4f5555", "#ae5039", "#ffe600", "#145b7d", "#6c4c49", "#6a3427", "#f0dc70", "#11264f", "#563624", "#8f4b38", "#fcf16e", "#7bbfea", "#3e4145", "#8e3e1f", "#decb00", "#33a3dc", "#3c3645", "#f36c21", "#cbc547", "#228fbd", "#464547", "#b4532a", "#6e6b41", "#2468a2", "#130c0e", "#b7704f", "#596032", "#2570a1", "#281f1d", "#de773f", "#525f42", "#2585a6", "#2f271d", "#c99979", "#5f5d46", "#1b315e", "#1d1626"];
        BM.geoJSON(data, {
            style: function () {
                return { color: 'white', fillColor: all[Math.floor(Math.random() * all.length)], weight: 1, fillOpacity: 0.9 };
            },
            //逐个绑定事件
            onEachFeature: function (feature, layer) {
                feature.properties && feature.properties.name && layer.bindTooltip(feature.properties.name, { direction: 'bottom', className: 'my_tooltip', permanent: true });
                  if (feature.properties.name == '四川省') {
                    drawarc(feature)
                }
                layer.on('click', function () {
                    $('.toptitle').html(feature.properties.name + "XXXXXX数据展示")
                    if ($('.left').css('width') == '0px') {
                        $(".left").animate({ width: 600 }, 1000)
                    }
                    setTimeout(function () {
                        d3.selectAll('.svg1').remove()
                        console.log(feature.properties.name);
                        var height = 600
                        var width = 600
                        //开始绘制饼状图
                        //获取数据
                        var data = $.get('/Public/d3json/bb.json', function (data) {
                            console.log(data);
                            //处理数据
                            partition = data => {
                                const root = d3.hierarchy(data)
                                    .sum(d => d.value)
                                    .sort((a, b) => b.value - a.value);
                                return d3.partition()
                                    .size([2 * Math.PI, root.height + 1])
                                    (root);
                            }
                            //颜色比例尺
                            color = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, data.children.length + 1))
                            //格式化
                            format = d3.format(",d")
                            //半径
                            radius = width / 6
                            //圆生成器
                            arc = d3.arc()
                                .startAngle(d => d.x0)
                                .endAngle(d => d.x1)
                                .padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
                                .padRadius(radius * 1.5)
                                .innerRadius(d => d.y0 * radius)
                                .outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1))
                            var root = partition(data);
                            root.each(d => d.current = d);
                            //创建一个svg图像
                            var svg = d3.select('.d3').append('svg').attr('height', height).attr('width', width).style("font", "10px sans-serif").attr('class', 'svg1');
                            var g = svg.append("g").attr("transform", `translate(${width / 2},${width / 2})`);
                            var path = g.append("g")
                                .selectAll("path")
                                .data(root.descendants().slice(1))
                                .join("path")
                                .attr("fill", d => { while (d.depth > 1) d = d.parent; return color(d.data.name); })
                                .attr("fill-opacity", d => arcVisible(d.current) ? (d.children ? 0.9 : 0.6) : 0)
                                .attr("d", d => arc(d.current));
                            //点击图形
                            path.filter(d => d.children)
                                .style("cursor", "pointer")
                                .on("click", clicked);
                            path.append("title")
                                .text(d => `${d.ancestors().map(d => d.data.name).reverse().join("/")}\n${format(d.value)}`);
                            var label = g.append("g")
                                .attr("pointer-events", "none")
                                .attr("text-anchor", "middle")
                                .style("user-select", "none")
                                .selectAll("text")
                                .data(root.descendants().slice(1))
                                .join("text")
                                .attr("dy", "0.35em")
                                .attr("fill-opacity", d => +labelVisible(d.current))
                                .attr("transform", d => labelTransform(d.current))
                                .text(d => d.data.name);
                            //添加原形
                            var parent = g.append("circle")
                                .datum(root)
                                .attr("r", radius)
                                .attr("fill", "none")
                                .attr("pointer-events", "all")
                                .on("click", clicked);
                            //点击事件
                            function clicked(p) {
                                parent.datum(p.parent || root);

                                root.each(d => d.target = {
                                    x0: Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
                                    x1: Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
                                    y0: Math.max(0, d.y0 - p.depth),
                                    y1: Math.max(0, d.y1 - p.depth)
                                });

                                const t = g.transition().duration(750);

                                // Transition the data on all arcs, even the ones that aren’t visible,
                                // so that if this transition is interrupted, entering arcs will start
                                // the next transition from the desired position.
                                //过度效果
                                path.transition(t)
                                    .tween("data", d => {
                                        const i = d3.interpolate(d.current, d.target);
                                        return t => d.current = i(t);
                                    })
                                    .filter(function (d) {
                                        return +this.getAttribute("fill-opacity") || arcVisible(d.target);
                                    })
                                    .attr("fill-opacity", d => arcVisible(d.target) ? (d.children ? 0.9 : 0.6) : 0)
                                    .attrTween("d", d => () => arc(d.current));

                                label.filter(function (d) {
                                    return +this.getAttribute("fill-opacity") || labelVisible(d.target);
                                }).transition(t)
                                    .attr("fill-opacity", d => +labelVisible(d.target))
                                    .attrTween("transform", d => () => labelTransform(d.current));
                            }
                            function arcVisible(d) {
                                return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
                            }

                            function labelVisible(d) {
                                return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
                            }

                            function labelTransform(d) {
                                const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
                                const y = (d.y0 + d.y1) / 2 * radius;
                                return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
                            }
                        })
                    }, 1000)

                })
            }
            //鼠标移入
        }).on('mouseover', function (e) {
            e.layer.setStyle({ color: 'white', fillColor: 'red', weight: 1, fillOpacity: 0.9 });
            //鼠标移出
        }).on('mouseout', function (e) {
            e.layer.setStyle({ color: 'white', fillColor: all[Math.floor(Math.random() * all.length)], weight: 1, fillOpacity: 0.9 });
        }).addTo(map);
    })
    //点击X 图片的事件
    $("span").on('click', function () {
        d3.selectAll('.svg1').remove()
        $(".left").animate({ width: 0 }, 500)
    })
  function drawarc(feature) {
        $('.toptitle').html(feature.properties.name + "XXXXXX数据展示")
        d3.selectAll('.svg1').remove()
        console.log(feature.properties.name);
        var height = 600
        var width = 600
        //开始绘制饼状图
        //获取数据
        var data = $.get('/Public/d3json/bb.json', function (data) {
            console.log(data);
            //处理数据
            partition = data => {
                const root = d3.hierarchy(data)
                    .sum(d => d.value)
                    .sort((a, b) => b.value - a.value);
                return d3.partition()
                    .size([2 * Math.PI, root.height + 1])
                    (root);
            }
            //颜色比例尺
            color = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, data.children.length + 1))
            //格式化
            format = d3.format(",d")
            //半径
            radius = width / 6
            //圆生成器
            arc = d3.arc()
                .startAngle(d => d.x0)
                .endAngle(d => d.x1)
                .padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
                .padRadius(radius * 1.5)
                .innerRadius(d => d.y0 * radius)
                .outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1))
            var root = partition(data);
            root.each(d => d.current = d);
            //创建一个svg图像
            var svg = d3.select('.d3').append('svg').attr('height', height).attr('width', width).style("font", "10px sans-serif").attr('class', 'svg1');
            var g = svg.append("g").attr("transform", `translate(${width / 2},${width / 2})`);
            var path = g.append("g")
                .selectAll("path")
                .data(root.descendants().slice(1))
                .join("path")
                .attr("fill", d => { while (d.depth > 1) d = d.parent; return color(d.data.name); })
                .attr("fill-opacity", d => arcVisible(d.current) ? (d.children ? 0.9 : 0.6) : 0)
                .attr("d", d => arc(d.current));
            //点击图形
            path.filter(d => d.children)
                .style("cursor", "pointer")
                .on("click", clicked);
            path.append("title")
                .text(d => `${d.ancestors().map(d => d.data.name).reverse().join("/")}\n${format(d.value)}`);
            var label = g.append("g")
                .attr("pointer-events", "none")
                .attr("text-anchor", "middle")
                .style("user-select", "none")
                .selectAll("text")
                .data(root.descendants().slice(1))
                .join("text")
                .attr("dy", "0.35em")
                .attr("fill-opacity", d => +labelVisible(d.current))
                .attr("transform", d => labelTransform(d.current))
                .text(d => d.data.name);
            //添加原形
            var parent = g.append("circle")
                .datum(root)
                .attr("r", radius)
                .attr("fill", "none")
                .attr("pointer-events", "all")
                .on("click", clicked);
            //点击事件
            function clicked(p) {
                parent.datum(p.parent || root);

                root.each(d => d.target = {
                    x0: Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
                    x1: Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
                    y0: Math.max(0, d.y0 - p.depth),
                    y1: Math.max(0, d.y1 - p.depth)
                });

                const t = g.transition().duration(750);

                // Transition the data on all arcs, even the ones that aren’t visible,
                // so that if this transition is interrupted, entering arcs will start
                // the next transition from the desired position.
                //过度效果
                path.transition(t)
                    .tween("data", d => {
                        const i = d3.interpolate(d.current, d.target);
                        return t => d.current = i(t);
                    })
                    .filter(function (d) {
                        return +this.getAttribute("fill-opacity") || arcVisible(d.target);
                    })
                    .attr("fill-opacity", d => arcVisible(d.target) ? (d.children ? 0.9 : 0.6) : 0)
                    .attrTween("d", d => () => arc(d.current));

                label.filter(function (d) {
                    return +this.getAttribute("fill-opacity") || labelVisible(d.target);
                }).transition(t)
                    .attr("fill-opacity", d => +labelVisible(d.target))
                    .attrTween("transform", d => () => labelTransform(d.current));
            }
            function arcVisible(d) {
                return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
            }

            function labelVisible(d) {
                return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
            }

            function labelTransform(d) {
                const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
                const y = (d.y0 + d.y1) / 2 * radius;
                return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
            }
        })
    }

</script>

</html>