Commit b3e350e2 authored by wends's avatar wends

update 改用echarts

parent fd4f831e
...@@ -28,3 +28,11 @@ export function getModelInstanceList(data) { ...@@ -28,3 +28,11 @@ export function getModelInstanceList(data) {
data data
}) })
} }
// 获取图表数据
export function getModelInstanceChart(data) {
return request({
url: `${baseURL}iot/dashboard/modelData`,
method: 'post',
data
})
}
<template>
<div :id="id" :class="className" :style="{height:height,minWidth:width}" />
</template>
<script>
import echarts from 'echarts'
import moment from 'moment'
import resize from './mixins/resize'
export default {
mixins: [resize],
props: {
className: {
type: String,
default: 'chart'
},
id: {
type: String,
default: 'chart'
},
title: {
type: String,
default: 'line'
},
width: {
type: String,
default: '500px'
},
height: {
type: String,
default: '400px'
},
lineData: {
type: Array,
default: () => {
return []
}
},
description: {
type: String,
default: ''
}
},
data() {
return {
chart: null
}
},
watch: {
lineData: function(val) {
this.updateChart(val)
}
},
mounted() {
this.initChart()
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose()
this.chart = null
},
methods: {
updateChart(data) {
const xAxis = []
const series = []
data.forEach(v => {
xAxis.push(moment(v.time).format('HH:mm:ss'))
series.push(v.value)
})
this.chart.setOption({
xAxis: {
data: xAxis
},
series: [{
name: this.title,
data: series
}]
})
},
initChart() {
this.chart = echarts.init(document.getElementById(this.id))
this.chart.setOption({
title: {
text: `${this.title}监控图表`,
subtext: `单位为: ${this.description}`
},
tooltip: {},
legend: {
data: [this.title]
},
xAxis: {
data: []
},
yAxis: {},
series: [{
name: this.title,
type: 'line',
data: []
}]
})
}
}
}
</script>
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
<el-checkbox <el-checkbox
v-for="(prop, i) in propertyList" v-for="(prop, i) in propertyList"
:key="200+i" :key="200+i"
:label="prop.id" :label="prop.propertyCode"
>{{ prop.propertyName }}</el-checkbox> >{{ prop.propertyName }}</el-checkbox>
</el-checkbox-group> </el-checkbox-group>
</el-form-item> </el-form-item>
...@@ -71,13 +71,12 @@ ...@@ -71,13 +71,12 @@
:span="12" :span="12"
style="height: 400px" style="height: 400px"
> >
<g2-line <e-line
:id="'line-' + chart.propertyCode" :id="'line-' + chart.propertyCode"
:title="chart.propertyName" :title="chart.propertyName"
:line-data="setChartData(chart.propertyCode)" :line-data="setChartData(chart.propertyCode)"
:description="chart.propertyUnit" :description="chart.propertyUnit"
style="min-width: 500px;height: 400px" ></e-line>
/>
</el-col> </el-col>
</el-row> </el-row>
</div> </div>
...@@ -87,13 +86,13 @@ ...@@ -87,13 +86,13 @@
</template> </template>
<script> <script>
import G2Line from '@/components/G2Plot/G2Line' import ELine from '@/components/Charts/ELine'
import { getAllModel, getModelDetail, getModelInstanceList } from '@/api/object-visible/dashboard' import { getAllModel, getModelDetail, getModelInstanceList, getModelInstanceChart } from '@/api/object-visible/dashboard'
export default { export default {
name: 'Dashboard', name: 'Dashboard',
components: { components: {
G2Line ELine
}, },
data() { data() {
return { return {
...@@ -118,79 +117,7 @@ export default { ...@@ -118,79 +117,7 @@ export default {
yFields: [ yFields: [
{ type: 'array', required: true, message: '请至少选择一个属性', trigger: 'change' } { type: 'array', required: true, message: '请至少选择一个属性', trigger: 'change' }
] ]
},
fakeData: [
{
time: null,
value: {
inpressure: 110,
outpressure: 21
}
},
{
time: null,
value: {
inpressure: 111,
outpressure: 25
}
},
{
time: null,
value: {
inpressure: 112,
outpressure: 24
} }
},
{
time: null,
value: {
inpressure: 115,
outpressure: 23
}
},
{
time: null,
value: {
inpressure: 117,
outpressure: 22
}
},
{
time: null,
value: {
inpressure: 110,
outpressure: 21
}
},
{
time: null,
value: {
inpressure: 111,
outpressure: 25
}
},
{
time: null,
value: {
inpressure: 112,
outpressure: 24
}
},
{
time: null,
value: {
inpressure: 115,
outpressure: 23
}
},
{
time: null,
value: {
inpressure: 117,
outpressure: 22
}
}
]
} }
}, },
created() { created() {
...@@ -234,7 +161,7 @@ export default { ...@@ -234,7 +161,7 @@ export default {
return this.chartData.map(v => { return this.chartData.map(v => {
return { return {
time: v.time, time: v.time,
value: v.value[yField] value: parseInt(v[yField])
} }
}) })
}, },
...@@ -244,46 +171,36 @@ export default { ...@@ -244,46 +171,36 @@ export default {
.then(_ => { .then(_ => {
this.chartData = [] this.chartData = []
this.chartList = [] this.chartList = []
this.mockData() const params = {
.then(_ => { modelId: parseInt(this.activeModel),
instanceId: this.chartField.instanceId,
propertyCodeList: [...this.chartField.yFields]
}
getModelInstanceChart(params)
.then(res => {
this.chartData = res.data.sort((a, b) => {
return (a > b) ? 1 : -1
})
const arr = [] const arr = []
this.propertyList.forEach(v => { this.propertyList.forEach(v => {
const index = this.chartField.yFields.findIndex(item => item === v.id) const index = this.chartField.yFields.findIndex(item => item === v.propertyCode)
index !== -1 && arr.push({ ...v }) index !== -1 && arr.push({ ...v })
}) })
this.chartList = [...arr] this.chartList = [...arr]
}) })
.then(_ => {
this.timer && clearInterval(this.timer) this.timer && clearInterval(this.timer)
this.timer = setInterval(async() => { this.timer = setInterval(async() => {
await this.mockData() const chartData = await getModelInstanceChart(params)
this.chartData = chartData.data.sort((a, b) => {
return (a > b) ? 1 : -1
})
this.count++ this.count++
}, this.chartField.refresh * 1000) }, this.chartField.refresh * 1000)
}) })
.catch(err => console.error(err)) .catch(_ => {})
},
mockData() {
return new Promise(resolve => {
this.fakeData.shift()
this.fakeData.push({
time: null,
value: {
inpressure: this.random(80, 120),
outpressure: this.random(20, 30)
}
})
const arr = []
const time = new Date().getTime()
for (let i = 0; i < 10; i++) {
arr.push({
time: time - (9 - i) * 1000,
value: {
...this.fakeData[i].value
}
})
}
this.chartData = [...arr]
resolve()
}) })
.catch(err => console.error(err))
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment