安装
看官方文档可以知道ECharts可以在webpack中使用看这里,故我们可以使用npm下载安装到项目中
npm install echarts --save
//下载ECharts
npm install @types/echarts --save
// ECharts的官方版本是基于JavaScript 的,下载ECharts的TypeScript定义文件
使用
1.在页面创建一个echart容器
<div #main1 id="main1" style="width: 100%;height: 280px"></div>
//或者
<div id="main1" style="width: 100%;height: 280px"></div>
2.在需要使用的页面的ts文件中引入echart并初始化
import ECharts from 'echarts';
或
import * as ECharts from "echarts";
- 使用传统的dom操作
let myChart = ECharts.init(document.getElementById('main2') as HTMLDivElement);
- 使用@ViewChild,建议使用此angular的dom操作
@ViewChild('main1') mychart1: ElementRef;
let myChart = ECharts.init(this.mychart1.nativeElement);
完整代码:
let myChart = ECharts.init(document.getElementById('main2') as HTMLDivElement);
option = {
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'
}]
};
myChart.setOption(option );
参考: