line-chart.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.selectedDateType){
  42. vm.selectedDateType = arg.selectedDateType;
  43. }
  44. if(arg.unit){
  45. vm.unit = arg.unit;
  46. }
  47. drawLine(vm, arg);
  48. })
  49. }
  50. });
  51. function drawLine(vm, arg){
  52. var lineCharts = echarts.init(document.getElementById('lineChart'));
  53. var xData = arg.xData,
  54. yDatas = arg.yDatas,
  55. names = arg.quotaNames,
  56. colors = arg.colors,
  57. markLineValue = arg.markLineValue,
  58. markLineName = arg.markLineName;
  59. //处理数据, 数据按照10条数一屏展示
  60. var lastIndex = xData.length % 10;
  61. if(xData.length >10 ){
  62. dataZoom_end = 100-(9/xData.length)*100;
  63. }else{
  64. dataZoom_end = 0;
  65. }
  66. //初始结束时间
  67. var lastValue = xData[xData.length - 1];
  68. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  69. vm.endDate = lastValue;
  70. }else if(vm.selectedDateType == 3){
  71. var val = lastValue.substr(5,2)+"月"
  72. vm.endDate = lastValue.substr(0,4)+"年"+val;
  73. }
  74. var options = {
  75. tooltip: {
  76. trigger: 'axis'
  77. },
  78. color: colors,
  79. legend: {
  80. top: '0px',
  81. data: names,
  82. borderColor: "#f1f1f1"
  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. boundaryGap: false,
  95. data: xData,
  96. axisLabel: {
  97. interval:0,//横轴信息全部显示
  98. formatter: function (value, index) {
  99. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  100. if(index == 0){
  101. vm.startDate = value;
  102. return value.substr(5,2)+"月"+value.substr(8,2);
  103. }else{
  104. if(index == 9){
  105. vm.endDate = value;
  106. }
  107. return value.substr(8,2);
  108. }
  109. }else if(vm.selectedDateType == 3){
  110. var val = value.substr(5,2)+"月"
  111. if(index == 0){
  112. vm.startDate = value.substr(0,4)+"年"+val;
  113. }else{
  114. if(index == 9){
  115. vm.endDate = value.substr(0,4)+"年"+val;
  116. }
  117. }
  118. return val;
  119. }
  120. }
  121. }
  122. },
  123. yAxis: {
  124. type: 'value',
  125. axisPointer: {
  126. snap: true
  127. },
  128. scale: true,
  129. minInterval: 1,
  130. boundaryGap: ['10%', '30%'],
  131. splitLine: {show:false}
  132. },
  133. dataZoom: [{//给x轴设置滚动条
  134. // show: false,
  135. start: dataZoom_end,
  136. end: 100,
  137. type: 'slider',
  138. zoomLock: true,
  139. },{ //下面这个属性是内容区域配置
  140. start: dataZoom_end,
  141. end: 100,
  142. type: 'inside',
  143. zoomLock: true,
  144. }]
  145. };
  146. var series = [];
  147. for(var i=0; i<yDatas.length; i++){
  148. var obj = {
  149. name: names[i],
  150. type: 'line',
  151. smooth: true,
  152. data: yDatas[i],
  153. lineStyle:{
  154. normal:{
  155. color: colors[i]
  156. }
  157. }
  158. };
  159. series.push(obj);
  160. }
  161. options.series = series;
  162. if(markLineValue){
  163. //目前统计只有签约统计页面有目标量
  164. $.extend(options.yAxis, {
  165. min: function(value) {
  166. if(value.min > markLineValue){
  167. return markLineValue / 2;
  168. }else{
  169. return value.min / 2;
  170. }
  171. },
  172. max: function(value){
  173. if(value.max > markLineValue){
  174. return parseInt(value.max * 1.5);
  175. }else{
  176. return parseInt(markLineValue * 1.2);
  177. }
  178. }
  179. });
  180. $.extend(options.series[0], {markLine: {
  181. data:[
  182. {yAxis: markLineValue, name: markLineName}
  183. ],
  184. label:{
  185. show: true,
  186. position: 'middle',
  187. formatter: '{b}: {c}'
  188. },
  189. lineStyle:{
  190. color: 'rgb(194,53,49)'
  191. }
  192. }});
  193. }
  194. // console.log(JSON.stringify(options));
  195. $("#lineChart").removeAttr('_echarts_instance_')
  196. lineCharts.setOption(options);
  197. window.lineCharts = lineCharts;
  198. }
  199. })()