ChartJS ChartConfiguration在“选项.刻度.x”中缺少“时间”参数

jq6vz3qz  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(158)

我正在从chartjs 2.9.4更新到3.8.0,在ChartConfiguration对象上设置options.scales.x.time.unit参数时遇到错误。

error TS2339: Property 'time' does not exist on type 
'_DeepPartialObject<{ type: "linear"; } & CartesianScaleOptions & { beginAtZero: boolean; suggestedMin?: number; suggestedMax?: number; grace?: string | number; ticks: { format: NumberFormatOptions; precision: number; stepSize: number; count: number; }; }> | _DeepPartialObject<...> | _DeepPartialObject<...> | _DeepPa...'.

当我第一次遇到这个问题时,我以为它与ng2-charts库有关,但即使没有使用这个库,我也会得到同样的错误。下面是导致错误的一小段代码。

import { Component, OnInit } from '@angular/core';
import { ChartConfiguration } from 'chart.js';

@Component({
  selector: 'daily-chart',
  templateUrl: './daily-chart.component.html',
  styleUrls: ['./daily-chart.component.scss']
})
export class DailyChartComponent implements OnInit {
  chartConfig: ChartConfiguration<'line'> = {
    data: {
      datasets: []
    },
    type: 'line',
    options: {
      scales: {
        x: {
          type: 'time',
          time: {
            unit: 'day',
          },
        }
      }
    }
  }

  constructor() { }

  ngOnInit() {
    this.chartConfig.options.scales.x.time.unit = 'week'; // <--- This line results in the error
  }
}

我正在使用以下库:

  • chart.js:3.8.0
  • ng2-charts:3.0.11版本
  • 13.3.5个月
  • tslib:2.4.0
  • 第4.6.4节
  • x = 1.0.0,
  • x = 1.4.0,
  • 在一个实施例中,
cx6n0qe3

cx6n0qe31#

这不是一个完美的解决方案,因为它使用了到any的转换,但作为快速修复,您可以尝试以下方法:

import { ChartConfiguration, TimeScaleOptions } from 'chart.js';

...

ngOnInit() {
  const xScaleOptions: TimeScaleOptions = <any>this.chartConfig.options.scales.x;
  xScaleOptions.time.unit = 'week';
}

相关问题