当前位置:   article > 正文

Vue 中循环渲染多个相同 echarts 图表,并传入不同的数据_vue里如何给相同的echart图标传进去不同的数据显示在不同元素内

vue里如何给相同的echart图标传进去不同的数据显示在不同元素内

在开发过程中我们常常需要,在一个页面中使用相同的图表来展示同级别的多个事物(如:同级别的多个不同id的仓库、同级别的多个不同id的设备等)。

例如:

  1. <template>
  2. <div class="projectCost">
  3. <div class='overviewModel'>
  4. <div class='one' v-for="(item,index) in levelNameList" :key="index">
  5. <div class="roseChart" :id='forId(index)'></div>
  6. <p class='title'>{{item.levelName}}</p>
  7. <div class='list'>
  8. <echarts-list
  9. v-for="(el, i) in item.boilers"
  10. :key="i"
  11. :color="colorList[i]"
  12. :data="el"></echarts-list>
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. import EchartsList from './EchartList'
  20. import { echartsFont } from "@/utils/util";
  21. export default {
  22. props:{
  23. levelNameList:{ //父组件传递过来的数据
  24. type: Array,
  25. default: () => {return []}
  26. }
  27. },
  28. components: {EchartsList},
  29. data(){
  30. return {
  31. colorList: [
  32. "#0a9aea",
  33. "#FFC100",
  34. "#e50ec2",
  35. "#4fdd14",
  36. "#0780ed",
  37. ],
  38. getId: [],
  39. }
  40. },
  41. mounted(){
  42. //每一个echarts都做到自适应
  43. window.addEventListener("resize", () => {
  44. this.getId && this.getId.forEach((item,index) => {
  45. this.getId[index].resize()
  46. });
  47. });
  48. },
  49. methods:{
  50. forId(index) {
  51. return 'roseChart' + index
  52. },
  53. drawRose(){
  54. this.$nextTick(function(){
  55. for(var i = 0;i < this.levelNameList.length;i++ ){
  56. this.getId.push(echarts.init(document.getElementById('roseChart'+i)));
  57. this.getId[i].setOption({
  58. title: {
  59. //这个地方是计算总数的
  60. text: "{a|" + this.levelNameList[i].boilers.reduce((pre, cur) => {return pre + cur.value*1},0) + "}\n{c|" + "总数" + "}",
  61. x: "center",
  62. y: "center",
  63. textStyle: {
  64. rich: {
  65. a: {
  66. fontSize: echartsFont(0.25),
  67. color: "#333",
  68. fontWeight: "600",
  69. lineHeight: 40,
  70. },
  71. c: {
  72. fontSize: echartsFont(0.14),
  73. color: "#666",
  74. padding: [5, 0],
  75. },
  76. },
  77. },
  78. },
  79. tooltip: {
  80. trigger: 'item',
  81. show: false,
  82. },
  83. legend: {
  84. top: '5%',
  85. left: 'center'
  86. },
  87. color: this.colorList,
  88. series: [
  89. {
  90. name: '最外层细圆环',
  91. type: 'pie',
  92. radius: ['70%', '97%'],
  93. center: ["50%", "50%"],
  94. avoidLabelOverlap: false,
  95. silent: true,
  96. hoverAnimation: false,
  97. label: {
  98. show: false,
  99. },
  100. labelLine: {
  101. show: false
  102. },
  103. //展示要渲染的数据,渲染不同的内容
  104. data: this.levelNameList[i].boilers.map(item => item.value)
  105. }
  106. ]
  107. })
  108. }
  109. })
  110. }
  111. },
  112. watch: {
  113. levelNameList:{
  114. immediate: true,
  115. deep: true,
  116. handler(value) {
  117. if(!value) return
  118. this.getId = []
  119. console.log('监听levelNameList的变化',value);
  120. this.drawRose()
  121. }
  122. }
  123. }
  124. }
  125. </script>
  126. <style lang="less" scoped>
  127. .projectCost{
  128. width: 100%;
  129. height: 100%;
  130. .overviewModel{
  131. width: 100%;
  132. overflow-y: auto;
  133. height: 100%;
  134. padding-right: 13px;
  135. .one{
  136. position: relative;
  137. width: 100%;
  138. border-bottom: 2px solid #e9e9e9;
  139. .roseChart{
  140. width: 35%;
  141. margin-top: 1.25vw;
  142. height: 14vw !important;
  143. }
  144. }
  145. .one:last-child{
  146. border: none;
  147. }
  148. .title{
  149. position: absolute;
  150. font-size: 0.926vw;
  151. font-weight: 500;
  152. top: 0;
  153. left: 0;
  154. color: #333333;
  155. width: 100%;
  156. height: 30px;
  157. z-index: 1;
  158. }
  159. .list{
  160. z-index: 2;
  161. position: absolute;
  162. top: 0.925vw;
  163. width: calc(65% - 30px);
  164. right: 20px;
  165. box-sizing: border-box;
  166. }
  167. }
  168. }
  169. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/107798
推荐阅读