Gauge.html
7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-chart-axis-Gauge'>/**
</span> * @class Ext.chart.axis.Gauge
*
* Gauge Axis is the axis to be used with a Gauge series. The Gauge axis
* displays numeric data from an interval defined by the `minimum`, `maximum` and
* `step` configuration properties. The placement of the numeric data can be changed
* by altering the `margin` option that is set to `10` by default.
*
* A possible configuration for this axis would look like:
*
* axes: [{
* type: 'gauge',
* position: 'gauge',
* minimum: 0,
* maximum: 100,
* steps: 10,
* margin: 7
* }],
*/
Ext.define('Ext.chart.axis.Gauge', {
/* Begin Definitions */
extend: 'Ext.chart.axis.Abstract',
/* End Definitions */
<span id='Ext-chart-axis-Gauge-cfg-minimum'> /**
</span> * @cfg {Number} minimum (required)
* The minimum value of the interval to be displayed in the axis.
*/
<span id='Ext-chart-axis-Gauge-cfg-maximum'> /**
</span> * @cfg {Number} maximum (required)
* The maximum value of the interval to be displayed in the axis.
*/
<span id='Ext-chart-axis-Gauge-cfg-steps'> /**
</span> * @cfg {Number} steps (required)
* The number of steps and tick marks to add to the interval.
*/
<span id='Ext-chart-axis-Gauge-cfg-margin'> /**
</span> * @cfg {Number} [margin=10]
* The offset positioning of the tick marks and labels in pixels.
*/
<span id='Ext-chart-axis-Gauge-cfg-title'> /**
</span> * @cfg {String} title
* The title for the Axis.
*/
position: 'gauge',
alias: 'axis.gauge',
drawAxis: function(init) {
var chart = this.chart,
surface = chart.surface,
bbox = chart.chartBBox,
centerX = bbox.x + (bbox.width / 2),
centerY = bbox.y + bbox.height,
margin = this.margin || 10,
rho = Math.min(bbox.width, 2 * bbox.height) /2 + margin,
sprites = [], sprite,
steps = this.steps,
i, pi = Math.PI,
cos = Math.cos,
sin = Math.sin;
if (this.sprites && !chart.resizing) {
this.drawLabel();
return;
}
if (this.margin >= 0) {
if (!this.sprites) {
//draw circles
for (i = 0; i <= steps; i++) {
sprite = surface.add({
type: 'path',
path: ['M', centerX + (rho - margin) * cos(i / steps * pi - pi),
centerY + (rho - margin) * sin(i / steps * pi - pi),
'L', centerX + rho * cos(i / steps * pi - pi),
centerY + rho * sin(i / steps * pi - pi), 'Z'],
stroke: '#ccc'
});
sprite.setAttributes({
hidden: false
}, true);
sprites.push(sprite);
}
} else {
sprites = this.sprites;
//draw circles
for (i = 0; i <= steps; i++) {
sprites[i].setAttributes({
path: ['M', centerX + (rho - margin) * cos(i / steps * pi - pi),
centerY + (rho - margin) * sin(i / steps * pi - pi),
'L', centerX + rho * cos(i / steps * pi - pi),
centerY + rho * sin(i / steps * pi - pi), 'Z'],
stroke: '#ccc'
}, true);
}
}
}
this.sprites = sprites;
this.drawLabel();
if (this.title) {
this.drawTitle();
}
},
drawTitle: function() {
var me = this,
chart = me.chart,
surface = chart.surface,
bbox = chart.chartBBox,
labelSprite = me.titleSprite,
labelBBox;
if (!labelSprite) {
me.titleSprite = labelSprite = surface.add({
type: 'text',
zIndex: 2
});
}
labelSprite.setAttributes(Ext.apply({
text: me.title
}, me.label || {}), true);
labelBBox = labelSprite.getBBox();
labelSprite.setAttributes({
x: bbox.x + (bbox.width / 2) - (labelBBox.width / 2),
y: bbox.y + bbox.height - (labelBBox.height / 2) - 4
}, true);
},
<span id='Ext-chart-axis-Gauge-method-setTitle'> /**
</span> * Updates the {@link #title} of this axis.
* @param {String} title
*/
setTitle: function(title) {
this.title = title;
this.drawTitle();
},
drawLabel: function() {
var chart = this.chart,
surface = chart.surface,
bbox = chart.chartBBox,
centerX = bbox.x + (bbox.width / 2),
centerY = bbox.y + bbox.height,
margin = this.margin || 10,
rho = Math.min(bbox.width, 2 * bbox.height) /2 + 2 * margin,
round = Math.round,
labelArray = [], label,
maxValue = this.maximum || 0,
minValue = this.minimum || 0,
steps = this.steps, i = 0,
adjY,
pi = Math.PI,
cos = Math.cos,
sin = Math.sin,
labelConf = this.label,
renderer = labelConf.renderer || function(v) { return v; };
if (!this.labelArray) {
//draw scale
for (i = 0; i <= steps; i++) {
// TODO Adjust for height of text / 2 instead
adjY = (i === 0 || i === steps) ? 7 : 0;
label = surface.add({
type: 'text',
text: renderer(round(minValue + i / steps * (maxValue - minValue))),
x: centerX + rho * cos(i / steps * pi - pi),
y: centerY + rho * sin(i / steps * pi - pi) - adjY,
'text-anchor': 'middle',
'stroke-width': 0.2,
zIndex: 10,
stroke: '#333'
});
label.setAttributes({
hidden: false
}, true);
labelArray.push(label);
}
}
else {
labelArray = this.labelArray;
//draw values
for (i = 0; i <= steps; i++) {
// TODO Adjust for height of text / 2 instead
adjY = (i === 0 || i === steps) ? 7 : 0;
labelArray[i].setAttributes({
text: renderer(round(minValue + i / steps * (maxValue - minValue))),
x: centerX + rho * cos(i / steps * pi - pi),
y: centerY + rho * sin(i / steps * pi - pi) - adjY
}, true);
}
}
this.labelArray = labelArray;
}
});</pre>
</body>
</html>