vue2+Echarts数据可视化 【帕累托图】

在这里插入图片描述

接口得到的数据如下

在这里插入图片描述
要经过排序 ,计算累计百分比得到数据

蓝色 柱状图数据: 取count字段值

横坐标:取 id值

折线图:根据柱状图的数据计算累计百分比

 getInterface(data) {
            getParetoChart(data).then((res) => {
                if (res) {
                    this.xList = res.map(item => {
                        return window.vm.$i18n.t(`MsgAlarmType.${item.id}`)
                    });
                    let data = res.map(item => {
                        return item.count
                    });
                    // 对数据进行排序
                    data.sort((a, b) => b - a);
                    this.countList = data
                    // 计算总和
                    const total = data.reduce((sum, value) => sum + value, 0);
                    // 计算累计百分比
                    let cumulativePercentage = 0;
                    this.percentList = data.map(value => {
                        const percentage = (value / total) * 100;
                        cumulativePercentage += percentage;
                        return cumulativePercentage.toFixed(2);
                    });


                }

            });
        },

完整代码

<template>
  <div class="bkChart">
       <date-range-picker v-model="deviceFormData.time" class="date-item"
                        :start-placeholder="$t('NeoLight.startTime')" :end-placeholder="$t('NeoLight.endTime')"
                        value-format="yyyy-MM-dd" @change="getList" />
       <lineChart :labelList="xList" :barValueList="countList" :lineValueList="percentList" />
  </div>
</template>
<script>
import lineChart from "../../../components/Echarts/paretoChart.vue";
import { getParetoChart } from "@/api/neolight/inventory";
export default {
    name: "paretoChart",
    components: { lineChart },
    data() {
        return {
          deviceFormData: {
                time: []
            },
            xList: [],// x轴,横坐标值
            countList: [],//柱状图数据
            percentList: [],//折线图数据,计算累计百分比
        };
    },
    mounted() {
    },
    methods: {
        getList() {
            let data = {
                createDate: this.deviceFormData.time,
            };
            this.getInterface(data)
        },
        getInterface(data) {
            getParetoChart(data).then((res) => {
                if (res) {
                    this.xList = res.map(item => {
                        return window.vm.$i18n.t(`MsgAlarmType.${item.id}`)
                    });
                    let data = res.map(item => {
                        return item.count
                    });

                    // 对数据进行排序
                    data.sort((a, b) => b - a);
                    this.countList = data
                    // 计算总和
                    const total = data.reduce((sum, value) => sum + value, 0);
                    // 计算累计百分比
                    let cumulativePercentage = 0;
                    this.percentList = data.map(value => {
                        const percentage = (value / total) * 100;
                        cumulativePercentage += percentage;
                        return cumulativePercentage.toFixed(2);
                    });
                }
            });
        },
    },
};
</script>

paretoChart.vue文件

<template>
    <div ref="Echart" id="myChart" :class="className" :style="{ height: height, width: width }" />
</template>
  
<script>
export default {
    props: {
        labelList: Array,
        barValueList: Array,
        lineValueList: Array,
        className: {
            type: String,
            default: "chart",
        },
        width: {
            type: String,
            default: "100%",
        },
        height: {
            type: String,
            default: "500px",
        },
    },
    data() {
        return {
            chart: null,
        };
    },
    watch: {
        labelList: {
            handler(newQuestion, oldQuestion) {
                this.labelList = newQuestion;
                this.initChart();

            },
            deep: true,
        },
        barValueList: {
            handler(newValue, oldValue) {
                this.barValueList = newValue;
                this.initChart();
            },
            deep: true,
        },
        lineValueList: {
            handler(newValue, oldValue) {
                this.lineValueList = newValue;
                this.initChart();
            },
            deep: true,
        },
    },
    mounted() {
        this.initChart();
        this.__resizeHandler = debounce(() => {
            if (this.chart) {
                this.chart.resize();
            }
        }, 100);
        window.addEventListener("resize", this.__resizeHandler);
    },

    methods: {
        initChart() {
            this.chart = this.$echarts.init(this.$refs.Echart);
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        crossStyle: {
                            color: '#fff'
                        }
                    }
                },
                legend: {

                    left: "center", //图例距离整个容器左边
                    icon: "rect", //设置图例图标的形状为实心圆,这个不填,默认是矩形
                    itemGap: 25, //图例图标与文字间的间距
                    textStyle: {
                        fontSize: 12, //图例文字字体大小
                        color: "#ffffff", //图例文字颜色
                    },
                },
                xAxis: [
                    {
                        type: 'category',
                        data: this.labelList,
                        axisPointer: {
                            type: 'shadow'
                        },
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: window.vm.$i18n.t('MsgAlarmType.amount'),
                        interval: 10,
                        axisLabel: {
                            formatter: '{value}'
                        }
                    },
                    {
                        type: 'value',
                        name: '%',
                        // min: 0,
                        // max: 25,
                        interval: 5,
                        axisLabel: {
                            formatter: '{value} %'
                        }
                    }
                ],
                series: [
                    {
                        name: window.vm.$i18n.t('MsgAlarmType.amount'),
                        type: 'bar',
                        tooltip: {
                            valueFormatter: function (value) {
                                return value
                            }
                        },
                        data: this.barValueList
                    },
                    {
                        name: window.vm.$i18n.t('MsgAlarmType.accPercent'),
                        type: 'line',
                        yAxisIndex: 1,
                        tooltip: {
                            valueFormatter: function (value) {
                                return value + '%';
                            }
                        },
                        data: this.lineValueList
                    }
                ]

            });
        },
    },
    beforeDestroy() {
        if (!this.chart) {
            return;
        }
        window.removeEventListener("resize", this.__resizeHandler);
        this.chart.dispose();
        this.chart = null;
    },
};
</script>

参考页面链接:https://blog.csdn.net/qq_36752532/article/details/122074267

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值