在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。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
假设我们有一个 Flask 后端 API 返回下面格式的 JSON 数据: ```json { "labels": ["January", "February", "March", "April", "May", "June", "July"], "data": [65, 59, 80, 81, 56, 55, 40] } ``` 我们想要在 Angular 的组件使用 ECharts 展示这个数据,可以按照以下步骤操作: 1. 在组件引入 ECharts: ```typescript import * as echarts from 'echarts'; ``` 2. 在组件定义一个 `chartOption` 对象,并使用 JSON 数据填充它: ```typescript chartOption = { xAxis: { type: 'category', data: [] }, yAxis: { type: 'value' }, series: [{ data: [], type: 'line' }] }; ngOnInit() { this.chartOption.xAxis.data = this.data.labels; this.chartOption.series[0].data = this.data.data; } ``` 我们在 `ngOnInit` 生命周期将 JSON 数据的 `labels` 和 `data` 分别赋值给 `chartOption` 的 `xAxis.data` 和 `series[0].data`。 3. 在组件的 HTML 模板添加一个 `div` 元素用于渲染 ECharts: ```html <div #chart style="width: 100%; height: 400px;"></div> ``` 4. 在组件定义一个 `@ViewChild` 装饰器,用来获取 `div` 元素的引用: ```typescript @ViewChild('chart') chart: ElementRef; ``` 5. 在组件定义一个 `renderChart` 方法,用来渲染 ECharts: ```typescript renderChart() { const chart = echarts.init(this.chart.nativeElement); chart.setOption(this.chartOption); } ``` 6. 在组件的 `ngAfterViewInit` 生命周期调用 `renderChart` 方法: ```typescript ngAfterViewInit() { this.renderChart(); } ``` `ngAfterViewInit` 生命周期在视图和子视图初始化完成之后调用,此时 `div` 元素已经被渲染出来,可以使用 `renderChart` 方法将 ECharts 渲染到这个元素。 完整的组件代码如下: ```typescript import { Component, ElementRef, ViewChild } from '@angular/core'; import * as echarts from 'echarts'; @Component({ selector: 'app-echarts-demo', template: '<div #chart style="width: 100%; height: 400px;"></div>' }) export class EchartsDemoComponent { @ViewChild('chart') chart: ElementRef; chartOption = { xAxis: { type: 'category', data: [] }, yAxis: { type: 'value' }, series: [{ data: [], type: 'line' }] }; data = { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], data: [65, 59, 80, 81, 56, 55, 40] }; ngOnInit() { this.chartOption.xAxis.data = this.data.labels; this.chartOption.series[0].data = this.data.data; } ngAfterViewInit() { this.renderChart(); } renderChart() { const chart = echarts.init(this.chart.nativeElement); chart.setOption(this.chartOption); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值