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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
export let graphicLayer // 矢量图层对象
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 30.286465, lng: 117.620524, alt: 510892, heading: 358, pitch: -50 }
  },
  layers: [
    {
      id: 1987,
      type: "geojson",
      name: "淮海经济区11市",
      url: "//data.mars3d.cn/file/geojson/huaihai.json",
      symbol: {
        styleOptions: {
          materialType: mars3d.MaterialType.PolyGradient,
          color: "#3388cc",
          opacity: 0.7,
          alphaPower: 1.3,
          length: "{gdp}"
        },
        styleField: "Name",
        styleFieldOptions: {
          济宁市: { color: "#D4AACE" },
          临沂市: { color: "#8DC763" },
          菏泽市: { color: "#F7F39A" },
          枣庄市: { color: "#F7F39A" },
          徐州市: { color: "#96F0F1" },
          宿迁市: { color: "#EAC9A8" },
          连云港市: { color: "#F7F39A" },
          商丘市: { color: "#D4AACE" },
          宿州市: { color: "#8DC763" },
          亳州市: { color: "#96F0F1" },
          淮北市: { color: "#EAC9A8" }
        }
      },
      show: true
    }
  ]
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  map.basemap = 2017 // 蓝色底图
 
  // 创建Graphic图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  mars3d.Util.fetchJson({ url: "//data.mars3d.cn/file/apidemo/huaihai-jj.json" })
    .then(function (res) {
      conventChartsData(res.data) // 单击显示的popup
      showYearZT(res.data) // 柱状图
      bindHaihuaiPopup()
    })
    .catch(function () {
      globalMsg("获取信息失败,请稍候再试")
    })
 
  map.on(mars3d.EventType.load, function (event) {
    console.log("矢量数据对象加载完成", event)
  })
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
/**
 * 展示某年的椎体
 *
 * @param {object} data 通过JSON获取的数据
 * @returns {void} 无
 */
function showYearZT(data) {
  const yearArr = Object.keys(data)
  const arr = data[yearArr[0]]
 
  for (let i = 0; i < arr.length; i += 1) {
    const attr = arr[i]
    const jwd = getJWDByName(attr.name)
 
    const num1 = attr["第一产业"]
    const num2 = attr["第二产业"]
    const num3 = attr["第三产业"]
    const numall = Number(num1 + num2 + num3).toFixed(2)
    const html = `${attr.name}<br/>
                  <span style="color:#63AEFF">第一产业:${num1}</span><br/>
                  <span style="color:#FFB861">第二产业:${num2}</span><br/>
                  <span style="color:#FF6D5D">第三产业:${num3}</span>`
 
    const height1 = Math.floor(num1 * 10)
    const height2 = Math.floor(num2 * 10)
    const height3 = Math.floor(num3 * 10)
 
    const p1 = Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height3 / 2)
    const p2 = Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height3 + height2 / 2)
    const p3 = Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height3 + height2 + height1 / 2)
 
    // 添加柱体
    createZT(p1, height3, "#63AEFF", html)
    createZT(p2, height2, "#FFB861", html)
    createZT(p3, height1, "#FF6D5D", html)
 
    // 添加文字
    const primitive = new mars3d.graphic.LabelPrimitive({
      position: Cesium.Cartesian3.fromDegrees(jwd[0], jwd[1], height1 + height2 + height3),
      style: {
        text: numall,
        font_size: 18,
        font_family: "楷体",
        color: "#00ff00",
        outline: true,
        outlineColor: "#000000",
        outlineWidth: 1,
        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        pixelOffset: new Cesium.Cartesian2(0, -20)
      }
    })
    graphicLayer.addGraphic(primitive)
    primitive.bindTooltip(html)
  }
}
 
//  创建柱体
function createZT(position, len, color, html) {
  const graphic = new mars3d.graphic.CylinderEntity({
    position: position,
    style: {
      length: len,
      topRadius: 6000.0,
      bottomRadius: 6000.0,
      color: color
    }
  })
  graphicLayer.addGraphic(graphic)
 
  graphic.bindTooltip(html)
 
  // graphic._position_show = position
  // graphic._length_show = len
  return graphic
}
 
const cityPosition = [
  { name: "亳州", jwd: [116.203602, 33.496075] },
  { name: "商丘", jwd: [115.871509, 34.297084] },
  { name: "淮北", jwd: [116.688413, 33.689214] },
  { name: "宿州", jwd: [117.234682, 33.740035] },
  { name: "徐州", jwd: [117.70509, 34.350708] },
  { name: "宿迁", jwd: [118.559349, 33.807355] },
  { name: "连云港", jwd: [118.875445, 34.619808] },
  { name: "临沂", jwd: [118.026908, 35.262767] },
  { name: "枣庄", jwd: [117.320268, 35.072555] },
  { name: "济宁", jwd: [116.856599, 35.500232] },
  { name: "菏泽", jwd: [115.716086, 35.05629] }
]
 
