init-charts.js 4.2 KB

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