lineBarChartChb.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div class="lineBarCharts" ref="lineBarCharts" />
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. name: '',
  8. props: {
  9. title: {
  10. type: String,
  11. default: ''
  12. },
  13. xAxisData: {
  14. type: Array,
  15. default: []
  16. },
  17. seriesData: {
  18. type: Object,
  19. default: {}
  20. }
  21. },
  22. data () {
  23. return {
  24. uEcharts: null,
  25. uOption: null,
  26. }
  27. },
  28. watch: {
  29. seriesData: {
  30. handler(newValue, oldValue){
  31. console.log(newValue, oldValue)
  32. if(newValue){
  33. this.setSeriesData(newValue)
  34. }
  35. },
  36. deep: true,
  37. // immediate: true
  38. }
  39. },
  40. created () {
  41. },
  42. mounted () {
  43. this.$nextTick(() => {
  44. this.initChart()
  45. this.setSeriesData()
  46. })
  47. },
  48. methods: {
  49. initChart(){
  50. this.uEcharts = echarts.init(this.$refs.lineBarCharts)
  51. let color = ['#42CEAF', '#FF882A', '#16B4FE', '#747AFE','#f9c129', '#13c2c2', '#79ce42', '#f55837'];
  52. // fc8452
  53. this.uOption = {
  54. // title: {
  55. // text: this.title + '图',
  56. // left: 'center'
  57. // },
  58. color: color,
  59. tooltip: {
  60. trigger: 'axis',
  61. formatter: function (params) {
  62. console.log(params)
  63. let str = params[0].name + "<br />";
  64. params.forEach((item) => {
  65. str +=
  66. '<span style="display:inline-block;margin-right:5px;border-radius:50%;width:10px;height:10px;left:5px;background-color:'+item.color+'"></span>' + item.seriesName + " : " + (Number(item.value) / 10000).toFixed(2) +'万元' + "<br />";
  67. });
  68. return str;
  69. },
  70. },
  71. grid: {
  72. left: '7%',
  73. right: '5%',
  74. bottom: '10%',
  75. },
  76. legend: {
  77. type: 'scroll',
  78. width: '60%',
  79. // orient: 'vertical',
  80. // right: 90,
  81. // left: 20,
  82. top: 0,
  83. // bottom: 20,
  84. // selectedMode: 'single',
  85. padding: [
  86. 5, // 上
  87. 150, // 右
  88. 0, // 下
  89. 20, // 左
  90. ],
  91. },
  92. toolbox: {
  93. show: true,
  94. feature: {
  95. // dataView: { readOnly: false }, // 展示数据--不过没用
  96. magicType: { type: ['bar', 'line'] },
  97. // restore: {}, // 还原
  98. saveAsImage: {}
  99. }
  100. },
  101. // dataZoom: [
  102. // {
  103. // startValue: '2023-02-01'
  104. // },
  105. // {
  106. // type: 'inside'
  107. // }
  108. // ],
  109. xAxis: {
  110. type: 'category',
  111. // boundaryGap: true, // 解决数据溢出X轴的问题
  112. data: this.xAxisData
  113. },
  114. yAxis: {
  115. type: 'value',
  116. axisLabel: {
  117. // 可以动态设置是数量还是金额
  118. // formatter: '{value} 万元'
  119. // 设置动态单位转换
  120. formatter: function (value, index) {
  121. // if (value >= 100000000) {
  122. // return value / 100000000 + "亿";
  123. // } else if (value >= 10000000) {
  124. // return value / 10000000 + "千万";
  125. // } else if (value >= 1000000) {
  126. // return value / 1000000 + "百万";
  127. // } else if (value >= 100000) {
  128. // return value / 100000 + "十万";
  129. // } else if (value >= 10000) {
  130. // return value / 10000 + "万";
  131. // } else if (value >= 1000) {
  132. // return value / 1000 + "千";
  133. // } else {
  134. // return value;
  135. // }
  136. // if(value > 0){
  137. // return (Number(value) / 10000).toFixed(1) + "万"
  138. // } else {
  139. // return value;
  140. // }
  141. return (Number(value) / 10000).toFixed(1) + "万"
  142. }
  143. }
  144. },
  145. series: [
  146. // {
  147. // name: 'Highest',
  148. // type: 'line',
  149. // data: [10, 11, 13, 11, 12, 12, 9],
  150. // markPoint: {
  151. // data: [
  152. // { type: 'max', name: 'Max' },
  153. // { type: 'min', name: 'Min' }
  154. // ]
  155. // },
  156. // // markLine: {
  157. // // data: [{ type: 'average', name: 'Avg' }]
  158. // // }
  159. // },
  160. ]
  161. };
  162. this.uOption && this.uEcharts.setOption(this.uOption);
  163. window.addEventListener('resize', ()=>{
  164. this.uEcharts.resize()
  165. })
  166. },
  167. setSeriesData(data){
  168. // console.log(this.uEcharts)
  169. var seriesData = data ? data : this.seriesData
  170. console.log(seriesData)
  171. // var option = this.uOption
  172. this.uOption.xAxis.data = this.xAxisData
  173. this.uOption.series = seriesData.seriesData.map(res => {
  174. return {
  175. name: res.name,
  176. type: 'bar',
  177. smooth: true,
  178. label: {
  179. show: true,
  180. formatter: function (params) {
  181. if(params.value === 0){
  182. return ''
  183. } else {
  184. return (Number(params.value) / 10000).toFixed(2);
  185. }
  186. },
  187. },
  188. data: res.data,
  189. }
  190. })
  191. // this.uOption.series.unshift({
  192. // name: '总数',
  193. // type: 'line',
  194. // smooth: true,
  195. // // label: {
  196. // // normal: {
  197. // // offset:['50', '80'],
  198. // // show: true,
  199. // // position: 'insideBottom',
  200. // // formatter:'{c}',
  201. // // textStyle:{ color:'#000' }
  202. // // }
  203. // // },
  204. // // itemStyle:{
  205. // // normal:{
  206. // // color:'rgba(128, 128, 128, 0)'
  207. // // }
  208. // // },
  209. // data: seriesData.sumZcbData,
  210. // })
  211. // console.log(this.uOption.series)
  212. this.uEcharts.hideLoading()
  213. this.uEcharts.setOption(this.uOption, true)
  214. }
  215. }
  216. }
  217. </script>
  218. <style lang="less" scoped>
  219. .lineBarCharts{
  220. width: 100%;
  221. height: 100%;
  222. }
  223. </style>