init-charts.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. (function($, win) {
  2. $(function() {
  3. var ic = {
  4. init: function(op) {
  5. if(typeof op != 'object') {
  6. return;
  7. }
  8. var o = {};
  9. o.el = op.el || null; //元素
  10. o.xAxisData = op.xAxisData || null; //X轴
  11. o.seriesData = op.seriesData || null; //值
  12. o.chartsType = op.chartsType || ''; //图形
  13. o.legend = op.legend || '';
  14. o.color = op.color || '';
  15. o.cb = op.cb || ''; //图形
  16. o.grid = op.grid || null;
  17. return this.createECharts(o);
  18. },
  19. // 折线图
  20. setZXChartsOptions: function(xAxisData, seriesData, legend) {
  21. var options = {
  22. backgroundColor: '#fff',
  23. tooltip: {
  24. trigger: 'axis'
  25. },
  26. grid: {
  27. x: 30,
  28. y: 20,
  29. x2: 20,
  30. y2: 30,
  31. borderWidth:0
  32. },
  33. toolbox: {
  34. show: false
  35. },
  36. calculable: false,
  37. xAxis: [{
  38. type: "category",
  39. boundaryGap: false,
  40. data: xAxisData,
  41. axisLabel: {
  42. margin: 6
  43. },
  44. splitLine: {
  45. show: false
  46. },
  47. axisLabel:{
  48. interval:0,//横轴信息全部显示
  49. // rotate:-30,//-30度角倾斜显示
  50. }
  51. }],
  52. yAxis: [{
  53. axisLine: {
  54. show: true
  55. },
  56. axisLabel: {
  57. show: true
  58. },
  59. axisTick: {
  60. show: true
  61. },
  62. splitLine: {
  63. show: false
  64. }
  65. }],
  66. series: seriesData
  67. }
  68. legend && (options.legend = legend);
  69. return options;
  70. },
  71. // 饼状图
  72. initBZChartsOptions: function( seriesData, c) {
  73. var options = {
  74. tooltip : {
  75. trigger: 'item',
  76. formatter: "{a} <br/>{b} : {c} ({d}%)"
  77. },
  78. color: c || ['#17b3ec', '#FF774F'],
  79. series : [
  80. {
  81. // name: seriesName,
  82. type: 'pie',
  83. radius: '55%',
  84. center: ['50%', '60%'],
  85. label: {
  86. formatter: '{b}: {c} ({d}%)'
  87. },
  88. data: seriesData,
  89. hoverAnimation: false
  90. }
  91. ]
  92. };
  93. return options;
  94. },
  95. // 柱状
  96. initZZChartsOptions: function(xAxisData, seriesData, color, grid) {
  97. var options = {
  98. tooltip: {
  99. trigger: 'item'
  100. },
  101. toolbox: {
  102. dataZoom: true,
  103. show: true,
  104. orient: 'vertical',
  105. x: 'right',
  106. y: 'center'
  107. },
  108. grid: grid ? grid : {
  109. x: 35,
  110. y: 50,
  111. x2: 20,
  112. y2: 30,
  113. borderWidth:0
  114. },
  115. xAxis: [{
  116. type: 'category',
  117. data: xAxisData,
  118. axisLabel:{
  119. interval:0,
  120. showMinLabel: true,
  121. showMaxLabel: true
  122. },
  123. splitLine: {
  124. show: false
  125. }
  126. }],
  127. yAxis: [{
  128. type: 'value',
  129. axisLine: {
  130. show: true
  131. },
  132. axisLabel: {
  133. show: true
  134. },
  135. axisTick: {
  136. show: true
  137. },
  138. splitLine: {
  139. show: false
  140. }
  141. }],
  142. series: [{
  143. clickable: true,
  144. itemStyle: {
  145. normal: {
  146. // color: '#fbba31'
  147. }
  148. },
  149. itemStyle : {
  150. normal: {
  151. label : {
  152. show: true, position: 'top'
  153. },
  154. color: color?color: '#fbba31'
  155. }
  156. },
  157. barWidth: 20,
  158. type: 'bar',
  159. data: seriesData
  160. }]
  161. }
  162. return options;
  163. },
  164. createECharts: function(o) {
  165. var me = this;
  166. return new Promise(function(resolve, reject) {
  167. // 路径配置
  168. require.config({
  169. paths: {
  170. echarts: 'http://echarts.baidu.com/build/dist'
  171. }
  172. });
  173. require(['echarts',
  174. 'echarts/chart/line',
  175. 'echarts/chart/pie',
  176. 'echarts/chart/bar'
  177. ] // 使用柱状图就加载bar模块,按需加载]
  178. ,
  179. function(echarts) {
  180. var myCharts = echarts.init(o.el);
  181. switch(o.chartsType) {
  182. case 1:
  183. myCharts.setOption(me.setZXChartsOptions(o.xAxisData, o.seriesData, o.legend));
  184. break;
  185. case 2:
  186. myCharts.setOption(me.initBZChartsOptions( o.seriesData, o.color));
  187. break;
  188. case 3:
  189. var ecConfig = require('echarts/config');
  190. myCharts.setOption(me.initZZChartsOptions(o.xAxisData, o.seriesData, o.color, o.grid));
  191. o.cb && (function() {
  192. myCharts.on(ecConfig.EVENT.CLICK, o.cb);
  193. })();
  194. break;
  195. }
  196. resolve(myCharts);
  197. });
  198. })
  199. }
  200. };
  201. win.$ic = ic;
  202. });
  203. })(jQuery, window);