|
16 | 16 | return { |
17 | 17 | restrict: 'EA', |
18 | 18 | scope: { |
19 | | - options: '=' |
| 19 | + options: '=', |
| 20 | + instance: '=?' |
20 | 21 | }, |
21 | 22 | controller: 'AngularChartController' |
22 | 23 | }; |
|
467 | 468 | 'use strict'; |
468 | 469 |
|
469 | 470 | /* istanbul ignore next */ |
470 | | - AngularChartService.$inject = ['$timeout', 'AngularChartWatcher', 'AngularChartConverter', 'AngularChartState']; |
| 471 | + AngularChartService.$inject = ['$timeout', '$q', 'AngularChartWatcher', 'AngularChartConverter', 'AngularChartState']; |
471 | 472 | var angular = window.angular ? window.angular : 'undefined' !== typeof require ? require('angular') : undefined; |
472 | 473 | /* istanbul ignore next */ |
473 | 474 | var c3 = window.c3 ? window.c3 : 'undefined' !== typeof require ? require('c3') : undefined; |
474 | 475 |
|
475 | | - function AngularChartService($timeout, AngularChartWatcher, AngularChartConverter, AngularChartState) { |
| 476 | + function AngularChartService($timeout, $q, AngularChartWatcher, AngularChartConverter, AngularChartState) { |
476 | 477 |
|
477 | 478 | var ChartService = function(baseConfig, scope) { |
478 | | - this.chart = null; |
| 479 | + this.deferredChart = $q.defer(); |
| 480 | + this.chart = this.deferredChart.promise; |
479 | 481 | this.baseConfiguration = {}; |
480 | 482 | this.configuration = {}; |
481 | 483 | this.scopeReference = null; |
|
535 | 537 | this.convertOptions(); |
536 | 538 | this.applyChartOptions(); |
537 | 539 | this.synchronizeState(); |
538 | | - this.generateChart(); |
539 | | - this.stateCallback(); |
| 540 | + |
| 541 | + // call long running generation async |
| 542 | + var chartService = this; |
| 543 | + $timeout(function() { |
| 544 | + chartService.generateChart(chartService); |
| 545 | + chartService.stateCallback(); |
| 546 | + }); |
540 | 547 | }; |
541 | 548 |
|
542 | 549 | /** |
|
588 | 595 | /** |
589 | 596 | * Render the chart. |
590 | 597 | */ |
591 | | - ChartService.prototype.generateChart = function() { |
| 598 | + ChartService.prototype.generateChart = function(chartService) { |
592 | 599 | // TODO add own onresize listener? |
593 | 600 | // TODO regenerate chart only one or two times per second |
594 | 601 | // TODO evaluate if it makes sense to destroy the chart first |
595 | | - this.chart = c3.generate(this.configuration); |
| 602 | + |
| 603 | + var chart = c3.generate(chartService.configuration); |
| 604 | + chartService.deferredChart.resolve(chart); |
| 605 | + chartService.scopeReference.instance = chart; |
| 606 | + chartService.chart = chart; |
596 | 607 | }; |
597 | 608 |
|
598 | 609 | /** |
|
607 | 618 | * Destroy the chart if one ist present. |
608 | 619 | */ |
609 | 620 | ChartService.prototype.destroyChart = function() { |
610 | | - this.chart.destroy(); |
| 621 | + if (this.chart.destroy) { |
| 622 | + this.chart.destroy(); |
| 623 | + } |
611 | 624 | }; |
612 | 625 |
|
613 | 626 | ChartService.prototype.merge = angular.merge || deepMerge; |
|
851 | 864 | unwrapPromise(); |
852 | 865 | addIdentifier(); |
853 | 866 | addInlineStyle(); |
854 | | - chartService = AngularChartService.getInstance(configuration, $scope); |
| 867 | + getInstance(); |
855 | 868 | registerDestroyListener(); |
856 | 869 | } |
857 | 870 |
|
|
880 | 893 | angular.element($element).css('display', 'block'); |
881 | 894 | } |
882 | 895 |
|
| 896 | + function getInstance() { |
| 897 | + chartService = AngularChartService.getInstance(configuration, $scope); |
| 898 | + $scope.instance = chartService.chart; |
| 899 | + } |
| 900 | + |
883 | 901 | /** |
884 | 902 | * Remove all references when directive is destroyed |
885 | 903 | */ |
|
0 commit comments