UNPKG

ng2-charts

Version:

Reactive, responsive, beautiful charts for Angular based on Chart.js

1 lines 14.7 kB
{"version":3,"file":"ng2-charts.mjs","sources":["../../../../libs/ng2-charts/src/lib/ng-charts.provider.ts","../../../../libs/ng2-charts/src/lib/theme.service.ts","../../../../libs/ng2-charts/src/lib/base-chart.directive.ts","../../../../libs/ng2-charts/src/index.ts","../../../../libs/ng2-charts/src/ng2-charts.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\nimport {\n ChartComponentLike,\n Defaults,\n registerables as defaultRegisterables,\n} from 'chart.js';\nimport { DeepPartial } from 'chart.js/dist/types/utils';\nimport { merge } from 'lodash-es';\n\nexport const NG_CHARTS_CONFIGURATION =\n new InjectionToken<NgChartsConfiguration>('Configuration for ngCharts');\n\nexport type NgChartsConfiguration = {\n /**\n * Any registerable that can be used with `Chart.register()`, such as plugins, controllers, scales, and elements.\n */\n registerables?: readonly ChartComponentLike[];\n\n /**\n * Default configuration that can be used with `defaults.set()`.\n */\n defaults?: DeepPartial<Defaults>;\n};\n\n/**\n * Provide all the default registerable as defined by Chart.js\n */\nexport function withDefaultRegisterables(\n ...registerables: ChartComponentLike[]\n): NgChartsConfiguration {\n return { registerables: [...defaultRegisterables, ...registerables] };\n}\n\n/**\n * Provide configuration for ngCharts. In most cases, you have to pass it some registerables. So either\n * `withDefaultRegisterables()`, or a custom list of registerables tailored to your needs to reduce bundle size.\n */\nexport function provideCharts(\n ...configurations: readonly NgChartsConfiguration[]\n) {\n const config: NgChartsConfiguration = merge({}, ...configurations);\n return { provide: NG_CHARTS_CONFIGURATION, useValue: config };\n}\n","import { Injectable } from '@angular/core';\nimport { BehaviorSubject } from 'rxjs';\nimport { ChartConfiguration, ChartOptions } from 'chart.js';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class ThemeService {\n private pColorschemesOptions?: ChartOptions;\n public colorschemesOptions: BehaviorSubject<ChartOptions | undefined> =\n new BehaviorSubject<ChartOptions | undefined>(undefined);\n\n setColorschemesOptions(options: ChartConfiguration['options']): void {\n this.pColorschemesOptions = options;\n this.colorschemesOptions.next(options);\n }\n\n getColorschemesOptions(): ChartConfiguration['options'] {\n return this.pColorschemesOptions;\n }\n}\n","import {\n Directive,\n ElementRef,\n EventEmitter,\n Inject,\n Input,\n NgZone,\n OnChanges,\n OnDestroy,\n Optional,\n Output,\n SimpleChanges,\n} from '@angular/core';\nimport {\n Chart,\n ChartConfiguration,\n ChartEvent,\n ChartType,\n DefaultDataPoint,\n defaults,\n Plugin,\n UpdateMode,\n} from 'chart.js';\nimport { ThemeService } from './theme.service';\nimport { Subscription } from 'rxjs';\nimport { distinctUntilChanged } from 'rxjs/operators';\nimport { merge } from 'lodash-es';\nimport {\n NG_CHARTS_CONFIGURATION,\n NgChartsConfiguration,\n} from './ng-charts.provider';\n\n@Directive({\n // eslint-disable-next-line @angular-eslint/directive-selector\n selector: 'canvas[baseChart]',\n exportAs: 'base-chart',\n standalone: true,\n})\nexport class BaseChartDirective<\n TType extends ChartType = ChartType,\n TData = DefaultDataPoint<TType>,\n TLabel = unknown,\n >\n implements OnDestroy, OnChanges\n{\n @Input() public type: ChartConfiguration<TType, TData, TLabel>['type'] =\n 'bar' as TType;\n @Input() public legend?: boolean;\n @Input() public data?: ChartConfiguration<TType, TData, TLabel>['data'];\n @Input() public options: ChartConfiguration<TType, TData, TLabel>['options'];\n @Input() public plugins: Plugin<TType>[] = [];\n\n @Input() public labels?: ChartConfiguration<\n TType,\n TData,\n TLabel\n >['data']['labels'];\n @Input() public datasets?: ChartConfiguration<\n TType,\n TData,\n TLabel\n >['data']['datasets'];\n\n @Output() public chartClick: EventEmitter<{\n event?: ChartEvent;\n active?: object[];\n }> = new EventEmitter();\n @Output() public chartHover: EventEmitter<{\n event: ChartEvent;\n active: object[];\n }> = new EventEmitter();\n\n public ctx: string;\n public chart?: Chart<TType, TData, TLabel>;\n\n private subs: Subscription[] = [];\n private themeOverrides: ChartConfiguration['options'] = {};\n\n public constructor(\n element: ElementRef,\n private zone: NgZone,\n private themeService: ThemeService,\n @Optional() @Inject(NG_CHARTS_CONFIGURATION) config?: NgChartsConfiguration,\n ) {\n if (config?.registerables) {\n Chart.register(...config.registerables);\n }\n\n if (config?.defaults) {\n defaults.set(config.defaults);\n }\n\n this.ctx = element.nativeElement.getContext('2d');\n this.subs.push(\n this.themeService.colorschemesOptions\n .pipe(distinctUntilChanged())\n .subscribe((r) => this.themeChanged(r)),\n );\n }\n\n ngOnChanges(changes: SimpleChanges): void {\n const requireRender = ['type'];\n const propertyNames = Object.getOwnPropertyNames(changes);\n\n if (\n propertyNames.some((key) => requireRender.includes(key)) ||\n propertyNames.every((key) => changes[key].isFirstChange())\n ) {\n this.render();\n } else {\n const config = this.getChartConfiguration();\n\n // Using assign to avoid changing the original object reference\n if (this.chart) {\n Object.assign(this.chart.config.data, config.data);\n if (this.chart.config.plugins) {\n Object.assign(this.chart.config.plugins, config.plugins);\n }\n if (this.chart.config.options) {\n Object.assign(this.chart.config.options, config.options);\n }\n }\n\n this.update();\n }\n }\n\n public ngOnDestroy(): void {\n if (this.chart) {\n this.chart.destroy();\n this.chart = void 0;\n }\n this.subs.forEach((s) => s.unsubscribe());\n }\n\n public render(): Chart<TType, TData, TLabel> {\n if (this.chart) {\n this.chart.destroy();\n }\n\n return this.zone.runOutsideAngular(\n () => (this.chart = new Chart(this.ctx, this.getChartConfiguration())),\n );\n }\n\n public update(mode?: UpdateMode): void {\n if (this.chart) {\n this.zone.runOutsideAngular(() => this.chart?.update(mode));\n }\n }\n\n public hideDataset(index: number, hidden: boolean): void {\n if (this.chart) {\n this.chart.getDatasetMeta(index).hidden = hidden;\n this.update();\n }\n }\n\n public isDatasetHidden(index: number): boolean | undefined {\n return this.chart?.getDatasetMeta(index)?.hidden;\n }\n\n public toBase64Image(): string | undefined {\n return this.chart?.toBase64Image();\n }\n\n private themeChanged(options: ChartConfiguration['options']): void {\n this.themeOverrides = options;\n if (this.chart) {\n if (this.chart.config.options) {\n Object.assign(this.chart.config.options, this.getChartOptions());\n }\n\n this.update();\n }\n }\n\n private getChartOptions(): ChartConfiguration<\n TType,\n TData,\n TLabel\n >['options'] {\n return merge(\n {\n onHover: (event: ChartEvent, active: object[]) => {\n if (!this.chartHover.observed && !this.chartHover.observers?.length) {\n return;\n }\n\n this.zone.run(() => this.chartHover.emit({ event, active }));\n },\n onClick: (event?: ChartEvent, active?: object[]) => {\n if (!this.chartClick.observed && !this.chartClick.observers?.length) {\n return;\n }\n\n this.zone.run(() => this.chartClick.emit({ event, active }));\n },\n },\n this.themeOverrides,\n this.options,\n {\n plugins: {\n legend: {\n display: this.legend,\n },\n },\n },\n );\n }\n\n private getChartConfiguration(): ChartConfiguration<TType, TData, TLabel> {\n return {\n type: this.type,\n data: this.getChartData(),\n options: this.getChartOptions(),\n plugins: this.plugins,\n };\n }\n\n private getChartData(): ChartConfiguration<TType, TData, TLabel>['data'] {\n return this.data\n ? this.data\n : {\n labels: this.labels || [],\n datasets: this.datasets || [],\n };\n }\n}\n","/*\n * Public API Surface of ng2-charts\n */\n\nexport * from './lib/ng-charts.provider';\nexport * from './lib/base-chart.directive';\nexport * from './lib/theme.service';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":["registerables","defaultRegisterables"],"mappings":";;;;;;;MASa,uBAAuB,GAClC,IAAI,cAAc,CAAwB,4BAA4B;AAcxE;;AAEG;AACa,SAAA,wBAAwB,CACtC,GAAGA,eAAmC,EAAA;IAEtC,OAAO,EAAE,aAAa,EAAE,CAAC,GAAGC,aAAoB,EAAE,GAAGD,eAAa,CAAC,EAAE;AACvE;AAEA;;;AAGG;AACa,SAAA,aAAa,CAC3B,GAAG,cAAgD,EAAA;IAEnD,MAAM,MAAM,GAA0B,KAAK,CAAC,EAAE,EAAE,GAAG,cAAc,CAAC;IAClE,OAAO,EAAE,OAAO,EAAE,uBAAuB,EAAE,QAAQ,EAAE,MAAM,EAAE;AAC/D;;MCnCa,YAAY,CAAA;AAHzB,IAAA,WAAA,GA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