line-chart.js 10 KB

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