line-chart.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. markLineValue = arg.markLineValue,
  55. markLineName = arg.markLineName;
  56. //处理数据, 数据按照10条数一屏展示
  57. var lastIndex = xData.length % 10;
  58. if(xData.length >10 ){
  59. dataZoom_end = 100-(9/xData.length)*100;
  60. }else{
  61. dataZoom_end = 0;
  62. }
  63. //初始结束时间
  64. var lastValue = xData[xData.length - 1];
  65. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  66. vm.endDate = lastValue;
  67. }else if(vm.selectedDateType == 3){
  68. var val = lastValue.substr(5,2)+"月"
  69. vm.endDate = lastValue.substr(0,4)+"年"+val;
  70. }
  71. var options = {
  72. tooltip: {
  73. trigger: 'axis'
  74. },
  75. color: colors,
  76. legend: {
  77. top: '0px',
  78. data: names,
  79. borderColor: "#f1f1f1"
  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. boundaryGap: false,
  92. data: xData,
  93. axisLabel: {
  94. interval:0,//横轴信息全部显示
  95. formatter: function (value, index) {
  96. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  97. if(index == 0){
  98. vm.startDate = value;
  99. return value.substr(5,2)+"月"+value.substr(8,2);
  100. }else{
  101. if(index == 9){
  102. vm.endDate = value;
  103. }
  104. return value.substr(8,2);
  105. }
  106. }else if(vm.selectedDateType == 3){
  107. var val = value.substr(5,2)+"月"
  108. if(index == 0){
  109. vm.startDate = value.substr(0,4)+"年"+val;
  110. }else{
  111. if(index == 9){
  112. vm.endDate = value.substr(0,4)+"年"+val;
  113. }
  114. }
  115. return val;
  116. }
  117. }
  118. }
  119. },
  120. yAxis: {
  121. type: 'value',
  122. axisPointer: {
  123. snap: true
  124. },
  125. scale: true,
  126. minInterval: 1,
  127. boundaryGap: ['10%', '30%'],
  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. };
  143. var series = [];
  144. for(var i=0; i<yDatas.length; i++){
  145. var obj = {
  146. name: names[i],
  147. type: 'line',
  148. smooth: true,
  149. data: yDatas[i],
  150. lineStyle:{
  151. normal:{
  152. color: colors[i]
  153. }
  154. }
  155. };
  156. series.push(obj);
  157. }
  158. options.series = series;
  159. if(markLineValue){
  160. //目前统计只有签约统计页面有目标量
  161. $.extend(options.yAxis, {
  162. min: function(value) {
  163. if(value.min > markLineValue){
  164. return markLineValue / 2;
  165. }else{
  166. return value.min / 2;
  167. }
  168. },
  169. max: function(value){
  170. if(value.max > markLineValue){
  171. return parseInt(value.max * 1.5);
  172. }else{
  173. return parseInt(markLineValue * 1.2);
  174. }
  175. }
  176. });
  177. $.extend(options.series[0], {markLine: {
  178. data:[
  179. {yAxis: markLineValue, name: markLineName}
  180. ],
  181. label:{
  182. show: true,
  183. position: 'middle',
  184. formatter: '{b}: {c}'
  185. },
  186. lineStyle:{
  187. color: 'rgb(194,53,49)'
  188. }
  189. }});
  190. }
  191. // console.log(JSON.stringify(options));
  192. $("#lineChart").removeAttr('_echarts_instance_')
  193. lineCharts.setOption(options);
  194. }
  195. })()