
<div class="container">
  <h2>Installation</h2>
  <markdown ngPreserveWhitespaces>
    1. Install ***ng2-charts*** using the schematics:

    ```bash
    ng add ng2-charts
    ```

  </markdown>

  <h2>API</h2>
  <markdown ngPreserveWhitespaces>
    ### Usage
    In order to use ***ng2-charts*** you need to import the directive in your standalone component:

    ```typescript
    import &#123; BaseChartDirective &#125; from 'ng2-charts';

    &#64;Component(&#123;
    standalone: true,
    imports: [BaseChartDirective],
    &#125;)
    export class MyComponent &#123; &#125;
    ```
    ### Global configuration
    You also need to provide a global configuration in your `app.config.ts` (or wherever that makes sense if you are lazy-loading things):

    ```typescript
    import &#123;
    provideCharts,
    withDefaultRegisterables,
    &#125; from 'ng2-charts';

    bootstrapApplication(AppComponent, &#123;
    providers: [
    provideCharts(withDefaultRegisterables()),
    ],
    &#125;).catch((err) => console.error(err));
    ```
    Alternatively, include a minimal configuration to reduce the bundle size, eg:

    ```typescript
    provideCharts(&#123; registerables: [BarController, Legend, Colors] &#125;)
    ```

    ### Chart types
    There are one directive for all chart types: `baseChart`, and there are 8 types of charts in the default
    chart.js package: [`line`](/ng2-charts/line), [`bar`](/ng2-charts/bar),
    [`radar`](/ng2-charts/radar), [`pie`](/ng2-charts/pie),
    [`polarArea`](/ng2-charts/polar-area), [`doughnut`](/ng2-charts/doughnut),
    [`bubble`](/ng2-charts/bubble) and [`scatter`](/ng2-charts/scatter).

    ### Properties

    **Note**: For more information about possible options please refer to original
    [chart.js](http://www.chartjs.org/docs) documentation

    - `data` - set of points of the chart. See https://www.chartjs.org/docs/latest/general/data-structures.html
    for some examples and further reference. Use this property or `datasets`/`labels` depending on what's
    convenient.
    - `datasets` - Same as the `datasets` property for the `data` input. `data` Has priority over this.
    - `labels` - Same as the `labels` property for the `data` input. `data` has priority over this.
    - `type` (`ChartType`) - indicates the type of charts. Defaults to `bar`.
    - `options` (`ChartOptions`) - chart options (as from [Chart.js
    documentation](http://www.chartjs.org/docs/))
    - `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

    ### Chart instance
    You can get access to the chart instance by using the `&#64;ViewChild` annotation and a suitable selector for
    the directive (see the Angular [docs](https://angular.io/api/core/ViewChild)). For example, to get the chart
    instance and call the `toBase64Image()` method, you can add the following to the parent component:

    ```typescript
    &#64;ViewChild(BaseChartDirective) chart?: BaseChartDirective;

    public someAction(): void {{ '{' }}
    this.chart?.toBase64Image();
    {{ '}' }}
    ```

    ### Colors

    There are a set several default colors. If there is more data than colors, more colors are generated
    randomly. Colors can be replaced using the dataset options or overriding the global defaults.

    ### 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, so in order to reset any existing option
    or customization you will have to define `undefined` properties explicitly. For example:

    ```typescript
    type Theme = 'light-theme' | 'dark-theme';

    private _selectedTheme: Theme = 'light-theme';
    public get selectedTheme() {{ '{' }}
    return this._selectedTheme;
    &#125;
    public set selectedTheme(value) {{ '{' }}
    this._selectedTheme = value;
    let overrides: ChartOptions;
    if (this.selectedTheme === 'dark-theme') {{ '{' }}
    overrides = {{ '{' }}
    scales: {{ '{' }}
    x: [{{ '{' }}
    ticks: {{ '{' }} fontColor: 'white' &#125;,
    gridLines: {{ '{' }} color: 'rgba(255,255,255,0.1)' &#125;
    &#125;],
    y: [{{ '{' }}
    ticks: {{ '{' }} fontColor: 'white' &#125;,
    gridLines: {{ '{' }} color: 'rgba(255,255,255,0.1)' &#125;
    &#125;]
    &#125;
    &#125;;
    &#125; else {{ '{' }}
    overrides = {{ '{' }}
    scales: undefined
    &#125;;
    &#125;

    this.themeService.setColorschemesOptions(overrides);
    &#125;

    constructor(private themeService: ThemeService) {{ '{' }} &#125;

    setCurrentTheme(theme: Theme) {{ '{' }}
    this.selectedTheme = theme;
    &#125;
    ```

    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.
  </markdown>
</div>
