Echarts地图柱状图问题

1、Echarts地图柱状图问题

需要根据功能修改自己所需的地图模块,当前功能是根据line和散点图进行处理,根据经纬度形成一个柱状图,

比如:需要两个或者多个柱状图

1、添加多个经纬度,添加多个柱状图。经纬度跟柱状图一一对应

2、根据定位,自己写柱状图。一一添加到地图上去。分为两个部分,1.Echarts地图,2.柱状图,可以写div

2、Echarts地图饼状图问题

需要渲染地图成功后,再通过setOption添加series。

3、柱状图例子

河南省经纬度数据【js/Map/henan.json · 梦_阿飞/allStaticResources - Gitee.com

npm install echarts -S

<template>
  <div class="DataList">
    <div class="Echart-map" id="echartmapWidth">
        <div id="echart" ref="echart"></div>
    </div>
  </div>
</template>
<script>
import * as echarts from "echarts";
import { useStore } from "vuex";
import hnJson from "@/utils/henan.json";
import { onMounted, onUnmounted, watch, ref, computed } from "vue";
export default {
  name: "DataList",
  setup() {
    let echart = echarts;
    let echartMap = null;
    let geoCoordMap = {};
    let sddf = [];
    let option = {
      series: [],
    };
    const store = useStore();
    const state = store;
    let isCollapse = ref(
      computed(() => {
        return state.state.app.isCollapse;
      })
    );
    const loading = ref(false);
    
    let screenWidth = ref(
      window.innerWidth ||
        document.documentElement.clientWidth ||
        document.body.clientWidth
    );
    let timer = ref(false); //处理监听
    watch(screenWidth, () => {
      if (!timer.value) {
        timer.value = true;
        setTimeout(function () {
          timer.value = false;
          echartMap.resize();
        }, 100);
      }
    });
    watch(isCollapse, () => {
      setTimeout(() => {
        echartMap.resize();
      }, 300);
    });
    const customerBatteryCityData = [];
    hnJson.features.map((item) => {
      geoCoordMap[item.properties.name] = item.properties.center;
      var data = {
        name: item.properties.name,
        value: item.properties.adcode,
      };
      customerBatteryCityData.push(data);
    });

    const initChart = () => {
      echart.registerMap("henan", hnJson);
      echartMap = echart.init(document.getElementById("echart"));

      echartMap.setOption({
        backgroundColor: "#131C38",
        tooltip: {
          // borderWidth: 0,
          trigger: "item",
          show: true,
          enterable: true,
          textStyle: {
            fontSize: 20,
            color: "#fff",
          },
          backgroundColor: "rgba(0,2,89,0.8)",
          formatter: "{b}<br />{c}",
        },
        geo: [
          {
            map: "henan",
            aspectScale: 0.9,
            roam: false, // 是否允许缩放
            // zoom: 1.2, // 默认显示级别
            zoom: 1, // 默认显示级别
            layoutSize: "70%",
            silent: false, //点击
            layoutCenter: ["45%", "55%"],
            itemStyle: {
              normal: {
                areaColor: {
                  type: "linear-gradient",
                  x: 0,
                  y: 400,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: "rgba(37,108,190,0.3)", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "rgba(15,169,195,0.3)", // 50% 处的颜色
                    },
                  ],
                  global: true, // 缺省为 false
                },
                borderColor: "#4ecee6",
                borderWidth: 1,
              },
              emphasis: {
                areaColor: {
                  type: "linear-gradient",
                  x: 0,
                  y: 300,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: "rgba(37,108,190,1)", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "rgba(15,169,195,1)", // 50% 处的颜色
                    },
                  ],
                  global: true, // 缺省为 false
                },
              },
            },
            emphasis: {
              // itemStyle: {
              //   areaColor: "#0160AD",
              // },
              label: {
                show: true,
                color: "#fff",
              },
            },
            zlevel: 3,
            label: {
              show: true,
              color: "#fff",
            },
          },
        ],
        series: [
          {
            geoIndex: 0,
            showLegendSymbol: true,
            type: "map",
            roam: true,
            label: {
              normal: {
                show: true,
                textStyle: {
                  color: "#fff",
                },
              },
              emphasis: {
                show: true,
                textStyle: {
                  color: "#fff",
                },
              },
            },
            itemStyle: {},
            map: "henan", // 使用
            data: customerBatteryCityData,
            select: {
              disabled: true,
              label: {
                color: "#fff",
              },
            },
            emphasis: {
              label: {
                color: "#fff",
              },
            },
          },
          {
            type: "lines",
            zlevel: 5,
            effect: {
              show: false,
              // period: 4, //箭头指向速度,值越小速度越快
              // trailLength: 0.02, //特效尾迹长度[0,1]值越大,尾迹越长重
              // symbol: 'arrow', //箭头图标
              // symbol: imgDatUrl,
              symbolSize: 5, // 图标大小
            },
            lineStyle: {
              width: 17, // 尾迹线条宽度
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [
                  {
                    offset: 0,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "blue", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
              opacity: 1, // 尾迹线条透明度
              curveness: 0, // 尾迹线条曲直度
            },
            label: {
              show: 0,
              position: "end",
              formatter: "245",
            },
            silent: true,
            data: lineData(),
          },
          {
            type: "scatter",
            coordinateSystem: "geo",
            geoIndex: 0,
            zlevel: 5,
            label: {
              show: true,
              position: "top",
              // formatter: (params) => this.data[params.dataIndex].value,
              formatter: function (params) {
                return customerBatteryCityData[params.dataIndex].value;
              },
              padding: [4, 8],
              // backgroundColor: "#fff",
              borderRadius: 5,
              // borderColor: "#fff",
              borderWidth: 1,
              // color: "#fff",
            },
            symbol: "diamond",
            symbolSize: [15, 8],
            // symbolSize: [0, 0],
            itemStyle: {
              // color: "red",
              color: "#fff",
              opacity: 1,
            },
            silent: true,
            data: scatterData(),
          },
          {
            type: "scatter",
            coordinateSystem: "geo",
            geoIndex: 0,
            zlevel: 4,
            // label: {
            //   formatter: "{b}",
            //   position: "top",
            //   color: "#fff",
            //   fontSize: 12,
            //   distance: 10,
            //   show: true,
            // },
            symbol: "diamond",
            symbolSize: [17, 8],
            itemStyle: {
              // color: '#F7AF21',
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [
                  {
                    offset: 0,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "blue", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "blue", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
              opacity: 1,
              // shadowColor: '#fff',
              // shadowBlur: 5,
              // shadowOffsetY: 2
            },
            silent: true,
            data: scatterData2(),
          },
        ],
      });
      echartMap.resize();
      // 执行完地图讲serier用setOption重新塞入
    };
    // 动态计算柱形图的高度(定一个max)
    const lineMaxHeight = () => {
      const maxValue = Math.max(
        ...customerBatteryCityData.map((item) => item.value)
      );
      return 0.9 / maxValue;
    };
    // 柱状体的主干
    const lineData = () => {
      return customerBatteryCityData.map((item) => {
        return {
          coords: [
            geoCoordMap[item.name],
            [
              geoCoordMap[item.name][0],
              geoCoordMap[item.name][1] + item.value * lineMaxHeight(),
            ],
          ],
        };
      });
    };
    // 柱状体的顶部
    const scatterData = () => {
      return customerBatteryCityData.map((item) => {
        return [
          geoCoordMap[item.name][0],
          geoCoordMap[item.name][1] + item.value * lineMaxHeight(),
        ];
      });
    };
    // 柱状体的底部
    const scatterData2 = () => {
      return customerBatteryCityData.map((item) => {
        return {
          name: item.name,
          value: geoCoordMap[item.name],
        };
      });
    };
    const destroyedChart = () => {
      echartMap.dispose();
    };
    // // 地图饼状图
    const mapPie = () => {
      // 地图饼状图
      sddf = [
        {
          type: "pie",
          radius: "5%",
          // 设置成绝对的像素值
          center: echartMap.convertToPixel("geo", [113.238266, 35.23904]),
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: "center",
          },
          emphasis: {
            label: {
              show: false,
              fontSize: "40",
              fontWeight: "bold",
            },
          },
          labelLine: {
            show: false,
          },
          data: [
            { value: 100, name: "搜索引擎" },
            { value: 735, name: "直接访问" },
          ],
        },
        {
          type: "pie",
          radius: "5%",
          // 设置成绝对的像素值
          center: echartMap.convertToPixel("geo", [113.883991, 35.302616]),
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: "center",
          },
          emphasis: {
            label: {
              show: false,
              fontSize: "40",
              fontWeight: "bold",
            },
          },
          labelLine: {
            show: false,
          },
          data: [
            { value: 484, name: "联盟广告" },
            { value: 300, name: "视频广告" },
          ],
        },
      ];
      option.series = sddf;
      echartMap.setOption(option);
    };
    const search = () => {
      loading.value = true;
      setTimeout(() => {
        loading.value = false;
      }, 1000);
    };

    onMounted(() => {
      initChart();
      // // mapPie()
      window.onresize = () => {
        return (() => {
          window.screenWidth =
            window.innerWidth ||
            document.documentElement.clientWidth ||
            document.body.clientWidth;
          screenWidth.value = window.screenWidth;
        })();
      };
    });
    onUnmounted(() => {
      // destroyedChart();
    });
    return {
      initChart,
      destroyedChart,
      mapPie,
      search,
      screenWidth,
      timer,
      loading,
      isCollapse,
    };
  },
};
</script>
<style scoped lang='scss'>
.Echart-map {
  overflow: hidden;
  width: 100%;
}
#echart {
  width: 100%;
  height: 500px;
  padding: 16px;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
