示例说明
vue-echarts-linkage 联动组件的基础示例。
注意:基础示例包含了最基本的工具栏功能,包括:
- 数据视图
- 外部允许传入额外数据
1. 数据视图
- 工具栏中新增数据视图功能按钮,点击按钮后,子画面中会显示数据视图。
点我查看代码
vue
<template>
<div class="btn-container">
<el-button type="primary" size="small" @click="addLinkageBtnClick">新增echarts实例</el-button>
<el-button type="primary" size="small" @click="addLotEmptyLinkageBtnClick">批量新增空白echarts</el-button>
<div class="drag-rect drag-rect-line" draggable="true"><span>可拖拽折线系列</span></div>
</div>
<!-- 可自定义配置显示列数(cols) | 最大图表数(echarts-max-count) | 空白图表数(empty-echart-count) -->
<VueEchartsLinkage
ref="echartsLinkageRef_2"
:cols="2"
:is-echarts-height-change="false"
:echarts-max-count="10"
grid-align :theme="theme" :use-graphic-location="false"
:echarts-height-fixed-count="2" @drop-echart="dropEchart"
:extra-option="extraOption"
@listener-excel-view="listenerExcelView" />
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { ElButton } from 'element-plus';
import { RandomUtil } from "@/components/utils/index";
import { VueEchartsLinkage } from 'vue-echarts-linkage';
import type { OneDataType, DropEchartType, ListenerExcelViewType, excelViewType, excelViewHeadType } from 'vue-echarts-linkage';
import "vue-echarts-linkage/dist/style.css";
import { MyTheme } from "@/composables/MyTheme";
const { theme } = new MyTheme();
const echartsLinkageRef_2 = ref<InstanceType<typeof VueEchartsLinkage>>();
let seriesType = 'line' as 'line' | 'bar';
let switchFlag = false;
// 额外的配置项
const extraOption = {
toolbox: {
feature: {
myThemeButton: {
show: false
},
myEnlargeShrinkButton: {
show: false
},
myExcelView: {
show: true
},
mySaveAsImage: {
show: false
},
myDeleteButton: {
show: false
}
}
},
}
// 监听excel数据视图按钮点击事件
const listenerExcelView = (data: ListenerExcelViewType, callback: (params: excelViewType) => void) => {
console.log("listenerExcelView", data);
const { id, seriesLink } = data;
console.log("id", id);
console.log("seriesLink", seriesLink);
let params: excelViewType;
if (seriesLink && seriesLink.isLinkMode) {
const primaryKeyValues = seriesLink?.linkData.map(item => item.label); // 提取主键值
params = { // 多卷
headXname: '长度',
preAdd: [
{ name: '卷号', value: primaryKeyValues, isPrimaryKey: true },
{ name: '宽度', value: [1000, 1500] },
{ name: '厚度', value: [0.35, 0.40] },
] as excelViewHeadType[],
}
} else {
params = { // 单卷
headXname: '长度',
preAdd: [{ name: '卷号', value: 'P202410210001', isPrimaryKey: true } as excelViewHeadType],
postAdd: [{ name: '备注', value: '备注信息' } as excelViewHeadType],
}
}
callback(params);
}
// 新增按钮
const addLinkageBtnClick = () => {
const seriesData = RandomUtil.getSeriesData(1000);
const maxEchartsIdSeq = echartsLinkageRef_2.value!.getMaxEchartsIdSeq();
const oneDataType: OneDataType = {
name: `新增图表${maxEchartsIdSeq + 1}`,
yAxisName: `[${Math.floor(Math.random() * 10) > 5 ? 'mm' : '℃'}]`,
type: 'line',
seriesData: seriesData,
// markLineArray: [RandomUtil.getRandomDataFromInterval(0, 1000), RandomUtil.getRandomDataFromInterval(0, 1000)]
visualMapSeries: {
pieces: [{ min: 5000, max: 8000 }],
piecesOnTooltip: { show: true, value: '自定义pieces' }
},
};
echartsLinkageRef_2.value!.addEchart(oneDataType);
}
// 批量新增空白echarts,携带legend数据
const addLotEmptyLinkageBtnClick = () => {
for (let i = 0; i < 1; i++) {
const oneDataTypeArray: OneDataType[] = [];
for (let j = 0; j < 3; j++) {
const maxEchartsIdSeq = echartsLinkageRef_2.value!.getMaxEchartsIdSeq();
const oneDataType: OneDataType = {
name: `新增图表${maxEchartsIdSeq + 1}-${Math.floor(Math.random() * 1000)}`,
type: 'line',
seriesData: [],
customData: `新增图表${maxEchartsIdSeq + 1}-${Math.floor(Math.random() * 1000)}`,
xAxisName: '[m]',
yAxisName: `[${Math.floor(Math.random() * 10) > 5 ? 'mm' : '℃'}]`,
};
oneDataTypeArray.push(oneDataType);
}
echartsLinkageRef_2.value!.addEchart(oneDataTypeArray);
}
}
// 新增series按钮
const addLinkageSeriesCommon = (type: 'line' | 'bar' = 'line', id?: string) => {
let seriesData = RandomUtil.getSeriesData(6000);
const baseLineData = JSON.parse(JSON.stringify(seriesData));
if (Math.random() > 0.5) {
for (let i = 0; i < 10; i++) {
baseLineData[i][1] = 100000;
}
}
if (switchFlag) {
seriesData = RandomUtil.getSwitchData(6000);
}
const maxEchartsIdSeq = echartsLinkageRef_2.value!.getMaxEchartsIdSeq();
id = id || 'echart' + maxEchartsIdSeq;
const random = Math.floor(Math.random() * 100);
const oneDataType: OneDataType = {
name: `新增图表${maxEchartsIdSeq}-${random}`,
yAxisName: `[${Math.floor(Math.random() * 10) > 5 ? 'mm' : '℃'}]`,
type: type,
seriesData: seriesData,
// visualMapSeries: {
// pieces: [{ min: 5000, max: 8000 }],
// baseLine: {
// mode: 'below',
// value: baseLineData,
// isShowOnToolTip: true,
// }
// },
// 多卷首尾连接设置
// seriesLink: {
// isLinkMode: true,
// linkData: [
// { label: 'P202410210001', data: RandomUtil.getSeriesData(1000) },
// { label: 'P202410210002', data: RandomUtil.getSeriesData(1000) },
// { label: 'P202410210003', data: RandomUtil.getSeriesData(1000) },
// { label: 'P202410210004', data: RandomUtil.getSeriesData(1000) },
// ]
// },
};
if (switchFlag) {
oneDataType.dataType = 'switch';
switchFlag = false;
}
echartsLinkageRef_2.value!.addEchartSeries(id, oneDataType);
}
// 拖拽回调事件
const dropEchart = (data: DropEchartType) => {
addLinkageSeriesCommon(seriesType, data.id);
}
const init = () => {
addLinkageBtnClick();
addLinkageBtnClick();
addLinkageBtnClick();
addLinkageBtnClick();
}
onMounted(() => {
init();
});
</script>
<style scoped lang="less">
.btn-container {
height: 5vh;
padding: 10px;
display: flex;
align-items: center;
column-gap: 10px;
.drag-rect {
display: flex;
justify-content: center;
align-items: center;
height: 24px;
border-radius: 4px;
padding: 5px 11px;
background-image: linear-gradient(to right, #4286f4, #00b4d8);
border: 1px solid #00b4d8;
span {
color: #fff;
font-size: 12px;
line-height: 12px;
}
}
}
.echarts-linkage-container {
width: 100%;
height: 60vh;
}
</style>
<style scoped lang="less">
.el-button {
margin-left: 0;
}
</style>
<style>
.echarts-linkage-container .main-container {
padding-bottom: 0 !important;
}
</style>
More docs
...