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
<template>
  <!-- UI面板 -->
  <mars-pannel :visible="true" right="10" top="10">
    <a-space>
      <mars-button @click="measureSection">绘制线</mars-button>
      <mars-button @click="clear">清除</mars-button>
    </a-space>
  </mars-pannel>
 
  <!-- ecahrt图表 -->
  <mars-pannel v-model:visible="isShow" left="10" width="calc(100% - 20px)" bottom="40">
    <div class="echatsView">
      <div id="echartsView1" style="width: 100%; height: 100%"></div>
    </div>
  </mars-pannel>
 
  <location-to />
</template>
 
<script setup lang="ts">
import { nextTick, onMounted, ref } from "vue"
import LocationTo from "@mars/components/mars-sample/location-to.vue"
import * as echarts from "echarts"
import * as mapWork from "./map.js"
 
const isShow = ref<boolean>(false)
 
let myChart1: echarts.ECharts
 
// 图表自适应
window.onresize = function () {
  myChart1.resize()
}
 
onMounted(() => {
  myChart1 = echarts.init(document.getElementById("echartsView1")!)
})
 
mapWork.eventTarget.on("measureEnd", function (event: any) {
  isShow.value = true
  nextTick(() => {
    setEchartsData(event)
  })
})
 
mapWork.eventTarget.on("measureClick", function (event: any) {
  if (event.value) {
    nextTick(() => {
      setEchartsData(event.value)
    })
  }
})
 
const measureSection = () => {
  mapWork.measureSection()
}
 
const clear = () => {
  mapWork.removeAll()
  isShow.value = false
  myChart1.clear()
}
 
function setEchartsData(data: any) {
  if (data == null || data.arrPoint == null) {
    return
  }
  const arrPoint = data.arrPoint
  let inhtml = ""
 
  const option = {
    grid: {
      left: 10,
      right: 10,
      bottom: 10,
      containLabel: true
    },
    dataZoom: [
      {
        type: "inside",
        throttle: 50
      }
    ],
    tooltip: {
      trigger: "axis",
      // position: function (point, params, dom, rect, size) {
      //    return [10, 20];
      // },
      formatter: (params: any) => {
        if (params.length === 0) {
          mapWork.hideTipMarker()
          return inhtml
        }
 
        const hbgd = params[0].value // 海拔高度
        const point = arrPoint[params[0].dataIndex] // 所在经纬度
        const result = mapWork.calculation(params[0])
 
        inhtml = `当前位置<br />
                      距起点:${result.len}<br />
                      海拔:<span style='color:${params[0].color};'>${result.hbgdStr}</span><br />
                      经度:${point.lng}<br />
                      纬度:${point.lat}`
 
        mapWork.showTipMarker(point, hbgd, inhtml)
 
        return inhtml
      }
    },
    xAxis: [
      {
        name: "行程",
        type: "category",
        boundaryGap: false,
        axisLine: {
          show: true
        },
        axisLabel: {
          show: true,
          formatter: "{value} 米"
        },
        data: data.arrLen
      }
    ],
    yAxis: [
      {
        name: "高程",
        type: "value",
        min: getMinZ(arrPoint),
        axisLabel: {
          formatter: "{value} 米"
        }
      }
    ],
    series: [
      {
        name: "高程值",
        type: "line",
        smooth: true,
        symbol: "none",
        sampling: "average",
        itemStyle: {
          normal: {
            color: "rgb(255, 70, 131)"
          }
        },
        areaStyle: {
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: "rgb(255, 158, 68)"
              },
              {
                offset: 1,
                color: "rgb(255, 70, 131)"
              }
            ])
          }
        },
        data: data.arrHB
      }
    ]
  }
 
  myChart1.setOption(option)
  myChart1.resize()
}
 
function getMinZ(arr: any) {
  let minz = "dataMin"
  if (arr == null || arr.length === 0) {
    return minz
  }
 
  minz = arr[0].alt
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].alt < minz) {
      minz = arr[i].alt
    }
  }
  return minz
}
</script>
<style scoped lang="less">
.echartsBox {
  top: auto !important;
  left: 50px;
  width: calc(100% - 60px);
  bottom: 40px;
}
.echatsView {
  width: 100%;
  height: 200px;
}
</style>