ComSplitBarChart.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <div class="chart" ref="chart"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. import { fontSize } from '../../util'
  7. export default {
  8. name: 'ComSplitBarChart', //合同结算柱状图
  9. props: {
  10. chartData: {}
  11. },
  12. data() {
  13. return { myChart: null }
  14. },
  15. watch: {
  16. chartData: {
  17. handler(n, o) {
  18. this.initialChart()
  19. }
  20. }
  21. },
  22. mounted() {
  23. //图表大小自适应
  24. window.addEventListener('resize', this.onResize)
  25. },
  26. methods: {
  27. destroyChart() {
  28. if (this.myChart != null) {
  29. this.myChart.dispose()
  30. this.myChart = null
  31. }
  32. },
  33. onResize() {
  34. this.initialChart()
  35. },
  36. getBarColor(index) {
  37. const colors = [
  38. { index: 0, color0: '#CB2222', color1: '#FF4747' },
  39. { index: 1, color0: '#D7742C', color1: '#F6A75B' },
  40. { index: 2, color0: '#BEA23F', color1: '#E1D35B' }
  41. ]
  42. return colors.find((i) => i.index == index) || { index: 3, color0: '#0A89FF', color1: '#05CFE1' }
  43. },
  44. initialChart() {
  45. this.destroyChart()
  46. let list = this.chartData
  47. let barData = []
  48. list.forEach((item, index) => {
  49. barData.push({
  50. ...item,
  51. itemStyle: {
  52. color: {
  53. type: 'linear',
  54. x: 0,
  55. y: 0,
  56. x2: 1,
  57. y2: 0,
  58. colorStops: [
  59. {
  60. offset: 0,
  61. color: this.getBarColor(index).color0 // 0% 处的颜色
  62. },
  63. {
  64. offset: 1,
  65. color: this.getBarColor(index).color1 // 100% 处的颜色
  66. }
  67. ]
  68. }
  69. }
  70. })
  71. })
  72. let yName = list.map((item) => item.name)
  73. let xData = list.map((item) => item.value)
  74. let barWidth = 10
  75. let option = {
  76. backgroundColor: 'transparent',
  77. xAxis: {
  78. splitLine: {
  79. show: false
  80. },
  81. axisLabel: {
  82. show: false
  83. },
  84. axisTick: {
  85. show: false
  86. },
  87. axisLine: {
  88. show: false
  89. }
  90. },
  91. grid: {
  92. containLabel: true,
  93. left: 0,
  94. top: '5%',
  95. right: '35%',
  96. bottom: 0
  97. },
  98. yAxis: [
  99. {
  100. inverse: true,
  101. axisLine: {
  102. show: false
  103. },
  104. axisTick: {
  105. show: false
  106. },
  107. axisLabel: {
  108. inside: true,
  109. verticalAlign: 'bottom',
  110. lineHeight: fontSize(37),
  111. margin: 0,
  112. //
  113. fontSize: fontSize(14),
  114. fontFamily: 'Source Han Sans CN',
  115. fontWeight: 500,
  116. color: '#FFFFFF'
  117. },
  118. data: yName
  119. }
  120. ],
  121. series: [
  122. {
  123. //内
  124. type: 'bar',
  125. barWidth,
  126. legendHoverLink: false,
  127. symbolRepeat: true,
  128. silent: true,
  129. data: barData,
  130. z: 1,
  131. animationEasing: 'elasticOut'
  132. },
  133. {
  134. // 背景
  135. type: 'pictorialBar',
  136. animationDuration: 0,
  137. symbolRepeat: 'fixed',
  138. symbolMargin: '20%',
  139. symbol: 'roundRect',
  140. symbolSize: [6, barWidth],
  141. itemStyle: {
  142. color: 'transparent'
  143. },
  144. label: {
  145. show: true,
  146. position: 'right',
  147. offset: [0, 2],
  148. distance: 30,
  149. textStyle: {
  150. fontSize: 14,
  151. fontFamily: 'Source Han Sans CN',
  152. fontWeight: 500,
  153. color: '#FEFFFF'
  154. },
  155. formatter: function (a, b) {
  156. return `${a.value}万元`
  157. }
  158. },
  159. data: xData,
  160. z: 0,
  161. animationEasing: 'elasticOut'
  162. },
  163. {
  164. //分隔
  165. type: 'pictorialBar',
  166. itemStyle: {
  167. color: '#000'
  168. },
  169. symbolRepeat: 'fixed',
  170. symbolMargin: 4,
  171. symbol: 'roundRect',
  172. symbolClip: true,
  173. symbolSize: [2, barWidth],
  174. symbolPosition: 'start',
  175. symbolOffset: [0, 0],
  176. data: list,
  177. z: 2,
  178. animationEasing: 'elasticOut'
  179. }
  180. ]
  181. }
  182. this.creatChart(option, this.$refs.chart)
  183. },
  184. creatChart(option, ref) {
  185. this.myChart = echarts.init(ref) //this.$refs.chart
  186. this.myChart.resize()
  187. this.myChart.setOption(option, {
  188. notMerge: true
  189. })
  190. }
  191. },
  192. beforeDestroy() {
  193. window.removeEventListener('resize', this.onResize)
  194. this.destroyChart()
  195. }
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. .chart {
  200. width: 100%;
  201. height: 100%;
  202. }
  203. </style>