]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | Ext.define('KitchenSink.view.charts.line.RealTimeController', {\r |
2 | extend: 'Ext.app.ViewController',\r | |
3 | alias: 'controller.line-real-time',\r | |
4 | \r | |
5 | onTimeChartRendered: function (chart) {\r | |
6 | chart.getStore().removeAll();\r | |
7 | this.addNewTimeData();\r | |
8 | this.timeChartTask = Ext.TaskManager.start({\r | |
9 | run: this.addNewTimeData,\r | |
10 | interval: 1000,\r | |
11 | repeat: 120,\r | |
12 | scope: this\r | |
13 | });\r | |
14 | },\r | |
15 | \r | |
16 | onAxisLabelRender: function (axis, label, layoutContext) { // only render interger values\r | |
17 | return Math.abs(layoutContext.renderer(label) % 1) < 1e-5 ? Math.round(label) : '';\r | |
18 | },\r | |
19 | \r | |
20 | onTimeChartDestroy: function () {\r | |
21 | if (this.timeChartTask) {\r | |
22 | Ext.TaskManager.stop(this.timeChartTask);\r | |
23 | }\r | |
24 | },\r | |
25 | \r | |
26 | onNumberChartRendered: function (chart) {\r | |
27 | chart.getStore().removeAll();\r | |
28 | this.addNewNumberData();\r | |
29 | this.numberChartTask = Ext.TaskManager.start({\r | |
30 | run: this.addNewNumberData,\r | |
31 | interval: 1000,\r | |
32 | repeat: 240,\r | |
33 | scope: this\r | |
34 | });\r | |
35 | },\r | |
36 | \r | |
37 | onNumberChartDestroy: function () {\r | |
38 | if (this.numberChartTask) {\r | |
39 | Ext.TaskManager.stop(this.numberChartTask);\r | |
40 | }\r | |
41 | },\r | |
42 | \r | |
43 | onTabChange: function (tabPanel, newCard, oldCard) {\r | |
44 | if (newCard.getItemId() === 'numeric') {\r | |
45 | Ext.TaskManager.stop(this.timeChartTask);\r | |
46 | Ext.TaskManager.start(this.numberChartTask);\r | |
47 | } else {\r | |
48 | Ext.TaskManager.stop(this.numberChartTask);\r | |
49 | Ext.TaskManager.start(this.timeChartTask);\r | |
50 | }\r | |
51 | },\r | |
52 | \r | |
53 | getNextValue: function (previousValue, min, max, delta) {\r | |
54 | delta = delta || 3;\r | |
55 | min = min || 0;\r | |
56 | max = max || 20;\r | |
57 | \r | |
58 | delta = Ext.Number.randomInt(-delta, delta);\r | |
59 | \r | |
60 | if (Ext.isNumber(previousValue)) {\r | |
61 | return Ext.Number.constrain(previousValue + delta, min, max);\r | |
62 | }\r | |
63 | return Ext.Number.randomInt(min, max);\r | |
64 | },\r | |
65 | \r | |
66 | addNewTimeData: function() {\r | |
67 | var me = this,\r | |
68 | chart = me.lookupReference('time-chart'),\r | |
69 | store = chart.getStore(),\r | |
70 | count = store.getCount(),\r | |
71 | xAxis = chart.getAxes()[1],\r | |
72 | visibleRange = 10000,\r | |
73 | second = 1000,\r | |
74 | xValue, lastRecord;\r | |
75 | \r | |
76 | if (count > 0) {\r | |
77 | lastRecord = store.getAt(count - 1);\r | |
78 | xValue = lastRecord.get('xValue') + second;\r | |
79 | if (xValue - me.startTime > visibleRange) {\r | |
80 | me.startTime = xValue - visibleRange;\r | |
81 | xAxis.setMinimum(this.startTime);\r | |
82 | xAxis.setMaximum(xValue);\r | |
83 | }\r | |
84 | store.add({\r | |
85 | xValue: xValue,\r | |
86 | metric1: me.getNextValue(lastRecord.get('metric1')),\r | |
87 | metric2: me.getNextValue(lastRecord.get('metric2'))\r | |
88 | });\r | |
89 | \r | |
90 | } else {\r | |
91 | chart.animationSuspended = true;\r | |
92 | me.startTime = Math.floor(Ext.Date.now() / second) * second;\r | |
93 | xAxis.setMinimum(me.startTime);\r | |
94 | xAxis.setMaximum(me.startTime + visibleRange);\r | |
95 | \r | |
96 | store.add({\r | |
97 | xValue: this.startTime,\r | |
98 | metric1: me.getNextValue(),\r | |
99 | metric2: me.getNextValue()\r | |
100 | });\r | |
101 | chart.animationSuspended = false;\r | |
102 | }\r | |
103 | },\r | |
104 | \r | |
105 | addNewNumberData: function() {\r | |
106 | var chart = this.lookupReference('number-chart'),\r | |
107 | store = chart.getStore(),\r | |
108 | count = store.getCount(),\r | |
109 | xAxis = chart.getAxes()[1],\r | |
110 | visibleRange = 20,\r | |
111 | minY = 0,\r | |
112 | maxY = 100,\r | |
113 | deltaY = 5,\r | |
114 | xValue, lastRecord;\r | |
115 | \r | |
116 | if (count > 0) {\r | |
117 | lastRecord = store.getAt(count - 1);\r | |
118 | xValue = lastRecord.get('xValue') + 1;\r | |
119 | if (xValue > visibleRange) {\r | |
120 | xAxis.setMinimum(xValue - visibleRange);\r | |
121 | xAxis.setMaximum(xValue);\r | |
122 | }\r | |
123 | store.add({\r | |
124 | xValue: xValue,\r | |
125 | yValue: this.getNextValue(lastRecord.get('yValue'), minY, maxY, deltaY)\r | |
126 | });\r | |
127 | \r | |
128 | } else {\r | |
129 | chart.animationSuspended = true;\r | |
130 | xAxis.setMinimum(0);\r | |
131 | xAxis.setMaximum(visibleRange);\r | |
132 | \r | |
133 | store.add({\r | |
134 | xValue: 0,\r | |
135 | yValue: this.getNextValue((minY + maxY) / 2, minY, maxY)\r | |
136 | });\r | |
137 | chart.animationSuspended = false;\r | |
138 | }\r | |
139 | }\r | |
140 | \r | |
141 | }); |