com-chart-1.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!-- 统计图1 -->
  2. <template>
  3. <div class="echarts-div" id="bar-chart" ref="bar-chart" style="height: 220px;"></div>
  4. </template>
  5. <script>
  6. module.exports = {
  7. props: {
  8. chartData: {
  9. type: Array,
  10. required: true
  11. },
  12. type: {
  13. type: String,
  14. default: 'bar'
  15. }
  16. },
  17. data() {
  18. return {};
  19. },
  20. watch: {
  21. chartData: {
  22. deep: true,
  23. handler(val) {
  24. this.setOptions(val);
  25. }
  26. },
  27. type: {
  28. deep: true,
  29. handler(val) {
  30. this.setOptions(this.chartData);
  31. }
  32. }
  33. },
  34. created() {
  35. // 刷新所有图标数据
  36. this.$nextTick(() => {
  37. this.setOptions(this.chartData);
  38. });
  39. },
  40. methods: {
  41. // 刷新柱状图
  42. setOptions: function (chartData) {
  43. let xAxisData = [];
  44. let seriesData = [];
  45. if (chartData) {
  46. chartData.forEach((item, index) => {
  47. xAxisData.push(item.name + '月份');
  48. seriesData.push(item.value);
  49. });
  50. }
  51. //开始渲染
  52. var ele = this.$refs['bar-chart'];
  53. var myChart = echarts.init(ele);
  54. var option = {
  55. color: ['rgba(37, 97, 239, 1)'],
  56. tooltip: {
  57. trigger: 'axis',
  58. formatter: '{b} 交易额:{c}',
  59. axisPointer: {
  60. type: 'shadow'
  61. }
  62. },
  63. grid: { x: 50, y: 30, x2: 25, y2: 25 }, //设置canvas内部表格的内距
  64. xAxis: [
  65. {
  66. type: 'category',
  67. data: xAxisData,
  68. axisTick: {
  69. show: false
  70. },
  71. axisLine: {
  72. lineStyle: {
  73. color: '#e6e6e6'
  74. }
  75. },
  76. axisLabel: {
  77. show: true,
  78. textStyle: {
  79. color: '#545555',
  80. fontSize: '14'
  81. }
  82. }
  83. }
  84. ],
  85. yAxis: [
  86. {
  87. type: 'value',
  88. minInterval: 1,
  89. axisTick: {
  90. show: false
  91. },
  92. splitArea: {
  93. show: false
  94. },
  95. splitLine: {
  96. lineStyle: {
  97. color: '#e6e6e6',
  98. width: 0.7
  99. }
  100. },
  101. axisLine: {
  102. lineStyle: {
  103. color: '#e6e6e6'
  104. }
  105. },
  106. axisLabel: {
  107. textStyle: {
  108. color: '#545555'
  109. }
  110. }
  111. }
  112. ],
  113. series: [
  114. {
  115. type: this.type,
  116. barWidth: '40%',
  117. data: seriesData,
  118. smooth: true,
  119. itemStyle: {
  120. normal: {
  121. opacity: 1,
  122. barBorderRadius: [3, 3, 0, 0],
  123. areaStyle: {
  124. color: '#8fc3f8'
  125. },
  126. label: {
  127. show: true,
  128. position: 'top',
  129. textStyle: {
  130. color: '#545555',
  131. fontSize: 14
  132. }
  133. }
  134. }
  135. }
  136. }
  137. ]
  138. };
  139. myChart.setOption(option);
  140. window.myChartList.push(myChart);
  141. }
  142. }
  143. };
  144. </script>
  145. <style scoped></style>