| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <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: 'BarMulti',
- props: {
- chartData: {
- type: Array,
- default: () => [],
- required: true,
- },
- option: {
- type: Object,
- default: () => ({}),
- },
- type: {
- type: String as PropType<string>,
- default: 'bar',
- },
- xAxisPropType: {
- type: String,
- required: true,
- },
- propTypeArr: {
- type: Map,
- default: () => new Map(),
- required: true,
- },
- 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,
- textStyle: {
- color: '#ffffffee',
- },
- },
- grid: {
- left: 60,
- right: 50,
- bottom: 50,
- },
- xAxis: {
- type: 'category',
- data: [],
- },
- yAxis: {
- type: 'value',
- nameTextStyle: {
- fontSize: 14,
- },
- },
- 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((type) => {
- let obj = { name: props.propTypeArr.get(type), type: props.type };
- //data数据
- obj['data'] = props.chartData.map((item) => item[type]);
- 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>
|