sandbox.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. Ext4.onReady(function() {
  2. var memoryArray,
  3. processArray,
  4. colors,
  5. memoryStore,
  6. processesMemoryStore,
  7. cpuLoadStore,
  8. data,
  9. memoryPieChartConfig,
  10. barChartConfig,
  11. cpuLoadChartConfig,
  12. cpuLoadChartConfig2,
  13. win,
  14. cpuLoadTimer, pass;
  15. memoryArray = ['Wired', 'Active', 'Inactive', 'Free'];
  16. processArray = ['explorer', 'monitor', 'charts', 'desktop', 'Ext3', 'Ext4'];
  17. colors = ['rgb(244, 16, 0)',
  18. 'rgb(248, 130, 1)',
  19. 'rgb(0, 7, 255)',
  20. 'rgb(84, 254, 0)'];
  21. Ext4.chart.theme.Memory = Ext4.extend(Ext4.chart.theme.Base, {
  22. constructor: function(config) {
  23. Ext4.chart.theme.Base.prototype.constructor.call(this, Ext4.apply({
  24. colors: colors
  25. }, config));
  26. }
  27. });
  28. function generateData(a) {
  29. var data = [],
  30. i,
  31. names = a,
  32. rest = a.length, total = rest, consume;
  33. for (i = 0; i < a.length; i++) {
  34. consume = Math.floor(Math.random() * rest * 100) / 100 + 2;
  35. rest = rest - (consume - 5);
  36. data.push({
  37. name: names[i],
  38. memory: consume
  39. });
  40. }
  41. return data;
  42. }
  43. memoryStore = Ext4.create('store.json', {
  44. fields: ['name', 'memory'],
  45. data: generateData(memoryArray)
  46. });
  47. processesMemoryStore = Ext4.create('store.json', {
  48. fields: ['name', 'memory'],
  49. data: generateData(processArray)
  50. });
  51. cpuLoadStore = Ext4.create('store.json', {
  52. fields: ['core1', 'core2', 'time']
  53. });
  54. data = [];
  55. function generateCpuLoad() {
  56. function generate(factor) {
  57. var value = factor + ((Math.floor(Math.random() * 2) % 2) ? -1 : 1) * Math.floor(Math.random() * 9);
  58. if (value < 0 || value > 100) {
  59. value = 50;
  60. }
  61. return value;
  62. }
  63. if (data.length === 0) {
  64. data.push({
  65. core1: 0,
  66. core2: 0,
  67. time: 0
  68. });
  69. for (var i = 1; i < 100; i++) {
  70. data.push({
  71. core1: generate(data[i - 1].core1),
  72. core2: generate(data[i - 1].core2),
  73. time: i
  74. });
  75. }
  76. cpuLoadStore.loadData(data);
  77. }
  78. else {
  79. cpuLoadStore.data.removeAt(0);
  80. cpuLoadStore.data.each(function(item, key) {
  81. item.data.time = key;
  82. });
  83. var lastData = cpuLoadStore.last().data;
  84. cpuLoadStore.loadData([{
  85. core1: generate(lastData.core1),
  86. core2: generate(lastData.core2),
  87. time: lastData.time + 1
  88. }], true);
  89. }
  90. }
  91. generateCpuLoad();
  92. memoryPieChartConfig = {
  93. flex: 1,
  94. xtype: 'chart',
  95. animate: {
  96. duration: 250
  97. },
  98. store: memoryStore,
  99. shadow: true,
  100. legend: {
  101. position: 'right',
  102. update: false
  103. },
  104. insetPadding: 40,
  105. theme: 'Memory:gradients',
  106. series: [{
  107. donut: 30,
  108. type: 'pie',
  109. field: 'memory',
  110. showInLegend: true,
  111. tips: {
  112. trackMouse: true,
  113. width: 140,
  114. height: 28,
  115. renderer: function(storeItem, item) {
  116. //calculate percentage.
  117. var total = 0;
  118. memoryStore.each(function(rec) {
  119. total += rec.get('memory');
  120. });
  121. this.setTitle(storeItem.get('name') + ': ' + Math.round(storeItem.get('memory') / total * 100) + '%');
  122. }
  123. },
  124. highlight: {
  125. segment: {
  126. margin: 20
  127. }
  128. },
  129. labelTitle: {
  130. font: '13px Arial'
  131. },
  132. label: {
  133. field: 'name',
  134. display: 'rotate',
  135. contrast: true,
  136. font: '12px Arial'
  137. }
  138. }]
  139. };
  140. barChartConfig = {
  141. flex: 1,
  142. xtype: 'chart',
  143. theme: 'Category1',
  144. store: processesMemoryStore,
  145. animate: {
  146. easing: 'ease-in-out',
  147. duration: 750
  148. },
  149. axes: [{
  150. type: 'Numeric',
  151. position: 'left',
  152. minimum: 0,
  153. maximum: 10,
  154. fields: ['memory'],
  155. title: 'Memory',
  156. labelTitle: {
  157. font: '13px Arial'
  158. },
  159. label: {
  160. font: '11px Arial'
  161. }
  162. },{
  163. type: 'Category',
  164. position: 'bottom',
  165. fields: ['name'],
  166. title: 'System Processes',
  167. labelTitle: {
  168. font: 'bold 14px Arial'
  169. },
  170. label: {
  171. rotation: {
  172. degrees: 45
  173. }
  174. }
  175. },{
  176. type: 'Numeric',
  177. position: 'top',
  178. fields: ['memory'],
  179. title: 'Memory Usage',
  180. labelTitle: {
  181. font: 'bold 14px Arial'
  182. },
  183. label: {
  184. fill: '#FFFFFF',
  185. stroke: '#FFFFFF'
  186. },
  187. axisStyle: {
  188. fill: '#FFFFFF',
  189. stroke: '#FFFFFF'
  190. }
  191. }],
  192. series: [{
  193. title: 'Processes',
  194. type: 'column',
  195. xField: 'name',
  196. yField: 'memory',
  197. renderer: function(sprite, record, attr, index, store) {
  198. var highColor = Ext4.draw.Color.fromString('#e84b67'),
  199. lowColor = Ext4.draw.Color.fromString('#b1da5a'),
  200. value = record.get('memory'),
  201. color;
  202. if (value > 5) {
  203. color = lowColor.getDarker((value - 5) / 15).toString();
  204. }
  205. else {
  206. color = lowColor.getLighter(((5 - value) / 20)).toString();
  207. }
  208. if (value >= 8) {
  209. color = '#CD0000';
  210. }
  211. return Ext.apply(attr, {
  212. fill: color
  213. });
  214. }
  215. }]
  216. };
  217. cpuLoadChartConfig = {
  218. flex: 1,
  219. xtype: 'chart',
  220. theme: 'Category1',
  221. animate: false,
  222. store: cpuLoadStore,
  223. legend: {
  224. position: 'bottom',
  225. update: false
  226. },
  227. axes: [{
  228. type: 'Numeric',
  229. position: 'left',
  230. minimum: 0,
  231. maximum: 100,
  232. fields: ['core1'],
  233. title: 'CPU Load',
  234. grid: true,
  235. labelTitle: {
  236. font: '13px Arial'
  237. },
  238. label: {
  239. font: '11px Arial'
  240. }
  241. }],
  242. series: [{
  243. title: 'Core 1 (3.4GHz)',
  244. type: 'line',
  245. lineWidth: 4,
  246. showMarkers: false,
  247. fill: true,
  248. axis: ['left'],
  249. xField: 'time',
  250. yField: 'core1',
  251. style: {
  252. 'stroke-width': 1
  253. }
  254. }]
  255. };
  256. cpuLoadChartConfig2 = {
  257. flex: 1,
  258. xtype: 'chart',
  259. theme: 'Category2',
  260. animate: false,
  261. store: cpuLoadStore,
  262. legend: {
  263. position: 'bottom',
  264. update: false
  265. },
  266. axes: [{
  267. type: 'Numeric',
  268. position: 'left',
  269. minimum: 0,
  270. maximum: 100,
  271. grid: true,
  272. fields: ['core2'],
  273. title: 'CPU Load',
  274. labelTitle: {
  275. font: '13px Arial'
  276. },
  277. label: {
  278. font: '11px Arial'
  279. }
  280. }],
  281. series: [{
  282. title: 'Core 2 (3.4GHz)',
  283. type: 'line',
  284. lineWidth: 4,
  285. showMarkers: false,
  286. fill: true,
  287. axis: ['left'],
  288. xField: 'time',
  289. yField: 'core2',
  290. style: {
  291. 'stroke-width': 1
  292. }
  293. }]
  294. };
  295. win = Ext4.widget('window', {
  296. x: 90,
  297. y: 50,
  298. width: 800,
  299. height: 600,
  300. title: 'System Statistics',
  301. renderTo: Ext4.getBody(),
  302. closeAction: 'hide',
  303. layout: 'fit',
  304. items: [{
  305. xtype: 'panel',
  306. layout: {
  307. type: 'hbox',
  308. align: 'stretch'
  309. },
  310. items: [{
  311. flex: 1,
  312. height: 600,
  313. width: 400,
  314. xtype: 'container',
  315. layout: {
  316. type: 'vbox',
  317. align: 'stretch'
  318. },
  319. items: [
  320. cpuLoadChartConfig,
  321. cpuLoadChartConfig2
  322. ]
  323. }, {
  324. flex: 1,
  325. width: 400,
  326. height: 600,
  327. xtype: 'container',
  328. layout: {
  329. type: 'vbox',
  330. align: 'stretch'
  331. },
  332. items: [
  333. memoryPieChartConfig,
  334. barChartConfig
  335. ]
  336. }]
  337. }]
  338. });
  339. pass = 0;
  340. function doGenerateCpuLoad() {
  341. clearTimeout(cpuLoadTimer);
  342. cpuLoadTimer = setTimeout(function() {
  343. if (pass % 3 === 0) {
  344. memoryStore.loadData(generateData(memoryArray));
  345. }
  346. if (pass % 5 === 0) {
  347. processesMemoryStore.loadData(generateData(processArray));
  348. }
  349. generateCpuLoad();
  350. doGenerateCpuLoad();
  351. pass++;
  352. }, 500);
  353. }
  354. Ext.get('chart-win-shortcut').on('click', function() {
  355. doGenerateCpuLoad();
  356. win.show();
  357. });
  358. });