亲宝软件园·资讯

展开

Echart更换背景图

念念不望必有回响 人气:0

需求

解题思路

1.思路一

在canvas外层添加一个div背景图用上面的图片,然后把map地图背景图调成透明,这种方法有很大问题,就是后面怎么做hover显示不同的省份,怎么做缩放呢,难不成还要监听canvas的组件事件来缩放div,所以果断放弃。

2.思路二

仔细观察这张图是不是有规律的,它是一个一个小方格组成的就像贴地板砖一样,那我是不是可以利用地图的纹理来做文章,经过查找Echart api 正好有设置纹理的配置,赞。

解题方法

1.标准动作一

先找ui切个"地板砖",地板转如下图 (ps:地板砖有点小哈)

2.标准动作二

在代码html里面增加img标签 设置图片为切好的地砖

  <div class="graph-chart-wrap">
    <div class="graph-chart" ref="graph"></div>
    <img ref="image" style="width:10px"  v-show="false" src="/assets/image/public/repeat2.png" alt />
  </div>

在定义options的上面获取img

   const dom = this.$refs["image"];

配置options

     geo: {
        map: 'china',
        show: true,
        roam: true,
        label: {
          normal: {
            show: true
          },
          emphasis: {
            show: true
          }
        },
        type: "map",
        geoIndex: "1",
        zoom: 1.2,
        itemStyle: {
          normal: {
            areaColor: {
              image: dom, // 这里是引用上面的img html
              repeat: "repeat" // 地板是重复铺贴吗
            },
            borderColor: "rgba(0,0,0,0.2)"
          },
          emphasis: {
            areaColor:'#69a5ff',
            color: "#fff"
          },
          label: {
            show: false
          },
          shadowColor: 'rgba(0, 0, 0, 1)',
          shadowBlur: 100
        }
      },

全部代码

<template>
  <div class="graph-chart-wrap">
    <div class="graph-chart" ref="graph"></div>
    <img ref="image" style="width:10px"  v-show="false" src="../../../../assets/image/public/repeat2.png" alt />
  </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import * as echarts from 'echarts';
import { getTreemap } from '@/api/home';
import { fontSize } from '@/utils/index';
const echartMapJSON = require('@/assets/map/china.json');


@Component({
  name: 'graph',
  components: {}
})
export default class extends Vue {
  private dataList = [];
  mounted() {
    this.getTreemap();

    setInterval(() => {
      this.getTreemap();
    }, 60000);
    this.createChart();
  }

  private async getTreemap() {
    const res = await getTreemap();
    if (res && res.data.code === 200) {
      this.dataList = res.data.data;
    }
  }

