ng2-charts

Beautiful charts for Angular based on Chart.js

  • 所有者: valor-software/ng2-charts
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Looking for new maintainer

I have very little time to devote to this important project, if you think you are qualified, send me a message that you would like to become a contributor. Also, feel free to submit PR's

ng2-charts npm version npm downloads Travis CI slack

Beautiful charts for Angular2 based on Chart.js

Library updated for Angular 7

NPM
NPM

Usage & Demo

Sample using ng2-charts@2.2.4

https://valor-software.com/ng2-charts/


Installation

  1. You can install ng2-charts using npm
npm install ng2-charts@2.2.4 --save
  1. You need to install and include Chart.js library in your application (it is a peer dependency of this library) (more info can be found in the official chart.js documentation)
npm install chart.js --save

Stackblitz Starting Templates

API

Import

import { ChartsModule } from 'ng2-charts';

// In your App's module:
imports: [
   ChartsModule
]

Chart types

There is one directive for all chart types: baseChart, and there are 8 types of charts: line, bar, radar, pie, polarArea, doughnut, bubble and scatter.

Properties

Note: For more information about possible options please refer to original chart.js documentation

  • data (SingleOrMultiDataSet) - set of points of the chart, it should be MultiDataSet only for line, bar, radar and doughnut, otherwise SingleDataSet
  • datasets ({ data: SingleDataSet, label: string }[]) - data see about, the label for the dataset which appears in the legend and tooltips
  • labels (Label[]) - x axis labels. It's necessary for charts: line, bar and radar. And just labels (on hover) for charts: polarArea, pie and doughnut. Label is either a single string, or it may be a string[] representing a multi-line label where each array element is on a new line.
  • chartType (ChartType) - indicates the type of charts, it can be: line, bar, radar, pie, polarArea, doughnut
  • options (ChartOptions) - chart options (as from Chart.js documentation)
  • colors (Color[]) - data colors, will use default and, or random colors if not specified (see below)
  • legend: (boolean = false) - if true show legend below the chart, otherwise not be shown

Events

  • chartClick: fires when click on a chart has occurred, returns information regarding active points and labels
  • chartHover: fires when mousemove (hover) on a chart has occurred, returns information regarding active points and labels

Colors

There are several sets of default colors. Colors can be replaced using the colors attribute. If there is more data than colors, colors are generated randomly.

Dynamic Theming

The ChartsModule provides a service called ThemeService which allows clients to set a structure specifying colors override settings. This service may be called when the dynamic theme changes, with colors which fit the theme. The structure is interpreted as an override, with special functionality when dealing with arrays. Example:

type Theme = 'light-theme', 'dark-theme';

private _selectedTheme: Theme = 'light-theme';
public get selectedTheme() {
  return this._selectedTheme;
}
public set selectedTheme(value) {
  this._selectedTheme = value;
  let overrides: ChartOptions;
  if (this.selectedTheme === 'dark-theme') {
    overrides = {
      legend: {
        labels: { fontColor: 'white' }
      },
      scales: {
        xAxes: [{
          ticks: { fontColor: 'white' },
          gridLines: { color: 'rgba(255,255,255,0.1)' }
        }],
        yAxes: [{
          ticks: { fontColor: 'white' },
          gridLines: { color: 'rgba(255,255,255,0.1)' }
        }]
      }
    };
  } else {
    overrides = {};
  }
  this.themeService.setColorschemesOptions(overrides);
}

constructor(private themeService: ThemeService) { }

setCurrentTheme(theme: Theme) {
  this.selectedTheme = theme;
}

The overrides object has the same type as the chart options object ChartOptions, and wherever a simple field is encountered it replaces the matching field in the options object. When an array is encountered (as in the xAxes and yAxes fields above), the single object inside the array is used as a template to override all array elements in the matching field in the options object. So in the case above, every axis will have its ticks and gridline colors changed.

Schematics

There are schematics that may be used to generate chart components using Angular CLI. The components are defined in package ng2-charts-schematics.

Installation of Schematics Package

npm install --save-dev ng2-charts-schematics

Example of Generating a Line Chart using Angular CLI

ng generate ng2-charts-schematics:line my-line-chart

This calls angular's component schematics and then modifies the result, so all the options for the component schematic are also usable here. This schematics will also add the ChartsModule as an imported module in the main app module (or another module as specified in the --module command switch).

Troubleshooting

Please follow this guidelines when reporting bugs and feature requests:

  1. Use GitHub Issues board to report bugs and feature requests (not our email address)
  2. Please always write steps to reproduce the error. That way we can focus on fixing the bug, not scratching our heads trying to reproduce it.

Thanks for understanding!

License

The MIT License (see the LICENSE file for the full text)

主要指标

概览
名称与所有者valor-software/ng2-charts
主编程语言TypeScript
编程语言TypeScript (语言数: 6)
平台
许可证MIT License
所有者活动
创建于2015-08-27 10:34:52
推送于2025-01-06 11:46:03
最后一次提交2025-01-06 11:46:02
发布数90
最新版本名称v8.0.0 (发布于 )
第一版名称v1.0.2 (发布于 2016-04-13 17:38:48)
用户参与
星数2.4k
关注者数67
派生数575
提交数547
已启用问题?
问题数888
打开的问题数66
拉请求数232
打开的拉请求数6
关闭的拉请求数887
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?