子组件使用echarts,让图表更新

1.定义容器

<template>
    <div style="display: flex;" v-if="props.billList">
        <!-- 饼状图容器 -->
        <div ref="pieChart" style="width: 32vw; height: 45vh"></div>
        <!-- 柱形图容器 -->
        <div ref="barChart" style="width: 32vw; height: 45vh"></div>
        <!-- 折线图容器 -->
        <div ref="lineChart" style="width: 32vw; height: 45vh"></div>
    </div>
</template>

2.导入所需

  import * as echarts from 'echarts';
    import { Ref } from 'vue';

3.初始化以及挂载

  const pieChart: Ref<HTMLElement | null> = ref();
    const barChart: Ref<HTMLElement | null> = ref();
    const lineChart: Ref<HTMLElement | null> = ref();

    const props = defineProps({
        billList: {
            type: Array,
            required: true
        },
    })
    //类别数据
    
    const categoryDataList =ref([])
    const pieChartInstance =ref(null)
    //金属材质
    const materList = ref([])
    const barChartInstance =ref(null)
    //主石材质
    const mainStoneList = ref([])
    const lineChartInstance =ref(null)

4.数据处理以及更新

  watch(() => props.billList, () => {
        // 数据处理
        categoryDataList.value = []
        //金属材质
        materList.value = []
        //主石材质
        mainStoneList.value = []
        if (props.billList.length) {
            props.billList.map(item => {
                // 检查 categoryDataList 中是否已存在相同的 category 属性名
                const existingIndex = categoryDataList.value.findIndex(data => data.name === item.category);
                if (existingIndex !== -1) {
                    // 如果已存在相同的 category 属性名,则将其值加一
                    categoryDataList.value[existingIndex].value += 1;
                } else {
                    // 如果不存在相同的 category 属性名,则将该项添加到 categoryDataList 数组中
                    categoryDataList.value.push({ name: item.category, value: 1 });
                }

                //金属材质
                const metalInfo = item.metalInfo ? JSON.parse(item.metalInfo) : null
                let material = ""
                if (metalInfo) {
                    material = metalInfo[0] != undefined || metalInfo[0] != null ? metalInfo[0].material : null
                }

                const existingIndex1 = materList.value.findIndex(data => data.name === material)
                if (existingIndex1 !== -1) {
                    materList.value[existingIndex1].value += 1
                } else {
                    materList.value.push({ name: material, value: 1 })
                }
                const mainInfo = item.mainStoneInfo ? JSON.parse(item.mainStoneInfo) : null
                let mainStone = ""
                if (mainInfo) {
                    mainStone = mainInfo[0] != undefined || mainInfo[0] != null ? mainInfo[0].material : null
                }


                console.log("====>", mainStone)
                const existingIndex2 = mainStoneList.value.findIndex(data => data.name === mainStone)
                if (existingIndex2 !== -1) {
                    mainStoneList.value[existingIndex2].value += 1
                } else {
                    mainStoneList.value.push({ name: mainStone, value: 1 })
                }

                return { categoryDataList, materList, mainStoneList }
            })
        }
    })

