欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 带Label的韦恩图(vue)

带Label的韦恩图(vue)

2025/4/8 5:18:14 来源:https://blog.csdn.net/Pure_White520/article/details/147041835  浏览:    关键词:带Label的韦恩图(vue)

韦恩图

效果:
在这里插入图片描述

实现:
图形使用venn.jsd3.js绘制
label使用元素定位绘制(可根据数据动态计算)

代码:

  1. tempalte
  <div class="venn mgt-4 center-flex" v-else ref="vennBox" style="height: 140px;"><div class="venn-right" :style="{ left: lengedPos.rightX + 'px' }" ref="vennRight"><p class="color-666 fs-12 ellipsis lh-17">XXXX</p><p class="lh-20">720人</p></div><div class="venn-left" :style="{ right: lengedPos.leftX + 'px' }" ref="vennRight"><p class="color-666 fs-12 ellipsis lh-17">XXX</p><p class="lh-20">720人</p></div><div class="venn-bottom flex-align" ref="vennBottom"><div class="venn-text"><p class="color-666 fs-12 lh-17">XXXX</p><p class="lh-20">720人</p></div><img src="@/assets/img/dataApplication/bottom.png" /></div><div ref="vennContainer" class="venn-container" style="width: 325px;height: 120px;"></div></div>
  1. script
import * as d3 from 'd3';
import { VennDiagram } from 'venn.js';data() {return {intersectionNum: 20,lengedPos: {rightX: 0,leftX: 0},};},mounted() {this.$nextTick(() => {setTimeout(() => {this.renderVennDiagram()}, 100);})},methods: {renderVennDiagram() {const container = this.$refs.vennContainer;// 数据const sets = [{ sets: ['A'], size: 100, label: 'A' },{ sets: ['B'], size: 100, label: 'B' },{ sets: ['A', 'B'], size: this.intersectionNum, label: 'A ∩ B' }];// 绘制韦恩图const chart = VennDiagram().styled(false).width(325).height(120);const svg = d3.select(container).append('svg').attr('width', 325).attr('height', 120);svg.datum(sets).call(chart);this.$nextTick(() => {const gElement = document.querySelector('g.venn-area.venn-circle[data-venn-sets="B"]');const gRect = gElement.getBoundingClientRect();const parentRect = this.$refs.vennBox.getBoundingClientRect();this.lengedPos.rightX = gRect.left - parentRect.left + gRect.width + 2;const gAElement = document.querySelector('g.venn-area.venn-circle[data-venn-sets="A"]');const gARect = gAElement.getBoundingClientRect();this.lengedPos.leftX = parentRect.right - gARect.left + 0})},
  1. style

::v-deep .venn-container {>svg {transform: scaleY(0.95);}g {position: relative;}svg {text {fill: transparent;display: none;}}[data-venn-sets="A"] {fill: #1083FB;}[data-venn-sets="B"] {fill: #6CD3FF;}[data-venn-sets="A_B"] {fill: #FFC300;path {stroke: white;stroke-width: 2px;}}}.venn {position: relative;&-left {position: absolute;top: 15px;&::after {content: '';position: absolute;right: -17.23px;background: url(~@/assets/img/dataApplication/left.png) center/100% 100% no-repeat;top: 50%;transform: translateY(-50%);width: 17.23px;height: 4.8px;}}&-right {position: absolute;top: 15px;&::before {content: '';position: absolute;left: -21.06px;background: url(~@/assets/img/dataApplication/right.png) center/100% 100% no-repeat;top: 50%;transform: translateY(-50%);width: 21.06px;height: 4.8px;}}&-bottom {position: absolute;left: 50%;transform: translateX(-100%) translateY(100%);bottom: 37px;.venn-text {position: absolute;left: 0;top: -3px;}img {width: 84px;height: 20px;}&::after {width: 84px;height: 20px;display: inline-block;background: url(~@/assets/img/dataApplication/bottom.png) center/100% 100% no-repeat;content: '';position: absolute;right: 0;}.venn-text {position: absolute;left: 0;transform: translateX(-100%);}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com