]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - panel/RRDChart.js
RRDChart.js: use the fieldtitle or fieldname as axistitle
[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 } else if (me.fieldTitles && me.fieldTitles.length === 1) {
113 axisTitle = me.fieldTitles[0];
114 } else if (me.fields.length === 1) {
115 axisTitle = me.fields[0];
116 }
117
118 me.axes[0].setTitle(axisTitle);
119
120 me.addTool([{
121 type: 'minus',
122 disabled: true,
123 tooltip: gettext('Undo Zoom'),
124 handler: function(){
125 var undoButton = me.interactions[0].getUndoButton();
126 if (undoButton.handler) {
127 undoButton.handler();
128 }
129 }
130 },{
131 type: 'restore',
132 tooltip: gettext('Toggle Legend'),
133 handler: function(){
134 me.legend.setVisible(!me.legend.isVisible());
135 }
136 }]);
137 // add a series for each field we get
138 me.fields.forEach(function(item, index){
139 var title = item;
140 if (me.fieldTitles && me.fieldTitles[index]) {
141 title = me.fieldTitles[index];
142 }
143 me.addSeries({
144 type: 'line',
145 xField: 'time',
146 yField: item,
147 title: title,
148 fill: true,
149 style: {
150 lineWidth: 1.5,
151 opacity: 0.60
152 },
153 marker: {
154 opacity: 0,
155 scaling: 0.01,
156 fx: {
157 duration: 200,
158 easing: 'easeOut'
159 }
160 },
161 highlightCfg: {
162 opacity: 1,
163 scaling: 1.5
164 },
165 tooltip: {
166 trackMouse: true,
167 renderer: 'onSeriesTooltipRender'
168 }
169 });
170 });
171
172 // enable animation after the store is loaded
173 me.store.onAfter('load', function() {
174 me.setAnimation(true);
175 }, this, {single: true});
176 }
177 });