无法在 Angular 上获取动态数据到 echart(ngx-admin)

问题描述 投票:0回答:1

当我在订阅之外的任何地方调用数据时,我会得到未定义的结果。 我正在使用 Chartjs,我也面临着同样的问题, 谁能向我解释一下我做错了什么。

在条形图组件中,这是我尝试过的。

@Component({
  selector: 'ngx-d3-bar',
  template: `
    <chart echarts [options]="options" class="echart"></chart>

  `,
})
export class D3BarComponent implements OnDestroy {
  Productlist : Product[];
  product :Product= new Product()
  options: any = {};
  themeSubscription: any;
  ax_Y : any[]=[] ;
  ax_X : any ;
  constructor(private theme: NbThemeService , private  prodservice : ProductService) {
  }

  ngAfterViewInit() {

    this.themeSubscription = this.theme.getJsTheme().subscribe(config => {
      this.prodservice.getAllproducts().subscribe((data) => {
        this.Productlist = data;
        console.log("all products",this.Productlist);
        this.ax_Y = this.Productlist.map(x=>x.price) ;
        this.ax_X = this.Productlist.map(x=>x.type) ;
        console.log("les x",this.ax_X)
        console.log("les y",this.ax_Y)

        //i get the values from ax_x and ax_y after maping everything is workingfine

      });

      const colors: any = config.variables;
      const echarts: any = config.variables.echarts;

      this.options = {
        backgroundColor: echarts.bg,
        color: [colors.primaryLight],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            type: 'category',
            //data:[['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']]
            data:this.ax_X, // but here the table is empty there is no dynamic data
            axisTick: {
              alignWithLabel: true,
            },
            axisLine: {
              lineStyle: {
                color: echarts.axisLineColor,
              },
            },
            axisLabel: {
              textStyle: {
                color: echarts.textColor,
              },
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
            axisLine: {
              lineStyle: {
                color: echarts.axisLineColor,
              },
            },
            splitLine: {
              lineStyle: {
                color: echarts.splitLineColor,
              },
            },
            axisLabel: {
              textStyle: {
                color: echarts.textColor,
              },
            },
          },
        ],
        series: [
          {
            name: 'Score',
            type: 'bar',
            barWidth: '60%',
            // data: [10, 52, 200, 334, 390, 330, 220]
            data: this.totalpric_Y, //my dynamic data
          },
          console.log('whyyyyyyyyyyyyy',this.ax_Y)
        ],
      };
    });
  }

  ngOnDestroy(): void {
    this.themeSubscription.unsubscribe();
  }
}

这是检查中的数据,一切正常,直到我在函数之外的任何地方调用数据

angular echarts
1个回答
0
投票

解决方案:

@Component({
 selector: 'ngx-d3-bar',
 template: `
<chart echarts [options]="options" class="echart"></chart>

  `,
    })
   export class D3BarComponent implements OnDestroy {
   Productlist : Product[];
   product :Product= new Product()
   options: any = {};
   themeSubscription: any;
    ax_Y : any[]=[] ;
   ax_X : any ;
   constructor(private theme: NbThemeService , private  prodservice : 
   ProductService) {
   }

   ngAfterViewInit() {

  this.themeSubscription = this.theme.getJsTheme().subscribe(config => {
  this.prodservice.getAllproducts().subscribe((data) => {
    this.Productlist = data;
    console.log("all products",this.Productlist);
    this.ax_Y = this.Productlist.map(x=>x.price) ;
    this.ax_X = this.Productlist.map(x=>x.type) ;
    console.log("les x",this.ax_X)
    console.log("les y",this.ax_Y)


  const colors: any = config.variables;
  const echarts: any = config.variables.echarts;

  this.options = {
    backgroundColor: echarts.bg,
    color: [colors.primaryLight],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        //data:[['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']]
        data:this.ax_X, // but here the table is empty there is no dynamic data
        axisTick: {
          alignWithLabel: true,
        },
        axisLine: {
          lineStyle: {
            color: echarts.axisLineColor,
          },
        },
        axisLabel: {
          textStyle: {
            color: echarts.textColor,
          },
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: echarts.axisLineColor,
          },
        },
        splitLine: {
          lineStyle: {
            color: echarts.splitLineColor,
          },
        },
        axisLabel: {
          textStyle: {
            color: echarts.textColor,
          },
        },
      },
    ],
    series: [
      {
        name: 'Score',
        type: 'bar',
        barWidth: '60%',
        // data: [10, 52, 200, 334, 390, 330, 220]
        data: this.totalpric_Y, //my dynamic data
      },
      console.log('whyyyyyyyyyyyyy',this.ax_Y)
    ],
  };
   });
       });
   }

 ngOnDestroy(): void {
 this.themeSubscription.unsubscribe();
      }
  }
© www.soinside.com 2019 - 2024. All rights reserved.