配置项

 const updateChart = () => {
        if (!pieChartInstance.value) return;
        pieChartInstance.value.setOption({
            title: {
                text: '类型饼状图',
                x: "center",
            },
            legend: {
                show: true,
                left: "7vw",
                orient: "vertical"
            },
            tooltip: {
                trigger: 'item'
            },
            series: {
                type: 'pie',
                data: categoryDataList.value.map(item => ({
                    name: item.name,
                    value: item.value
                })),
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                padAngle: 5,
                itemStyle: {
                    borderRadius: 10,
                },
                label: {
                    show: true,
                    position: 'outside',
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 14,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: true,
                    length: 6,
                    length2: 17
                },

            },
        });


    }
    // 更新柱状图
    const updateBarChart = () => {
        // 更新柱状图的逻辑...
        if (!barChartInstance.value) return
        barChartInstance.value.setOption({
            title: {
                text: '金属材质柱形图',
                x: "center",
            },
            legend: {

                show: true,
                left: "7vw",
                orient: "vertical",
                itemStyle: {
                    color: "rgba(51, 103, 114, 1)"

                }
            },
            xAxis: {
                type: 'category',
                data: materList.value.map(item => item.name),
                name: "金属材质",
                offset: 14,
                // boundaryGap:false,
            },
            yAxis: {
                type: 'value',
                name: "数量"
            },
            series: [{
                data: materList.value.map(item => item.value),
                type: 'bar',
                name: "金属材质",
                realtimeSort: true,
                itemStyle: {
                    color: "rgba(51, 103, 114, 1)"
                }
            }],
        });
    };

    // 更新折线图
    const updateLineChart = () => {
        // 更新折线图的逻辑...
        if (!lineChartInstance.value) return
        lineChartInstance.value.setOption({
            title: {
                text: '主石折线图',
                x: "center",
            },
            legend: {
                show: true,
                left: "7vw",
                orient: "vertical"
            },

            xAxis: {
                type: 'category',
                data: mainStoneList.value.map(item => item.name),
                name: "主石材质",
                offset: 14,
                nameTextStyle: {
                    fontSize: "14px",
                }
            },
            yAxis: {
                type: 'value',
                name: "数量"
            },
            series: [{
                data: mainStoneList.value.map(item => item.value),
                type: 'line',
                itemStyle: {
                    color: "rgba(51, 103, 114, 1)"
                },
                name: "主石数量"
            }],
        });
    };
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Vue 中使用 Echarts 绘制自定义图表组件可以按照以下步骤进行: 1. 首先,确保已经安装了 echarts 库。可以使用 npm 或 yarn 进行安装: ``` npm install echarts --save ``` 2. 创建一个 Vue 组件,用于包含 Echarts 图表。可以命名为 `EchartsChart.vue`。 ```html <template> <div :ref="chartRef" class="echarts-chart"></div> </template> <script> import echarts from 'echarts'; export default { props: ['chartData'], data() { return { chartRef: 'echartsChart', chartInstance: null, }; }, mounted() { this.chartInstance = echarts.init(this.$refs[this.chartRef]); this.renderChart(); }, methods: { renderChart() { // 使用 chartData 配置图表数据和样式 this.chartInstance.setOption(this.chartData); }, }, beforeDestroy() { if (this.chartInstance) { this.chartInstance.dispose(); // 销毁图表实例,释放资源 } }, }; </script> <style scoped> .echarts-chart { width: 100%; height: 400px; /* 根据需求设置图表高度 */ } </style> ``` 在上面的示例中,我们创建了一个 `EchartsChart` 组件,接受一个 `chartData` 属性,用于配置图表的数据和样式。在 `mounted` 钩中,我们初始化了 Echarts 实例,并在 `renderChart` 方法中使用 `setOption` 方法渲染图表。在组件销毁前,我们通过调用 `dispose` 方法销毁图表实例,释放资源。 3. 在使用自定义图表组件的父组件中,引入并使用 `EchartsChart` 组件。可以通过传递 `chartData` 属性来配置图表。 ```html <template> <div> <echarts-chart :chartData="chartData"></echarts-chart> </div> </template> <script> import EchartsChart from '@/components/EchartsChart.vue'; export default { components: { EchartsChart, }, data() { return { chartData: { // 配置图表的数据和样式 }, }; }, }; </script> ``` 在上面的示例中,我们在父组件中引入了 `EchartsChart` 组件,并通过 `chartData` 属性配置图表的数据和样式。 你可以根据自己的需求在 `chartData` 中配置图表的相关数据,例如 `title`、`tooltip`、`xAxis`、`yAxis`、`series` 等。具体的配置可以参考 Echarts 的文档。 希望这个示例能够帮助你创建自定义的 Echarts 图表组件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值