ThreeStackedBarAndLine.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div id="ThreeStackedBarAndLine" ref="ThreeStackedBarAndLine"></div>
  3. </template>
  4. <script>
  5. //这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  6. export default {
  7. components: {},
  8. data() {
  9. return {};
  10. },
  11. //监听属性 类似于data概念
  12. computed: {},
  13. //监控data中的数据变化
  14. watch: {},
  15. //方法集合
  16. methods: {
  17. init_echart(obj) {
  18. const _this = this;
  19. _this.chart = echarts.init(this.$refs.ThreeStackedBarAndLine);
  20. let y1Data = [110, 90, 120, 70, 60]
  21. let option = {
  22. backgroundColor: 'rgba(0, 0, 0, 0)',
  23. // tooltip: {
  24. // trigger: 'axis',
  25. // extraCssText: 'box-shadow: 0 0 8px rgba(0, 0, 0, 0.3);',
  26. // backgroundColor: '#3A4667', // 背景
  27. // borderColor: '#3A4667',
  28. // textStyle: {
  29. // color: '#fff'
  30. // },
  31. // },
  32. tooltip: {
  33. backgroundColor: 'RGBA(20, 106, 178, 0.4)',
  34. trigger: "axis",
  35. textStyle: {
  36. fontSize: 14,
  37. color: '#fff'
  38. }
  39. },
  40. grid: {
  41. top: '30%',
  42. left: '6%',
  43. right: '6%',
  44. bottom: '5%',
  45. containLabel: true,
  46. },
  47. color: ['#186bb8', '#4ee1ac', '#efc943'],
  48. legend: {
  49. data: obj.legend_data,
  50. top: '1%',
  51. // textStyle: {
  52. // color: "#666666"
  53. // },
  54. textStyle: {
  55. fontSize: 12,
  56. color: '#fff',
  57. padding: [0, 0, 0, 0],
  58. rich: {
  59. a: {
  60. verticalAlign: 'middle',
  61. },
  62. },
  63. },
  64. itemWidth: 15,
  65. itemHeight: 10,
  66. itemGap: 10
  67. },
  68. xAxis: [{
  69. type: 'category',
  70. data: obj.x_data,
  71. axisLabel: {
  72. show: true,
  73. fontSize: 12,
  74. color: '#fff',
  75. align: 'center',
  76. verticalAlign: 'top',
  77. },
  78. axisLine: {
  79. show: true,
  80. lineStyle: {
  81. width: 1,
  82. color: 'rgba(239, 247, 253, .7)'
  83. }
  84. },
  85. axisTick: {
  86. show: false,
  87. },
  88. }
  89. ],
  90. yAxis: [{
  91. name: obj.yAxis?obj.yAxis[0].name:'项目个数/个',
  92. nameTextStyle: {
  93. color: "#fff",
  94. fontSize: 12,
  95. padding: [0, 0, 4, 0], //name文字位置 对应 上右下左
  96. },
  97. minInterval:obj.yAxis?undefined: 1, // 设置y轴的最小间隔为1
  98. axisLabel: {
  99. interval: 0,
  100. show: true,
  101. fontSize: 10,
  102. color: '#fff',
  103. },
  104. axisLine: {
  105. show: false,
  106. },
  107. axisTick: {
  108. show: false,
  109. },
  110. splitLine: {
  111. show: false,
  112. lineStyle: {
  113. color: 'rgba(239, 247, 253, .1)'
  114. }
  115. },
  116. }, {
  117. name: obj.yAxis?obj.yAxis[1].name:'面积/公顷',
  118. nameTextStyle: {
  119. color: "#fff",
  120. fontSize: 12,
  121. padding: [0, 0, 4, 0], //name文字位置 对应 上右下左
  122. },
  123. axisLabel: {
  124. interval: 0,
  125. show: true,
  126. fontSize: 10,
  127. color: '#fff',
  128. },
  129. axisLine: {
  130. show: false,
  131. },
  132. axisTick: {
  133. show: false,
  134. },
  135. splitLine: {
  136. show: false,
  137. lineStyle: {
  138. color: 'rgba(239, 247, 253, .1)'
  139. }
  140. },
  141. }],
  142. series: [],
  143. }
  144. if(!obj.result)
  145. obj.result = [[],[],[],[] ];
  146. if(!obj.legend_data)
  147. obj.legend_data = ['','','','' ];
  148. if(!obj.legendmap)
  149. obj.legendmap = [
  150. { type: "line", data: obj.line_data },
  151. { type: "bar", data: obj.result[0] },
  152. { type: "bar", data: obj.result[1] },
  153. { type: "bar", data: obj.result[2] },
  154. ];
  155. obj.legendmap.forEach( (mapele,i) => {
  156. if (mapele.type=='line') {
  157. option.series.push({
  158. name: obj.legend_data[i],
  159. type: 'line',
  160. smooth: true,
  161. itemStyle: {
  162. normal: {
  163. color: mapele.color||'#FFCC64' // 折线的颜色
  164. }
  165. },
  166. yAxisIndex: 1, // 指定使用第二个Y轴
  167. data: mapele.data||obj.result[i],
  168. });
  169. }else{
  170. option.series.push({
  171. name: obj.legend_data[i],
  172. type: 'bar',
  173. stack: '渠道'+i,//stack相同上下显示
  174. barWidth: 14,
  175. data: mapele.data||obj.result[i],
  176. });
  177. }
  178. });
  179. this.chart.setOption(option);
  180. // this.chart.on("legendselectchanged", function (params) {
  181. // let iseyes = params.selected[params.name];
  182. // _this.$emit("echartClick", params.name, { iseyes });
  183. // });
  184. this.chart.on("click", function (params) {
  185. _this.$emit("echartClick", params.name, { color: params.color,sIndex:params.seriesIndex });
  186. });
  187. },
  188. setOptions(obj) {
  189. this.init_echart(obj);
  190. }
  191. },
  192. beforeCreate() { }, //生命周期 - 创建之前
  193. created() { }, //生命周期 - 创建完成(可以访问当前this实例)
  194. beforeMount() { }, //生命周期 - 挂载之前
  195. mounted() {
  196. const that = this;
  197. this.$nextTick((res) => {
  198. // let obj = {
  199. // legend_data: ['临时用地面积', '正常使用', '剩余1个月', '已到期'],
  200. // x_data: ['崖州区', '天涯区', '吉阳区', '海棠区'],
  201. // line_data: [110, 90, 120, 70, 60],
  202. // result: [[12, 12, 12, 12, 1, 2], [24, 12, 12, 0, 1, 2], [12, 0, 0, 24, 21]]
  203. // }
  204. // this.setOptions(obj);
  205. })
  206. }, //生命周期 - 挂在完成
  207. beforeUpdate() { }, //生命周期 - 更新之前
  208. updated() { }, //生命周期 - 更新之后
  209. beforeDestroy() { }, //生命周期 - 销毁之前
  210. destroy() { },//生命周期 - 销毁完成
  211. activated() { }, //若组件实例是 <KeepAlive> 缓存树的一部分,当组件被插入到 DOM 中时调用。
  212. deactivated() { } //若组件实例是 <KeepAlive> 缓存树的一部分,当组件从 DOM 中被移除时调用。
  213. };
  214. </script>
  215. <style scoped>
  216. #ThreeStackedBarAndLine {
  217. left: 0rem;
  218. top: 0.1rem;
  219. width: 23vw;
  220. height: 12rem;
  221. }
  222. </style>