line-chart2.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. var lastValue = xData[xData.length - 1];
  127. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  128. vm.endDate = lastValue;
  129. }else if(vm.selectedDateType == 3){
  130. var val = lastValue.substr(5,2)+"月"
  131. vm.endDate = arg.endDate;
  132. vm.startDate=arg.startDate
  133. }
  134. var options = {
  135. tooltip: {
  136. trigger: 'axis'
  137. },
  138. color: colors,
  139. grid: {
  140. show: false,
  141. left: '20px',
  142. right: '20px',
  143. bottom: '40px',
  144. top: '20px',
  145. containLabel: true
  146. },
  147. xAxis: {
  148. type: 'category',
  149. boundaryGap: false,
  150. data: xData,
  151. axisLabel: {
  152. interval:0,//横轴信息全部显示
  153. formatter: function (value, index) {
  154. if(vm.selectedDateType == 1 || vm.selectedDateType == 2){
  155. // if(index == 0){
  156. // vm.startDate = value;
  157. // return value.substr(5,2)+"月"+value.substr(8,2);
  158. // }else{
  159. // if(index == 19){
  160. // vm.endDate = value;
  161. // }
  162. // return value.substr(8,2);
  163. // }
  164. }else if(vm.selectedDateType == 3){
  165. var val = value.substr(5,2)+"月"
  166. if(index == 0){
  167. // vm.startDate = value;
  168. }else{
  169. if(index == 19){
  170. vm.endDate = value;
  171. }
  172. }
  173. return val;
  174. }
  175. }
  176. }
  177. },
  178. yAxis: {
  179. type: 'value',
  180. axisPointer: {
  181. snap: true
  182. },
  183. scale: true,
  184. minInterval: 1,
  185. boundaryGap: ['10%', '30%'],
  186. splitLine: {show:false}
  187. },
  188. dataZoom: [{//给x轴设置滚动条
  189. // show: false,
  190. start: dataZoom_end,
  191. end: 100,
  192. type: 'slider',
  193. zoomLock: false,
  194. },{ //下面这个属性是内容区域配置
  195. start: dataZoom_end,
  196. end: 100,
  197. type: 'inside',
  198. zoomLock: false,
  199. }]
  200. };
  201. var series = [];
  202. for(var i=0; i<yDatas.length; i++){
  203. var obj = {
  204. name: names[i],
  205. type: 'line',
  206. smooth: true,
  207. data: yDatas[i],
  208. lineStyle:{
  209. normal:{
  210. color: colors[i]
  211. }
  212. }
  213. };
  214. series.push(obj);
  215. }
  216. options.series = series;
  217. if(markLineValue){
  218. //目前统计只有签约统计页面有目标量
  219. $.extend(options.yAxis, {
  220. min: function(value) {
  221. if(value.min > markLineValue){
  222. return markLineValue / 2;
  223. }else{
  224. return value.min / 2;
  225. }
  226. },
  227. max: function(value){
  228. if(value.max > markLineValue){
  229. return parseInt(value.max * 1.5);
  230. }else{
  231. return parseInt(markLineValue * 1.2);
  232. }
  233. }
  234. });
  235. $.extend(options.series[0], {markLine: {
  236. data:[
  237. {yAxis: markLineValue, name: markLineName}
  238. ],
  239. label:{
  240. show: true,
  241. position: 'middle',
  242. formatter: '{b}: {c}'
  243. },
  244. lineStyle:{
  245. color: 'rgb(194,53,49)'
  246. }
  247. }});
  248. }
  249. // console.log(JSON.stringify(options));
  250. $("#"+vm.lineid).removeAttr('_echarts_instance_')
  251. lineCharts.setOption(options);
  252. window.lineCharts = lineCharts;
  253. }
  254. })()