Graph3D.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div id="BarGraph3D" ref="BarGraph3D"></div>
  3. </template>
  4. <script>
  5. //这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  6. export default {
  7. components: {},
  8. props: {
  9. legendData: {
  10. type: Array,
  11. },
  12. xdata: {
  13. type: Array,
  14. },
  15. result: {
  16. type: Array,
  17. }
  18. },
  19. watch: { // 监听到数据然后赋值
  20. legendData(oldval, newval) {
  21. this.ChangeData()
  22. },
  23. xdata(oldval, newval) {
  24. this.ChangeData()
  25. }, result(oldval, newval) {
  26. this.ChangeData()
  27. },
  28. deep: true
  29. },
  30. data() {
  31. return {
  32. chart: null,
  33. option: null,
  34. };
  35. },
  36. //监听属性 类似于data概念
  37. computed: {},
  38. //监控data中的数据变化
  39. watch: {},
  40. //方法集合
  41. methods: {
  42. init_3DBarGraph_echart(legendData, xdata, result) {
  43. const _this = this;
  44. _this.chart = echarts.init(this.$refs.BarGraph3D);
  45. // Mock数据
  46. const mockData = {
  47. // xdata: ['居民用地', '公共管理用地', '商业商服用地', '工矿用地', '工矿用地仓储用地', "交通运输用地"],
  48. xdata: xdata,
  49. result: result
  50. }
  51. const color = [
  52. [{ offset: 0, color: 'rgba(183, 117, 12, 0.8)' }, { offset: 0.5, color: 'rgba(183, 117, 12, 0.8)' }, { offset: 0.5, color: 'rgba(249, 180, 71, 0.8)' }, { offset: 1, color: 'rgba(249, 180, 71, 0.8)' }],
  53. [{ offset: 0, color: 'rgba(34, 129, 209, 0.8)' }, { offset: 0.5, color: 'rgba(34, 129, 209, 0.8)' }, { offset: 0.5, color: 'rgba(97, 173, 237, 0.8)' }, { offset: 1, color: 'rgba(97, 173, 237, 0.8)' }],
  54. ]
  55. const color2 = ['rgba(34, 129, 209, 0.8)', 'rgba(34, 129, 209, 0.8)',]
  56. const tooltip = {
  57. backgroundColor: 'RGBA(20, 106, 178, 0.4)',
  58. trigger: "axis",
  59. textStyle: {
  60. fontSize: 14,
  61. color: '#fff'
  62. },
  63. formatter: function (params) {
  64. var res = `${params[0].name} <br/>`
  65. for (const item of params) {
  66. console.log('item: ', item);
  67. if (item.value > 0) {
  68. return res += `</span> ${item.seriesName} :${item.value}<br/>`
  69. }
  70. }
  71. }
  72. }
  73. const legend = {
  74. data: legendData, //图例名称
  75. textStyle: { fontSize: 12, color: '#fff' },
  76. itemWidth: 24,
  77. itemHeight: 15,
  78. itemGap: 15,
  79. top: '5%',
  80. right: '2%',
  81. selectedMode: false
  82. }
  83. const grid = { top: '10%', left: '5%', right: '10%', bottom: '15%' }
  84. // xAxis
  85. const xAxis = {
  86. axisTick: { show: false },
  87. axisLine: { lineStyle: { color: '#BCD3E5' } },
  88. axisLabel: {
  89. textStyle: { fontSize: 12, color: '#BCD3E5' },
  90. },
  91. axisLabel: {
  92. interval: 0,
  93. formatter: function (value) {
  94. // 当文字长度大于2时,使用slice方法截取字符串并拼接省略号;否则直接返回原文字
  95. if (value.length > 4) {
  96. return `${value.slice(0, 4)}...`;
  97. } else {
  98. return value;
  99. }
  100. },
  101. },
  102. data: mockData.xdata,
  103. }
  104. // yAxis
  105. const yAxis = [{
  106. axisTick: { show: false },
  107. axisLine: { show: false },
  108. splitLine: { lineStyle: { color: 'rgba(255,255,255, .05)' } },
  109. axisLabel: { show: false, textStyle: { fontSize: 14, color: '#fff' } }
  110. }, {
  111. show: false,
  112. splitLine: { show: false },
  113. axisLine: { show: false },
  114. axisTick: { show: false },
  115. }]
  116. const diamondData = mockData.result.reduce((pre, cur, index) => {
  117. pre[index] = cur.data.map((el, id) => el + (pre[index - 1] ? pre[index - 1][id] : 0))
  118. return pre
  119. }, [])
  120. let series = mockData.result.reduce((p, c, i, array) => {
  121. p.push({
  122. z: i + 1,
  123. stack: '总量',
  124. type: 'bar',
  125. name: c.name,
  126. barGap: 18,
  127. barWidth: 18,
  128. data: c.data,
  129. label: {
  130. show: true,
  131. position: "top",
  132. fontSize: "10",
  133. color: "#fff",
  134. formatter: function (params) {
  135. if (params.value === 0) {
  136. return ''; // 或者 return null;
  137. }
  138. // 如果不为0,则按需要返回标签内容
  139. return params.value;
  140. }
  141. },
  142. itemStyle: { color: { type: 'linear', x: 0, x2: 1, y: 0, y2: 0, colorStops: color[i] } },
  143. }, {
  144. z: i + 10,
  145. type: 'pictorialBar',
  146. symbolPosition: 'end',
  147. symbol: 'diamond',
  148. symbolOffset: [0, '-50%'],
  149. symbolSize: [18, 12],
  150. data: diamondData[i],
  151. itemStyle: { color: color2[i] },
  152. tooltip: { show: false },
  153. })
  154. // 是否最后一个了?
  155. if (p.length === (array.length) * 2) {
  156. p.push({
  157. z: array.length * 2,
  158. type: "pictorialBar",
  159. symbolPosition: "start",
  160. data: mockData.result[0].data,
  161. symbol: "diamond",
  162. symbolOffset: ["0%", "50%"],
  163. symbolSize: [18, 10],
  164. itemStyle: { color: { type: 'linear', x: 0, x2: 1, y: 0, y2: 0, colorStops: color[0] } },
  165. tooltip: { show: false },
  166. })
  167. return p
  168. }
  169. return p
  170. }, [])
  171. const dataZoom = [{
  172. show: false,
  173. type: 'slider',
  174. startValue: 0,
  175. endValue: 7,
  176. moveOnMouseWheel: true,
  177. moveOnMouseMove: true,
  178. zoomOnMouseWheel: false,
  179. }]
  180. _this.option = {
  181. tooltip, legend, xAxis, yAxis, series, grid, dataZoom, backgroundColor: 'rgba(0, 0, 0, 0)'
  182. }
  183. this.chart.setOption(this.option);
  184. },
  185. setOptions(legendData, xdata, result) {
  186. this.init_3DBarGraph_echart(legendData, xdata, result);
  187. }
  188. },
  189. beforeCreate() { }, //生命周期 - 创建之前
  190. created() { }, //生命周期 - 创建完成(可以访问当前this实例)
  191. beforeMount() { }, //生命周期 - 挂载之前
  192. mounted() {
  193. const _this = this;
  194. // this.init_3DBarGraph_echart(_this.$props.legendFlag, _this.$props.xdata, _this.$props.result);
  195. }, //生命周期 - 挂在完成
  196. beforeUpdate() { }, //生命周期 - 更新之前
  197. updated() { }, //生命周期 - 更新之后
  198. beforeDestroy() { }, //生命周期 - 销毁之前
  199. destroy() { },//生命周期 - 销毁完成
  200. activated() { }, //若组件实例是 <KeepAlive> 缓存树的一部分,当组件被插入到 DOM 中时调用。
  201. deactivated() { } //若组件实例是 <KeepAlive> 缓存树的一部分,当组件从 DOM 中被移除时调用。
  202. };
  203. </script>
  204. <style scoped>
  205. #BarGraph3D {
  206. left: 0rem;
  207. top: 0.1rem;
  208. width: 27rem;
  209. height: 11rem;
  210. }
  211. </style>