(function(){ var startDate, endDate, chooseYear; Vue.component('line-chart',{ template: '
\
\
\ {{panelName}}\
\
{{startDate}} ~ {{endDate}}
\
\
\
\ 数据时间:\ \ \
\
\
\
', props:['lineid', 'showDatePanel', "chooseYear"], data: function(){ return { selectedDateType: 3, startDate: "", //数据展示时显示的开始时间 endDate: "", //数据展示时显示的结束时间 panelName: "", unit: "人", dateValue: "", defaultValue: [], pickerOptions2: { shortcuts: [{ text: '最近一周', onClick: function(picker) { var end = new Date(); var start = new Date(); start.setTime(start.getTime() - 3600 * 1000 * 24 * 7); picker.$emit('pick', [start, end]); } }, { text: '最近一个月', onClick: function(picker) { var end = new Date(); var start = new Date(); start.setTime(start.getTime() - 3600 * 1000 * 24 * 30); picker.$emit('pick', [start, end]); } }, { text: '最近三个月', onClick: function(picker) { var end = new Date(); var start = new Date(); start.setTime(start.getTime() - 3600 * 1000 * 24 * 90); picker.$emit('pick', [start, end]); } }], disabledDate: function(d){ var start = new Date(getStartDate(chooseYear)+" 00:00:00"), end = new Date(getEndDate(chooseYear)+" 23:59:59"); return d < start || d > end; } } } }, methods: { changeType: function(type){ this.selectedDateType = type; //触发页面更新折线图的数据,触发器是父类页面自定在组件上的 this.$emit("getlinedata", {dateType: type}); }, dateChange: function(val){ var start = val[0].format("yyyy-MM-dd"), end = val[1].format("yyyy-MM-dd"); this.$emit("getlinedata", {dateType: this.selectedDateType, dateValue: [start, end]}); } }, mounted: function(){ var vm = this; EventBus.$on("draw-line-chart", function(arg){ vm.panelName = arg.panelName; if(arg.selectedDateType){ vm.selectedDateType = arg.selectedDateType; } if(arg.unit){ vm.unit = arg.unit; } vm.dateValue = arg.dateValue; vm.chooseYear = arg.chooseYear; chooseYear = arg.chooseYear; drawLine(vm, arg); }) EventBus.$on("change-date-value", function(arg){ this.defaultValue = arg; }) // vm.changeType(3) } }); function drawLine(vm, arg){ var lineCharts = echarts.init(document.getElementById(vm.lineid)); var xData = arg.xData, yDatas = arg.yDatas, names = arg.quotaNames, colors = arg.colors, markLineValue = arg.markLineValue, markLineName = arg.markLineName; //处理数据, 数据按照20条数一屏展示 // var lastIndex = xData.length % 20; if(xData.length > 20 ){ dataZoom_end = 100-(19/xData.length)*100; }else{ dataZoom_end = 0; } //初始结束时间 var lastValue = xData[xData.length - 1]; if(vm.selectedDateType == 1 || vm.selectedDateType == 2){ vm.endDate = lastValue; }else if(vm.selectedDateType == 3){ var val = lastValue.substr(5,2)+"月" vm.endDate = arg.endDate; vm.startDate=arg.startDate } var options = { tooltip: { trigger: 'axis' }, color: colors, grid: { show: false, left: '20px', right: '20px', bottom: '40px', top: '20px', containLabel: true }, xAxis: { type: 'category', boundaryGap: false, data: xData, axisLabel: { interval:0,//横轴信息全部显示 formatter: function (value, index) { if(vm.selectedDateType == 1 || vm.selectedDateType == 2){ // if(index == 0){ // vm.startDate = value; // return value.substr(5,2)+"月"+value.substr(8,2); // }else{ // if(index == 19){ // vm.endDate = value; // } // return value.substr(8,2); // } }else if(vm.selectedDateType == 3){ var val = value.substr(5,2)+"月" if(index == 0){ // vm.startDate = value; }else{ if(index == 19){ vm.endDate = value; } } return val; } } } }, yAxis: { type: 'value', axisPointer: { snap: true }, scale: true, minInterval: 1, boundaryGap: ['10%', '30%'], splitLine: {show:false} }, dataZoom: [{//给x轴设置滚动条 // show: false, start: dataZoom_end, end: 100, type: 'slider', zoomLock: false, },{ //下面这个属性是内容区域配置 start: dataZoom_end, end: 100, type: 'inside', zoomLock: false, }] }; var series = []; for(var i=0; i markLineValue){ return markLineValue / 2; }else{ return value.min / 2; } }, max: function(value){ if(value.max > markLineValue){ return parseInt(value.max * 1.5); }else{ return parseInt(markLineValue * 1.2); } } }); $.extend(options.series[0], {markLine: { data:[ {yAxis: markLineValue, name: markLineName} ], label:{ show: true, position: 'middle', formatter: '{b}: {c}' }, lineStyle:{ color: 'rgb(194,53,49)' } }}); } // console.log(JSON.stringify(options)); $("#"+vm.lineid).removeAttr('_echarts_instance_') lineCharts.setOption(options); window.lineCharts = lineCharts; } })()