  private createChart() {
    const GeoCoordMap: any = {
      北京市: [116.4551, 40.2539],
      辽宁: [123.1238, 42.1216],
      重庆: [108.384366, 30.439702],
      浙江: [119.5313, 29.8773],
      福建: [119.4543, 25.9222],
      广东: [113.12244, 23.009505],
      上海: [121.4648, 31.2891]
    };
    const data = [
      { name: '北京', value: 199 },
      { name: '天津', value: 42 },
      { name: '河北', value: 102 },
      { name: '山西', value: 81 },
      { name: '内蒙古', value: 47 },
      { name: '辽宁', value: 67 },
      { name: '吉林', value: 82 },
      { name: '黑龙江', value: 123 },
      { name: '上海', value: 24 },
      { name: '江苏', value: 92 },
      { name: '浙江', value: 114 },
      { name: '安徽', value: 109 },
      { name: '福建', value: 116 },
      { name: '江西', value: 91 },
      { name: '山东', value: 119 },
      { name: '河南', value: 137 },
      { name: '湖北', value: 116 },
      { name: '湖南', value: 114 },
      { name: '重庆', value: 91 },
      { name: '四川', value: 125 },
      { name: '贵州', value: 62 },
      { name: '云南', value: 83 },
      { name: '西藏', value: 9 },
      { name: '陕西', value: 80 },
      { name: '甘肃', value: 56 },
      { name: '青海', value: 10 },
      { name: '宁夏', value: 18 },
      { name: '新疆', value: 180 },
      { name: '广东', value: 123 },
      { name: '广西', value: 59 },
      { name: '海南', value: 14 }
    ];
    const colorList = [
      '#5776DE','#709EED','#6434D5','#E26395','#E8A057','#6434D5'
    ]

    const convertData = (data: any) => {
      let res = [];
      for (let i = 0; i < data.length; i++) {
          const geoCoord = GeoCoordMap[data[i].name];
          if (geoCoord) {
              res.push({
                  name: data[i].name,
                  value: geoCoord.concat(data[i].value)
              });
          }
      }
      return res;
    };

    const dom = this.$refs["image"];

    let options: any = {
      title: {
        top: 20,
        // text: '“会员活跃度” - 山东省',
        subtext: '',
        x: 'center',
        textStyle: {
          color: '#fff'
        }
      },  
      tooltip: {
        trigger: 'item',
        backgroundColor:'#fff',
        className: 'echarts-tooltip echarts-tooltip-dark echarts-tooltip-user',
        borderColor:'#e6e6e6',
        padding: 4,
        triggerOn:'click'
      },
      geo: {
        map: 'china',
        show: true,
        roam: true,
        label: {
          normal: {
            show: true
          },
          emphasis: {
            show: true
          }
        },
        type: "map",
        geoIndex: "1",
        zoom: 1.2,
        itemStyle: {
          normal: {
            areaColor: {
              image: dom,
              repeat: "repeat"
            },
            borderColor: "rgba(0,0,0,0.2)"
          },
          emphasis: {
            areaColor:'#69a5ff',
            color: "#fff"
          },
          label: {
            show: false
          },
          shadowColor: 'rgba(0, 0, 0, 1)',
          shadowBlur: 100
        }
      },
      series: [
        {
          symbolSize: 5,
          label: {
            normal: {
              formatter: '{b}',
              position: 'right',
              show: false
            },
            emphasis: {
              show: false
            }
          },
          itemStyle: {
            normal: {
              color: '#fff'
            }
          },
          name: 'light',
          type: 'scatter',
          coordinateSystem: 'geo',
          data: convertData(data)
        },
        {
          name: 'Top 5',
          type: 'effectScatter',
          coordinateSystem: 'geo',
          symbol: 'circle',
          symbolSize: [8, 8],
          itemStyle: {
            normal: {
              color:function(params:{ dataIndex: any }){
                return colorList[params.dataIndex]
              }
            }
          },
          data: convertData(data),
          showEffectOn: 'render',
          rippleEffect: {
            brushType: 'stroke'
          },
          hoverAnimation: true,
          zlevel: 1,
          tooltip: {
            formatter: function (params: { value: any[]; name: any; dataIndex: number }) {
              return `<div style="width: aoto;text-align:center;line-height:28px; height: auto;color:#fff;font-size: 10px; padding:5px;margin: 0;background:${colorList[params.dataIndex]};border-radius:10px">
              <p style="line-height:20px;padding:0;margin:0">${params.name}工厂线</p>
              <p style="line-height:20px;padding:0;margin:0">今日实时生产总数</p>
              <p style="line-height:20px;padding:0;margin:0">${params.value[2]}</p>
              </div>`
            },
            position:'top',
            backgroundColor:'#fff'
          }
        },
      ]
    };
    const graphChart = echarts.init(this.$refs.graph as HTMLCanvasElement);
    echarts.registerMap('china', echartMapJSON);
    graphChart.setOption(options);
    let len = -1;
    setInterval(() => {
      const dataLen = colorList.length;
      len = (len + 1) % dataLen;
      graphChart.dispatchAction({
        type: 'hideTip'
      });
      graphChart.dispatchAction({
        type: 'showTip',
        seriesIndex: 1,
        dataIndex: len
      });
    }, 2000);
  }
}
</script>

最终效果

加载全部内容

相关教程
猜你喜欢
用户评论