| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <template>
- <div ref="chartRef" :style="{ height, width }"></div>
- </template>
- <script lang="ts">
- import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
- import { useECharts } from '/@/hooks/web/useECharts';
- export default defineComponent({
- name: 'LineMulti',
- props: {
- /**
- * 图表数据,配合xAxisPropType、propTypeArr使用
- */
- chartData: {
- type: Array,
- default: () => [],
- required: true,
- },
- option: {
- type: Object,
- default: () => ({}),
- },
- /**
- * x轴数据对应的prop,将从chartData中映射prop到一个Set中。
- *
- * @example
- * ```js
- * const props = { xAxisPropType: 'name', chartData: [{ name: 'a' }, { name: 'b' }] }
- *
- * // truns out
- * option.xAxis.data = ['a', 'b'];
- * ```
- */
- xAxisPropType: {
- type: String,
- required: true,
- },
- /**
- * 提供一个Map,该Map将用于生成基于chartData的echart.series数据。
- *
- * @example
- * ```js
- * const props = { propTypeArr: new Map([['valueA', '值A']]), chartData: [{ valueA: 'a' }, { valueA: 'b' }] }
- *
- * // truns out
- * option.series = [{ name: '值A', data: ['a', 'b'] }]
- * ```
- */
- propTypeArr: {
- type: Map,
- default: () => new Map(),
- required: true,
- },
- type: {
- type: String as PropType<string>,
- default: 'line',
- },
- width: {
- type: String as PropType<string>,
- default: '100%',
- },
- height: {
- type: String as PropType<string>,
- default: 'calc(100vh - 78px)',
- },
- },
- emits: ['click'],
- setup(props, { emit }) {
- const chartRef = ref<HTMLDivElement | null>(null);
- const { setOptions, getInstance } = useECharts(chartRef as Ref<HTMLDivElement>);
- const option = reactive({
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'shadow',
- label: {
- show: true,
- backgroundColor: '#333',
- },
- },
- },
- legend: {
- top: 10,
- },
- grid: {
- top: 60,
- },
- xAxis: {
- type: 'category',
- data: [],
- },
- yAxis: {
- type: 'value',
- },
- series: [],
- });
- watchEffect(() => {
- props.chartData && initCharts();
- });
- function initCharts() {
- if (props.option) {
- Object.assign(option, props.option);
- }
- //图例类型
- // let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
- //轴数据
- let xAxisData = Array.from(new Set(props.chartData.map((item) => item[props.xAxisPropType])));
- let seriesData = [];
- [...props.propTypeArr.keys()].forEach((filed) => {
- let obj = { name: props.propTypeArr.get(filed), type: props.type };
- // let chartArr = props.chartData.filter((item) => filed === item.type);
- //data数据
- obj['data'] = props.chartData.map((item) => item[filed]);
- seriesData.push(obj);
- });
- option.series = seriesData;
- option.xAxis.data = xAxisData;
- setOptions(option, false);
- getInstance()?.off('click', onClick);
- getInstance()?.on('click', onClick);
- }
- function onClick(params) {
- emit('click', params);
- }
- return { chartRef };
- },
- });
- </script>
|