angular中对echarts图表进行封装(环形进度条、双Y轴折线区域图)

在大屏的开发过程中,需要大量的使用echarts图表,我们可以腾出时间将echarts图表进行二次封装,以便后期循环使用。

1、二次封装echarts:

封装为公共组件,在使用的时候只需要传入option项即可

import { AfterViewInit, Component, ElementRef, Input, OnDestroy } from "@angular/core";
import * as echarts from 'echarts';

@Component({
  selector: 'my-charts',
  template: ``,
  styles: [`
    :host {
      display: inline-block;
      height: 100%;
      width: 100%;
      padding: 0;
      margin: 0;
      border: none;
      cursor: pointer;
      overflow: hidden;
    }
  `]

})
export class MYChartsComponent implements AfterViewInit, OnDestroy {

  chart;
  _option;

  constructor(private el: ElementRef) {
  }

  @Input()
  set option(option) {
    this._option = option
    if (this.chart) {
      this.chart.setOption(option, true);
    }
  }

  ngAfterViewInit() {
    this.chart = echarts.init(this.el.nativeElement);
    this.chart.setOption(this._option, true);
    window.addEventListener('resize', this.onWindowResize)
  }

  onWindowResize = () => {
    this.chart.resize()
  }

  ngOnDestroy() {
    window.removeEventListener('resize', this.onWindowResize)
  }

}
2、将不同的echarts图表封装到一个charts.options.ts文件中:

这个文件将你所有图表的配置项写成key-value的形式存入进去,这样可以在你使用到某个相同的echarts图表时,只需要在外部动态的修改data数据即可。

import * as echarts from "echarts";

const CHARTS_OPTION = {
   progressAllotment: {
     // 环形进度条的代码部分 
   },
   taskNumByMonth: {
      // 双Y轴折线区域图
   }
};

export { CHARTS_OPTION };


环形进度条说明:
环形进度条有一些地方需要记录一下:

  • 1、echarts环形使用top: 'center'默认是依据的主标题居中对齐,因此,再去加入副标题时,就会产生没有垂直居中对齐的情况,需要手动修改top: '40%'的百分比
  • 2、clockwise属性设置为true指的是环形为顺时针展示
    参考图例.png
  • 参考代码:
  progressAllotment: {
    title: {
      text: 0 + "%",
      textStyle: {
        color: "#9A9EBA",
        fontSize: 18,
      },
      subtext: "当日配款进度",
      subtextStyle: {
        fontSize: 16,
        color: "#106199",
      },
      align: "auto",
      itemGap: 10,
      left: "center",
      top: "40%",
    },
    angleAxis: {
      max: 100,
      clockwise: true,
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      splitLine: {
        show: false,
      },
    },
    radiusAxis: {
      type: "category",
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      splitLine: {
        show: false,
      },
    },
    polar: {
      center: ["50%", "50%"],
      radius: "160%", //图形大小
    },

    series: [
      {
        type: "bar",
        data: [
          {
            name: "当前配款进度",
            value: 0,
            itemStyle: {
              normal: {
                color: "#15A3EF",
              },
            },
          },
        ],
        coordinateSystem: "polar",
        roundCap: true,
        barWidth: 10,
        barGap: "-100%", // 两环重叠
        z: 2,
      },
      {
        type: "bar",
        data: [
          {
            value: 100,
            itemStyle: {
              color: "#1E3566",
              shadowColor: "rgba(0, 0, 0, 0.2)",
              shadowOffsetY: 2,
            },
          },
        ],
        coordinateSystem: "polar",
        roundCap: true,
        barWidth: 10,
        barGap: "-100%", // 两环重叠
        z: 1,
      },
    ],
  },

双Y轴折线区域图:
这里同样有些地方需要留意的,在echarts3.0版本以上,貌似y轴坐标就需要手动去划分刻度以及设置最大值或最小值。

  • 1、双y轴显示:在yAxis配置了两个对象内容的基础上,将series中的第二个对象的yAxisIndex设为1即可
  • 2、左右坐标轴刻度不均等,导致间隔出现大小不一:使用interval: Math.ceil(坐标的最大值 / 4)这样就成功的划分为了4等分
  • 3、折线图渐变区域块的设置:渐变区域的话只需要在areaStyle中去设置渐变颜色即可
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "#74EAB0", // 0% 处的颜色
            },
            {
              offset: 0.1,
              color: "#448AC8", // 10% 处的颜色
            },
            {
              offset: 1,
              color: "transparent", // 100% 处的颜色
            },
          ]),
        },
  • 4、拐点的样式自定义:在series中去自定义itemStyle样式即可
        itemStyle: {
          normal: {
            color: "#3AB578",
            lineStyle: {
              color: "#3AB578",
              width: 1,
            },
          },
        },
