实现echart的地图添加点并且根据状态进行渲染颜色

背景

vue3.2
echart

实现

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import * as echarts from 'echarts'
import chinaMap from './china.json'
import { GetPoint } from '@/api/mainapi'; // 导入GetPoint方法
import zuobiaoSvg from '../../../assets/svg/zuobiao.svg';
import { toRaw } from '@vue/reactivity';


 

onMounted(()=>{
     init(); // 将init函数调用移动到test赋值之后
    fetchData(); // 在mounted钩子函数中调用fetchData函数
   
})

let test =  null;

            

const fetchData = () => {
  GetPoint().then(response => {
//    console.log(response);
      test=response.data;
      //console.log(test);
    init(); // 将init函数调用移动到test赋值之后

});
}
const chart = ref();



async function init() {
    var myChart = echarts.init(chart.value);
    echarts.registerMap('china',chinaMap);
 

  // data.mapData = response.data
    var option = {
        series:[{
            roam: true,
            type:'map',
            map:'china',
           center: [ 112.909956,31.775001], //定义中心点

            label:{
                show:false,
                fontsize:10,
                color:'white',
                
            },
            
            tooltip: {
                show: true,
                formatter: function(e) {
                    // return e.data.displayname;
                    return 'hello';
                },
                // position: [500, 10],
                triggerOn: 'click'
            },
            itemStyle:{
                    areaColor:'#395481',
                    borderColor:'#148A9E',
                    borderWidth:'0.8',
                    shadowColor: "#14FAFE",
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    shadowBlur: 2,
            },
            emphasis:{
                label:{
                show:true,
                fontsize:10,
                color:'white'
            },
            itemStyle:{
                    areaColor:'#27508E',
                    borderColor:'#12EFED',
                    borderWidth:'1'
            },
            },
        
            markPoint: {
            symbol:'pin',
            style: {
                        image: zuobiaoSvg,
                    
                        width: 24, // 根据 SVG 图片的大小调整宽度
                        height: 24, // 根据 SVG 图片的大小调整高度
                    },
            symbolSize: [20, 20], // 图片大小
            label: {
              position: 'top',
              color: 'white',
            },
                
            
            // coord数组存放地址坐标
            // data: test
            data: test.map(item => {
                    return {
                        ...item,
            itemStyle: {
                        color: item.workState === 0 ? 'red' :
                            item.workState === 1 ? 'yellow' :
                            item.workState === 2 ? 'pink' :
                            item.workState === 3 ? 'blue' :
                            'gray'
                    }
                    }
                })
            }
        }]
       
    };
    myChart.setOption(option);
    

    
}

</script>

主要代码

itemStyle: {
color: item.workState === 0 ? ‘red’ :
item.workState === 1 ? ‘yellow’ :
item.workState === 2 ? ‘pink’ :
item.workState === 3 ? ‘blue’ :
‘gray’
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值