NEZ-1280 feat: 图表刷新

This commit is contained in:
chenjinsong
2021-12-02 21:46:07 +08:00
parent ddb1acce18
commit 49f4320723
8 changed files with 92 additions and 54 deletions

View File

@@ -82,6 +82,7 @@
} }
.nz-chart { .nz-chart {
position: relative;
height: 100%; height: 100%;
.nz-chart__component { .nz-chart__component {

View File

@@ -1,5 +1,6 @@
<template> <template>
<div class="nz-chart"> <div class="nz-chart">
<loading :loading="loading"></loading>
<chart-no-data v-if="isNoData"></chart-no-data> <chart-no-data v-if="isNoData"></chart-no-data>
<template v-else> <template v-else>
<chart-time-series <chart-time-series
@@ -21,6 +22,7 @@
</template> </template>
<script> <script>
import loading from '@/components/common/loading'
import chartAssetInfo from './chart/chartAssetInfo' import chartAssetInfo from './chart/chartAssetInfo'
import chartBar from './chart/chartBar' import chartBar from './chart/chartBar'
import chartClock from './chart/chartClock' import chartClock from './chart/chartClock'
@@ -45,6 +47,7 @@ import lodash from 'lodash'
export default { export default {
name: 'chart', name: 'chart',
components: { components: {
loading,
chartAssetInfo, chartAssetInfo,
chartBar, chartBar,
chartClock, chartClock,
@@ -68,11 +71,8 @@ export default {
chartInfo: Object, chartInfo: Object,
chartData: [Object, Array, String], // 数据查询后传入chart组件chart组件内不查询只根据接传递的数据来渲染 chartData: [Object, Array, String], // 数据查询后传入chart组件chart组件内不查询只根据接传递的数据来渲染
customChartOption: Object, // 需要自定义echarts的option时传入非必须传入该值时仍需传对应格式的chartData customChartOption: Object, // 需要自定义echarts的option时传入非必须传入该值时仍需传对应格式的chartData
isFullscreen: Boolean isFullscreen: Boolean,
}, loading: Boolean
data () {
return {
}
}, },
computed: { computed: {
isNoData () { isNoData () {
@@ -97,7 +97,3 @@ export default {
} }
} }
</script> </script>
<style scoped>
</style>

View File

@@ -80,7 +80,7 @@ export default {
setTimeout(() => { setTimeout(() => {
const myChart = this.isInit ? echarts.init(document.getElementById(`chart-canvas-${this.chartId}`)) : getChart(this.chartId) const myChart = this.isInit ? echarts.init(document.getElementById(`chart-canvas-${this.chartId}`)) : getChart(this.chartId)
myChart.setOption(chartOption) myChart.setOption(chartOption)
setChart(this.chartId, myChart) // 缓存不使用vue的data是为避免整个chart被监听导致卡顿 this.isInit && setChart(this.chartId, myChart) // 缓存不使用vue的data是为避免整个chart被监听导致卡顿
this.isInit = false this.isInit = false
}, 200) }, 200)
}, },

View File

@@ -62,7 +62,7 @@ export default {
this.$emit('showFullscreen', true) this.$emit('showFullscreen', true)
}, },
refreshChart () { refreshChart () {
this.$emit('refresh')
}, },
editChart () { editChart () {

View File

@@ -5,6 +5,7 @@
<chart-header <chart-header
v-if="!isFullscreen" v-if="!isFullscreen"
:chart-info="chartInfo" :chart-info="chartInfo"
@refresh="refresh"
@showFullscreen="showFullscreen" @showFullscreen="showFullscreen"
></chart-header> ></chart-header>
<!-- chart --> <!-- chart -->
@@ -14,6 +15,7 @@
:chart-data="chartData" :chart-data="chartData"
:chart-info="chartInfo" :chart-info="chartInfo"
:is-fullscreen="isFullscreen" :is-fullscreen="isFullscreen"
:loading="loading"
></chart> ></chart>
</div> </div>
</template> </template>
@@ -40,12 +42,14 @@ export default {
}, },
data () { data () {
return { return {
chartData: [] chartData: [],
loading: true
} }
}, },
methods: { methods: {
// 参数 isRefresh 标识是否是刷新操作 // 参数 isRefresh 标识是否是刷新操作
getChartData (isRefresh) { getChartData (isRefresh) {
this.loading = true
// TODO assetInfo、endpointInfo、echarts等进行不同的处理 // TODO assetInfo、endpointInfo、echarts等进行不同的处理
let startTime = '' let startTime = ''
let endTime = '' let endTime = ''
@@ -54,7 +58,7 @@ export default {
const origin = new Date(this.timeRange[1]) const origin = new Date(this.timeRange[1])
const numInterval = now.getTime() - origin.getTime() const numInterval = now.getTime() - origin.getTime()
if (numInterval >= 60000) { // 大于1分钟则start、end均往后移numInterval否则时间不变 if (numInterval >= 60000) { // 大于1分钟则start、end均往后移numInterval否则时间不变
startTime = this.getNewTime(this.timeRange[0], numInterval) startTime = bus.getNewTime(this.timeRange[0], numInterval)
endTime = bus.timeFormate(now, 'yyyy-MM-dd hh:mm:ss') endTime = bus.timeFormate(now, 'yyyy-MM-dd hh:mm:ss')
} else { } else {
startTime = this.timeRange[0] startTime = this.timeRange[0]
@@ -72,6 +76,7 @@ export default {
this.query(elements, startTime, endTime, step) this.query(elements, startTime, endTime, step)
}, },
query (elements, startTime, endTime, step) { query (elements, startTime, endTime, step) {
try {
switch (this.chartInfo.dataSource) { switch (this.chartInfo.dataSource) {
case 1: case 1:
case 2: { case 2: {
@@ -103,21 +108,29 @@ export default {
} }
}) })
this.chartData = chartData this.chartData = chartData
}).finally(() => {
this.loading = false
}) })
break break
} }
case 3: { case 3: {
this.loading = false
break break
} }
case 4: { case 4: {
if (this.chartInfo.type === 'hexagonFigure') { if (this.chartInfo.type === 'hexagonFigure') {
this.getHexagonFigureData().then(res => { this.getHexagonFigureData().then(res => {
this.chartData = res this.chartData = res
}).finally(() => {
this.loading = false
}) })
} }
break break
} }
} }
} catch (e) {
this.loading = false
}
}, },
getHexagonFigureData () { getHexagonFigureData () {
return new Promise(resolve => { return new Promise(resolve => {
@@ -149,10 +162,21 @@ export default {
resize () { resize () {
this.$refs.chart.resize() this.$refs.chart.resize()
}, },
refresh () {
this.getChartData(true)
},
showFullscreen (show) { showFullscreen (show) {
this.$emit('showFullscreen', show, this.chartInfo) this.$emit('showFullscreen', show, this.chartInfo)
} }
}, },
watch: {
timeRange: {
deep: true,
handler (n) {
this.refresh()
}
}
},
mounted () { mounted () {
this.getChartData() this.getChartData()
} }

View File

@@ -9,7 +9,7 @@
export default { export default {
name: 'loading', name: 'loading',
props: { props: {
loading: Boolean
}, },
data () { data () {
return { return {
@@ -23,6 +23,11 @@ export default {
endLoading () { endLoading () {
this.showLoading = false this.showLoading = false
} }
},
watch: {
loading (n) {
this.showLoading = n
}
} }
} }
</script> </script>

View File

@@ -95,7 +95,6 @@
@on-remove-chart="delChart" @on-remove-chart="delChart"
@on-add-group-item-chart="addGroupItem" @on-add-group-item-chart="addGroupItem"
></chart-list> ></chart-list>
<chart-list ref="chartList" :class="{'show-top':showTopBtn}" :from="fromRoute.panel" :nowTimeType="nowTimeType" :panel-lock="panelLock" @on-edit-chart="editChart" @on-refresh-time="refreshTime" @on-remove-chart="delChart" @on-add-group-item-chart="addGroupItem"></chart-list>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -290,6 +290,19 @@ export default new Vue({
new Date(this.computeTimezone(new Date().getTime())) new Date(this.computeTimezone(new Date().getTime()))
] ]
}, },
getRefreshTimeByTimeRange (timeRange) {
const interval = timeRange[1] - timeRange[0]
const newTime = new Date().getTime()
return [
new Date(new Date(this.computeTimezone(newTime - interval))),
new Date(this.computeTimezone(newTime))
]
},
getNewTime (time, num) {
const date = new Date(time)
const newDate = new Date(parseInt(date.getTime(), 10) + num)
return this.timeFormate(newDate, 'yyyy-MM-dd hh:mm:ss')
},
getOffsetTimezoneData (offset = 0) { getOffsetTimezoneData (offset = 0) {
return new Date(this.computeTimezone(new Date().getTime())).setHours(new Date(this.computeTimezone(new Date().getTime())).getHours() + offset) return new Date(this.computeTimezone(new Date().getTime())).setHours(new Date(this.computeTimezone(new Date().getTime())).getHours() + offset)
}, },