line-chart-single.js 9.6 KB

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