init-charts.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. show: false,
  76. trigger: 'item',
  77. formatter: "{a} <br/>{b}: {c}"
  78. },
  79. color: c || ['#17b3ec', '#FF774F'],
  80. calculable: false,
  81. series: [{
  82. type: 'pie',
  83. radius: '65%',
  84. center: [70, '50%'],
  85. label: {
  86. normal: {
  87. show: false,
  88. textStyle: {
  89. color: "#000"
  90. }
  91. }
  92. },
  93. itemStyle: {
  94. normal: {
  95. label: {
  96. show: false
  97. },
  98. labelLine: {
  99. show: false
  100. }
  101. }
  102. },
  103. data: seriesData
  104. }]
  105. }
  106. return options;
  107. },
  108. // 柱状
  109. initZZChartsOptions: function(xAxisData, seriesData, color, grid) {
  110. var options = {
  111. tooltip: {
  112. trigger: 'item'
  113. },
  114. toolbox: {
  115. dataZoom: true,
  116. show: true,
  117. orient: 'vertical',
  118. x: 'right',
  119. y: 'center'
  120. },
  121. grid: grid ? grid : {
  122. x: 35,
  123. y: 50,
  124. x2: 20,
  125. y2: 30,
  126. borderWidth:0
  127. },
  128. xAxis: [{
  129. type: 'category',
  130. data: xAxisData,
  131. axisLabel:{
  132. interval:0,
  133. showMinLabel: true,
  134. showMaxLabel: true
  135. },
  136. splitLine: {
  137. show: false
  138. }
  139. }],
  140. yAxis: [{
  141. type: 'value',
  142. splitLine: {
  143. show: false
  144. }
  145. }],
  146. series: [{
  147. clickable: true,
  148. itemStyle: {
  149. normal: {
  150. // color: '#fbba31'
  151. }
  152. },
  153. itemStyle : {
  154. normal: {
  155. label : {
  156. show: true, position: 'top'
  157. },
  158. color: color?color: '#fbba31'
  159. }
  160. },
  161. barWidth: 20,
  162. type: 'bar',
  163. data: seriesData
  164. }]
  165. }
  166. return options;
  167. },
  168. createECharts: function(o) {
  169. var me = this;
  170. return new Promise(function(resolve, reject) {
  171. // 路径配置
  172. require.config({
  173. paths: {
  174. echarts: 'http://echarts.baidu.com/build/dist'
  175. }
  176. });
  177. require(['echarts',
  178. 'echarts/chart/line',
  179. 'echarts/chart/pie',
  180. 'echarts/chart/bar'
  181. ] // 使用柱状图就加载bar模块,按需加载]
  182. ,
  183. function(echarts) {
  184. var myCharts = echarts.init(o.el);
  185. switch(o.chartsType) {
  186. case 1:
  187. myCharts.setOption(me.setZXChartsOptions(o.xAxisData, o.seriesData, o.legend));
  188. break;
  189. case 2:
  190. myCharts.setOption(me.initBZChartsOptions( o.seriesData, o.color));
  191. break;
  192. case 3:
  193. var ecConfig = require('echarts/config');
  194. myCharts.setOption(me.initZZChartsOptions(o.xAxisData, o.seriesData, o.color, o.grid));
  195. o.cb && (function() {
  196. myCharts.on(ecConfig.EVENT.CLICK, o.cb);
  197. })();
  198. break;
  199. }
  200. resolve(myCharts);
  201. });
  202. })
  203. }
  204. };
  205. win.$ic = ic;
  206. });
  207. })(jQuery, window);