echarts中,地图的柱状渐变可以通过配置visualMap来实现。引用中提到,echarts的visualMap默认是根据地图块对应的value数值的大小自动分配颜色,但如果想自定义整体渐变色的场景,可以通过以下步骤来实现。 首先,需要在echarts的配置项中添加visualMap组件,设置type为'continuous'(连续型),并指定最小值(min)和最大值(max)的范围。然后,通过设置color属性为数组形式,定义渐变的颜色范围。可以指定多个颜色值,echarts会自动根据value值在该颜色范围内进行渐变。同时,也可以通过设置text属性来自定义颜色范围的标签显示。 下面是一个简单的echarts配置示例,展示了如何实现地图的柱状渐变: ``` javascript option = { visualMap: { type: 'continuous', min: 0, max: 100, color: ['#FF0000', '#FFFF00', '#00FF00'], text: ['High', 'Low'], calculable: true }, series: [ { type: 'map', mapType: 'world', data: [ { name: 'China', value: 80 }, { name: 'USA', value: 50 }, { name: 'Russia', value: 30 } // 其他地区的数据 ] } ] }; ``` 在以上示例中,visualMap设置了最小值为0,最大值为100,并定义了一个由红色到绿色渐变的颜色范围。通过series中的data属性,可以指定各个地区的value值,echarts会根据value值来确定柱状的颜色。 需要注意的是,以上只是一个简单的示例,实际使用中可能需要根据具体需求进行调整和扩展。可以通过查阅echarts官方文档以获取更详细的配置信息和示例代码。引用给出了一个完成echarts项目所需的具体知识点,包括div、css布局、flex布局、css3动画、css3渐变、css3边框图片、原生js和jquery使用、rem适配以及echarts基础。 总结,通过在echarts的配置项中添加visualMap组件,并设置type为'continuous',可以实现地图的柱状渐变效果。通过设置最小值、最大值和颜色范围,可以自定义整体渐变色的样式。具体的配置和实现方式可以参考echarts官方文档和示例代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [echarts地图整体渐变色](https://blog.csdn.net/Bs__Q/article/details/109823437)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [数据可视化练习,里面有笔记代码和素材](https://download.csdn.net/download/weixin_61513227/87596198)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值