chart.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').directive('ngChart', ['$window', 'chartTheme', function ($window, chartTheme) {
  4. return {
  5. restrict: 'E',
  6. template: '<div></div>',
  7. scope: {
  8. options: '=ngData',
  9. theme: '=ngTheme'
  10. },
  11. link: function (scope, element, attrs) {
  12. var options = {};
  13. angular.extend(options, attrs);
  14. var wrapper = element.find('div');
  15. var wrapperParent = element.parent();
  16. var parentHeight = wrapperParent.height();
  17. var height = parseInt(attrs.height) || parentHeight || 200;
  18. wrapper.css('height', height + 'px');
  19. var chart = echarts.init(wrapper[0], chartTheme.get(scope.theme));
  20. var setOptions = function (value) {
  21. chart.setOption(value);
  22. };
  23. angular.element($window).on('resize', function () {
  24. chart.resize();
  25. scope.$apply();
  26. });
  27. scope.$watch('options', function (value) {
  28. if (value) {
  29. setOptions(value);
  30. }
  31. }, true);
  32. scope.$on('$destroy', function() {
  33. if (chart && !chart.isDisposed()) {
  34. chart.dispose();
  35. }
  36. });
  37. }
  38. };
  39. }]).directive('ngPopChart', ['$window', 'chartTheme', function ($window, chartTheme) {
  40. return {
  41. restrict: 'A',
  42. scope: {
  43. options: '=ngData',
  44. theme: '=ngTheme'
  45. },
  46. link: function (scope, element, attrs) {
  47. var options = {
  48. ngPopoverClass: '',
  49. ngContainer: 'body',
  50. ngTrigger: 'click',
  51. ngPlacement: 'top'
  52. };
  53. angular.extend(options, attrs);
  54. var chart = null;
  55. var loadingIcon = '<div class="loading"><i class="fa fa-spinner fa-spin fa-2x"></i></div>';
  56. element.popover({
  57. container: options.ngContainer,
  58. content: '<div class="chart-pop-wrapper"><div class="chart-pop ' + options.ngPopoverClass + '">' + loadingIcon +'</div></div>',
  59. html: true,
  60. placement: options.ngPlacement,
  61. template: '<div class="popover chart-popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>',
  62. trigger: options.ngTrigger
  63. }).on('shown.bs.popover', function () {
  64. var wrapper = angular.element('.chart-pop');
  65. var wrapperParent = wrapper.parent();
  66. var parentHeight = wrapperParent.height();
  67. wrapper.empty();
  68. var height = parseInt(attrs.height) || parentHeight || 200;
  69. wrapper.css('height', height + 'px');
  70. chart = echarts.init(wrapper[0], chartTheme.get(scope.theme));
  71. }).on('hide.bs.popover', function () {
  72. if (chart && !chart.isDisposed()) {
  73. chart.dispose();
  74. }
  75. }).on('hidden.bs.popover', function () {
  76. angular.element('.chart-pop').empty().append(loadingIcon);
  77. });
  78. var setOptions = function (value) {
  79. if (chart && !chart.isDisposed()) {
  80. chart.setOption(value);
  81. }
  82. };
  83. scope.$watch('options', function (value) {
  84. if (value) {
  85. setOptions(value);
  86. }
  87. }, true);
  88. }
  89. };
  90. }]).factory('chartTheme', ['chartDefaultTheme', 'chartDarkTheme', function (chartDefaultTheme, chartDarkTheme) {
  91. var themes = {
  92. defaultTheme: chartDefaultTheme,
  93. darkTheme: chartDarkTheme,
  94. };
  95. return {
  96. get: function (name) {
  97. if (name !== 'default' && themes[name + 'Theme']) {
  98. return angular.extend({}, themes.defaultTheme, themes[name + 'Theme']);
  99. } else {
  100. return themes.defaultTheme;
  101. }
  102. }
  103. };
  104. }]).factory('chartDefaultTheme', function () {
  105. return {
  106. color: ['#74a329', '#3a89e9'],
  107. legend: {
  108. top: 'bottom'
  109. },
  110. toolbox: {
  111. show: false
  112. },
  113. tooltip: {
  114. show: true,
  115. trigger: 'axis',
  116. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  117. axisPointer: {
  118. type: 'line',
  119. lineStyle: {
  120. color: '#233333',
  121. type: 'dashed',
  122. width: 1
  123. },
  124. crossStyle: {
  125. color: '#008acd',
  126. width: 1
  127. },
  128. shadowStyle: {
  129. color: 'rgba(200,200,200,0.2)'
  130. }
  131. }
  132. },
  133. grid: {
  134. x: 40,
  135. y: 20,
  136. x2: 30,
  137. y2: 50
  138. },
  139. categoryAxis: {
  140. axisLine: {
  141. show: false
  142. },
  143. axisTick: {
  144. show: false
  145. },
  146. splitLine: {
  147. lineStyle: {
  148. color: '#f3f3f3'
  149. }
  150. }
  151. },
  152. valueAxis: {
  153. axisLine: {
  154. show: false
  155. },
  156. axisTick: {
  157. show: false
  158. },
  159. splitLine: {
  160. lineStyle: {
  161. color: '#f3f3f3'
  162. }
  163. },
  164. splitArea: {
  165. show: false
  166. }
  167. },
  168. line: {
  169. itemStyle: {
  170. normal: {
  171. lineStyle: {
  172. width: 2,
  173. type: 'solid'
  174. }
  175. }
  176. },
  177. smooth: true,
  178. symbolSize: 6
  179. },
  180. textStyle: {
  181. fontFamily: 'Hiragino Sans GB, Microsoft YaHei, STHeiti, Helvetica Neue, Helvetica, Arial, sans-serif'
  182. },
  183. animationDuration: 500
  184. };
  185. }).factory('chartDarkTheme', function () {
  186. return {
  187. tooltip: {
  188. show: true,
  189. trigger: 'axis',
  190. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  191. axisPointer: {
  192. type: 'line',
  193. lineStyle: {
  194. color: '#ddd',
  195. type: 'dashed',
  196. width: 1
  197. },
  198. crossStyle: {
  199. color: '#ddd',
  200. width: 1
  201. },
  202. shadowStyle: {
  203. color: 'rgba(200,200,200,0.2)'
  204. }
  205. }
  206. },
  207. categoryAxis: {
  208. axisLine: {
  209. show: false
  210. },
  211. axisTick: {
  212. show: false
  213. },
  214. splitLine: {
  215. lineStyle: {
  216. color: '#333'
  217. }
  218. }
  219. },
  220. valueAxis: {
  221. axisLine: {
  222. show: false
  223. },
  224. axisTick: {
  225. show: false
  226. },
  227. axisLabel: {
  228. show: true,
  229. textStyle: {
  230. color: '#eee',
  231. }
  232. },
  233. splitLine: {
  234. lineStyle: {
  235. color: '#333'
  236. }
  237. },
  238. splitArea: {
  239. show: false
  240. }
  241. }
  242. };
  243. });
  244. }());