Angular8 引入百度Echarts,进行图表分析

最近突发奇想,想自己做一个理财分析记录的网页,主要给自己记账,然后想到了把每天,每周的消费用图标分析出来,于是就需要用到百度的echarts图标插件,talk is cheep, show me the code。

1. npm 安装ngx-echarts及相关的依赖

  1. npm install echarts -S
  2. npm install ngx-echarts -S
  3. npm install @types/echarts -D

2. 在angular.json添加js引导文件

  1. {
  2. "scripts": [
  3. "../node_modules/echarts/dist/echarts.min.js" // or echarts.js for debug purpose
  4. ],
  5. }

3. AppModule引入NgxEchartsModule

这里有个坑,记得在你调用了echarts的相关模块的Module里面引入NgxEchartsModule,不然会报错:Template parse errors: Can’t bind to ‘options’ since it isn’t a known property of ‘div’.所以切记

  1. import { NgxEchartsModule } from 'ngx-echarts';
  2. @NgModule({
  3. imports: [
  4. ...,
  5. NgxEchartsModule
  6. ],
  7. ...
  8. })
  9. export class AppModule { }

4. 使用echarts

  • html
    1. <div echarts [options]="chartOption" class="demo-chart"></div>
  • scss
    1. .demo-chart {
    2. height: 400px;
    3. }
  • component
    1. import { EChartOption } from 'echarts';
    2. chartOption: EChartOption = {
    3. xAxis: {
    4. type: 'category',
    5. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    6. },
    7. yAxis: {
    8. type: 'value'
    9. },
    10. series: [{
    11. data: [820, 932, 901, 934, 1290, 1330, 1320],
    12. type: 'line'
    13. }]
    14. }

    5. 参考网站

  • https://www.npmjs.com/package/ngx-echarts
  • angular echarts 在线模板