在angular项目中使用echarts图表,以及动态更新echart数据

在angular中使用echart,必须安装echart依赖和ngx-echart依赖
在app.module.ts中导入ngx-echarts依赖之后,在@NgModule中导入 echart依赖
安装步骤
npm install echarts -S
npm install ngx-echarts -S

app.module.ts 代码如下:

import { NgxEchartsModule } from 'ngx-echarts';
@NgModule({
 imports: [
   NgxEchartsModule.forRoot({
     /**
      * This will import all modules from echarts.
      * If you only need custom modules,
      * please refer to [Custom Build] section.
      */
     echarts: () => import('echarts'), // or import('./path-to-my-custom-echarts')
   }),
 ],
})

在component中使用echarts,代码:

<div echarts   [options]="options" class="demo-chart"  (chartInit)="onChartInit($event)"></div>

component.ts:

import { EChartOption } from 'echarts';
export class TeachnumberComponent implements OnInit {
  onChartInit(ec) {
    this.echartsIntance = ec;
    //获取到echart对象
    
  },
  methods(data1) {
  //数据切换之后调用此函数来渲染echart
    this.options = {
      backgroundColor: '#31313D',
    
      title: {
        show: false
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      legend: {
        show: false
      },
      grid: {
        containLabel: true,
        show: false
      },
      xAxis: {
        type: 'category',
        boundaryGap: [0, 0.01],
        show: true,
        data: data1.yAxisData,
      },
      yAxis: {

        type: 'value',

        axisLine: {
          show: false 
        },
        axisTick: {
          show: false
        },
        axisLabel: {
          show: true,
        }
      },
      textStyle: {
        fontSize: 18,
        color: '#fff'
      },
      series: [
        {
          type: 'bar',
          
          data: data1.seriesData,
          itemStyle: {
            normal: {
              label: {
                formatter: function (c) {
                  return (c.data).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                },
                show: true,
                position: "top",
                textStyle: {
                  fontWeight: "normal",
                  fontSize: "12",
                  color: "#fff"
                }
              },
              color: function (params) {                          
                var colorList = ['#FADD6F', '#8F6CD0', '#FE914C', '#F899C5', '#3CC8A1', '#44B9FF'].reverse()
                return colorList[params.dataIndex]
              }
            },

          },
        },
      ]
    };

    this.echartsIntance.setOption(this.options); //手动重新渲染echart
  }
}

动态更新数据核心内容:通过onChartInit()函数,在初始化时获取到echart对象,然后使用echartsIntance.setOption()方法重新渲染echart。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值