RayChart组件新增watchOptions属性

This commit is contained in:
ray_wuhao 2023-02-22 13:31:41 +08:00
parent a112799b42
commit 360ae3c373
2 changed files with 28 additions and 3 deletions

View File

@ -3,4 +3,5 @@
height: var(--ray-chart-height); height: var(--ray-chart-height);
border: none; border: none;
outline: none; outline: none;
box-sizing: border-box;
} }

View File

@ -17,6 +17,7 @@ import {
PieChart, PieChart,
CandlestickChart, CandlestickChart,
ScatterChart, ScatterChart,
PictorialBarChart,
} from 'echarts/charts' // 系列类型(后缀都为 `SeriesOption`) } from 'echarts/charts' // 系列类型(后缀都为 `SeriesOption`)
import { LabelLayout, UniversalTransition } from 'echarts/features' // 标签自动布局, 全局过渡动画等特性 import { LabelLayout, UniversalTransition } from 'echarts/features' // 标签自动布局, 全局过渡动画等特性
import { CanvasRenderer } from 'echarts/renderers' // `echarts` 渲染器 import { CanvasRenderer } from 'echarts/renderers' // `echarts` 渲染器
@ -131,15 +132,17 @@ const RayChart = defineComponent({
default: false, default: false,
}, },
options: { options: {
type: Object, type: Object as PropType<echarts.EChartsCoreOption>,
default: () => ({}), default: () => ({}),
}, },
success: { success: {
/** /**
* *
* @param `chart` * chart
* *
* *
*
* () => EChartsInstance
*/ */
type: Function, type: Function,
default: () => ({}), default: () => ({}),
@ -148,6 +151,8 @@ const RayChart = defineComponent({
/** /**
* *
* *
*
* () => void
*/ */
type: Function, type: Function,
default: () => ({}), default: () => ({}),
@ -178,6 +183,11 @@ const RayChart = defineComponent({
type: Array, type: Array,
default: () => [], default: () => [],
}, },
watchOptions: {
/** 主动监听 options 变化 */
type: Boolean,
default: true,
},
}, },
setup(props) { setup(props) {
const settingStore = useSetting() const settingStore = useSetting()
@ -221,11 +231,12 @@ const RayChart = defineComponent({
PieChart, PieChart,
CandlestickChart, CandlestickChart,
ScatterChart, ScatterChart,
PictorialBarChart,
]) // 注册类型 ]) // 注册类型
echarts.use([LabelLayout, UniversalTransition]) // 注册布局, 过度效果 echarts.use([LabelLayout, UniversalTransition]) // 注册布局, 过度效果
// 如果业务场景中需要 `svg` 渲染器, 手动导入渲染器后使用该行代码即可 // 如果业务场景中需要 `svg` 渲染器, 手动导入渲染器后使用该行代码即可(不过为了体积考虑, 移除了 SVG 渲染器)
// echarts.use([props.canvasRender ? CanvasRenderer : SVGRenderer]) // echarts.use([props.canvasRender ? CanvasRenderer : SVGRenderer])
echarts.use([CanvasRenderer]) // 注册渲染器 echarts.use([CanvasRenderer]) // 注册渲染器
@ -300,6 +311,8 @@ const RayChart = defineComponent({
props.success?.(echartInstance) props.success?.(echartInstance)
} catch (e) { } catch (e) {
props.error?.() props.error?.()
console.error(e)
} }
} }
@ -369,6 +382,17 @@ const RayChart = defineComponent({
}, },
) )
if (props.watchOptions) {
watch(
() => props.watchOptions,
() => {
const options = useMergeOptions()
echartInstance?.setOption(options)
},
)
}
onBeforeMount(async () => { onBeforeMount(async () => {
await registerChartCore() await registerChartCore()
}) })