]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - panel/RRDChart.js
add RRDChart class
[proxmox-widget-toolkit.git] / panel / RRDChart.js
1 Ext.define('Proxmox.widget.RRDChart', {
2 extend: 'Ext.chart.CartesianChart',
3 alias: 'widget.proxmoxRRDChart',
4
5 unit: undefined, // bytes, bytespersecond, percent
6
7 controller: {
8 xclass: 'Ext.app.ViewController',
9
10 convertToUnits: function(value) {
11 var units = ['', 'k','M','G','T', 'P'];
12 var si = 0;
13 while(value >= 1000 && si < (units.length -1)){
14 value = value / 1000;
15 si++;
16 }
17
18 // javascript floating point weirdness
19 value = Ext.Number.correctFloat(value);
20
21 // limit to 2 decimal points
22 value = Ext.util.Format.number(value, "0.##");
23
24 return value.toString() + " " + units[si];
25 },
26
27 leftAxisRenderer: function(axis, label, layoutContext) {
28 var me = this;
29
30 return me.convertToUnits(label);
31 },
32
33 onSeriesTooltipRender: function(tooltip, record, item) {
34 var me = this.getView();
35
36 var suffix = '';
37
38 if (me.unit === 'percent') {
39 suffix = '%';
40 } else if (me.unit === 'bytes') {
41 suffix = 'B';
42 } else if (me.unit === 'bytespersecond') {
43 suffix = 'B/s';
44 }
45
46 var prefix = item.field;
47 if (me.fieldTitles && me.fieldTitles[me.fields.indexOf(item.field)]) {
48 prefix = me.fieldTitles[me.fields.indexOf(item.field)];
49 }
50 tooltip.setHtml(prefix + ': ' + this.convertToUnits(record.get(item.field)) + suffix +
51 '<br>' + new Date(record.get('time')));
52 },
53
54 onAfterAnimation: function(chart, eopts) {
55 // if the undobuton is disabled,
56 // disable our tool
57
58 var ourUndoZoomButton = chart.tools[0];
59 var undoButton = chart.interactions[0].getUndoButton();
60 ourUndoZoomButton.setDisabled(undoButton.isDisabled());
61 }
62 },
63
64 width: 770,
65 height: 300,
66 animation: false,
67 interactions: [{
68 type: 'crosszoom'
69 }],
70 axes: [{
71 type: 'numeric',
72 position: 'left',
73 grid: true,
74 renderer: 'leftAxisRenderer',
75 //renderer: function(axis, label) { return label; },
76 minimum: 0
77 }, {
78 type: 'time',
79 position: 'bottom',
80 grid: true,
81 fields: ['time']
82 }],
83 legend: {
84 docked: 'bottom'
85 },
86 listeners: {
87 animationend: 'onAfterAnimation'
88 },
89
90
91 initComponent: function() {
92 var me = this;
93
94 if (!me.store) {
95 throw "cannot work without store";
96 }
97
98 if (!me.fields) {
99 throw "cannot work without fields";
100 }
101
102 me.callParent();
103
104 // add correct label for left axis
105 var axisTitle = "";
106 if (me.unit === 'percent') {
107 axisTitle = "%";
108 } else if (me.unit === 'bytes') {
109 axisTitle = "Bytes";
110 } else if (me.unit === 'bytespersecond') {
111 axisTitle = "Bytes/s";
112 }
113
114 me.axes[0].setTitle(axisTitle);
115
116 me.addTool([{
117 type: 'minus',
118 disabled: true,
119 tooltip: gettext('Undo Zoom'),
120 handler: function(){
121 var undoButton = me.interactions[0].getUndoButton();
122 if (undoButton.handler) {
123 undoButton.handler();
124 }
125 }
126 },{
127 type: 'restore',
128 tooltip: gettext('Toggle Legend'),
129 handler: function(){
130 me.legend.setVisible(!me.legend.isVisible());
131 }
132 }]);
133 // add a series for each field we get
134 me.fields.forEach(function(item, index){
135 var title = item;
136 if (me.fieldTitles && me.fieldTitles[index]) {
137 title = me.fieldTitles[index];
138 }
139 me.addSeries({
140 type: 'line',
141 xField: 'time',
142 yField: item,
143 title: title,
144 fill: true,
145 style: {
146 lineWidth: 1.5,
147 opacity: 0.60
148 },
149 marker: {
150 opacity: 0,
151 scaling: 0.01,
152 fx: {
153 duration: 200,
154 easing: 'easeOut'
155 }
156 },
157 highlightCfg: {
158 opacity: 1,
159 scaling: 1.5
160 },
161 tooltip: {
162 trackMouse: true,
163 renderer: 'onSeriesTooltipRender'
164 }
165 });
166 });
167
168 // enable animation after the store is loaded
169 me.store.onAfter('load', function() {
170 me.setAnimation(true);
171 }, this, {single: true});
172 }
173 });