]>
Commit | Line | Data |
---|---|---|
9531c659 TL |
1 | Ext.define('Proxmox.chart.axis.segmenter.NumericBase2', { |
2 | extend: 'Ext.chart.axis.segmenter.Numeric', | |
3 | alias: 'segmenter.numericBase2', | |
4 | ||
5 | // derived from the original numeric segmenter but using 2 instead of 10 as base | |
6 | preferredStep: function(min, estStepSize) { | |
7 | // Getting an order of magnitude of the estStepSize with a common logarithm. | |
8 | let order = Math.floor(Math.log2(estStepSize)); | |
9 | let scale = Math.pow(2, order); | |
10 | ||
11 | estStepSize /= scale; | |
12 | ||
13 | // FIXME: below is not useful when using base 2 instead of base 10, we could | |
14 | // just directly set estStepSize to 2 | |
15 | if (estStepSize <= 1) { | |
16 | estStepSize = 1; | |
17 | } else if (estStepSize < 2) { | |
18 | estStepSize = 2; | |
19 | } | |
20 | return { | |
21 | unit: { | |
22 | // When passed estStepSize is less than 1, its order of magnitude | |
23 | // is equal to -number_of_leading_zeros in the estStepSize. | |
24 | fixes: -order, // Number of fractional digits. | |
25 | scale: scale, | |
26 | }, | |
27 | step: estStepSize, | |
28 | }; | |
29 | }, | |
30 | ||
31 | /** | |
32 | * Wraps the provided estimated step size of a range without altering it into a step size object. | |
33 | * | |
34 | * @param {*} min The start point of range. | |
35 | * @param {*} estStepSize The estimated step size. | |
36 | * @return {Object} Return the step size by an object of step x unit. | |
37 | * @return {Number} return.step The step count of units. | |
38 | * @return {Object} return.unit The unit. | |
39 | */ | |
40 | // derived from the original numeric segmenter but using 2 instead of 10 as base | |
41 | exactStep: function(min, estStepSize) { | |
42 | let order = Math.floor(Math.log2(estStepSize)); | |
43 | let scale = Math.pow(2, order); | |
44 | ||
45 | return { | |
46 | unit: { | |
47 | // add one decimal point if estStepSize is not a multiple of scale | |
48 | fixes: -order + (estStepSize % scale === 0 ? 0 : 1), | |
49 | scale: 1, | |
50 | }, | |
51 | step: estStepSize, | |
52 | }; | |
53 | }, | |
54 | }); | |
55 | ||
0c786d2b DM |
56 | Ext.define('Proxmox.widget.RRDChart', { |
57 | extend: 'Ext.chart.CartesianChart', | |
58 | alias: 'widget.proxmoxRRDChart', | |
59 | ||
60 | unit: undefined, // bytes, bytespersecond, percent | |
13fc756d | 61 | |
4337ad5b TL |
62 | powerOfTwo: false, |
63 | ||
518dd0b6 DC |
64 | // set to empty string to suppress warning in debug mode |
65 | downloadServerUrl: '-', | |
66 | ||
0c786d2b DM |
67 | controller: { |
68 | xclass: 'Ext.app.ViewController', | |
69 | ||
4337ad5b TL |
70 | init: function(view) { |
71 | this.powerOfTwo = view.powerOfTwo; | |
72 | }, | |
73 | ||
0c786d2b | 74 | convertToUnits: function(value) { |
05a977a2 TL |
75 | let units = ['', 'k', 'M', 'G', 'T', 'P']; |
76 | let si = 0; | |
c58d4100 TL |
77 | let format = '0.##'; |
78 | if (value < 0.1) format += '#'; | |
4337ad5b TL |
79 | const baseValue = this.powerOfTwo ? 1024 : 1000; |
80 | while (value >= baseValue && si < units.length -1) { | |
81 | value = value / baseValue; | |
0c786d2b DM |
82 | si++; |
83 | } | |
84 | ||
85 | // javascript floating point weirdness | |
86 | value = Ext.Number.correctFloat(value); | |
13fc756d | 87 | |
c58d4100 TL |
88 | // limit decimal points |
89 | value = Ext.util.Format.number(value, format); | |
13fc756d | 90 | |
4337ad5b TL |
91 | let unit = units[si]; |
92 | if (this.powerOfTwo) unit += 'i'; | |
93 | ||
94 | return `${value.toString()} ${unit}`; | |
0c786d2b DM |
95 | }, |
96 | ||
97 | leftAxisRenderer: function(axis, label, layoutContext) { | |
05a977a2 | 98 | let me = this; |
0c786d2b DM |
99 | return me.convertToUnits(label); |
100 | }, | |
101 | ||
102 | onSeriesTooltipRender: function(tooltip, record, item) { | |
05a977a2 | 103 | let view = this.getView(); |
13fc756d | 104 | |
05a977a2 TL |
105 | let suffix = ''; |
106 | if (view.unit === 'percent') { | |
0c786d2b | 107 | suffix = '%'; |
05a977a2 | 108 | } else if (view.unit === 'bytes') { |
0c786d2b | 109 | suffix = 'B'; |
05a977a2 | 110 | } else if (view.unit === 'bytespersecond') { |
0c786d2b DM |
111 | suffix = 'B/s'; |
112 | } | |
13fc756d | 113 | |
05a977a2 TL |
114 | let prefix = item.field; |
115 | if (view.fieldTitles && view.fieldTitles[view.fields.indexOf(item.field)]) { | |
116 | prefix = view.fieldTitles[view.fields.indexOf(item.field)]; | |
0c786d2b | 117 | } |
efa61051 TL |
118 | let v = this.convertToUnits(record.get(item.field)); |
119 | let t = new Date(record.get('time')); | |
120 | tooltip.setHtml(`${prefix}: ${v}${suffix}<br>${t}`); | |
0c786d2b DM |
121 | }, |
122 | ||
123 | onAfterAnimation: function(chart, eopts) { | |
efa61051 | 124 | // if the undo button is disabled, disable our tool |
05a977a2 TL |
125 | let ourUndoZoomButton = chart.header.tools[0]; |
126 | let undoButton = chart.interactions[0].getUndoButton(); | |
0c786d2b | 127 | ourUndoZoomButton.setDisabled(undoButton.isDisabled()); |
01031528 | 128 | }, |
0c786d2b | 129 | }, |
13fc756d | 130 | |
0c786d2b DM |
131 | width: 770, |
132 | height: 300, | |
133 | animation: false, | |
efa61051 TL |
134 | interactions: [ |
135 | { | |
01031528 | 136 | type: 'crosszoom', |
efa61051 TL |
137 | }, |
138 | ], | |
0c786d2b | 139 | legend: { |
e662b4a0 | 140 | padding: 0, |
0c786d2b DM |
141 | }, |
142 | listeners: { | |
01031528 | 143 | animationend: 'onAfterAnimation', |
0c786d2b DM |
144 | }, |
145 | ||
9531c659 TL |
146 | constructor: function(config) { |
147 | let me = this; | |
148 | ||
149 | let segmenter = config.powerOfTwo ? 'numericBase2' : 'numeric'; | |
150 | config.axes = [ | |
151 | { | |
152 | type: 'numeric', | |
153 | position: 'left', | |
154 | grid: true, | |
155 | renderer: 'leftAxisRenderer', | |
156 | minimum: 0, | |
157 | segmenter, | |
158 | }, | |
159 | { | |
160 | type: 'time', | |
161 | position: 'bottom', | |
162 | grid: true, | |
163 | fields: ['time'], | |
164 | }, | |
165 | ]; | |
166 | me.callParent([config]); | |
167 | }, | |
168 | ||
0c786d2b | 169 | initComponent: function() { |
05a977a2 | 170 | let me = this; |
0c786d2b DM |
171 | |
172 | if (!me.store) { | |
173 | throw "cannot work without store"; | |
174 | } | |
175 | ||
176 | if (!me.fields) { | |
177 | throw "cannot work without fields"; | |
178 | } | |
179 | ||
180 | me.callParent(); | |
181 | ||
182 | // add correct label for left axis | |
05a977a2 | 183 | let axisTitle = ""; |
0c786d2b DM |
184 | if (me.unit === 'percent') { |
185 | axisTitle = "%"; | |
186 | } else if (me.unit === 'bytes') { | |
187 | axisTitle = "Bytes"; | |
188 | } else if (me.unit === 'bytespersecond') { | |
189 | axisTitle = "Bytes/s"; | |
51613ace DC |
190 | } else if (me.fieldTitles && me.fieldTitles.length === 1) { |
191 | axisTitle = me.fieldTitles[0]; | |
192 | } else if (me.fields.length === 1) { | |
193 | axisTitle = me.fields[0]; | |
0c786d2b DM |
194 | } |
195 | ||
196 | me.axes[0].setTitle(axisTitle); | |
197 | ||
e662b4a0 | 198 | me.updateHeader(); |
0855ba61 TL |
199 | |
200 | if (me.header && me.legend) { | |
201 | me.header.padding = '4 9 4'; | |
202 | me.header.add(me.legend); | |
203 | } | |
e662b4a0 | 204 | |
fcb5b70f | 205 | if (!me.noTool) { |
e662b4a0 | 206 | me.addTool({ |
fcb5b70f DC |
207 | type: 'minus', |
208 | disabled: true, | |
209 | tooltip: gettext('Undo Zoom'), | |
01031528 | 210 | handler: function() { |
05a977a2 | 211 | let undoButton = me.interactions[0].getUndoButton(); |
fcb5b70f DC |
212 | if (undoButton.handler) { |
213 | undoButton.handler(); | |
214 | } | |
01031528 | 215 | }, |
e662b4a0 | 216 | }); |
fcb5b70f DC |
217 | } |
218 | ||
0c786d2b | 219 | // add a series for each field we get |
01031528 | 220 | me.fields.forEach(function(item, index) { |
05a977a2 | 221 | let title = item; |
0c786d2b DM |
222 | if (me.fieldTitles && me.fieldTitles[index]) { |
223 | title = me.fieldTitles[index]; | |
224 | } | |
9950ec0f DC |
225 | me.addSeries(Ext.apply( |
226 | { | |
227 | type: 'line', | |
228 | xField: 'time', | |
229 | yField: item, | |
230 | title: title, | |
231 | fill: true, | |
232 | style: { | |
233 | lineWidth: 1.5, | |
01031528 | 234 | opacity: 0.60, |
9950ec0f DC |
235 | }, |
236 | marker: { | |
237 | opacity: 0, | |
238 | scaling: 0.01, | |
239 | fx: { | |
240 | duration: 200, | |
01031528 TL |
241 | easing: 'easeOut', |
242 | }, | |
9950ec0f DC |
243 | }, |
244 | highlightCfg: { | |
245 | opacity: 1, | |
01031528 | 246 | scaling: 1.5, |
9950ec0f DC |
247 | }, |
248 | tooltip: { | |
249 | trackMouse: true, | |
01031528 TL |
250 | renderer: 'onSeriesTooltipRender', |
251 | }, | |
0c786d2b | 252 | }, |
01031528 | 253 | me.seriesConfig, |
9950ec0f | 254 | )); |
0c786d2b DM |
255 | }); |
256 | ||
257 | // enable animation after the store is loaded | |
258 | me.store.onAfter('load', function() { | |
259 | me.setAnimation(true); | |
01031528 TL |
260 | }, this, { single: true }); |
261 | }, | |
0c786d2b | 262 | }); |