line-chart.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. (function(){
  2. Vue.component('line-chart',{
  3. template: '<div class="mtb10 bgc-fff c-border pb10">\
  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="line-chart" id="lineChart" style="height: 200px; width: 100%;"></div>\
  19. </div>',
  20. props:[],
  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("getlinedata", {dateType: type});
  35. }
  36. },
  37. mounted: function(){
  38. var vm = this;
  39. EventBus.$on("draw-line-chart", function(arg){
  40. vm.panelName = arg.panelName;
  41. if(arg.unit){
  42. vm.unit = arg.unit;
  43. }
  44. drawLine(vm, arg);
  45. })
  46. }
  47. });
  48. function drawLine(vm, arg){
  49. var lineCharts = echarts.init(document.getElementById('lineChart'));
  50. var xData = arg.xData,
  51. yDatas = arg.yDatas,
  52. names = arg.quotaNames,
  53. colors = arg.colors;
  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. var options = {
  70. tooltip: {
  71. trigger: 'axis'
  72. },
  73. color: colors,
  74. legend: {
  75. top: '0px',
  76. data: names,
  77. borderColor: "#f1f1f1"
  78. },
  79. grid: {
  80. show: false,
  81. left: '20px',
  82. right: '20px',
  83. bottom: '40px',
  84. top: '20px',
  85. containLabel: true
  86. },
  87. xAxis: {
  88. type: 'category',
  89. boundaryGap: false,
  90. data: xData,
  91. axisLabel: {
  92. interval:0,//横轴信息全部显示
  93. formatter: function (value, index) {
  94. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  95. if(index == 0){
  96. vm.startDate = value;
  97. return value.substr(5,2)+"月"+value.substr(8,2);
  98. }else{
  99. if(index == 9){
  100. vm.endDate = value;
  101. }
  102. return value.substr(8,2);
  103. }
  104. }else if(vm.selectedDateType == 3){
  105. var val = value.substr(5,2)+"月"
  106. if(index == 0){
  107. vm.startDate = value.substr(0,4)+"年"+val;
  108. }else{
  109. if(index == 9){
  110. vm.endDate = value.substr(0,4)+"年"+val;
  111. }
  112. }
  113. return val;
  114. }
  115. }
  116. }
  117. },
  118. yAxis: {
  119. type: 'value',
  120. axisPointer: {
  121. snap: true
  122. },
  123. scale: true,
  124. minInterval: 1,
  125. boundaryGap: ['10%', '30%'],
  126. splitLine: {show:false}
  127. },
  128. dataZoom: [{//给x轴设置滚动条
  129. // show: false,
  130. start: dataZoom_end,
  131. end: 100,
  132. type: 'slider',
  133. zoomLock: true,
  134. },{ //下面这个属性是内容区域配置
  135. start: dataZoom_end,
  136. end: 100,
  137. type: 'inside',
  138. zoomLock: true,
  139. }]
  140. };
  141. var series = [];
  142. for(var i=0; i<yDatas.length; i++){
  143. var obj = {
  144. name: names[i],
  145. type: 'line',
  146. smooth: true,
  147. data: yDatas[i],
  148. lineStyle:{
  149. normal:{
  150. color: colors[i]
  151. }
  152. }
  153. };
  154. series.push(obj);
  155. }
  156. options.series = series;
  157. $("#lineChart").removeAttr('_echarts_instance_')
  158. lineCharts.setOption(options);
  159. }
  160. })()