line-chart-single.js 9.5 KB

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