目录

echart的学习4.地图在微信小程序下的使用

目录

echart的学习:4.地图在微信小程序下的使用

这个系列的博客没什么大的用处,因为echart基本都有范例的了。我只是为了自己能更好的学习下这个插件,而写博客总结下经验

由于echart在小程序的使用已经有demo了, 再为了他专门写一篇博客也是浪费时间,所以就写一篇在微信小程序中的使用,直接在原有demo上修改

https://i-blog.csdnimg.cn/blog_migrate/8905cc8e0ac572a591ea4b703930bb35.png

接下来,对比下两者的格式(以江苏省为例子)

https://i-blog.csdnimg.cn/blog_migrate/187092b19ed8fb3fedd2c0a0ce6c7f48.png

所以将其修改格式,就变成了:

https://i-blog.csdnimg.cn/blog_migrate/3d50b0a6b0eeca170ab98fd7f03f9653.png

代码:(只修改了徐州市,其他的不变)

import * as echarts from '../../ec-canvas/echarts';
import geoJson from './mapData.js';

const app = getApp();

function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  echarts.registerMap('henan', geoJson);

  const option = {
    tooltip: {
      trigger: 'item'
    },

    visualMap: {
      min: 0,
      max: 100,
      left: 'left',
      top: 'bottom',
      text: ['高', '低'], // 文本,默认为数值文本
      calculable: true
    },
    toolbox: {
      show: true,
      orient: 'vertical',
      left: 'right',
      top: 'center',
      feature: {
        dataView: { readOnly: false },
        restore: {},
        saveAsImage: {}
      }
    },
    series: [{
      type: 'map',
      mapType: 'henan',
      label: {
        normal: {
          show: true
        },
        emphasis: {
          textStyle: {
            color: '#fff'
          }
        }
      },
      itemStyle: {

        normal: {
          borderColor: '#389BB7',
          areaColor: '#fff',
        },
        emphasis: {
          areaColor: '#389BB7',
          borderWidth: 0
        }
      },
      animation: false,

      data: [
        { name: '徐州市', value: 1000 },
        { name: '洛阳市', value: 10 },
        { name: '开封市', value: 20 },
        { name: '信阳市', value: 30 },
        { name: '驻马店市', value: 40 },
        { name: '南阳市', value: 41 },
        { name: '周口市', value: 15 },
        { name: '许昌市', value: 25 },
        { name: '平顶山市', value: 35 },
        { name: '新乡市', value: 35 },
        { name: '漯河市', value: 35 },
        { name: '商丘市', value: 35 },
        { name: '三门峡市', value: 35 },
        { name: '济源市', value: 35 },
        { name: '焦作市', value: 35 },
        { name: '安阳市', value: 35 },
        { name: '鹤壁市', value: 35 },
        { name: '濮阳市', value: 35 },
        { name: '开封市', value: 45 }
      ]

    }],

  };

  chart.setOption(option);

  return chart;
}

Page({
  onShareAppMessage: function (res) {
    return {
      title: 'ECharts 可以在微信小程序中使用啦!',
      path: '/pages/index/index',
      success: function () { },
      fail: function () { }
    }
  },
  data: {
    ec: {
      onInit: initChart
    }
  },

  onReady() {
  }
});

随后运行:

https://i-blog.csdnimg.cn/blog_migrate/895f8334da96f4f38a4b7532034e4ef3.png

不多变化,主要就以上讲到的这几点,因为一些新手不太习惯用echart的地图,所以特意讲下而已

参考资料:

1.echart配置项文档:https://echarts.baidu.com/option.html#title

2.echart的示例:https://echarts.baidu.com/examples

3.echart的github(微信小程序):