classBar.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div :id="id" style="height: 100%;width:100%;"></div>
  3. </template>
  4. <script>
  5. export default {
  6. data () {
  7. return {
  8. myChart: '',
  9. }
  10. },
  11. props:{
  12. id:{
  13. type: String
  14. },
  15. todayData:{
  16. type: Object,
  17. default: function(){
  18. return {'一年級': 9, '二年級': 11, '三年級': 16, '四年級':6, '五年級':13, '六年級':22}
  19. }
  20. },
  21. lastWeekData:{
  22. type: Object,
  23. default: function(){
  24. return {'一年級': 5, '二年級': 7, '三年級': 12, '四年級':1, '五年級':2, '六年級':24}
  25. }
  26. },
  27. callBack: {
  28. type: String
  29. }
  30. },
  31. mounted(){
  32. this.drawLine();
  33. },
  34. methods:{
  35. drawLine(){
  36. let _this = this
  37. // 取得Y軸Label
  38. let yLabel = Object.keys(this.todayData);
  39. let todayDataArray = [];
  40. let lastWeekDataArray = [];
  41. // 今日資料
  42. yLabel.map(function(key) {
  43. todayDataArray.push(_this.todayData[key]);
  44. });
  45. // 上週資料
  46. yLabel.map(function(key) {
  47. lastWeekDataArray.push(_this.lastWeekData[key]);
  48. });
  49. // 基于准备好的dom,初始化echarts实例
  50. this.myChart = this.$echarts.init(document.getElementById(this.id));
  51. this.myChart.setOption({
  52. backgroundColor: '#343a4073',
  53. tooltip: {
  54. trigger: 'axis',
  55. axisPointer: {
  56. type: 'shadow'
  57. },
  58. // formatter: function(params){
  59. // // 故意開啟 但不設定使ToolTip 沒有彈跳的作用 但有Hover 功能
  60. // }
  61. },
  62. grid: {
  63. left: '5%',
  64. right: '0',
  65. bottom: '0',
  66. top:'0',
  67. containLabel: true
  68. },
  69. xAxis: {
  70. type: 'value',
  71. boundaryGap: false,
  72. axisLabel: {
  73. inside: true,
  74. textStyle: {
  75. color:'transparent'
  76. }
  77. },
  78. splitLine: {
  79. show: true,
  80. lineStyle: {
  81. color: 'rgba(185, 193, 173, 0.63)'
  82. }
  83. }
  84. },
  85. yAxis: {
  86. type: 'category',
  87. axisLabel: {
  88. textStyle: {
  89. fontSize: 12,
  90. color: '#94998a'
  91. }
  92. },
  93. splitLine: {
  94. lineStyle: {
  95. color: 'rgba(185, 193, 173, 0.63)',
  96. },
  97. show: true
  98. },
  99. axisLine: {
  100. lineStyle: {
  101. color: 'rgba(185, 193, 173, 0.63)',
  102. width: 1,
  103. }
  104. },
  105. data: yLabel,
  106. },
  107. series: [
  108. {
  109. name: 'today',
  110. type: 'bar',
  111. data: todayDataArray,
  112. barWidth: 15, //柱子宽度
  113. itemStyle: {
  114. normal: { //渐变色
  115. color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  116. offset: 0,
  117. color: "rgba(28, 208, 161, 1)" // 0% 处的颜色
  118. },{
  119. offset: 1,
  120. color: "rgba(28, 208, 161, 0.5)" // 100% 处的颜色
  121. }], false)
  122. }
  123. }
  124. },
  125. {
  126. name: 'lastWeek',
  127. type: 'bar',
  128. data: lastWeekDataArray,
  129. barWidth: 15, //柱子宽度
  130. itemStyle: {
  131. normal: { //渐变色
  132. color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  133. offset: 0,
  134. color: "rgba(228, 233, 220, 1)" // 0% 处的颜色
  135. },{
  136. offset: 1,
  137. color: "rgba(228, 233, 220, 0.5)" // 100% 处的颜色
  138. }], false)
  139. }
  140. }
  141. }
  142. ]
  143. })
  144. //highlight觸發項
  145. this.myChart.on('highlight', function (params) {
  146. _this.$emit('highLightInfo', {'id': yLabel[params.batch[0].dataIndex], 'today': todayDataArray[params.batch[0].dataIndex], 'lastWeek': lastWeekDataArray[params.batch[1].dataIndex]});
  147. });
  148. this.myChart.on('downplay', function (params) {
  149. _this.$emit('downplay', yLabel[params.batch[0].dataIndex])
  150. });
  151. }
  152. }
  153. }
  154. </script>
  155. <style>
  156. </style>