ProductPromotion
Logo

Angular.JS

made by https://0x3d.site

GitHub - valor-software/ng2-charts: Beautiful charts for Angular based on Chart.js
Beautiful charts for Angular based on Chart.js. Contribute to valor-software/ng2-charts development by creating an account on GitHub.
Visit Site

GitHub - valor-software/ng2-charts: Beautiful charts for Angular based on Chart.js

GitHub - valor-software/ng2-charts: Beautiful charts for Angular based on Chart.js

ng2-charts npm version npm downloads Travis CI slack

Beautiful charts for Angular based on Chart.js

Usage & Demo

Samples using ng2-charts

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


Installation

You can install ng2-charts by using the Angular CLI:

ng add ng2-charts

The required packages will be automatically installed, and your app.config.ts will be updated with the required changes to start using the library right away.

Manual install through package managers

  1. You can install ng2-charts using npm:

    npm install ng2-charts --save
    

    or yarn:

    yarn add ng2-charts --save
    
  2. You will also 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
    

    or with yarn:

    yarn add  chart.js --save
    
  3. Import the directive in your standalone component:

    import { BaseChartDirective } from 'ng2-charts';
    
    @Component({
      standalone: true,
      imports: [BaseChartDirective],
    })
    export class MyComponent {}
    
  4. Provide a configuration, typically in your main.ts:

    import { provideCharts, withDefaultRegisterables } from 'ng2-charts';
    
    bootstrapApplication(AppComponent, {
      providers: [provideCharts(withDefaultRegisterables())],
    }).catch((err) => console.error(err));
    

    Alternatively, include a minimal configuration to reduce the bundle size, eg:

    provideCharts({ registerables: [BarController, Legend, Colors] });
    

    Or in your AppModule:

    import { provideCharts, withDefaultRegisterables } from 'ng2-charts';
    
    @NgModule({
      providers: [provideCharts(withDefaultRegisterables())],
      bootstrap: [AppComponent],
    })
    export class AppModule {}
    

Angular version compatibility table

API

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. You can use the directive on a canvas element as follows:

<canvas baseChart [data]="barChartData" [options]="barChartOptions" [type]="'bar'"> </canvas>

Properties

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

  • type: (ChartType) - indicates the type of chart, it can be: line, bar, radar, pie, polarArea, doughnut or any custom type added to Chart.js
  • data: (ChartData<TType, TData, TLabel>) - the whole data structure to be rendered in the chart. Support different flexible formats and parsing options, see here. In alternative, and depending on the type of your chart, you can use the labels and datasets properties to specify individual options.
  • labels: (TLabel[]) - Datasets labels. It's necessary for charts: line, bar and radar. And just labels (on hover) for charts: polarArea, pie and doughnut. Labels are matched in order with the datasets array.
  • datasets: ( ChartDataset<TType, TData>[]) - Same as the datasets property of the data input. See here for details.
  • options: (ChartOptions<TType>) - chart options (as per chart.js documentation).
  • legend: (boolean = false) - if true, chart legend is 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

By default, this library uses the colors as defined by Chart.js. If you need more control on colors, eg: generating colors on the fly, see the advanced color palettes.

Dynamic Theming

The ThemeService 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<AppChartMetaConfig>){
}

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.

Troubleshooting

Please follow this guidelines when reporting bugs and feature requests:

  1. Use GitHub Issues to report bugs and feature requests.
  2. Please always provide an example project and 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)

If you like this library and want to say thanks, consider buying me a coffee!

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory