【问题标题】:Updating Apexchart series on daily bases in angular每天以角度更新 Apex 图表系列
【发布时间】:2021-01-22 12:24:48
【问题描述】:

我正在使用 apexchart 但不知道如何使用更新系列我尝试过直接发送值

HTML

<apx-chart [chart]="{
                          type: 'line',
                          height: 20,
                          background: '#696868',
                          sparkline: {
                              enabled: true
                          }
                      }" [colors]="['#f2f2f2']" [markers]="{
                          size: 0
                      }" [series]="chartOptions.series" [stroke]="{
                              color: '#4191ff',
                              width: 1
                      }" [xaxis]="{
                          crosshairs: {
                              width: 0
                          }
                      }" [yaxis]="{
                          min: 0
                      }" ></apx-chart>

Typescript

    export type ChartOptions = {
      series: ApexAxisChartSeries;
      chart: ApexChart;
    };

 

    @ViewChild("chart", { static: false }) chart: ChartComponent;
      public chartOptions: Partial<ChartOptions>;
      constructor(private service: SharedService) {
        this.chartOptions = {
          series: [
            {
              name: "kg/hr",
              data: ([10, 41, 35, 51, 49, 62, 69, 91, 148])
            }
          ]
        }
      }
ngOnInit() {

   
    this.ApexChart_Function();
    this.id = setInterval(() => {
      this.ApexChart_Function(); 
    }, 5000);
    
}
      ApexChart_Function() {
    const ARRAY_LENGTH = 10;
    let randomArray = [];
    for(let i =0; i<ARRAY_LENGTH; i++) {randomArray.push((Math.random() * 100).toFixed())}
    randomArray = JSON.parse("[" + randomArray + "]")
    console.log(randomArray);
    this.chartOptions.series = [{
      name: 'series_1',
      data: randomArray
    }];
    console.log(randomArray);    
  }
ngOnDestroy(): void {
    if(this.id){
      clearInterval(this.id);
    }
}

我在 ngOnInit 中创建了一个间隔,并调用我创建的函数来更新系列,它对我有用。并且还可以在使用后清除或销毁间隔。谢谢希望对你有帮助

【问题讨论】:

  • 我已经找到了解决问题的方法,我正在更新问题

标签: angular typescript apexcharts


【解决方案1】:

您应该在 ts 文件中使用 ViewChild 模板引用。首先将模板引用添加到“apex-chart”组件。我已将“#chart”添加到 apex-chart 组件。

html

<apx-chart #chart [chart]="{
                          type: 'line',
                          height: 20,
                          background: '#696868',
                          sparkline: {
                              enabled: true
                          }
                      }" [colors]="['#f2f2f2']" [markers]="{
                          size: 0
                      }" [series]="chartOptions.series" [stroke]="{
                              color: '#4191ff',
                              width: 1
                      }" [xaxis]="{
                          crosshairs: {
                              width: 0
                          }
                      }" [yaxis]="{
                          min: 0
                      }" ></apx-chart></code>

现在使用您的打字稿文件中的模板引用来更新您的系列数据,像这样

this.chart.updateSeries([
      {
        名称:'series_1',
      数据:随机数组
      }
    ])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    相关资源
    最近更新 更多