ng-chartist
Version:
Chartist component for Angular
1 lines • 7.78 kB
Source Map (JSON)
{"version":3,"file":"ng-chartist.mjs","sources":["../../../projects/ng-chartist/src/lib/chartist.component.ts","../../../projects/ng-chartist/src/lib/chartist.module.ts","../../../projects/ng-chartist/src/public-api.ts","../../../projects/ng-chartist/src/ng-chartist.ts"],"sourcesContent":["import {\n Component,\n ElementRef,\n EventEmitter,\n Input,\n OnChanges,\n OnDestroy,\n OnInit,\n Output,\n SimpleChanges,\n inject,\n} from \"@angular/core\";\nimport {\n BarChart,\n BarChartData,\n BarChartOptions,\n LineChart,\n LineChartData,\n LineChartOptions,\n PieChart,\n PieChartData,\n PieChartOptions,\n ResponsiveOptions,\n} from \"chartist\";\n\ntype ChartTypes = BarChart | LineChart | PieChart;\n\nexport interface BarChartConfiguration {\n type: \"Bar\";\n data: BarChartData;\n options?: BarChartOptions;\n responsiveOptions?: ResponsiveOptions<BarChartOptions>;\n}\n\nexport interface LineChartConfiguration {\n type: \"Line\";\n data: LineChartData;\n options?: LineChartOptions;\n responsiveOptions?: ResponsiveOptions<LineChartOptions>;\n}\n\nexport interface PieChartConfiguration {\n type: \"Pie\";\n data: PieChartData;\n options?: PieChartOptions;\n responsiveOptions?: ResponsiveOptions<PieChartOptions>;\n}\n\nexport type Configuration =\n | BarChartConfiguration\n | LineChartConfiguration\n | PieChartConfiguration;\n\n/**\n * Represents chart events.\n */\nexport interface ChartEvent {\n [eventName: string]: (data: unknown) => void;\n}\n\n/**\n * Angular component which renders Chartist chart.\n *\n * See Chartist {@link https://gionkunz.github.io/chartist-js/api-documentation.html API documentation} and\n * {@link https://gionkunz.github.io/chartist-js/examples.html examples} for more information.\n * ### Example\n ```html\n <x-chartist\n [configuration]=\"configuration\"\n [events]=\"events\"\n ></x-chartist>\n ```\n */\n@Component({\n selector: \"x-chartist\",\n template: \"\",\n styles: [\n `\n :host {\n display: block;\n }\n `,\n ],\n})\nexport class ChartistComponent implements OnInit, OnChanges, OnDestroy {\n private elementRef = inject(ElementRef);\n\n @Input()\n configuration: Configuration;\n\n /**\n * Events object where keys are Chartist event names and values are event handler functions.\n *\n * Supported events are: draw, optionsChanged, data, animationBegin, animationEnd, created.\n *\n * Event handler function will receive a data argument which contains event data.\n */\n @Input()\n events: ChartEvent;\n\n /**\n * Event emitted after Chartist chart has been initialized.\n *\n * Event handler function will receive chart instance argument.\n */\n @Output()\n initialized = new EventEmitter<ChartTypes>();\n\n chart: ChartTypes;\n\n ngOnInit(): void {\n if (this.configuration.type && this.configuration.data) {\n this.renderChart();\n }\n }\n\n ngOnChanges(changes: SimpleChanges): void {\n this.update(changes);\n }\n\n ngOnDestroy(): void {\n if (this.chart) {\n this.chart.detach();\n this.chart = null;\n }\n }\n\n renderChart() {\n const nativeElement = this.elementRef.nativeElement;\n const { type, data, options, responsiveOptions } = this.configuration;\n\n if (type === \"Bar\") {\n this.chart = new BarChart(\n nativeElement,\n data,\n options,\n responsiveOptions,\n );\n } else if (type === \"Line\") {\n this.chart = new LineChart(\n nativeElement,\n data,\n options,\n responsiveOptions,\n );\n } else if (type === \"Pie\") {\n this.chart = new PieChart(\n nativeElement,\n data,\n options,\n responsiveOptions,\n );\n } else {\n throw new Error(`${type} is not a known chart type`);\n }\n\n if (this.events) {\n this.bindEvents();\n }\n\n this.initialized.emit(this.chart);\n }\n\n update(changes: SimpleChanges): void {\n const { type, data, options } = this.configuration;\n\n if (!type || !data) {\n return;\n }\n\n const changedConfiguration = changes.configuration\n .currentValue as Configuration;\n\n if (!this.chart || changedConfiguration.type !== type) {\n this.renderChart();\n } else if (\n \"data\" in changedConfiguration ||\n \"options\" in changedConfiguration\n ) {\n this.chart.update(data, options);\n }\n }\n\n bindEvents(): void {\n for (const event of Object.keys(this.events)) {\n this.chart.on(event, this.events[event]);\n }\n }\n}\n","import { NgModule } from \"@angular/core\";\nimport { ChartistComponent } from \"./chartist.component\";\n\n@NgModule({\n imports: [ChartistComponent],\n exports: [ChartistComponent],\n})\nexport class ChartistModule {}\n","/*\n * Public API Surface of ng-chartist-lib\n */\n\nexport * from \"./lib/chartist.component\";\nexport * from \"./lib/chartist.module\";\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AA4DA;;;;;;;;;;;;AAYG;MAYU,iBAAiB,CAAA;AACpB,IAAA,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;AAGvC,IAAA,aAAa;AAEb;;;;;;AAMG;AAEH,IAAA,MAAM;AAEN;;;;AAIG;AAEH,IAAA,WAAW,GAAG,IAAI,YAAY,EAAc;AAE5C,IAAA,KAAK;IAEL,QAAQ,GAAA;AACN,QAAA,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE;YACtD,IAAI,CAAC,WAAW,EAAE;QACpB;IACF;AAEA,IAAA,WAAW,CAAC,OAAsB,EAAA;AAChC,QAAA,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;IACtB;IAEA,WAAW,GAAA;AACT,QAAA,IAAI,IAAI,CAAC,KAAK,EAAE;AACd,YAAA,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;AACnB,YAAA,IAAI,CAAC,KAAK,GAAG,IAAI;QACnB;IACF;IAEA,WAAW,GAAA;AACT,QAAA,MAAM,aAAa,GAAG,IAAI,CAAC,UAAU,CAAC,aAAa;AACnD,QAAA,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC,aAAa;AAErE,QAAA,IAAI,IAAI,KAAK,KAAK,EAAE;AAClB,YAAA,IAAI,CAAC,KAAK,GAAG,IAAI,QAAQ,CACvB,aAAa,EACb,IAAI,EACJ,OAAO,EACP,iBAAiB,CAClB;QACH;AAAO,aAAA,IAAI,IAAI,KAAK,MAAM,EAAE;AAC1B,YAAA,IAAI,CAAC,KAAK,GAAG,IAAI,SAAS,CACxB,aAAa,EACb,IAAI,EACJ,OAAO,EACP,iBAAiB,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