g2-timeline 折线图 - 图1 This article has not been translated, hope that your can PR to translated it. Help us!g2-timeline 折线图 - 图2

g2-timeline 折线图

使用 timeline 组件可以实现带有时间轴的柱状图展现,而其中的 x 属性,则是时间值的指向,默认最多支持同时展现两个指标,分别是 y1y2

  1. import { G2TimelineModule } from '@delon/chart/timeline';

代码演示

g2-timeline 折线图 - 图3

基础

带有时间轴的图表。

  1. import { Component, OnInit } from '@angular/core';
  2. import { G2TimelineClickItem, G2TimelineData } from '@delon/chart/timeline';
  3. import { NzMessageService } from 'ng-zorro-antd/message';
  4. @Component({
  5. selector: 'chart-timeline-basic',
  6. template: ` <g2-timeline
  7. [data]="chartData"
  8. [titleMap]="{ y1: '客流量', y2: '支付笔数' }"
  9. [height]="200"
  10. (clickItem)="handleClick($event)"
  11. ></g2-timeline>`,
  12. })
  13. export class ChartTimelineBasicComponent implements OnInit {
  14. chartData: G2TimelineData[] = [];
  15. constructor(private msg: NzMessageService) {}
  16. ngOnInit(): void {
  17. for (let i = 0; i < 20; i += 1) {
  18. this.chartData.push({
  19. time: new Date().getTime() + 1000 * 60 * 30 * i,
  20. y1: Math.floor(Math.random() * 100) + 1000,
  21. y2: Math.floor(Math.random() * 100) + 10,
  22. });
  23. }
  24. }
  25. handleClick(data: G2TimelineClickItem): void {
  26. this.msg.info(`客流量: ${data.item.y1}, 支付笔数: ${data.item.y2}`);
  27. }
  28. }

g2-timeline 折线图 - 图4

多指标

利用 maxAxis 属性来调整多个指标,最多支持 5 个指标值。

  1. import { Component, OnInit } from '@angular/core';
  2. import { G2TimelineData, G2TimelineMap } from '@delon/chart/timeline';
  3. @Component({
  4. selector: 'chart-timeline-max-axis',
  5. template: ` <g2-timeline maxAxis="5" [data]="chartData" [titleMap]="titleMap" [height]="300"></g2-timeline>`,
  6. })
  7. export class ChartTimelineMaxAxisComponent implements OnInit {
  8. chartData: G2TimelineData[] = [];
  9. titleMap: G2TimelineMap = { y1: '指标1', y2: '指标2', y3: '指标3', y4: '指标4', y5: '指标5' };
  10. ngOnInit(): void {
  11. for (let i = 0; i < 20; i += 1) {
  12. this.chartData.push({
  13. time: new Date().getTime() + 1000 * 60 * 30 * i,
  14. y1: Math.floor(Math.random() * 100) + 500,
  15. y2: Math.floor(Math.random() * 100) + 1000,
  16. y3: Math.floor(Math.random() * 100) + 1500,
  17. y4: Math.floor(Math.random() * 100) + 2000,
  18. y5: Math.floor(Math.random() * 100) + 2500,
  19. });
  20. }
  21. }
  22. }

g2-timeline 折线图 - 图5

时间格式化

利用 maskmaskSlider 来改变时间格式。

  1. import { Component, OnInit } from '@angular/core';
  2. import { G2TimelineClickItem, G2TimelineData } from '@delon/chart/timeline';
  3. import { NzMessageService } from 'ng-zorro-antd/message';
  4. @Component({
  5. selector: 'chart-timeline-mask',
  6. template: ` <g2-timeline
  7. [data]="chartData"
  8. [titleMap]="{ y1: '客流量', y2: '支付笔数' }"
  9. [height]="200"
  10. mask="MM月DD日"
  11. maskSlider="MM月dd日"
  12. (clickItem)="handleClick($event)"
  13. ></g2-timeline>`,
  14. })
  15. export class ChartTimelineMaskComponent implements OnInit {
  16. chartData: G2TimelineData[] = [];
  17. constructor(private msg: NzMessageService) {}
  18. ngOnInit(): void {
  19. for (let i = 0; i < 20; i += 1) {
  20. this.chartData.push({
  21. time: new Date().getTime() + 1000 * 60 * 60 * 24 * i,
  22. y1: Math.floor(Math.random() * 100) + 1000,
  23. y2: Math.floor(Math.random() * 100) + 10,
  24. });
  25. }
  26. }
  27. handleClick(data: G2TimelineClickItem): void {
  28. this.msg.info(`客流量: ${data.item.y1}, 支付笔数: ${data.item.y2}`);
  29. }
  30. }

API

g2-timeline

参数说明类型默认值
[delay]延迟渲染,单位:毫秒number0
[title]图表标题string,TemplateRef<void>-
[maxAxis]最大指标数量number2
[data]数据G2TimelineData[]-
[titleMap]指标别名G2TimelineMap-
[colorMap]颜色G2TimelineMap{ y1: ‘#5B8FF9’, y2: ‘#5AD8A6’, y3: ‘#5D7092’, y4: ‘#F6BD16’, y5: ‘#E86452’ }
[height]高度值number400
[padding]图表内部间距number[][40, 8, 64, 40]
[borderWidth]线条number2
[mask]日期格式,使用 G2 Mask日期格式stringHH:mm
[maskSlider]滑动条日期格式,使用 date-fns 日期格式stringHH:mm
[position]标题位置‘top’,’right’,’bottom’,’left’‘top’
[slider]是否需要滑动条booleantrue
[theme]定制图表主题string | LooseObject-
(clickItem)点击项回调EventEmitter<G2TimelineClickItem>-

G2TimelineData

参数说明类型默认值
[time]日期格式Date | number-
[y1]指标1数据number-
[y2]指标2数据number-
[y3]指标3数据number-
[y4]指标4数据number-
[y5]指标5数据number-

G2TimelineMap

参数说明类型默认值
[y1]指标1string-
[y2]指标2string-
[y3]指标3string-
[y4]指标4string-
[y5]指标5string-