BarChart.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. height: {
  21. type: String,
  22. default: '300px'
  23. },
  24. chartData: {
  25. type: Object,
  26. required: true
  27. }
  28. },
  29. watch: {
  30. chartData: {
  31. deep: true,
  32. handler(val) {
  33. this.setOptions(val)
  34. }
  35. }
  36. },
  37. data() {
  38. return {
  39. chart: null
  40. }
  41. },
  42. mounted() {
  43. this.$nextTick(() => {
  44. this.initChart()
  45. })
  46. },
  47. beforeDestroy() {
  48. if (!this.chart) {
  49. return
  50. }
  51. this.chart.dispose()
  52. this.chart = null
  53. },
  54. methods: {
  55. initChart() {
  56. this.chart = echarts.init(this.$el, 'macarons');
  57. this.chart = echarts.init(this.$el, 'macarons')
  58. this.setOptions(this.chartData)
  59. },
  60. setOptions({ y, x ,data } = {}) {
  61. // console.log(y,x,data,56)
  62. this.chart.setOption({
  63. tooltip: {
  64. trigger: 'axis',
  65. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  66. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
  67. }
  68. },
  69. // 缩放问题
  70. dataZoom: [
  71. {
  72. bottom:0,
  73. type: "slider",
  74. show: true,
  75. start: 0,
  76. end: 70,
  77. handleSize: 30,
  78. zoomOnMouseWheel: true,
  79. },
  80. {
  81. type: "inside",
  82. start: 0,
  83. end: 10,
  84. }
  85. ],
  86. grid: {
  87. top: 30,
  88. left: '2%',
  89. right: '2%',
  90. bottom: 20,
  91. containLabel: true
  92. },
  93. xAxis: [{
  94. type: 'category',
  95. data: x,
  96. splitLine:{
  97. show:true
  98. },
  99. axisLine: {
  100. lineStyle: {
  101. color: '#9BA2B0', //x轴的颜色
  102. width:'1'
  103. },
  104. },
  105. axisLabel: {
  106. interval: 0, // 设置数据间隔
  107. formatter:function(value){
  108. var str = "";
  109. var num = 8; //每行显示字数
  110. var valLength = value.length; //该项x轴字数
  111. var rowNum = Math.ceil(valLength / num); // 行数
  112. if(rowNum > 1)
  113. {
  114. for(var i = 0; i < rowNum; i++)
  115. {
  116. var temp = "";
  117. var start = i * num;
  118. var end = start + num;
  119. temp = value.substring(start, end) + "\n";
  120. str += temp;
  121. }
  122. return str;
  123. }
  124. else
  125. {
  126. return value;
  127. }
  128. },
  129. },
  130. }],
  131. yAxis: [{
  132. type: 'value',
  133. axisTick: {
  134. show: false
  135. },
  136. axisLine: {
  137. lineStyle: {
  138. type: 'solid',
  139. color: '#9BA2B0',
  140. width:'1'
  141. }
  142. }
  143. }],
  144. series: [
  145. {
  146. type: "bar",
  147. data: y,
  148. barWidth: '15',
  149. itemStyle: {
  150. //这里设置柱形图圆角 [左上角,右上角,右下角,左下角]
  151. barBorderRadius:[10, 10, 10, 10],
  152. color: {
  153. x: 0, //右
  154. y: 1, //下
  155. x2: 0, //左
  156. y2: 0, //上
  157. colorStops: [{
  158. offset: 0, // 颜色的开始位置
  159. color: 'rgba(2, 197, 29, 1)' // 0% 处的颜色
  160. },
  161. {
  162. offset: 1, // 颜色的结束位置
  163. color: 'rgba(29, 233, 182, 1)' // 100% 处的颜色
  164. }
  165. ]
  166. },
  167. },}
  168. ]
  169. })
  170. }
  171. }
  172. }
  173. </script>