注:
github:
https://github.com/xieziyu/ngx-echarts
官方网站:
https://xieziyu.github.io/ngx-echarts/#/home
echarts图表实例参考:
http://echarts.baidu.com/examples/
1.通过使用npm进行引入相关依赖
npm install echarts -S
npm install ngx-echarts -S
npm install @types/echarts -D
在angular.json中引入echarts.min.js
"scripts": [
....
"node_modules/echarts/dist/echarts.min.js"
]
如果需要ECharts GL,则需要引入以下依赖
npm install echarts-gl -S
2.在module中导入ngx-echarts的模块
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule} from '@angular/router';
import {NgxEchartsModule} from 'ngx-echarts';
import {EEchartComponent} from './e-echart.component';
@NgModule({
declarations: [EEchartComponent],
imports: [
CommonModule,
NgxEchartsModule,
RouterModule.forChild([{
path: '', component: EEchartComponent,
children: [
{path: '', redirectTo: '', pathMatch: 'full'}
]
}])
]
})
export class EEchartModule {
}
3.在html中编写echart图表代码
<div echarts [options]="chartOption" style="height: 400px" class="demo-chart"></div>
<div echarts [options]="option" style="height: 400px" class="demo-chart"></div>
4.在component中配置echart的options(配置信息)
import {Component, OnInit} from '@angular/core';
import {EChartOption} from 'echarts';
@Component({
selector: 'app-e-echart',
templateUrl: './e-echart.component.html',
styleUrls: ['./e-echart.component.scss']
})
export class EEchartComponent implements OnInit {
// 这些配置文件可以直接从echart中的官网去拉取,该组件完全支持echart的所有图形
public chartOption: EChartOption;
public option: EChartOption;
constructor() {
this.chartOption = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
}]
};
this.option = {
tooltip: {
trigger: 'axis',
axisPointer: { // 坐标轴指示器,坐标轴触发有效
type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
}
},
legend: {
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
},
series: [
{
name: '直接访问',
type: 'bar',
stack: '总量',
label: {
normal: {
show: true,
position: 'insideRight'
}
},
data: [320, 302, 301, 334, 390, 330, 320]
},
{
name: '邮件营销',
type: 'bar',
stack: '总量',
label: {
normal: {
show: true,
position: 'insideRight'
}
},
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: '联盟广告',
type: 'bar',
stack: '总量',
label: {
normal: {
show: true,
position: 'insideRight'
}
},
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: '视频广告',
type: 'bar',
stack: '总量',
label: {
normal: {
show: true,
position: 'insideRight'
}
},
data: [150, 212, 201, 154, 190, 330, 410]
},
{
name: '搜索引擎',
type: 'bar',
stack: '总量',
label: {
normal: {
show: true,
position: 'insideRight'
}
},
data: [820, 832, 901, 934, 1290, 1330, 1320]
}
]
};
}
ngOnInit() {
}
}
注:代码中的options都是参考echarts图表实例参考
,组件ngx-echarts是完全支持echarts的图表的,下图中有的都可以在ngx-echarts中使用
最终效果图