BarChart.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <div :class="className" :style="{ height: height, width: width }" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts';
  6. require('echarts/theme/macarons'); // echarts theme
  7. import resize from './mixins/resize';
  8. const animationDuration = 6000;
  9. export default {
  10. mixins: [resize],
  11. props: {
  12. className: {
  13. type: String,
  14. default: 'chart'
  15. },
  16. width: {
  17. type: String,
  18. default: '100%'
  19. },
  20. type: {
  21. type: String,
  22. default: 'bar'
  23. },
  24. legend: {},
  25. chartData: {
  26. type: Array,
  27. required: true
  28. },
  29. height: {
  30. type: String,
  31. default: '215px'
  32. }
  33. },
  34. data() {
  35. return {
  36. chart: null
  37. };
  38. },
  39. watch: {
  40. chartData: {
  41. deep: true,
  42. handler(val) {
  43. this.setOptions(val);
  44. }
  45. },
  46. type: {
  47. deep: true,
  48. handler(val) {
  49. this.setOptions(this.chartData);
  50. }
  51. }
  52. },
  53. mounted() {
  54. this.$nextTick(() => {
  55. this.initChart();
  56. });
  57. },
  58. beforeDestroy() {
  59. if (!this.chart) {
  60. return;
  61. }
  62. this.chart.dispose();
  63. this.chart = null;
  64. },
  65. methods: {
  66. initChart() {
  67. setTimeout(() => {
  68. this.chart = echarts.init(this.$el, 'macarons');
  69. this.setOptions(this.chartData);
  70. }, 300);
  71. },
  72. setOptions(chartData) {
  73. let xAxisData = [];
  74. let seriesData = [];
  75. if (chartData) {
  76. chartData.forEach((item, index) => {
  77. xAxisData.push(item.name+'月');
  78. seriesData.push(item.value);
  79. });
  80. }
  81. this.chart.setOption({
  82. color: ['rgba(37, 97, 239, 1)'],
  83. tooltip: {
  84. trigger: 'axis',
  85. axisPointer: {
  86. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
  87. },
  88. formatter: '{b} : {c}'
  89. },
  90. legend: {
  91. data: [this.legend]
  92. },
  93. grid: {
  94. top: '13%',
  95. left: '0%',
  96. right: '0%',
  97. bottom: '2%',
  98. containLabel: true,
  99. show: false
  100. },
  101. xAxis: [
  102. {
  103. type: 'category',
  104. data: xAxisData,
  105. axisTick: {
  106. show: false
  107. },
  108. axisLine: {
  109. lineStyle: {
  110. color: '#e6e6e6'
  111. }
  112. },
  113. axisLabel: {
  114. show: true,
  115. textStyle: {
  116. color: '#545555',
  117. fontSize: '14'
  118. }
  119. }
  120. }
  121. ],
  122. yAxis: [
  123. {
  124. type: 'value',
  125. nameTextStyle: {
  126. color: '#545555'
  127. },
  128. axisTick: {
  129. show: false
  130. },
  131. splitArea: {
  132. show: false
  133. },
  134. splitLine: {
  135. lineStyle: {
  136. color: '#e6e6e6',
  137. width: 0.7
  138. }
  139. },
  140. axisLine: {
  141. lineStyle: {
  142. color: '#e6e6e6'
  143. }
  144. },
  145. axisLabel: {
  146. textStyle: {
  147. color: '#545555'
  148. }
  149. }
  150. }
  151. ],
  152. series: [
  153. {
  154. /* name: this.legend, */
  155. type: this.type,
  156. barWidth: '35%',
  157. smooth: false,
  158. data: seriesData,
  159. animationDuration,
  160. itemStyle: {
  161. normal: {
  162. color: '#2561ef',
  163. lineStyle: {
  164. color: '#2561ef',
  165. width: 2
  166. },
  167. areaStyle: {
  168. color: '#e9effd'
  169. },
  170. label: {
  171. show: true,
  172. position: 'top',
  173. textStyle: {
  174. color: '#545555',
  175. fontSize: 13
  176. },
  177. formatter: '{c}元'
  178. }
  179. }
  180. }
  181. }
  182. ]
  183. });
  184. }
  185. }
  186. };
  187. </script>