当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/260030.html

相关文章:

  • imazing 2.17.16中文版怎么备份iPhone手机照片
  • 05 python数据容器
  • 相机倾斜棋盘格标定全记录 vs200+opencv安装
  • QT- QT-lximagerEidtor图片编辑器
  • PyQt 如何通过连续点击托盘图标显示隐藏主窗口并且在主窗口隐藏时调整界面到托盘图标附近
  • 什么是纯净IP?如何判断IP地址的纯净度?有哪些干净IP推荐?
  • MySQL和Minio数据备份
  • 在Go中过滤范型集合:性能回顾
  • MATLAB 最小二乘直线拟合方法二 (36)
  • Python 实现:OCR在图片中提取文字(基于Gradio实现)
  • idea插件开发报错: ZipException opening “slf4j.jar“: zip END header not found
  • 【Linux】多线程编程
  • 【Mysql】InnoDB的表空间(九)
  • 【09】ES6:Set 和 Map 数据结构
  • Java通过documents4j和libreoffice把word转为pdf
  • 物联网时代的访问控制研究综述
  • 【产品经理】需求池和版本树
  • Qt图像处理-OpenCv中Mat与QImage互转
  • 构建外卖小程序:技术代码实践
  • IDEA中显示方法、类注释信息
  • 《数据结构、算法与应用C++语言描述》- 堆排序 - 借助priority_queue的C++实现
  • 10.CSS浮动
  • Angular 2 学习笔记
  • xcode 修改 target 中设备朝向崩溃
  • ZLMediaKit 编译以及测试(Centos 7.9 环境)
  • 汽车清除积碳和清洗节气门
  • RocketMQ 总体概括
  • 使用qemu在arm上模拟x86并运行docker
  • IIS配置多域名跨域
  • el-form表单校验输入框值为0时 提示校验不通过