lineChart.vue 3.33 KB
<template>
	<view class="linechart">
		<div :id='bindId' class="chart"></div>
	</view>
</template>

<script>
	export default {
	  data () {
		return {
		}
	  },
	  mounted() {
		   console.log('chartData',this.chartData)
	  },
	  watch:{
		  bindId:function(newValue,oldValue){
		  },
		  chartData:{
			handler(newValue,oldValue){
				this.drawLine();
			},
			deep:true
		  },
	  },
	  props:{
			bindId:{
				type:String,
				default:''
			},
		   chartData: {
                type: Object,
                default: function(){
					return {}
				}
            }
	  },
	  methods: {
	  	 drawLine() {
            let myChart = {};
            let chartDom = document.getElementById(this.bindId);
			myChart = this.echarts.init(chartDom);
            myChart.showLoading('default', {
                text: '',
                color: '#409eff'
            });
            var option = {
				title:{
					show:true,
					 text:this.chartData.title,
					 left: '13px',
					 top: '13px',
					 textStyle: {
						 fontWeight: "normal",
						 fontSize: 14,
						 color: '#444'
					 }
				},
                backgroundColor: '#fff',
                color: ["#0069FF", "#3BB8FF", "#6784e3", "#ff9631", "#ffc62e","#7460ee","#eb6100"],
                tooltip: {
					triggerOn: 'none',
					position: function (pt) {
						return [pt[0], 130];
					}
				},
                grid: {
					left: '50px',
					right: '26px',
					top: '50px',
					bottom: '60px'
				},
                yAxis: {
                    scale: true,
                    axisLabel: {
						 textStyle: {
						    color: '#888'
						}
					},
					axisLine : {
					    show: true,
					    lineStyle: {
					        color: '#888'
					    }
					},
					axisTick:{
						show:false
					},
					splitLine:{
						lineStyle: {
						    color: '#f5f5f5'
						}
					}
                },
                xAxis: {
					axisTick:{
						show:false
					},
					axisLine : {
					    show: true,
					    lineStyle: {
					        color: '#888'
					    }
					},
					axisLabel : {
					    textStyle: {
					        color: '#888'
					    }
					},
					splitLine : {
					    show: false
					}
				},
				legend : {
				    orient: "horizontal",
				    x: "center",
				    textStyle: {
				        fontSize: 12,
				    },
				    itemWidth: 12,
				    itemHeight: 6,
				    itemGap: 10,
				    bottom: 2,
				    data: []
				},
				series:this.chartData.series
            }
           if(this.chartData.series.length>1){
			   this.chartData.series.forEach((item,index)=>{
				    var legendObj = {};
					legendObj.name = item.name;
					option.legend.data.push(legendObj);
					item.areaStyle={
						normal: {
							color: new this.echarts.graphic.LinearGradient(0, 0, 0, 1, [{
								offset: 0,
								color: option.color[index]
							}, {
								offset: 1,
								color: option.color[index]
							}])
						}
					}
			   })
		   }
             option.xAxis.data = this.chartData.xaxis.data;
           
            myChart.clear();
            setTimeout(() => {
                myChart.hideLoading();
            }, 500);
            myChart.setOption(option);
			console.log(option)
        }
	  }
	}
</script>

<style>
	.chart{
		width:706.52upx;
		height:438.4upx;
		margin: 0 auto;
		box-shadow:0px 0px 7px 0px rgba(0,0,0,0.14);
		border-radius:4px;
		background: #FFFFFF;
		margin-bottom: 18.11upx;;
	}
</style>