最近突发奇想,想自己做一个理财分析记录的网页,主要给自己记账,然后想到了把每天,每周的消费用图标分析出来,于是就需要用到百度的echarts图标插件,talk is cheep, show me the code。
1. npm 安装ngx-echarts及相关的依赖
npm install echarts -S
npm install ngx-echarts -S
npm install @types/echarts -D
2. 添加js引导文件
{
"scripts": [
"../node_modules/echarts/dist/echarts.min.js" // or echarts.js for debug purpose
],
}
3. AppModule引入NgxEchartsModule
这里有个坑,记得在你调用了echarts的相关模块的Module里面引入NgxEchartsModule,不然会报错:Template parse errors: Can't bind to 'options' since it isn't a known property of 'div'.所以切记
import { NgxEchartsModule } from 'ngx-echarts';
@NgModule({
imports: [
...,
NgxEchartsModule
],
...
})
export class AppModule { }
4. 使用echarts
html
<div echarts [options]="chartOption" class="demo-chart"></div>
scss
.demo-chart {
height: 400px;
}
component
import { EChartOption } from 'echarts';
chartOption: EChartOption = {
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'
}]
}