]> git.proxmox.com Git - extjs.git/blame - extjs/classic/classic/src/sparkline/TriState.js
add extjs 6.0.1 sources
[extjs.git] / extjs / classic / classic / src / sparkline / TriState.js
CommitLineData
6527f429
DM
1/**\r
2 * @class Ext.sparkline.TriState\r
3 *\r
4 * Plots bars based upon "win"/"draw" or "lose" status of the input {@link #values} array. Positive values mean\r
5 * a win, zero a draw, and negative a lose. \r
6 *\r
7 * See {@link Ext.sparkline.Base the base class} for a simple example.\r
8 */\r
9Ext.define('Ext.sparkline.TriState', {\r
10 extend: 'Ext.sparkline.BarBase',\r
11 requires: [\r
12 'Ext.sparkline.RangeMap'\r
13 ],\r
14\r
15 alias: 'widget.sparklinetristate',\r
16\r
17 config: {\r
18\r
19 /**\r
20 * @cfg {Number} [barWidth=4] The pixel width of each bar.\r
21 */\r
22 barWidth: 4,\r
23 \r
24 /**\r
25 * @cfg {Number} [barSpacing=1] The pixel spacing between each bar.\r
26 */\r
27 barSpacing: 1,\r
28 \r
29 /**\r
30 * @cfg {String} [posBarColor=#6f6] The color for positive value bars.\r
31 */\r
32 posBarColor: '#6f6',\r
33 \r
34 /**\r
35 * @cfg {String} [negBarColor=#f44] The color for negative value bars.\r
36 */\r
37 negBarColor: '#f44',\r
38 \r
39 /**\r
40 * @cfg {String} [zeroBarColor=#999] The color for zero value bars.\r
41 */\r
42 zeroBarColor: '#999',\r
43 \r
44 /**\r
45 * @cfg {Object} [colorMap] An object that uses range specifiers as keys to\r
46 * indicate bar color values for a range of values. A range specifier is\r
47 * specified in the form `[number]:[number]`, which indicates start and end range.\r
48 * Omitting either means an open ended range.\r
49 *\r
50 * For example, to render green bars on all values less than -1 and red on values\r
51 * higher than 1, you would use:\r
52 *\r
53 * colorMap: {\r
54 * // Open ended range, with max value -1\r
55 * ":-1": "green",\r
56 *\r
57 * // Open ended range, with min value 1\r
58 * "1:": "red"\r
59 * }\r
60 */\r
61 colorMap: {},\r
62 \r
63 tipTpl: new Ext.XTemplate('● {value:this.states}', {\r
64 states: function(v) {\r
65 var value = Number(v);\r
66 if (value === -1) {\r
67 return 'Loss';\r
68 }\r
69 if (value === 0) {\r
70 return 'Draw';\r
71 }\r
72 if (value === 1) {\r
73 return 'Win';\r
74 }\r
75 return v;\r
76 }\r
77 })\r
78 },\r
79\r
80 applyColorMap: function(colorMap) {\r
81 var me = this;\r
82\r
83 if (Ext.isArray(colorMap)) {\r
84 me.colorMapByIndex = colorMap;\r
85 me.colorMapByValue = null;\r
86 } else {\r
87 me.colorMapByIndex = null;\r
88 me.colorMapByValue = colorMap;\r
89 if (me.colorMapByValue && me.colorMapByValue.get == null) {\r
90 me.colorMapByValue = new Ext.sparkline.RangeMap(colorMap);\r
91 }\r
92 }\r
93 return colorMap;\r
94 },\r
95\r
96 // Ensure values is an array of numbers\r
97 applyValues: function(newValues) {\r
98 newValues = Ext.Array.map(Ext.Array.from(newValues), Number);\r
99 this.disabled = !(newValues && newValues.length);\r
100 return newValues;\r
101 },\r
102\r
103 onUpdate: function() {\r
104 this.totalBarWidth = this.getBarWidth() + this.getBarSpacing();\r
105 },\r
106\r
107 getBarWidth: function() {\r
108 var values = this.values;\r
109\r
110 return this._barWidth || (this.getWidth() - (values.length - 1) * this.getBarSpacing()) / values.length;\r
111 },\r
112\r
113 getRegion: function (x, y) {\r
114 return Math.floor(x / this.totalBarWidth);\r
115 },\r
116\r
117 getRegionFields: function (region) {\r
118 return {\r
119 isNull: this.values[region] == null,\r
120 value: this.values[region],\r
121 color: this.calcColor(this.values[region], region),\r
122 offset: region\r
123 };\r
124 },\r
125\r
126 calcColor: function (value, valuenum) {\r
127 var me = this,\r
128 values = me.values,\r
129 colorMapByIndex = me.colorMapByIndex,\r
130 colorMapByValue = me.colorMapByValue,\r
131 color, newColor;\r
132\r
133 if (colorMapByValue && (newColor = colorMapByValue.get(value))) {\r
134 color = newColor;\r
135 } else if (colorMapByIndex && colorMapByIndex.length > valuenum) {\r
136 color = colorMapByIndex[valuenum];\r
137 } else if (values[valuenum] < 0) {\r
138 color = me.getNegBarColor();\r
139 } else if (values[valuenum] > 0) {\r
140 color = me.getPosBarColor();\r
141 } else {\r
142 color = me.getZeroBarColor();\r
143 }\r
144 return color;\r
145 },\r
146\r
147 renderRegion: function (valuenum, highlight) {\r
148 var me = this,\r
149 values = me.values,\r
150 canvas = me.canvas,\r
151 canvasHeight, height, halfHeight, x, y, color;\r
152\r
153 canvasHeight = canvas.pixelHeight;\r
154 halfHeight = Math.round(canvasHeight / 2);\r
155\r
156 x = valuenum * me.totalBarWidth;\r
157 if (values[valuenum] < 0) {\r
158 y = halfHeight;\r
159 height = halfHeight - 1;\r
160 } else if (values[valuenum] > 0) {\r
161 y = 0;\r
162 height = halfHeight - 1;\r
163 } else {\r
164 y = halfHeight - 1;\r
165 height = 2;\r
166 }\r
167 color = me.calcColor(values[valuenum], valuenum);\r
168 if (color == null) {\r
169 return;\r
170 }\r
171 if (highlight) {\r
172 color = me.calcHighlightColor(color);\r
173 }\r
174 canvas.drawRect(x, y, me.getBarWidth() - 1, height - 1, color, color).append();\r
175 }\r
176});