鍍金池/ 問答/HTML5  Java  HTML/ angular4 中使用echarts 放在tab中數(shù)據(jù)不顯示 并且數(shù)據(jù)變化不能

angular4 中使用echarts 放在tab中數(shù)據(jù)不顯示 并且數(shù)據(jù)變化不能重新渲染

  <nz-tab *ngFor="let item of tabs index as i" nzTitle="{{item.title}}" (nzSelect)="selectTab(item.index)">
      <div echarts [options]="options[item.index-1]" [loading]="showloading" class="chart"></div>
    </nz-tab>
export class TaskList {
  // 任務(wù)列表
  public taskList: any[] = [];
  // 用戶權(quán)限 1 維修部 ;2 商務(wù)部;3 總經(jīng)理
  private userRole: number = 1;
  private options: any[] ;
  // private options: any;
  private time: any = 1;
  private tabIndex: any = 1;
  private tabs: any[] = [{ title: '房間出租量', index: 1 }, { title: '停車位出租量', index: 2 }, { title: '廣告位出租量', index: 3 }];

  constructor (private taskListService: TaskListService) {
    this.taskList = taskListService.getTaskList();
    this.options = [this.taskListService.getRentalsOption(1, this.time), this.taskListService.getRentalsOption(2, this.time), this.taskListService.getRentalsOption(3, this.time)];
  }

  public ngOnInit () {
  }
  // 選擇時間id 1 本日;2 本周;3 本月;4本年
  public resetTime (time: any): void {
    this.time = time;
    this.resetOptions(this.tabIndex, this.time);
  }
  // 選擇時間區(qū)間
  public resetTimeRegion (result: Date): void {
    console.log('onChange: ', result);
  }
  // 切換tab
  public selectTab (tabIndex: number): void {
    this.tabIndex = tabIndex;
    this.resetOptions(this.tabIndex, this.time);
  }
  // 重置charts options
  public resetOptions (tabIndex: number, time: any): void {
    this.options[(tabIndex - 1)] = this.taskListService.getRentalsOption(tabIndex, time);
    console.log(JSON.stringify(this.options[(this.tabIndex - 1)]));
 
  }
}
 public option: any = {
    color: ['#3ba1ff'],
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐標(biāo)軸指示器,坐標(biāo)軸觸發(fā)有效
            type : 'shadow',        // 默認(rèn)為直線,可選為:'line' | 'shadow'
        },
    },
    grid: {
        left: '3%',
        right: '3%',
        bottom: '3%',
        // containLabel: true,
    },
    xAxis : [
        {
            type : 'category',
            data : [],
            axisTick: {
                alignWithLabel: true,
            },
        },
    ],
    yAxis : [
        {
            type : 'value',
        },
    ],
    series : [
        {
            name: '直接訪問',
            type: 'bar',
            barWidth: '60%',
            data: [],
        },
    ],
    
  public getRentalsOption (tabIndex: number, time: any) {
    // 1 本日;2 本周;3 本月;4 本年 ;其他 時間區(qū)間
    if (typeof (time) != 'number') {
      this.option.xAxis[0].data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
      this.option.series[0].data = [20, 20, 20, 334, 390, 330, 220];
    }
    if (time == 1 ) {
      this.option.xAxis[0].data = ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'];
      this.option.series[0].data = [10, 52, 200, 334, 390, 330, 220, 10, 52, 200, 334, 390, 330, 220, 10, 52, 200, 334, 390, 330, 220, 10, 52, 200];
    }
    if (time == 2) {
      this.option.xAxis[0].data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
      this.option.series[0].data = [20, 20, 20, 334, 390, 330, 220];
    }
    if (time == 3) {
      const len = this.commonMethod.mGetDate();
      const xAxis: string[] = [];
      for (let i = 0; i < len; i++) {
        xAxis.push((i + 1).toString());
      }
      this.option.xAxis[0].data = xAxis;
      this.option.series[0].data = [10, 52, 200, 334, 390, 10, 52, 200, 334, 390, 10, 52, 200, 334, 390, 10, 52, 200, 334, 390, 10, 52, 200, 334, 390, 10, 52, 200, 334, 390];
    }
    if (time == 4) {
      this.option.xAxis[0].data = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
      this.option.series[0].data = [10, 52, 200, 334, 390, 330, 220, 200, 334, 390, 330, 220];
    }
    return this.option;
  }

 

clipboard.png

切換時間的選擇數(shù)據(jù)option變化 但是圖表沒有重新繪制

回答
編輯回答
懶洋洋

因為option為引用類型數(shù)據(jù),地址沒變,所以被認(rèn)為沒有數(shù)據(jù)改變,resetOptions最后加一句試一下:
this.options= Object.assign({},this.options);

2017年12月18日 12:05