Skip to content

为项目扩展 ECharts 图表 ​

要在水泵旁显示过去一小时的压力,先从内置「图表」拖入趋势曲线,设置横轴 categories 和序列 series。趋势、柱状、雷达、桑基等已有对应图元,均可与设备共用一张画面。图表类型与数据格式

何时需要自定义组件 ​

若客户已有特定主题或交互的图表,把它封装为 Vue 组件即可沿用原实现。让组件从 props 接收数据,填满图元边框,并在拖动调整尺寸时重绘。以下为使用 vue-echarts 的最小示例。

vue
<!-- 项目文件 PressureChart.vue,需先安装 echarts 和 vue-echarts -->
<script setup lang="ts">
import { computed } from 'vue';
import VChart from 'vue-echarts';
import { use } from 'echarts/core';
import { LineChart } from 'echarts/charts';
import { GridComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
use([LineChart, GridComponent, CanvasRenderer]);
const props = defineProps<{ readings: number[] }>();
const option = computed(() => ({
  xAxis: { type: 'category', data: props.readings.map((_, i) => String(i + 1)) },
  yAxis: { type: 'value', name: 'MPa' },
  series: [{ type: 'line', data: props.readings }]
}));
</script>
<template><VChart :option="option" autoresize style="width: 100%; height: 100%" /></template>

在应用创建后、挂载前将组件加入 Vue 注册表,再注册图元。设置 readings 后,预览收到新的数组即会更新图表。

ts
import { leftAsideStore } from 'maotu';
import PressureChart from './PressureChart.vue';

app.component('pressure-chart', PressureChart);
leftAsideStore.registerConfig('项目图表', [{
  id: 'pressure-chart', title: '出水压力', type: 'vue', thumbnail: '',
  width: 360, height: 180,
  props: { readings: { title: '压力序列', type: 'jsonEdit', val: [0.38, 0.41, 0.47] } }
}]);

自定义图表注册后可以像普通图元一样拖入画布、调整尺寸