chart.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var Chart = require("chart.js");
  15. var UIChart = /** @class */ (function () {
  16. function UIChart(el) {
  17. this.el = el;
  18. this.options = {};
  19. this.plugins = [];
  20. this.responsive = true;
  21. this.onDataSelect = new core_1.EventEmitter();
  22. }
  23. Object.defineProperty(UIChart.prototype, "data", {
  24. get: function () {
  25. return this._data;
  26. },
  27. set: function (val) {
  28. this._data = val;
  29. this.reinit();
  30. },
  31. enumerable: true,
  32. configurable: true
  33. });
  34. UIChart.prototype.ngAfterViewInit = function () {
  35. this.initChart();
  36. this.initialized = true;
  37. };
  38. UIChart.prototype.onCanvasClick = function (event) {
  39. if (this.chart) {
  40. var element = this.chart.getElementAtEvent(event);
  41. var dataset = this.chart.getDatasetAtEvent(event);
  42. if (element && element[0] && dataset) {
  43. this.onDataSelect.emit({ originalEvent: event, element: element[0], dataset: dataset });
  44. }
  45. }
  46. };
  47. UIChart.prototype.initChart = function () {
  48. var opts = this.options || {};
  49. opts.responsive = this.responsive;
  50. // allows chart to resize in responsive mode
  51. if (opts.responsive && (this.height || this.width)) {
  52. opts.maintainAspectRatio = false;
  53. }
  54. this.chart = new Chart(this.el.nativeElement.children[0].children[0], {
  55. type: this.type,
  56. data: this.data,
  57. options: this.options,
  58. plugins: this.plugins
  59. });
  60. };
  61. UIChart.prototype.getCanvas = function () {
  62. return this.el.nativeElement.children[0].children[0];
  63. };
  64. UIChart.prototype.getBase64Image = function () {
  65. return this.chart.toBase64Image();
  66. };
  67. UIChart.prototype.generateLegend = function () {
  68. if (this.chart) {
  69. return this.chart.generateLegend();
  70. }
  71. };
  72. UIChart.prototype.refresh = function () {
  73. if (this.chart) {
  74. this.chart.update();
  75. }
  76. };
  77. UIChart.prototype.reinit = function () {
  78. if (this.chart) {
  79. this.chart.destroy();
  80. this.initChart();
  81. }
  82. };
  83. UIChart.prototype.ngOnDestroy = function () {
  84. if (this.chart) {
  85. this.chart.destroy();
  86. this.initialized = false;
  87. this.chart = null;
  88. }
  89. };
  90. __decorate([
  91. core_1.Input(),
  92. __metadata("design:type", String)
  93. ], UIChart.prototype, "type", void 0);
  94. __decorate([
  95. core_1.Input(),
  96. __metadata("design:type", Object)
  97. ], UIChart.prototype, "options", void 0);
  98. __decorate([
  99. core_1.Input(),
  100. __metadata("design:type", Array)
  101. ], UIChart.prototype, "plugins", void 0);
  102. __decorate([
  103. core_1.Input(),
  104. __metadata("design:type", String)
  105. ], UIChart.prototype, "width", void 0);
  106. __decorate([
  107. core_1.Input(),
  108. __metadata("design:type", String)
  109. ], UIChart.prototype, "height", void 0);
  110. __decorate([
  111. core_1.Input(),
  112. __metadata("design:type", Boolean)
  113. ], UIChart.prototype, "responsive", void 0);
  114. __decorate([
  115. core_1.Output(),
  116. __metadata("design:type", core_1.EventEmitter)
  117. ], UIChart.prototype, "onDataSelect", void 0);
  118. __decorate([
  119. core_1.Input(),
  120. __metadata("design:type", Object),
  121. __metadata("design:paramtypes", [Object])
  122. ], UIChart.prototype, "data", null);
  123. UIChart = __decorate([
  124. core_1.Component({
  125. selector: 'p-chart',
  126. template: "\n <div style=\"position:relative\" [style.width]=\"responsive && !width ? null : width\" [style.height]=\"responsive && !height ? null : height\">\n <canvas [attr.width]=\"responsive && !width ? null : width\" [attr.height]=\"responsive && !height ? null : height\" (click)=\"onCanvasClick($event)\"></canvas>\n </div>\n "
  127. }),
  128. __metadata("design:paramtypes", [core_1.ElementRef])
  129. ], UIChart);
  130. return UIChart;
  131. }());
  132. exports.UIChart = UIChart;
  133. var ChartModule = /** @class */ (function () {
  134. function ChartModule() {
  135. }
  136. ChartModule = __decorate([
  137. core_1.NgModule({
  138. imports: [common_1.CommonModule],
  139. exports: [UIChart],
  140. declarations: [UIChart]
  141. })
  142. ], ChartModule);
  143. return ChartModule;
  144. }());
  145. exports.ChartModule = ChartModule;
  146. //# sourceMappingURL=chart.js.map