Advertisement

livecharts中仪表盘_Vue中使用Echarts仪表盘展示实时数据的实现

阅读量:

在vue中echarts仪表盘实时数据

彩笔一枚,简单记录一下。

业务场景:通过websocket实时推送数据,将数据渲染到仪表盘中。

第一步:

基于准备好的dom,初始化echarts仪表盘实例。

第二步:

使用父组件传递数据至子组件接收,在data对象中设置upPressure字段,并将来自设备端的压力参数赋值至此处以便后续方便地将压力数值传递至echarts展示

父组件中

子组件中

export default {

props: {

devicePressure: { type: String, require: true },

},

data() {

return {

upPressure: this.devicePressure,

};

},

第三步:

因为是实时数据,就需要在watch中监听数据变化,实时更新。

注:这里我只监听一个参数变化,没有使用deep: true。

watch: {

//监听devicePressure的数据变化。

devicePressure(newData, oldData) {

//把更新后的数据newData,赋值给需要传入echarts中的参数。

this.upPressure = newData;

//一定要调用echarts实例,要不然echarts不实时展示。

this.drawLine();

},

},

第四步:

在数据处理完成之后, 就需要将其展示到仪表盘上. 因此, 只需定位至相关图表区域, 找到echarts中所需的数据位置即可.

介于仪表盘样式,可结合官方文档自定义。

export default {

props: {

devicePressure: { type: String, require: true },

},

data() {

return {

upPressure: this.devicePressure,

};

},

mounted() {

this.drawLine();

},

watch: {

devicePressure(newData, oldData) {

this.upPressure = newData;

this.drawLine();

},

},

methods: {

drawLine() {

// 基于准备好的dom,初始化echarts实例

const visualOneChart = this.$echarts.instanciate(document.getElementById('visualOneChart'));

// 绘制图表

visualOneChart.setOption({

tooltip: {

formatter: "{a}
{b} : {c}Pa",

},

series: [

{

name: "压力值",

type: "gauge",

clockwise: true,

detail: {

formatter: this.upPressure,

textStyle: {

fontSize: 14,

},

},

data: [{ value: this.upPressure, name: "压力值" }],

radius: "90%",

axisLabel: {// 刻度标签。

show: true,

distance: -5,

color: "black",

fontSize: 10,

formatter: "{value}",

},

axisLine: {// 仪表盘轴线(轮廓线)相关配置。

show: true,

lineStyle: {// 仪表盘轴线样式。

opacity: 1,

width: 15,

shadowBlur: 10,

},

},

pointer: { // 仪表盘指针。

show: true,

length: "70%",

width: 4,

},

},

],

});

},

},

}

至此,本文对Vue中使用Echarts仪表盘展示实时数据的实现进行了全面介绍。如需了解更多相关内容,请参考之前的文章或继续浏览下面的相关文章。感谢大家一直以来的支持与关注!

全部评论 (0)

还没有任何评论哟~