]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * @class Ext.sparkline.Bullet\r | |
3 | *\r | |
4 | * Plots a bullet graph based upon the input {@link #values} array.\r | |
5 | *\r | |
6 | * See <a href="http://en.wikipedia.org/wiki/Bullet_graph">Bullet graphs Wikipedia Page</a> for more information.\r | |
7 | *\r | |
8 | * See {@link Ext.sparkline.Base the base class} for a simple example.\r | |
9 | */\r | |
10 | Ext.define('Ext.sparkline.Bullet', {\r | |
11 | extend: 'Ext.sparkline.Base',\r | |
12 | \r | |
13 | alias: 'widget.sparklinebullet',\r | |
14 | \r | |
15 | config: {\r | |
16 | \r | |
17 | /**\r | |
18 | * @cfg {String} [targetColor=#f33] The colour of the vertical target marker.\r | |
19 | */\r | |
20 | targetColor: '#f33',\r | |
21 | \r | |
22 | /**\r | |
23 | * @cfg {Number} [targetWidth=3] Width of the target bar in pixels.\r | |
24 | */\r | |
25 | targetWidth: 3,\r | |
26 | \r | |
27 | /**\r | |
28 | * @cfg {String} [performanceColor=#33f] The color of the performance measure horizontal bar.\r | |
29 | */\r | |
30 | performanceColor: '#33f',\r | |
31 | \r | |
32 | /**\r | |
33 | * @cfg {String[]} [rangeColors] An array of colors to use for each qualitative range background color.\r | |
34 | */\r | |
35 | rangeColors: ['#d3dafe', '#a8b6ff', '#7f94ff'],\r | |
36 | \r | |
37 | /**\r | |
38 | * @cfg {Number} [base] Set this to a number to change the base start number.\r | |
39 | */\r | |
40 | base: null,\r | |
41 | \r | |
42 | tipTpl: new Ext.XTemplate('{fieldkey:this.fields} - {value}', {\r | |
43 | fields: function(v) {\r | |
44 | if (v === 'r') {\r | |
45 | return 'Range';\r | |
46 | }\r | |
47 | if (v === 'p') {\r | |
48 | return 'Performance';\r | |
49 | }\r | |
50 | if (v === 't') {\r | |
51 | return 'Target';\r | |
52 | }\r | |
53 | }\r | |
54 | })\r | |
55 | },\r | |
56 | \r | |
57 | // Ensure values is an array of normalized values\r | |
58 | applyValues: function(newValues) {\r | |
59 | newValues = Ext.Array.map(Ext.Array.from(newValues), this.normalizeValue);\r | |
60 | this.disabled = !(newValues && newValues.length);\r | |
61 | return newValues;\r | |
62 | },\r | |
63 | \r | |
64 | onUpdate: function() {\r | |
65 | var me = this,\r | |
66 | values = me.values,\r | |
67 | min, max, vals,\r | |
68 | base = me.getBase();\r | |
69 | \r | |
70 | me.callParent(arguments);\r | |
71 | \r | |
72 | // target or performance could be null\r | |
73 | vals = values.slice();\r | |
74 | vals[0] = vals[0] === null ? vals[2] : vals[0];\r | |
75 | vals[1] = values[1] === null ? vals[2] : vals[1];\r | |
76 | min = Math.min.apply(Math, values);\r | |
77 | max = Math.max.apply(Math, values);\r | |
78 | if (base == null) {\r | |
79 | min = min < 0 ? min : 0;\r | |
80 | } else {\r | |
81 | min = base;\r | |
82 | }\r | |
83 | me.min = min;\r | |
84 | me.max = max;\r | |
85 | me.range = max - min;\r | |
86 | me.shapes = {};\r | |
87 | me.valueShapes = {};\r | |
88 | me.regiondata = {};\r | |
89 | if (!values.length) {\r | |
90 | me.disabled = true;\r | |
91 | }\r | |
92 | },\r | |
93 | \r | |
94 | getRegion: function(x, y) {\r | |
95 | var shapeid = this.canvas.getShapeAt(x, y);\r | |
96 | return (shapeid !== undefined && this.shapes[shapeid] !== undefined) ? this.shapes[shapeid] : undefined;\r | |
97 | },\r | |
98 | \r | |
99 | getRegionFields: function(region) {\r | |
100 | return {\r | |
101 | fieldkey: region.substr(0, 1),\r | |
102 | value: this.values[region.substr(1)],\r | |
103 | region: region\r | |
104 | };\r | |
105 | },\r | |
106 | \r | |
107 | renderHighlight: function(region) {\r | |
108 | switch (region.substr(0, 1)) {\r | |
109 | case 'r':\r | |
110 | this.renderRange(region.substr(1), true).append();\r | |
111 | break;\r | |
112 | case 'p':\r | |
113 | this.renderPerformance(true).append();\r | |
114 | break;\r | |
115 | case 't':\r | |
116 | this.renderTarget(true).append();\r | |
117 | break;\r | |
118 | }\r | |
119 | },\r | |
120 | \r | |
121 | renderRange: function(region, highlight) {\r | |
122 | var rangeval = this.values[region],\r | |
123 | rangewidth = Math.round(this.getWidth() * ((rangeval - this.min) / this.range)),\r | |
124 | color = this.getRangeColors()[region - 2];\r | |
125 | if (highlight) {\r | |
126 | color = this.calcHighlightColor(color);\r | |
127 | }\r | |
128 | return this.canvas.drawRect(0, 0, rangewidth - 1, this.getHeight() - 1, color, color);\r | |
129 | },\r | |
130 | \r | |
131 | renderPerformance: function(highlight) {\r | |
132 | var perfval = this.values[1],\r | |
133 | perfwidth = Math.round(this.getWidth() * ((perfval - this.min) / this.range)),\r | |
134 | color = this.getPerformanceColor();\r | |
135 | if (highlight) {\r | |
136 | color = this.calcHighlightColor(color);\r | |
137 | }\r | |
138 | return this.canvas.drawRect(0, Math.round(this.getHeight() * 0.3), perfwidth - 1,\r | |
139 | Math.round(this.getHeight() * 0.4) - 1, color, color);\r | |
140 | },\r | |
141 | \r | |
142 | renderTarget: function(highlight) {\r | |
143 | var targetval = this.values[0],\r | |
144 | targetWidth = this.getTargetWidth(),\r | |
145 | x = Math.round(this.getWidth() * ((targetval - this.min) / this.range) - (targetWidth / 2)),\r | |
146 | targettop = Math.round(this.getHeight() * 0.10),\r | |
147 | targetheight = this.getHeight() - (targettop * 2),\r | |
148 | color = this.getTargetColor();\r | |
149 | \r | |
150 | if (highlight) {\r | |
151 | color = this.calcHighlightColor(color);\r | |
152 | }\r | |
153 | return this.canvas.drawRect(x, targettop, targetWidth - 1, targetheight - 1, color, color);\r | |
154 | },\r | |
155 | \r | |
156 | renderGraph: function () {\r | |
157 | var me = this,\r | |
158 | vlen = me.values.length,\r | |
159 | canvas = me.canvas,\r | |
160 | i, shape,\r | |
161 | shapes = me.shapes || (me.shapes = {}),\r | |
162 | valueShapes = me.valueShapes || (me.valueShapes = {});\r | |
163 | \r | |
164 | if (!me.callParent()) {\r | |
165 | return;\r | |
166 | }\r | |
167 | for (i = 2; i < vlen; i++) {\r | |
168 | shape = me.renderRange(i).append();\r | |
169 | shapes[shape.id] = 'r' + i;\r | |
170 | valueShapes['r' + i] = shape.id;\r | |
171 | }\r | |
172 | if (me.values[1] !== null) {\r | |
173 | shape = me.renderPerformance().append();\r | |
174 | shapes[shape.id] = 'p1';\r | |
175 | valueShapes.p1 = shape.id;\r | |
176 | }\r | |
177 | if (me.values[0] !== null) {\r | |
178 | shape = this.renderTarget().append();\r | |
179 | shapes[shape.id] = 't0';\r | |
180 | valueShapes.t0 = shape.id;\r | |
181 | }\r | |
182 | \r | |
183 | // If mouse is over, apply the highlight\r | |
184 | if (me.currentPageXY && me.el.getRegion().contains(me.currentPageXY)) {\r | |
185 | me.updateDisplay();\r | |
186 | }\r | |
187 | canvas.render();\r | |
188 | }\r | |
189 | }); |