line-chart2.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. (function(){
  2. var startDate,
  3. endDate,
  4. chooseYear;
  5. Vue.component('line-chart',{
  6. template: '<div class="mtb10 bgc-fff c-border pb10" style="width:100%;height:400px;border-radius:6px;">\
  7. <div class="ui-grid ui-grid-middle plr15 p20">\
  8. <div class="ui-col-1">\
  9. <span class="c-333 c-f16">{{panelName}}</span>\
  10. </div>\
  11. <div v-if="!showDatePanel" class="ui-col-1 c-f16 c-333 c-t-right"><span>{{startDate}}</span> ~ <span>{{endDate}}</span></div>\
  12. </div>\
  13. <div class="ui-grid ui-grid-middle mt5 plr10">\
  14. <div v-if="showDatePanel" class="ui-col-1" >\
  15. <span>数据时间:</span>\
  16. <el-date-picker\
  17. v-model="dateValue"\
  18. type="daterange"\
  19. align="right"\
  20. unlink-panels\
  21. range-separator="至"\
  22. start-placeholder="开始日期"\
  23. end-placeholder="结束日期"\
  24. :default-value="defaultValue"\
  25. :picker-options="pickerOptions2"\
  26. @change="dateChange"\
  27. >\
  28. </el-date-picker>\
  29. </div>\
  30. </div>\
  31. <div class="line-chart" :id="lineid" style="height: 300px; width: 100%;"></div>\
  32. </div>',
  33. props:['lineid', 'showDatePanel', "chooseYear"],
  34. data: function(){
  35. return {
  36. selectedDateType: 3,
  37. startDate: "", //数据展示时显示的开始时间
  38. endDate: "", //数据展示时显示的结束时间
  39. panelName: "",
  40. unit: "人",
  41. dateValue: "",
  42. defaultValue: [],
  43. pickerOptions2: {
  44. shortcuts: [{
  45. text: '最近一周',
  46. onClick: function(picker) {
  47. var end = new Date();
  48. var start = new Date();
  49. start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
  50. picker.$emit('pick', [start, end]);
  51. }
  52. }, {
  53. text: '最近一个月',
  54. onClick: function(picker) {
  55. var end = new Date();
  56. var start = new Date();
  57. start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
  58. picker.$emit('pick', [start, end]);
  59. }
  60. }, {
  61. text: '最近三个月',
  62. onClick: function(picker) {
  63. var end = new Date();
  64. var start = new Date();
  65. start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
  66. picker.$emit('pick', [start, end]);
  67. }
  68. }],
  69. disabledDate: function(d){
  70. var start = new Date(getStartDate(chooseYear)+" 00:00:00"),
  71. end = new Date(getEndDate(chooseYear)+" 23:59:59");
  72. return d < start || d > end;
  73. }
  74. }
  75. }
  76. },
  77. methods: {
  78. changeType: function(type){
  79. this.selectedDateType = type;
  80. //触发页面更新折线图的数据,触发器是父类页面自定在组件上的
  81. this.$emit("getlinedata", {dateType: type});
  82. },
  83. dateChange: function(val){
  84. var start = val[0].format("yyyy-MM-dd"),
  85. end = val[1].format("yyyy-MM-dd");
  86. this.$emit("getlinedata", {dateType: this.selectedDateType, dateValue: [start, end]});
  87. }
  88. },
  89. mounted: function(){
  90. var vm = this;
  91. EventBus.$on("draw-line-chart", function(arg){
  92. vm.panelName = arg.panelName;
  93. if(arg.selectedDateType){
  94. vm.selectedDateType = arg.selectedDateType;
  95. }
  96. if(arg.unit){
  97. vm.unit = arg.unit;
  98. }
  99. vm.dateValue = arg.dateValue;
  100. vm.chooseYear = arg.chooseYear;
  101. chooseYear = arg.chooseYear;
  102. drawLine(vm, arg);
  103. })
  104. EventBus.$on("change-date-value", function(arg){
  105. this.defaultValue = arg;
  106. })
  107. // vm.changeType(3)
  108. }
  109. });
  110. function drawLine(vm, arg){
  111. var lineCharts = echarts.init(document.getElementById(vm.lineid));
  112. var xData = arg.xData,
  113. yDatas = arg.yDatas,
  114. names = arg.quotaNames,
  115. colors = arg.colors,
  116. markLineValue = arg.markLineValue,
  117. markLineName = arg.markLineName;
  118. //处理数据, 数据按照20条数一屏展示
  119. // var lastIndex = xData.length % 20;
  120. if(xData.length > 20 ){
  121. dataZoom_end = 100-(19/xData.length)*100;
  122. }else{
  123. dataZoom_end = 0;
  124. }
  125. //初始结束时间
  126. vm.endDate = arg.endDate;
  127. vm.startDate=arg.startDate
  128. // var lastValue = xData[xData.length - 1];
  129. // if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  130. // vm.endDate = lastValue;
  131. // }else if(vm.selectedDateType == 3){
  132. // var val = lastValue.substr(5,2)+"月"
  133. // vm.endDate = arg.endDate;
  134. // vm.startDate=arg.startDate
  135. // }
  136. var options = {
  137. tooltip: {
  138. trigger: 'axis'
  139. },
  140. color: colors,
  141. grid: {
  142. show: false,
  143. left: '20px',
  144. right: '20px',
  145. bottom: '40px',
  146. top: '20px',
  147. containLabel: true
  148. },
  149. xAxis: {
  150. type: 'category',
  151. boundaryGap: false,
  152. data: xData,
  153. axisLabel: {
  154. interval:0,//横轴信息全部显示
  155. formatter: function (value, index) {
  156. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  157. // if(index == 0){
  158. // vm.startDate = value;
  159. // return value.substr(5,2)+"月"+value.substr(8,2);
  160. // }else{
  161. // if(index == 19){
  162. // vm.endDate = value;
  163. // }
  164. // return value.substr(8,2);
  165. // }
  166. }else if(vm.selectedDateType == 3){
  167. var val = value.substr(5,2)+"月"
  168. if(index == 0){
  169. // vm.startDate = value;
  170. }else{
  171. if(index == 19){
  172. // vm.endDate = value;
  173. }
  174. }
  175. return val;
  176. }
  177. }
  178. }
  179. },
  180. yAxis: {
  181. type: 'value',
  182. axisPointer: {
  183. snap: true
  184. },
  185. scale: true,
  186. minInterval: 1,
  187. boundaryGap: ['10%', '30%'],
  188. splitLine: {show:false}
  189. },
  190. dataZoom: [{//给x轴设置滚动条
  191. // show: false,
  192. start: dataZoom_end,
  193. end: 100,
  194. type: 'slider',
  195. zoomLock: false,
  196. },{ //下面这个属性是内容区域配置
  197. start: dataZoom_end,
  198. end: 100,
  199. type: 'inside',
  200. zoomLock: false,
  201. }]
  202. };
  203. var series = [];
  204. for(var i=0; i<yDatas.length; i++){
  205. var obj = {
  206. name: names[i],
  207. type: 'line',
  208. smooth: true,
  209. data: yDatas[i],
  210. lineStyle:{
  211. normal:{
  212. color: colors[i]
  213. }
  214. }
  215. };
  216. series.push(obj);
  217. }
  218. options.series = series;
  219. if(markLineValue){
  220. //目前统计只有签约统计页面有目标量
  221. $.extend(options.yAxis, {
  222. min: function(value) {
  223. if(value.min > markLineValue){
  224. return markLineValue / 2;
  225. }else{
  226. return value.min / 2;
  227. }
  228. },
  229. max: function(value){
  230. if(value.max > markLineValue){
  231. return parseInt(value.max * 1.5);
  232. }else{
  233. return parseInt(markLineValue * 1.2);
  234. }
  235. }
  236. });
  237. $.extend(options.series[0], {markLine: {
  238. data:[
  239. {yAxis: markLineValue, name: markLineName}
  240. ],
  241. label:{
  242. show: true,
  243. position: 'middle',
  244. formatter: '{b}: {c}'
  245. },
  246. lineStyle:{
  247. color: 'rgb(194,53,49)'
  248. }
  249. }});
  250. }
  251. // console.log(JSON.stringify(options));
  252. $("#"+vm.lineid).removeAttr('_echarts_instance_')
  253. lineCharts.setOption(options);
  254. window.lineCharts = lineCharts;
  255. }
  256. })()