使用ngx-charts库代替ng2-charts,并在组件中设置chartOptions中的responsive属性为true,以使图表成为响应式的。
代码示例:
npm install @swimlane/ngx-charts --save
import { NgModule } from '@angular/core';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { Component } from '@angular/core';
import { single } from './data'; // 导入图表数据
@Component({
selector: 'app-root',
template: ' ',
})
export class AppComponent {
data = single; // 设置图表数据
colorScheme = 'vivid'; // 设置颜色方案
showLegend = true; // 显示图例
showXAxis = true; // 显示X轴
showYAxis = true; // 显示Y轴
xAxisLabel = 'Country'; // 设置X轴标签
yAxisLabel = 'Population'; // 设置Y轴标签
curve = shape.curveBasis; // 设置线段样式
tooltipDisabled = false; // 禁用tooltip
gradient = false; // 禁用渐变色
animations = true; // 开启动画
}
以上代码将创建一个包含线