指南
安装
bash
$ pnpm add @wuxianx/charts-vue echarts
bash
$ npm install @wuxianx/charts-vue echarts
bash
$ yarn add @wuxianx/charts-vue echarts
使用方法
添加到 Vue 项目中
js
import { createApp } from 'vue'
import * as echarts from 'echarts'
import { barSimple, lineSimple, radarRainbow, plugin as wuxianxCharts } from '@wuxianx/charts-vue'
import App from './app.vue'
const app = createApp(App)
app.use(wuxianxCharts({ use: { barSimple, lineSimple, radarRainbow }, ec: echarts }))
app.mount('#app')
在组件中使用
vue
<script setup>
const data = {
Taylor: 81,
Clark: 78,
Martin: 75,
Perez: 80,
Lewis: 86,
Wilson: 81,
Jackson: 87,
}
</script>
<template>
<div v-ec="['barSimple', data]" style="width: 100%; height: 300px;" />
</template>
响应式图表
使用 v-ec.watch
并传入一个 ref
来实现基本的响应式图表更新。
[ "barSimple", { "Taylor": 81, "Clark": 78, "Martin": 75, "Perez": 80, "Lewis": 86, "Wilson": 81, "Jackson": 87 } ]
vue
<script setup>
import { ref } from 'vue'
const data = {
Taylor: 81,
Clark: 78,
Martin: 75,
Perez: 80,
Lewis: 86,
Wilson: 81,
Jackson: 87,
}
const ec = ref(['barSimple', data])
function refresh() {
ec.value[0] = Math.random() > 0.5 ? 'barSimple' : 'lineSimple'
// Please note that this is a new `Object`, not a reference to `data`.
ec.value[1] = Object.keys(data).reduce((res, key) => {
res[key] = Math.round(Math.random() * 100)
return res
}, {})
}
</script>
<template>
<div>
<div v-ec.watch="ec" style="width: 100%; height: 300px;" />
<t-button block @click="refresh">
Refresh
</t-button>
<pre>{{ ec }}</pre>
</div>
</template>
自动调整(resize)
如果希望在发生 window.resize
时触发 chart.resize
,可以使用 v-ec.resize
。
vue
<script setup>
const data = {
Taylor: 502.84,
Clark: 205.97,
Martin: 332.79,
Perez: 282.55,
Wilson: 214.02,
}
</script>
<template>
<div
v-ec.resize="['lineGradientTrend', data]"
style="width: 100%; height: 300px;"
/>
</template>
获取图表实例
如果需要获取 echarts
实例,可以使用 echarts.getInstanceByDom
方法。请参考以下示例:
vue
<script setup>
import { ref } from 'vue'
import { getInstanceByDom } from 'echarts'
const data = [
[2016, { Perez: 75, Harris: 99, Walker: 89, Davis: 51, Johnson: 86 }],
[2017, { Perez: 85, Harris: 89, Walker: 59, Davis: 72, Johnson: 39 }],
[2018, { Perez: 35, Harris: 59, Walker: 58, Davis: 63, Johnson: 49 }],
]
const el = ref()
function exportChart() {
const ecInstance = getInstanceByDom(el.value)
const src = ecInstance.getDataURL({ type: 'png' })
downloadBase64File(src)
}
function downloadBase64File(data) {
const element = document.createElement('a')
element.setAttribute('href', data)
element.setAttribute('download', 'echarts-instance.png')
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
}
</script>
<template>
<div>
<div
ref="el"
v-ec.resize="['radarRainbow', data]"
style="width: 100%; height: 500px;"
/>
<t-button block style="margin-top: 10px;" @click="exportChart">
Download
</t-button>
</div>
</template>
Typescript 支持
如果使用 TypeScript,可以使用类型 WuxianxChartsValue<'barSimple'>
或函数 defineChartOptions('barSimple', ...)
进行类型限制。请参考以下示例:
vue
<script setup lang="ts">
import { type WuxianxChartsValue, defineChartOptions } from '@wuxianx/charts-vue'
const _data = {
Taylor: 81,
Clark: 78,
Martin: 75,
Perez: 80,
Lewis: 86,
Wilson: 81,
Jackson: 87,
}
const data1: WuxianxChartsValue<'barSimple'> = ['barSimple', _data]
const data2 = defineChartOptions('lineSimple', _data)
</script>
<template>
<div>
<div v-ec="data1" style="width: 100%; height: 300px;" />
<div v-ec="data2" style="width: 100%; height: 300px;" />
</div>
</template>