vue2结合echarts实现中国地图(省),以及数据交互

1、效果

vue2使用echarts实现地图加载

2、准备

安装echarts

npm install echarts --save

 页面准备

<template>
  <div class="map" ref="mapRef"></div>
</template>
<script>
const echarts = require('echarts');
// import mapJson from './assets/china.json';
import mapJson from 'echarts/map/json/china.json';
export default {
  data() {
    return {
      
    };
  },
  created() {
    
  },
  mounted() {
    
  },
  methods: {

  }
};
</script>
<style scoped lang="scss">
.map {
  width: 100%;
  height: 100%;
}
</style>

3、开始

直接上代码

<template>
  <div class="map" ref="mapRef"></div>
</template>
<script>
const echarts = require('echarts');
// import mapJson from './assets/china.json';
import mapJson from 'echarts/map/json/china.json';
export default {
  data() {
    return {
      option: {},
      mapRef: null
    };
  },
  created() {
    this.saveOption();
  },
  mounted() {
    this.mapRef = echarts.init(this.$refs.mapRef);
    echarts.registerMap('china', mapJson);
    this.getData();
    this.mapRef.on('click', params => {
      console.log('params', params);
      if (params.componentType === 'series') {
        const dataIndex = params.dataIndex;
        const seriesIndex = params.seriesIndex;
        const currentShowLabel = this.option.series[seriesIndex].data[dataIndex].label.show;
        console.log('currentShowLabel', currentShowLabel);
        this.option.series[seriesIndex].data[dataIndex].label.show = !currentShowLabel;
        this.mapRef.setOption(this.option, true);
      }
    });
    window.addEventListener('resize', this.echartsResize, false);
  },
  methods: {
    echartsResize() {
      this.mapRef.resize();
    },
    saveOption() {
      const option = {
        // echarts大小位置
        grid: {
          left: '0px',
          right: '80px',
          top: '10px',
          bottom: '10px'
        },
        geo: {
          map: 'china',
          zoom: 1,
          center: [102.848234, 32.82333],
          scaleLimit: {
            min: 1,
            max: 2
          },
          label: {
            color: '#fff',
            show: true
          },
          emphasis: {
            label: {
              color: '#fff',
              show: true
            },
            itemStyle: {
              areaColor: {
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: '#073684' // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: '#2B91B7' // 100% 处的颜色
                  }
                ]
              }
            }
          },
          roam: true,
          itemStyle: {
            areaColor: {
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {
                  offset: 0,
                  color: '#073684' // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#061E3D' // 100% 处的颜色
                }
              ]
            },
            borderColor: new echarts.graphic.LinearGradient(
              0,
              0,
              0,
              1,
              [
                {
                  offset: 0,
                  color: '#00F6FF'
                },
                {
                  offset: 1,
                  color: '#87ADCB'
                }
              ],
              false
            ),
            shadowColor: 'rgba(10,76,139,1)',
            shadowOffsetY: 0,
            shadowBlur: 60,
            borderWidth: 1
          },
          tooltip: {
            show: false
          }
        },
        // 要显示的散点数据
        series: [
          {
            type: 'scatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
              period: 4,
              brushType: 'stroke',
              scale: 4
            },
            label: {
              show: true,
              color: '#fff'
            },
            symbol: 'pin', // 定位图标样式
            symbolSize: 40,
            // 点的名称和经纬度
            data: [
              {
                name: 'xxxx医院',
                value: [106.093437, 30.791804],
                // 单独修改圆环上的文字
                label: {
                  show: true,
                  position: 'top',
                  backgroundColor: '#153872',
                  padding: 10,
                  fontSize: 16,
                  overflow: 'break',
                  formatter: [
                    '{a|xxxx医院}',
                    '{b|开方数量:50次}',
                    '{b|开方收入:100000元}'
                  ].join('\n'), // 圆环显示文字
                  rich: {
                    a: {
                      color: '#d8a04c',
                      fontSize: 20,
                      lineHeight: 20
                    },
                    b: {
                      // backgroundColor: {
                      //   image: 'xxx/xxx.jpg'
                      // },
                      // height: 40
                      color: '#ffffff',
                      fontSize: 16,
                      lineHeight: 20
                    }
                  }
                },
                itemStyle: {
                  color: '#fe2321'
                }
              },
              {
                name: 'xxxx医院',
                value: [105.863126, 32.43237],
                // 单独修改圆环上的文字
                label: {
                  show: false,
                  position: 'top',
                  backgroundColor: '#153872',
                  padding: 10,
                  fontSize: 16,
                  overflow: 'break',
                  formatter: [
                    '{a|xxxx民医院}',
                    '{b|开方数量:99次}',
                    '{b|开方收入:200000元}'
                  ].join('\n'), // 圆环显示文字
                  rich: {
                    a: {
                      color: '#d8a04c',
                      fontSize: 20,
                      lineHeight: 20
                    },
                    b: {
                      // backgroundColor: {
                      //   image: 'xxx/xxx.jpg'
                      // },
                      // height: 40
                      color: '#ffffff',
                      fontSize: 16,
                      lineHeight: 20
                    }
                  }
                },
                itemStyle: {
                  color: '#fe2321'
                }
              }
            ]
          }
        ]
      };
      this.option = option;
    },
    getData() {
      this.mapRef.setOption(this.option);
    }
  }
};
</script>
<style scoped lang="scss">
.map {
  width: 100%;
  height: 100%;
}
</style>

4、其他

当点击红色的定位图标的时候,可以看到一下的信息

可以根据这些信息进行其他的数据展示。比如发送请求==。

5、相关资料:

Documentation - Apache ECharts

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用EchartsVue2中实现数据大屏,可以按照以下步骤操作: 1. 安装Echarts库 在Vue2项目中,可以使用npm或yarn安装Echarts: ```bash npm install echarts --save ``` 或者 ```bash yarn add echarts ``` 2. 引入EchartsVue2项目中,可以在main.js中全局引入Echarts: ```js import echarts from 'echarts' Vue.prototype.$echarts = echarts ``` 3. 创建Echarts组件 在Vue2中,可以使用组件化的方式创建Echarts组件。首先,在components文件夹中创建一个Echarts.vue组件: ```vue <template> <div :id="chartId" style="width:100%;height:100%;"></div> </template> <script> export default { name: 'Echarts', props: { chartData: { type: Object, required: true }, chartId: { type: String, default: 'chart' } }, mounted() { this.initChart() }, methods: { initChart() { const chart = this.$echarts.init(document.getElementById(this.chartId)) chart.setOption(this.chartData) } } } </script> <style> </style> ``` 这个组件接受两个props:chartData和chartId。chartData是Echarts图表的数据,chartId是该组件渲染的元素的id。 4. 在父组件中使用Echarts组件 在父组件中,可以使用Echarts组件来创建图表。首先,需要引入Echarts组件: ```js import Echarts from './components/Echarts.vue' ``` 然后,在template中使用Echarts组件,并将chartData传递给它: ```vue <template> <div> <Echarts :chartData="chartData" :chartId="chartId"></Echarts> </div> </template> <script> import Echarts from './components/Echarts.vue' export default { name: 'DataScreen', components: { Echarts }, data() { return { chartId: 'chart1', chartData: { // Echarts数据 } } } } </script> <style> </style> ``` 这样,就可以在Vue2项目中使用Echarts创建数据大屏了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多喜乐 长安宁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值