//  根据名称获取坐标
function getJWDByName(name) {
  for (let i = 0; i < cityPosition.length; i += 1) {
    const item = cityPosition[i]
    if (item.name === name) {
      return item.jwd
    }
  }
  return []
}
 
//= ===============以下是单击显示的echarst图表的相关代码===============
let arrYear
let objCity = {}
 
// 转换值
function conventChartsData(arrOld) {
  console.log("转换前数据=>", arrOld)
 
  arrYear = Object.keys(arrOld) // [年份]
 
  objCity = {} // 十一个城市对应的各年度数据
 
  for (let a = 0; a < arrYear.length; a++) {
    const arrCity = arrOld[arrYear[a]] // 指定某年的11个城市对应数据
 
    // 循环十次
    for (let b = 0; b < arrCity.length; b++) {
      const item = arrCity[b]
 
      if (!objCity[item.code]) {
        objCity[item.code] = []
      }
      objCity[item.code].push(item.GDP)
    }
  }
 
  console.log("转换完成的数据=>", objCity)
}
 
function bindHaihuaiPopup() {
  const layerHuaihai = map.getLayer(1987, "id") // 获取config.json中对应图层
 
  // 在layer上绑定Popup单击弹窗
  layerHuaihai.bindPopup(
    `<div class="gdpView">
        <div class="gdpCharts" id="gdpCharts"></div>
        <input type="button" class="btnClosePopup closeButton" value="×" />
      </div>`,
    {
      template: false,
      horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
      verticalOrigin: Cesium.VerticalOrigin.CENTER
    }
  )
 
  let gdpCharts
  layerHuaihai.on(mars3d.EventType.popupOpen, function (event) {
    const container = event.container // popup对应的DOM
    console.log("图层上打开了popup", container)
 
    const option = getCityChartsOptions(event.graphic.attr)
    if (!option) {
      return
    }
 
    gdpCharts = echarts.init(container.querySelector("#gdpCharts"))
    gdpCharts.setOption(option)
  })
  layerHuaihai.on(mars3d.EventType.popupClose, function (event) {
    const container = event.container // popup对应的DOM
    console.log("图层上移除了popup", container)
 
    gdpCharts.dispose()
    gdpCharts = null
  })
}
 
function getCityChartsOptions(attr) {
  const code = attr.code.slice(0, 4)
  const arrGDPvalues = objCity[code]
  if (!arrGDPvalues) {
    globalMsg(attr.Name + " 无经济数据")
    return
  }
 
  // arrGDPvalues  是点击的城市的数值,需要以[b,0,value]的方式重新排列
  const arrData = []
  for (let b = 0; b < arrGDPvalues.length; b++) {
    arrData[b] = [b, 0, arrGDPvalues[b]]
  }
 
  const option = {
    visualMap: {
      max: 4500,
      show: false,
      inRange: {
        color: ["#32C5E9", "#67E0E3", "#FFDB5C", "#37A2DA", "#9FE6B8"]
      }
    },
    title: {
      text: attr.Name + "   近五年GDP(亿元)",
      textStyle: { color: "white", fontSize: "17", fontWidth: "normal" },
      top: "10",
      left: "5"
    },
    tooltip: {
      show: "true",
      trigger: "item",
      showContent: "true",
      position: "top",
      fontSize: "12",
      color: "black",
      formatter: function formatter(params) {
        return "GDP:" + params.data[2]
      }
    },
    // x轴是横向,是时间
    xAxis3D: {
      type: "category",
      data: arrYear,
      nameTextStyle: {
        color: "rgb(0, 0, 0, 0.1)"
      },
      // splitLine不可见时仅仅线不可见
      splitLine: {
        show: false
      }
    },
    // y轴被缩小
    yAxis3D: {
      type: "category",
      data: [" "],
      nameTextStyle: {
        color: "rgb(0, 0, 0, 0.1)"
      },
      splitLine: {
        show: false
      }
    },
    // z轴是gdp的值
    zAxis3D: {
      type: "value",
      name: "GDP",
      axisLine: {
        lineStyle: {
          color: "rgb(0, 0, 0, 0.1)"
        }
      },
      nameTextStyle: {
        color: "white",
        fontSize: "18"
      },
      nameGap: "50"
    },
    grid3D: {
      boxWidth: 180, // 缩大放小x轴
      boxDepth: 10, // 缩大放小y轴
      top: "20",
      // left: '50',
      // 視角的設置
      viewControl: {
        alpha: 8,
        beta: 0,
        distance: 162,
        center: [-20, 0, 0]
      },
      axisLabel: {
        color: "white",
        fontSize: 15
      },
      axisPointer: {
        // 坐标轴指示线,就是鼠标移入时,指向x轴,y轴的线
        show: false
      }
    },
    series: [
      {
        type: "bar3D",
        data: arrData,
        shading: "lambert",
        label: {
          position: "top",
          show: true,
          color: "white"
        },
        emphasis: {
          label: {
            color: "white",
            fontSize: "18"
          }
        }
      }
    ]
  }
  return option
}