init-charts.js 4.3 KB

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