echart-bar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. $(function() {
  2. "use strict";
  3. // ------------------------------
  4. // Basic bar chart
  5. // ------------------------------
  6. // based on prepared DOM, initialize echarts instance
  7. var myChart = echarts.init(document.getElementById('basic-bar'));
  8. // specify chart configuration item and data
  9. var option = {
  10. // Setup grid
  11. grid: {
  12. left: '1%',
  13. right: '2%',
  14. bottom: '3%',
  15. containLabel: true
  16. },
  17. // Add Tooltip
  18. tooltip : {
  19. trigger: 'axis'
  20. },
  21. legend: {
  22. data:['Site A','Site B']
  23. },
  24. toolbox: {
  25. show : true,
  26. feature : {
  27. magicType : {show: true, type: ['line', 'bar']},
  28. restore : {show: true},
  29. saveAsImage : {show: true}
  30. }
  31. },
  32. color: ["#38649f", "#389f99"],
  33. calculable : true,
  34. xAxis : [
  35. {
  36. type : 'category',
  37. data : ['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sept','Oct','Nov','Dec']
  38. }
  39. ],
  40. yAxis : [
  41. {
  42. type : 'value'
  43. }
  44. ],
  45. series : [
  46. {
  47. name:'Site A',
  48. type:'bar',
  49. data:[7.2, 5.3, 6.1, 32.1, 23.1, 89.2, 158.4, 178.1, 36.4, 22.7, 7.1, 9.4],
  50. markPoint : {
  51. data : [
  52. {type : 'max', name: 'Max'},
  53. {type : 'min', name: 'Min'}
  54. ]
  55. },
  56. markLine : {
  57. data : [
  58. {type : 'average', name: 'Average'}
  59. ]
  60. }
  61. },
  62. {
  63. name:'Site B',
  64. type:'bar',
  65. data:[19.4, 7.9, 8.9, 27.9, 24.8, 88.1, 167.8, 197.5, 47.1, 16.7, 7.1, 1.5],
  66. markPoint : {
  67. data : [
  68. {name : 'The highest year', value : 182.2, xAxis: 7, yAxis: 183, symbolSize:18},
  69. {name : 'Year minimum', value : 2.3, xAxis: 11, yAxis: 3}
  70. ]
  71. },
  72. markLine : {
  73. data : [
  74. {type : 'average', name : 'Average'}
  75. ]
  76. }
  77. }
  78. ]
  79. };
  80. // use configuration item and data specified to show chart
  81. myChart.setOption(option);
  82. // ------------------------------
  83. // Stacked bar chart
  84. // ------------------------------
  85. // based on prepared DOM, initialize echarts instance
  86. var stackedChart = echarts.init(document.getElementById('stacked-bar'));
  87. // specify chart configuration item and data
  88. var option = {
  89. // Setup grid
  90. grid: {
  91. x: 40,
  92. x2: 40,
  93. y: 45,
  94. y2: 25
  95. },
  96. // Add tooltip
  97. tooltip : {
  98. trigger: 'axis',
  99. axisPointer : { // Axis indicator axis trigger effective
  100. type : 'shadow' // The default is a straight line, optionally: 'line' | 'shadow'
  101. }
  102. },
  103. // Add legend
  104. legend: {
  105. data: ['Data1', 'Data2', 'Data3', 'Data4', 'Data5']
  106. },
  107. // Add custom colors
  108. color: ['#689f38', '#38649f', '#389f99', '#ff8f00', '#ee1044'],
  109. // Horizontal axis
  110. xAxis: [{
  111. type: 'value',
  112. }],
  113. // Vertical axis
  114. yAxis: [{
  115. type: 'category',
  116. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  117. }],
  118. // Add series
  119. series : [
  120. {
  121. name:'Data1',
  122. type:'bar',
  123. stack: 'Total',
  124. itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
  125. data:[321, 269, 358, 369, 348, 312, 299]
  126. },
  127. {
  128. name:'Data2',
  129. type:'bar',
  130. stack: 'Total',
  131. itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
  132. data:[159, 147, 123, 136, 87, 199, 248]
  133. },
  134. {
  135. name:'Data3',
  136. type:'bar',
  137. stack: 'Total',
  138. itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
  139. data:[269, 248, 158, 325, 248, 279, 357]
  140. },
  141. {
  142. name:'Data4',
  143. type:'bar',
  144. stack: 'Total',
  145. itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
  146. data:[101, 198, 247, 136, 178, 388, 499]
  147. },
  148. {
  149. name:'Data5',
  150. type:'bar',
  151. stack: 'Total',
  152. itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
  153. data:[869, 965, 975, 752, 1254, 1458, 875]
  154. }
  155. ]
  156. };
  157. // use configuration item and data specified to show chart
  158. stackedChart.setOption(option);
  159. //***************************
  160. // Stacked chart
  161. //***************************
  162. //***************************
  163. // Stacked Area chart
  164. //***************************
  165. var stackedbarcolumnChart = echarts.init(document.getElementById('stacked-column'));
  166. var option = {
  167. // Setup grid
  168. grid: {
  169. x: 40,
  170. x2: 40,
  171. y: 45,
  172. y2: 25
  173. },
  174. // Add tooltip
  175. tooltip : {
  176. trigger: 'axis',
  177. axisPointer : { // Axis indicator axis trigger effective
  178. type : 'shadow' // The default is a straight line, optionally: 'line' | 'shadow'
  179. }
  180. },
  181. // Add legend
  182. legend: {
  183. data: [ 'Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data7']
  184. },
  185. // Add custom colors
  186. color: ['#689f38', '#38649f', '#389f99', '#ff8f00', '#ee1044'],
  187. // Enable drag recalculate
  188. calculable: true,
  189. // Horizontal axis
  190. xAxis: [{
  191. type: 'category',
  192. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  193. }],
  194. // Vertical axis
  195. yAxis: [{
  196. type: 'value',
  197. }],
  198. // Add series
  199. series : [
  200. {
  201. name:'Data1',
  202. type:'bar',
  203. stack: 'data1',
  204. data:[178, 241, 210, 147, 299, 358, 487]
  205. },
  206. {
  207. name:'Data2',
  208. type:'bar',
  209. data:[875, 845, 985, 1254, 1425, 1235, 1425],
  210. markLine : {
  211. itemStyle:{
  212. normal:{
  213. lineStyle:{
  214. type: 'dashed'
  215. }
  216. }
  217. },
  218. data : [
  219. [{type : 'min'}, {type : 'max'}]
  220. ]
  221. }
  222. },
  223. {
  224. name:'Data3',
  225. type:'bar',
  226. barWidth : 12,
  227. stack: 'data',
  228. data:[654, 758, 754, 854, 1245, 1100, 1140]
  229. },
  230. {
  231. name:'Data4',
  232. type:'bar',
  233. stack: 'data',
  234. data:[104, 134, 125, 158, 245, 236, 278]
  235. },
  236. {
  237. name:'Data5',
  238. type:'bar',
  239. stack: 'data',
  240. data:[54, 123, 147, 85, 165, 158, 123]
  241. },
  242. {
  243. name:'Data6',
  244. type:'bar',
  245. stack: 'data',
  246. data:[21, 84, 79, 86, 135, 158, 210]
  247. }
  248. ]
  249. // Add series
  250. };
  251. stackedbarcolumnChart.setOption(option);
  252. // ------------------------------
  253. // Basic line chart
  254. // ------------------------------
  255. // based on prepared DOM, initialize echarts instance
  256. var barbasicChart = echarts.init(document.getElementById('bar-basic'));
  257. var option = {
  258. // Setup grid
  259. grid: {
  260. x: 60,
  261. x2: 40,
  262. y: 45,
  263. y2: 25
  264. },
  265. // Add tooltip
  266. tooltip: {
  267. trigger: 'axis'
  268. },
  269. // Add legend
  270. legend: {
  271. data: ['2018', '2019']
  272. },
  273. // Add custom colors
  274. color: ['#ee1044', '#389f99'],
  275. // Horizontal axis
  276. xAxis: [{
  277. type: 'value',
  278. boundaryGap: [0, 0.01]
  279. }],
  280. // Vertical axis
  281. yAxis: [{
  282. type: 'category',
  283. data: ['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6']
  284. }],
  285. // Add series
  286. series : [
  287. {
  288. name:'2018',
  289. type:'bar',
  290. data:[845, 542, 348, 298, 548, 398]
  291. },
  292. {
  293. name:'2019',
  294. type:'bar',
  295. data:[689, 754, 498, 123, 489, 598]
  296. }
  297. ]
  298. };
  299. // use configuration item and data specified to show chart
  300. barbasicChart.setOption(option);
  301. //------------------------------------------------------
  302. // Resize chart on menu width change and window resize
  303. //------------------------------------------------------
  304. $(function () {
  305. // Resize chart on menu width change and window resize
  306. $(window).on('resize', resize);
  307. $(".sidebartoggler").on('click', resize);
  308. // Resize function
  309. function resize() {
  310. setTimeout(function() {
  311. // Resize chart
  312. myChart.resize();
  313. stackedChart.resize();
  314. stackedbarcolumnChart.resize();
  315. barbasicChart.resize();
  316. }, 200);
  317. }
  318. });
  319. });