fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 30.589203, lng: 120.732051, alt: 18446, heading: 2, pitch: -49 }
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录首次创建的map
 
  mars3d.Util.fetchJson({ url: "//data.mars3d.cn/file/apidemo/lineroad.json" })
    .then(function (json) {
      createEchartsLayer(json.data)
    })
    .catch(function (error) {
      console.log("加载JSON出错", error)
    })
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
/**
 * 创建echart图层
 *
 * @param {object} data 后端接口,获取的数据
 * @returns {void} 无
 */
function createEchartsLayer(data) {
  const options = getEchartsOption(data)
  options.depthTest = false // 是否进行计算深度(大数据时,需要关闭)
 
  const echartsLayer = new mars3d.layer.EchartsLayer(options)
  map.addLayer(echartsLayer)
 
  // 图表自适应
  window.addEventListener("resize", function () {
    echartsLayer.resize()
  })
}
 
/**
 *echart图层
 *
 * @param {object} data 后端接口数据
 * @return {option} 根据获取的数据创建echart图表的数据
 */
function getEchartsOption(data) {
  const option = {
    animation: false,
 
    visualMap: {
      type: "piecewise",
      left: "right",
      bottom: 46,
      /* pieces: [
                {min: 15}, // 不指定 max,表示 max 为无限大(Infinity)。
                {min: 12, max: 15},
                {min: 9, max: 12},
                {min: 6, max: 9},
                {min: 3, max: 6},
                {max: 3}     // 不指定 min,表示 min 为无限大(-Infinity)。
        ], */
      min: 0,
      max: 15,
      splitNumber: 5,
      maxOpen: true,
      color: ["red", "yellow", "green"],
      textStyle: {
        color: "#ffffff"
      }
    },
    tooltip: {
      formatter: function (params, ticket, callback) {
        return "拥堵指数:" + params.value
      },
      trigger: "item"
    },
    series: [
      {
        type: "lines",
        coordinateSystem: "mars3dMap",
        polyline: true,
        data: data,
        lineStyle: {
          normal: {
            opacity: 1,
            width: 4
          },
          emphasis: {
            width: 6
          }
        },
        effect: {
          show: true,
          symbolSize: 2,
          color: "white"
        }
      }
    ]
  }
 
  return option
}