参考图片.png
  • 参考代码:
 taskNumByMonth: {
    legend: {
      data: ["订单数量", "订单金额", "配款耗时"],
      icon: "circle",
      right: 0,
      textStyle: {
        color: "white",
      },
    },
    grid: {
      left: "5%",
      right: "8%",
      bottom: "8%",
      containLabel: true,
    },
    tooltip: {
      trigger: "axis",
    },
    xAxis: {
      type: "category",
      data: ["16号","19号","21号","24号","27号","30号","3号","6号","9号","12号","15号"],
      boundaryGap: false,
      axisLabel: {
        color: "white",
        fontSize: 12,
      },
      axisLine: {
        lineStyle: {
          color: "#366383",
        },
      },
      axisTick: {
        show: false,
      },
    },
    yAxis: [
      {
        name: "单位: 万元",
        nameTextStyle: {
          color: "white",
        },
        type: "value",
        axisLabel: {
          color: "white",
          fontSize: 12,
        },
        min: 0,
        max: 20000, // 最大值
        interval: Math.ceil(20000 / 4), //  平均分为4份
        splitLine: {
          show: true,
          lineStyle: {
            type: "dashed",
            color: "#395275",
          },
        },
      },
      {
        name: "单位: 分钟",
        nameTextStyle: {
          color: "white",
        },
        type: "value",
        axisLabel: {
          color: "white",
          fontSize: 12,
        },
        min: 0,
        max: 160, // 最大值
        interval: Math.ceil(160 / 4), //  平均分为4份
        splitLine: {
          show: true,
          lineStyle: {
            type: "dashed",
            color: "#395275",
          },
        },
      },
    ],
    series: [
      {
        name: "单位: 万元",
        data: [8000,9000,10000,13000,14000,14000,15000,16000,14800,14000,20000],
        type: "line",
        smooth: false,
        symbol: "circle",
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "#74EAB0", // 0% 处的颜色
            },
            {
              offset: 0.1,
              color: "#448AC8", // 10% 处的颜色
            },
            {
              offset: 1,
              color: "transparent", // 100% 处的颜色
            },
          ]),
        },
        itemStyle: {
          normal: {
            color: "#3AB578",
            lineStyle: {
              color: "#3AB578",
              width: 1,
            },
          },
        },
        yAxisIndex: 0,
      },
      {
        name: "单位: 分钟",
        data: [116, 80, 128, 110, 120, 130, 150, 160, 145, 150, 158],
        type: "line",
        smooth: false,
        symbol: "circle",
        itemStyle: {
          normal: {
            color: "#FCB07E",
            lineStyle: {
              color: "#FCB07E",
              width: 1,
              type: "dotted",
            },
          },
        },
        yAxisIndex: 1,
      },
    ],
  }
3、导入配置项,调用公共组件,开始使用echarts:
  • 模板代码文件:
          <div nz-col nzSpan="12" style="width: 50%;height: 100%;">
            <div class="common">
              <span class="sub-title left-title">近30天订单量变化趋势图</span>
              <zj-charts [option]="chartsOption.taskNumByMonth"></zj-charts>
            </div>
          </div>
  • 组件ts代码部分:
import { HttpClient } from "@angular/common/http";
import { Component, OnDestroy, OnInit, ViewChild } from "@angular/core";
import * as _ from "lodash";
import { of, Subscription, zip } from "rxjs";
import { OrderDataService } from "./order-data.service";
import { CHARTS_OPTION } from "./charts.options";

interface Org {
  orgName?: string;
  orgAmount?: number;
  orgType?: Object;
}

@Component({
  selector: "order-data",
  templateUrl: "./order-data.component.html",
  styleUrls: ["./order-data.component.less"],
})
export class OrderDataComponent implements OnInit, OnDestroy {

  subscriptionList = new Array<Subscription>(1);
  timeHandleList = new Array(1);

  objectKeys = Object.keys;

  items: Org = {
    orgName: "桃花坞支行",
    orgAmount: 120,
    orgType: {
      "100元": 200,
      "50元": 9,
      "20元": 19,
      "10元": 10,
      "5元": 3,
      "1元": 2,
      "5角": 1,
      "1角": 0,
    },
  };

  // 模拟数据
  mock = {
    task: {},
    quota: {},
  };

  chartsOption = CHARTS_OPTION;
  constructor(private http: HttpClient, private _ser: OrderDataService) {}

  ngOnInit() {
    this.refreshDataOnce();
  }

  // 刷新订单数据模块的图表数据
  refreshDataOnce = () => {
    // 模拟数据
    let sourceOne = of({
      process: {
        percent: parseFloat((Math.random() * 100).toFixed(1)),
      },
    });
    this.mock["task"] = {
      total: parseFloat((Math.random() * 1000).toFixed(0)),
      amount: parseFloat((Math.random() * 10000).toFixed(1)),
    };
    this.mock["quota"] = {
      count: parseFloat((Math.random() * 1000).toFixed(0)),
      amount: parseFloat((Math.random() * 10000).toFixed(1)),
    };
    
    this.subscriptionList[0] = zip(
      sourceOne
    ).subscribe(([progressAllotment]) => {
        this.chartsOption.progressAllotment.series[0].data[0].value = progressAllotment["process"].percent;
        this.chartsOption.progressAllotment.title["text"] = progressAllotment["process"].percent + "%";
        this.chartsOption = _.cloneDeep(this.chartsOption);
        this.timeHandleList[0] = setTimeout(this.refreshDataOnce, 3 * 1000);
      },
      () => {
        this.timeHandleList[0] = setTimeout(this.refreshDataOnce, 3 * 1000);
      }
    );
  };

  ngOnDestroy(): void {
    this.subscriptionList.forEach((subscription) => subscription.unsubscribe());
    this.timeHandleList.forEach((timeHandle) => clearTimeout(timeHandle));
  }
}

4、结果展示
图1.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,607评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,239评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,960评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,750评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,764评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,604评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,347评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,253评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,702评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,893评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,015评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,734评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,352评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,934评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,052评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,216评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,969评论 2 355

推荐阅读更多精彩内容