line-chart.js 10 KB

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