You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache ECharts类型报错:'string'无法赋值给"pictorialBar"求解

ECharts类型转换问题:添加type: 'bar'后编译报错

原示例可正常运行,但配置项使用了any类型,我正尝试将其转换为EChartOption类型。

以下是当前代码,注释掉type: 'bar'时可正常编译:

testOptions: EChartsOption = Object.assign(
  {},
  {
    grid: {
      left: '6%',
      right: '6%',
      bottom: '4%',
      top: '3%',
      containLabel: true,
    },
    xAxis: {
      axisLabel: {
        color: 'gray',
        fontSize: '16',
      },
      axisLine: {
        lineStyle: {
          color: 'blue',
          width: 4,
        },
      },
      axisTick: {
        show: false,
      },
      splitLine: {
        lineStyle: {
          color: 'gray',
          width: 1,
        },
      },
    },
    yAxis: {
      data: this.labels,
      axisLabel: {
        color: 'pink',
        fontSize: 22,
      },
      axisLine: {
        lineStyle: {
          color: 'orange',
          width: 4,
        },
      },
      axisTick: {
        show: false,
      },
    },
    series: [
      {
        // For shadow
        //type: 'bar',
        data: this.data.map((v) => this.maxValue),
        cursor: 'default',
        itemStyle: {
          normal: {
            color: 'gray',
          },
          opacity: 1,
        },
        barWidth: '40%',
        barGap: '-100%',
        barCategoryGap: '30%',
        animation: false,
        z: 1,
      },
    ],
  }
);

若取消type: 'bar'的注释,会出现编译错误:

Error in src/echart.component.ts (23:3)
Type '{ grid: { left: string; right: string; bottom: string; top: string; containLabel: boolean; }; xAxis: { axisLabel: { color: string; fontSize: string; }; axisLine: { lineStyle: { color: string; width: number; }; }; axisTick: { ...; }; splitLine: { ...; }; }; yAxis: { ...; }; series: { ...; }[]; }' is not assignable to type 'EChartsOption'.
Types of property 'series' are incompatible.
Type '{ type: string; data: number[]; cursor: string; itemStyle: { normal: { color: string; }; opacity: number; }; barWidth: string; barGap: string; barCategoryGap: string; animation: boolean; z: number; }[]' is not assignable to type 'SeriesOption$1 | SeriesOption$1[] | undefined'.
Type '{ type: string; data: number[]; cursor: string; itemStyle: { normal: { color: string; }; opacity: number; }; barWidth: string; barGap: string; barCategoryGap: string; animation: boolean; z: number; }[]' is not assignable to type 'BarSeriesOption$1 | LineSeriesOption$1 | ScatterSeriesOption$1 | PieSeriesOption$1 | ... 18 more ... | SeriesOption$1[]'.
Type '{ type: string; data: number[]; cursor: string; itemStyle: { normal: { color: string; }; opacity: number; }; barWidth: string; barGap: string; barCategoryGap: string; animation: boolean; z: number; }[]' is not assignable to type 'SeriesOption$1[]'.
Type '{ type: string; data: number[]; cursor: string; itemStyle: { normal: { color: string; }; opacity: number; }; barWidth: string; barGap: string; barCategoryGap: string; animation: boolean; z: number; }' is not assignable to type 'SeriesOption$1'.
Type '{ type: string; data: number[]; cursor: string; itemStyle: { normal: { color: string; }; opacity: number; }; barWidth: string; barGap: string; barCategoryGap: string; animation: boolean; z: number; }' is not assignable to type 'PictorialBarSeriesOption$1'.
Type '{ type: string; data: number[]; cursor: string; itemStyle: { normal: { color: string; }; opacity: number; }; barWidth: string; barGap: string; barCategoryGap: string; animation: boolean; z: number; }' is not assignable to type 'PictorialBarSeriesOption'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"pictorialBar"'.

根据ECharts官方文档,bar是合法的type选项,求问题原因与解决方案。


内容的提问来源于stack exchange,提问作者Ole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 17:59:51