This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/views/charts2/charts/npm/NpmIpMap.vue

221 lines
7.8 KiB
Vue
Raw Normal View History

2022-07-26 16:44:35 +08:00
<template>
2022-07-26 22:07:53 +08:00
<div class="cn-chart__map">
2022-08-23 21:42:42 +08:00
<div class="map-canvas" id="npmDrillDownMap"></div>
2022-07-26 22:07:53 +08:00
</div>
2022-07-26 16:44:35 +08:00
</template>
<script>
2022-07-26 22:07:53 +08:00
import { shallowRef } from 'vue'
import * as am4Core from '@amcharts/amcharts4/core'
import * as am4Maps from '@amcharts/amcharts4/maps'
2022-08-23 21:42:42 +08:00
import { computeScore, getGeoData } from '@/utils/tools'
import { storageKey, unitTypes, countryNameIdMapping } from '@/utils/constants'
import { valueToRangeValue } from '@/utils/unit-convert'
import { getSecond } from '@/utils/date-util'
import { api, getData } from '@/utils/api'
import { get } from '@/utils/http'
import chartMixin from '@/views/charts2/chart-mixin'
2022-07-26 16:44:35 +08:00
export default {
2022-08-23 21:42:42 +08:00
name: 'NpmIpMap',
2022-07-26 22:07:53 +08:00
data () {
return {
myChart: null,
2022-08-04 12:03:15 +08:00
polygonSeries: null,
2022-08-08 22:31:08 +08:00
countrySeries: null,
worldImageSeries: null,
countryImageSeries: null,
2022-08-04 12:03:15 +08:00
// Server | Client
2022-08-23 21:42:42 +08:00
trafficDirection: 'Server'
2022-07-26 22:07:53 +08:00
}
},
2022-08-23 21:42:42 +08:00
mixins: [chartMixin],
2022-07-26 22:07:53 +08:00
methods: {
2022-08-23 21:42:42 +08:00
async initMap () {
2022-07-26 22:07:53 +08:00
// 初始化插件
2022-08-23 21:42:42 +08:00
this.toggleLoading(true)
const geoData = await getGeoData(storageKey.iso36112WorldLow)
const chart = am4Core.create('npmDrillDownMap', am4Maps.MapChart)
chart.geodata = geoData
2022-07-26 22:07:53 +08:00
chart.projection = new am4Maps.projections.Miller()
2022-08-23 21:42:42 +08:00
chart.homeZoomLevel = 1.5
2022-07-26 22:07:53 +08:00
chart.homeGeoPoint = {
latitude: 21,
longitude: 0
}
this.myChart = shallowRef(chart)
this.polygonSeries = shallowRef(this.polygonSeriesFactory())
2022-08-23 21:42:42 +08:00
this.worldImageSeries = shallowRef(this.imageSeriesFactory('score', this.polygonSeries))
2022-08-08 22:31:08 +08:00
// 渲染
this.loadAm4ChartMap(this.polygonSeries, this.worldImageSeries)
},
2022-08-23 21:42:42 +08:00
loadAm4ChartMap (polygonSeries, imageSeries) {
2022-08-08 22:31:08 +08:00
try {
// 清除数据
2022-08-23 21:42:42 +08:00
// polygonSeries.data.splice(0)
this.toggleLoading(true)
2022-08-08 22:31:08 +08:00
// 清除legend
this.myChart.children.each((s, i) => {
if (s && s.className !== 'Container') {
this.myChart.children.removeIndex(i)
}
})
2022-08-23 21:42:42 +08:00
const params = {
startTime: getSecond(this.timeFilter.startTime),
endTime: getSecond(this.timeFilter.endTime),
type: this.$store.getters.getDimensionType,
typeVal: this.$store.getters.getBreadcrumbColumnValue
}
getData(api.npm.overview.map, params).then(res => {
// 计算分数
const tcpRequest = get(api.npm.overview.mapTcp, params)
const httpRequest = get(api.npm.overview.mapHttp, params)
const sslRequest = get(api.npm.overview.mapSsl, params)
const tcpLostRequest = get(api.npm.overview.mapPacketLoss, params)
const packetRetransRequest = get(api.npm.overview.mapPacketRetrans, params)
Promise.all([tcpRequest, httpRequest, sslRequest, tcpLostRequest, packetRetransRequest]).then(res2 => {
const keyPre = ['tcp', 'http', 'ssl', 'tcpLost', 'packetRetrans']
const mapData = res
res2.forEach((r, i) => {
if (r.code === 200) {
mapData.forEach(t => {
let score = 0
const find = r.data.result.find(d => d.country === t.country)
if (find) {
score = computeScore(find, i)
}
t[keyPre[i] + 'Score'] = score
})
} else {
mapData.forEach(t => {
t[keyPre[i] + 'Score'] = 0
})
}
})
mapData.forEach(t => {
t.score = Math.ceil((t.tcpScore + t.httpScore + t.sslScore + t.tcpLostScore + t.packetRetransScore) * 6)
if (t.score > 6) {
t.score = 6
}
})
this.loadMarkerData(imageSeries, mapData)
})
}).finally(() => {
this.toggleLoading(false)
})
2022-08-08 22:31:08 +08:00
} catch (e) {
console.error(e)
}
},
2022-08-23 21:42:42 +08:00
loadMarkerData (imageSeries, data) {
imageSeries.data = data.map(r => ({
score: r.score,
name: r.province || r.country,
throughput: valueToRangeValue(r.throughBitsRate, unitTypes.bps).join(' '),
id: r.serverId,
color: this.scoreColor(r.score),
border: this.scoreColor(r.score)
}))
2022-08-09 21:19:21 +08:00
},
scoreColor (score) {
if (score >= 0 && score <= 2) {
return '#E26154'
} else if (score > 2 && score <= 4) {
return '#E5A219'
} else if (score > 4 && score <= 6) {
return '#7E9F54'
}
2022-07-26 22:07:53 +08:00
},
2022-08-23 21:42:42 +08:00
generatePolygonTooltipHTML () {
const html = `
<div class="map-tooltip" style="padding-bottom: 10px;">
<div class="map-tooltip__title">{name}</div>
<div class="map-tooltip__content">
<div class="content-row">
<div class="row__label">Score</div>
<div class="row__value">{score}</div>
</div>
<div class="content-row">
<div class="row__label">Throughput</div>
<div class="row__value">{throughput}</div>
</div>
</div>
</div>`
return html
},
2022-07-26 22:07:53 +08:00
polygonSeriesFactory () {
const polygonSeries = this.myChart.series.push(new am4Maps.MapPolygonSeries())
polygonSeries.useGeodata = true
polygonSeries.exclude = ['AQ'] // 排除南极洲
polygonSeries.tooltip.getFillFromObject = false
polygonSeries.tooltip.background.fill = am4Core.color('#41495D')
polygonSeries.tooltip.background.filters.clear()
polygonSeries.tooltip.background.stroke = '#41495D'
const polygonTemplate = polygonSeries.mapPolygons.template
polygonTemplate.nonScalingStroke = true
polygonTemplate.strokeWidth = 0.5
polygonTemplate.stroke = am4Core.color('#CAD2D3')
polygonTemplate.fill = am4Core.color('#EFEFEF')
return polygonSeries
},
2022-08-23 21:42:42 +08:00
imageSeriesFactory (dataField, polygonSeries) {
2022-07-26 22:07:53 +08:00
// amcharts实例中增加地图图案series用来在地图上画圆点、方块、连线等
const imageSeries = this.myChart.series.push(new am4Maps.MapImageSeries())
// 指定接口数据中哪个字段名代表数值
imageSeries.dataFields.value = dataField || 'count'
// 取出图案的默认模板,用来接下来做自定义更改
const imageTemplate = imageSeries.mapImages.template
imageTemplate.nonScaling = true
// 通过地区ID来获取经纬度设置后无需自己提供经纬度
imageTemplate.adapter.add('latitude', function (latitude, target) {
2022-08-23 21:42:42 +08:00
const polygon = polygonSeries.getPolygonById(target.dataItem.dataContext.id)
2022-07-26 22:07:53 +08:00
if (polygon) {
return polygon.visualLatitude
}
return latitude
})
imageTemplate.adapter.add('longitude', function (longitude, target) {
2022-08-23 21:42:42 +08:00
const polygon = polygonSeries.getPolygonById(target.dataItem.dataContext.id)
2022-07-26 22:07:53 +08:00
if (polygon) {
return polygon.visualLongitude
}
return longitude
})
// 设置图案样式
const circle = imageTemplate.createChild(am4Core.Circle)
circle.propertyFields.fill = 'color'
circle.propertyFields.stroke = 'border'
circle.strokeWidth = 1
2022-08-23 21:42:42 +08:00
circle.fillOpacity = 0.8
circle.tooltipHTML = this.generatePolygonTooltipHTML()
2022-07-26 22:07:53 +08:00
imageSeries.tooltip.getFillFromObject = false
2022-08-23 21:42:42 +08:00
imageSeries.tooltip.background.fill = am4Core.color('#FFFFFF')
2022-07-26 22:07:53 +08:00
imageSeries.tooltip.background.filters.clear()
2022-08-23 21:42:42 +08:00
imageSeries.tooltip.background.stroke = '#C5C5C5'
2022-07-26 22:07:53 +08:00
imageSeries.heatRules.push({
target: circle,
property: 'radius',
2022-08-08 22:31:08 +08:00
min: 15,
max: 15,
2022-07-26 22:07:53 +08:00
dataField: 'value'
})
return imageSeries
}
},
mounted () {
this.initMap()
2022-08-23 21:42:42 +08:00
},
beforeUnmount () {
this.polygonSeries = null
this.countrySeries = null
this.worldImageSeries = null
this.countryImageSeries = null
this.myChart && this.myChart.dispose()
this.myChart = null
2022-07-26 22:07:53 +08:00
}
2022-07-26 16:44:35 +08:00
}
</script>