feat: echarts图表下载

Former-commit-id: 27e3dff661a360f682361e754ec07517c0308e58
This commit is contained in:
april
2023-06-12 18:39:46 +08:00
parent f27ac3a932
commit a124e20a9e

View File

@@ -1,146 +1,195 @@
<!-- 线 + 柱混合图 --> <!-- 线 + 柱混合图 -->
<template> <template>
<el-card> <el-card>
<template #header> 业绩柱状图 </template> <template #header>
<div class="title">
业绩柱状图
<el-tooltip effect="dark" content="点击试试下载" placement="bottom">
<i-ep-download
class="download"
@click="downloadEchart"
></i-ep-download>
</el-tooltip>
</div>
</template>
<div :id="id" :class="className" :style="{ height, width }" /> <div :id="id" :class="className" :style="{ height, width }" />
</el-card> </el-card>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import * as echarts from 'echarts'; import * as echarts from "echarts";
const props = defineProps({ const props = defineProps({
id: { id: {
type: String, type: String,
default: 'barChart' default: "barChart",
}, },
className: { className: {
type: String, type: String,
default: '' default: "",
}, },
width: { width: {
type: String, type: String,
default: '200px', default: "200px",
required: true required: true,
}, },
height: { height: {
type: String, type: String,
default: '200px', default: "200px",
required: true required: true,
} },
}); });
const options = { const options = {
grid: { grid: {
left: '2%', left: "2%",
right: '2%', right: "2%",
bottom: '10%', bottom: "10%",
containLabel: true containLabel: true,
}, },
tooltip: { tooltip: {
trigger: 'axis', trigger: "axis",
axisPointer: { axisPointer: {
type: 'cross', type: "cross",
crossStyle: { crossStyle: {
color: '#999' color: "#999",
} },
} },
}, },
legend: { legend: {
x: 'center', x: "center",
y: 'bottom', y: "bottom",
data: ['收入', '毛利润', '收入增长率', '利润增长率'], data: ["收入", "毛利润", "收入增长率", "利润增长率"],
textStyle: { textStyle: {
color: '#999' color: "#999",
} },
}, },
xAxis: [ xAxis: [
{ {
type: 'category', type: "category",
data: ['浙江', '北京', '上海', '广东', '深圳'], data: ["浙江", "北京", "上海", "广东", "深圳"],
axisPointer: { axisPointer: {
type: 'shadow' type: "shadow",
} },
} },
], ],
yAxis: [ yAxis: [
{ {
type: 'value', type: "value",
min: 0, min: 0,
max: 10000, max: 10000,
interval: 2000, interval: 2000,
axisLabel: { axisLabel: {
formatter: '{value} ' formatter: "{value} ",
} },
}, },
{ {
type: 'value', type: "value",
min: 0, min: 0,
max: 100, max: 100,
interval: 20, interval: 20,
axisLabel: { axisLabel: {
formatter: '{value}%' formatter: "{value}%",
} },
} },
], ],
series: [ series: [
{ {
name: '收入', name: "收入",
type: 'bar', type: "bar",
data: [7000, 7100, 7200, 7300, 7400], data: [7000, 7100, 7200, 7300, 7400],
barWidth: 20, barWidth: 20,
itemStyle: { itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#83bff6' }, { offset: 0, color: "#83bff6" },
{ offset: 0.5, color: '#188df0' }, { offset: 0.5, color: "#188df0" },
{ offset: 1, color: '#188df0' } { offset: 1, color: "#188df0" },
]) ]),
} },
}, },
{ {
name: '毛利润', name: "毛利润",
type: 'bar', type: "bar",
data: [8000, 8200, 8400, 8600, 8800], data: [8000, 8200, 8400, 8600, 8800],
barWidth: 20, barWidth: 20,
itemStyle: { itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#25d73c' }, { offset: 0, color: "#25d73c" },
{ offset: 0.5, color: '#1bc23d' }, { offset: 0.5, color: "#1bc23d" },
{ offset: 1, color: '#179e61' } { offset: 1, color: "#179e61" },
]) ]),
} },
}, },
{ {
name: '收入增长率', name: "收入增长率",
type: 'line', type: "line",
yAxisIndex: 1, yAxisIndex: 1,
data: [60, 65, 70, 75, 80], data: [60, 65, 70, 75, 80],
itemStyle: { itemStyle: {
color: '#67C23A' color: "#67C23A",
} },
}, },
{ {
name: '利润增长率', name: "利润增长率",
type: 'line', type: "line",
yAxisIndex: 1, yAxisIndex: 1,
data: [70, 75, 80, 85, 90], data: [70, 75, 80, 85, 90],
itemStyle: { itemStyle: {
color: '#409EFF' color: "#409EFF",
} },
} },
] ],
}; };
const chart = ref<any>("");
onMounted(() => { onMounted(() => {
// 图表初始化 // 图表初始化
const chart = echarts.init( chart.value = markRaw(
document.getElementById(props.id) as HTMLDivElement echarts.init(document.getElementById(props.id) as HTMLDivElement)
); );
chart.setOption(options);
chart.value.setOption(options);
// 大小自适应 // 大小自适应
window.addEventListener('resize', () => { window.addEventListener("resize", () => {
chart.resize(); chart.value.resize();
}); });
}); });
const downloadEchart = () => {
// 获取画布图表地址信息
const img = new Image();
img.src = chart.value.getDataURL({
type: "png",
pixelRatio: 1,
backgroundColor: "#fff",
});
// 当图片加载完成后,生成 URL 并下载
img.onload = () => {
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext("2d");
if (ctx) {
ctx.drawImage(img, 0, 0, img.width, img.height);
const link = document.createElement("a");
link.download = `业绩柱状图.png`;
link.href = canvas.toDataURL("image/png", 0.9);
document.body.appendChild(link);
link.click();
link.remove();
}
};
};
</script> </script>
<style lang="scss" scoped>
.title {
display: flex;
justify-content: space-between;
.download {
cursor: pointer;
&:hover {
color: #409eff;
}
}
}
</style>