bar-chart.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. (function(){
  2. Vue.component('bar-chart',{
  3. template: '<div class="bgc-fff c-border">\
  4. <div class="ui-grid ui-grid-middle plr10 c-border-b">\
  5. <div class="ui-col-0">\
  6. <span class="c-333 c-bold c-f14">{{panelName}}</span>\
  7. </div>\
  8. <div class="ui-col-1 c-t-right ptb5">\
  9. <span class="date-tag" :class="{active: selectedDateType == 1}" data-type="1" @click="changeType(1)">日</span><!--\
  10. --><span class="date-tag" :class="{active: selectedDateType == 2}" data-type="2" @click="changeType(2)">周</span><!--\
  11. --><span class="date-tag" :class="{active: selectedDateType == 3}" data-type="3" @click="changeType(3)">月</span>\
  12. </div>\
  13. </div>\
  14. <div class="clearfix mt5 plr10">\
  15. <div class="fl c-f12 c-909090"><span>{{startDate}}</span> ~ <span>{{endDate}}</span>数据</div>\
  16. <div class="fr c-f12 c-909090">单位:{{unit}}</div>\
  17. </div>\
  18. <div class="bar-chart" :id="barid" style="height: 200px; width: 100%;"></div>\
  19. </div>',
  20. props:['barid'],
  21. data: function(){
  22. return {
  23. selectedDateType: 1,
  24. startDate: "", //数据展示时显示的开始时间
  25. endDate: "", //数据展示时显示的结束时间
  26. panelName: "",
  27. unit: "人"
  28. }
  29. },
  30. methods: {
  31. changeType: function(type){
  32. this.selectedDateType = type;
  33. //触发页面更新折线图的数据,触发器是父类页面自定在组件上的
  34. this.$emit("getbardata", {dateType: type});
  35. }
  36. },
  37. mounted: function(){
  38. var vm = this;
  39. EventBus.$on("draw-bar-chart", function(arg){
  40. vm.panelName = arg.panelName;
  41. if(arg.unit){
  42. vm.unit = arg.unit;
  43. }
  44. drawBar(vm, arg);
  45. })
  46. }
  47. });
  48. function drawBar(vm, arg){
  49. var myChart = echarts.init(document.getElementById(vm.barid));
  50. var xData = arg.xData,
  51. yData = arg.yData,
  52. name = arg.quotaName,
  53. color = arg.color;
  54. //处理数据, 数据按照10条数一屏展示
  55. var lastIndex = xData.length % 10;
  56. if(xData.length >10 ){
  57. dataZoom_end = 100-(9/xData.length)*100;
  58. }else{
  59. dataZoom_end = 0;
  60. }
  61. //初始结束时间
  62. var lastValue = xData[xData.length - 1];
  63. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  64. vm.endDate = lastValue;
  65. }else if(vm.selectedDateType == 3){
  66. var val = lastValue.substr(5,2)+"月"
  67. vm.endDate = lastValue.substr(0,4)+"年"+val;
  68. }
  69. // 指定图表的配置项和数据
  70. var option = {
  71. tooltip: {
  72. trigger: 'item'
  73. },
  74. toolbox: {
  75. dataZoom: true,
  76. show: true,
  77. orient: 'vertical',
  78. x: 'right',
  79. y: 'center'
  80. },
  81. grid: {
  82. // show: false,
  83. left: '20px',
  84. right: '20px',
  85. bottom: '40px',
  86. top: '20px',
  87. containLabel: true
  88. },
  89. xAxis: [{
  90. type: 'category',
  91. data: xData,
  92. axisLabel: {
  93. interval:0,//横轴信息全部显示
  94. formatter: function (value, index) {
  95. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  96. if(index == 0){
  97. vm.startDate = value;
  98. return value.substr(5,2)+"月"+value.substr(8,2)+"日";
  99. }else{
  100. if(index == 9){
  101. vm.endDate = value;
  102. }
  103. return value.substr(8,2)+"日";
  104. }
  105. }else if(vm.selectedDateType == 3){
  106. var val = value.substr(5,2)+"月"
  107. if(index == 0){
  108. vm.startDate = value.substr(0,4)+"年"+val;
  109. }else{
  110. if(index == 9){
  111. vm.endDate = value.substr(0,4)+"年"+val;
  112. }
  113. }
  114. return val;
  115. }
  116. return value;
  117. }
  118. },
  119. splitLine: {
  120. show: false
  121. }
  122. }],
  123. yAxis: [{
  124. type: 'value',
  125. splitLine: {show:false}
  126. }],
  127. dataZoom: [{//给x轴设置滚动条
  128. // show: false,
  129. start: dataZoom_end,
  130. end: 100,
  131. type: 'slider',
  132. zoomLock: true,
  133. },{ //下面这个属性是内容区域配置
  134. start: dataZoom_end,
  135. end: 100,
  136. type: 'inside',
  137. zoomLock: true,
  138. }],
  139. series: [{
  140. clickable: true,
  141. name: name,
  142. itemStyle : {
  143. normal: {
  144. label : {
  145. show: true, position: 'top'
  146. },
  147. color: color
  148. }
  149. },
  150. barWidth: 20,
  151. type: 'bar',
  152. data: yData
  153. }]
  154. };
  155. // 使用刚指定的配置项和数据显示图表。
  156. myChart.setOption(option);
  157. }
  158. })()