bar-chart.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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}" @click="changeType(1)">日</span><!--\
  10. --><span class="date-tag" :class="{active: selectedDateType == 2}" @click="changeType(2)">周</span><!--\
  11. --><span class="date-tag" :class="{active: selectedDateType == 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.selectedDateType){
  42. vm.selectedDateType = arg.selectedDateType;
  43. }
  44. if(arg.unit){
  45. vm.unit = arg.unit;
  46. }
  47. drawBar(vm, arg);
  48. })
  49. }
  50. });
  51. function drawBar(vm, arg){
  52. var myChart = echarts.init(document.getElementById(vm.barid));
  53. var xData = arg.xData,
  54. yData = arg.yData,
  55. name = arg.quotaName,
  56. color = arg.color;
  57. //处理数据, 数据按照10条数一屏展示
  58. var lastIndex = xData.length % 10;
  59. if(xData.length >10 ){
  60. dataZoom_end = 100-(9/xData.length)*100;
  61. }else{
  62. dataZoom_end = 0;
  63. }
  64. //初始结束时间
  65. var lastValue = xData[xData.length - 1];
  66. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  67. vm.endDate = lastValue;
  68. }else if(vm.selectedDateType == 3){
  69. var val = lastValue.substr(5,2)+"月"
  70. vm.endDate = lastValue.substr(0,4)+"年"+val;
  71. }
  72. // 指定图表的配置项和数据
  73. var option = {
  74. tooltip: {
  75. trigger: 'item'
  76. },
  77. toolbox: {
  78. dataZoom: true,
  79. show: true,
  80. orient: 'vertical',
  81. x: 'right',
  82. y: 'center'
  83. },
  84. grid: {
  85. // show: false,
  86. left: '20px',
  87. right: '20px',
  88. bottom: '40px',
  89. top: '20px',
  90. containLabel: true
  91. },
  92. xAxis: [{
  93. type: 'category',
  94. data: xData,
  95. axisLabel: {
  96. interval:0,//横轴信息全部显示
  97. formatter: function (value, index) {
  98. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  99. if(index == 0){
  100. vm.startDate = value;
  101. return value.substr(5,2)+"月"+value.substr(8,2)+"日";
  102. }else{
  103. if(index == 9){
  104. vm.endDate = value;
  105. }
  106. return value.substr(8,2)+"日";
  107. }
  108. }else if(vm.selectedDateType == 3){
  109. var val = value.substr(5,2)+"月"
  110. if(index == 0){
  111. vm.startDate = value.substr(0,4)+"年"+val;
  112. }else{
  113. if(index == 9){
  114. vm.endDate = value.substr(0,4)+"年"+val;
  115. }
  116. }
  117. return val;
  118. }
  119. return value;
  120. }
  121. },
  122. splitLine: {
  123. show: false
  124. }
  125. }],
  126. yAxis: [{
  127. type: 'value',
  128. splitLine: {show:false}
  129. }],
  130. dataZoom: [{//给x轴设置滚动条
  131. // show: false,
  132. start: dataZoom_end,
  133. end: 100,
  134. type: 'slider',
  135. zoomLock: true,
  136. },{ //下面这个属性是内容区域配置
  137. start: dataZoom_end,
  138. end: 100,
  139. type: 'inside',
  140. zoomLock: true,
  141. }],
  142. series: [{
  143. clickable: true,
  144. name: name,
  145. itemStyle : {
  146. normal: {
  147. label : {
  148. show: true, position: 'top'
  149. },
  150. color: color
  151. }
  152. },
  153. barWidth: 20,
  154. type: 'bar',
  155. data: yData
  156. }]
  157. };
  158. // 使用刚指定的配置项和数据显示图表。
  159. myChart.setOption(option);
  160. window.barCharts = myChart;
  161. }
  162. })()