当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > 在 Angular 中使用Chart.js 和 ng2-charts的示例代码

在 Angular 中使用Chart.js 和 ng2-charts的示例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

淘宝减肥药 妙瘦,金钱豹子汤隆,叶倩彤下载

chart.js是一个流行的javascript图表库,ng2图表是angular 2+的包装器,可以轻松地将chart.js集成到angular中。 我们来看看基本用法。

安装

首先,在项目中安装 chart.js 和 ng2-charts:

# yarn:
$ yarn add ng2-charts chart.js

# or npm 
$ npm install ng2-charts charts.js --save

当然 ,如果你是使用angular cli构建的项目,你也可以很容易的添加chart.js 添加.angular-cli.json配置文件中,以便将它与应用绑定在一直:

//: .angular-cli.json (partial)
"script": [
 "../node_modules/chart.js/dist/chart.min.js"
]

现在,你需要在你的 app 模块或功能模块导入 ng2-charts 的chartsmodule:

//: app.module.ts

import { browsermodule } from '@angular/platform-browser';
import { ngmodule } from '@angular/core';
import { chartsmodule } from '@angular/charts';

import { appcomponent } from './app.component';

@ngmodule({
 declarations: [ appcomponent ],
 imports: [
  browsermodule,
  chartsmodule
 ],
 providers: [],
 bootstrap: [ appcomponent ]
})
export class appmodule {}

使用

ng2-charts 给我们提供了一个可以应用于html canvas元素的basechart指令。 以下是一个示例,其中显示了一些用于输入的选项以及该指令输出的chartclick事件:

//: app.component.html

<div style="width: 40%;">
 <canvas
   basechart
   [charttype]="'line'"
   [datasets]="chartdata"
   [labels]="chartlabels"
   [options]="chartoptions"
   [legend]="true"
   (chartclick)="onchartclick($event)">
 </canvas>
</div>

这就是组件类现在的样子:

//: app.component.ts

import { component } from '@angular/core';

@component({ ... })
export class appcomponent {
 chartoptions = {
  responsive: true
 };

 chartdata = [
  { data: [330, 600, 260, 700], label: 'account a' },
  { data: [120, 455, 100, 340], label: 'account b' },
  { data: [45, 67, 800, 500], label: 'account c' }
 ];

 chartlabels = ['january', 'february', 'mars', 'april'];

 onchartclick(event) {
  console.log(event);
 }
}

选项

以下就是不同的可选输入项:

charttype

设置图表的基本类型, 值可以是pipe,doughnut,bar,line,polararea,radar或horizontalbar。

legend

一个布尔值,用于是否在图表上方显示图例。

datasets

包含数据数组和每个数据集标签的对象数组。

data

如果你的图表很简单,只有一个数据集,你可以使用data而不是datasets。

labels

x轴的标签集合

options

包含图表选项的对象。 有关可用选项的详细信息,请参阅官方chart.js文档。

在上面的例子中,我们将图表设置为自适应模式,根据视口大小进行自动调整。

colors

在上面的例子中未显示,但你可以定义自己的颜色, 传入包含以下值的对象文字数组:

mycolors = [
 {
  backgroundcolor: 'rgba(103, 58, 183, .1)',
  bordercolor: 'rgb(103, 58, 183)',
  pointbackgroundcolor: 'rgb(103, 58, 183)',
  pointbordercolor: '#fff',
  pointhoverbackgroundcolor: '#fff',
  pointhoverbordercolor: 'rgba(103, 58, 183, .8)'
 },
 // ...colors for additional data sets
];

使用自定义颜色时,必须为每个数据集提供一个颜色对象字面量。

事件

发出两个事件,chartclick和charthover,它们允许对与图表交互的用户做出反应。 当前活动点和标签作为发射事件数据的一部分返回。

动态更新数据集

当然,chart.js的优点在于,您的图表可以轻松地通过动态更新/响应从后端或用户输入的数据。

下面这个示例中,我们为5月份添加了一个新的数据集合:

//: app.component.ts(partial)

newdatapoint(dataarr = [100, 100, 100], label) {

 this.chartdata.foreach((dataset, index) => {
  this.chartdata[index] = object.assign({}, this.chartdata[index], {
   data: [...this.chartdata[index].data, dataarr[index]]
  });
 });

 this.chartlabels = [...this.chartlabels, label];

}

它可以像这样使用:

//: app.component.html(partial)

<button (click)="newdatapoint([900, 50, 300], 'may')">
 add data point
</button>

你可能注意到了,我们不会对图表的数据集进行改动,而是使用新数据返回包含先前数据的新对象。 object.assign可以很容易地做到这一点。

在这个特定的例子中,如果没有提供参数时,我们为3个数据集设定了默认值为100。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网