Theme.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-chart-theme-Theme'>/**
  19. </span> * @class Ext.chart.theme.Theme
  20. *
  21. * Provides chart theming.
  22. *
  23. * Used as mixins by Ext.chart.Chart.
  24. */
  25. Ext.define('Ext.chart.theme.Theme', {
  26. /* Begin Definitions */
  27. requires: ['Ext.draw.Color'],
  28. /* End Definitions */
  29. theme: 'Base',
  30. themeAttrs: false,
  31. initTheme: function(theme) {
  32. var me = this,
  33. themes = Ext.chart.theme,
  34. key, gradients;
  35. if (theme) {
  36. theme = theme.split(':');
  37. for (key in themes) {
  38. if (key == theme[0]) {
  39. gradients = theme[1] == 'gradients';
  40. me.themeAttrs = new themes[key]({
  41. useGradients: gradients
  42. });
  43. if (gradients) {
  44. me.gradients = me.themeAttrs.gradients;
  45. }
  46. if (me.themeAttrs.background) {
  47. me.background = me.themeAttrs.background;
  48. }
  49. return;
  50. }
  51. }
  52. //&lt;debug&gt;
  53. Ext.Error.raise('No theme found named &quot;' + theme + '&quot;');
  54. //&lt;/debug&gt;
  55. }
  56. }
  57. },
  58. // This callback is executed right after when the class is created. This scope refers to the newly created class itself
  59. function() {
  60. /* Theme constructor: takes either a complex object with styles like:
  61. {
  62. axis: {
  63. fill: '#000',
  64. 'stroke-width': 1
  65. },
  66. axisLabelTop: {
  67. fill: '#000',
  68. font: '11px Arial'
  69. },
  70. axisLabelLeft: {
  71. fill: '#000',
  72. font: '11px Arial'
  73. },
  74. axisLabelRight: {
  75. fill: '#000',
  76. font: '11px Arial'
  77. },
  78. axisLabelBottom: {
  79. fill: '#000',
  80. font: '11px Arial'
  81. },
  82. axisTitleTop: {
  83. fill: '#000',
  84. font: '11px Arial'
  85. },
  86. axisTitleLeft: {
  87. fill: '#000',
  88. font: '11px Arial'
  89. },
  90. axisTitleRight: {
  91. fill: '#000',
  92. font: '11px Arial'
  93. },
  94. axisTitleBottom: {
  95. fill: '#000',
  96. font: '11px Arial'
  97. },
  98. series: {
  99. 'stroke-width': 1
  100. },
  101. seriesLabel: {
  102. font: '12px Arial',
  103. fill: '#333'
  104. },
  105. marker: {
  106. stroke: '#555',
  107. fill: '#000',
  108. radius: 3,
  109. size: 3
  110. },
  111. seriesThemes: [{
  112. fill: '#C6DBEF'
  113. }, {
  114. fill: '#9ECAE1'
  115. }, {
  116. fill: '#6BAED6'
  117. }, {
  118. fill: '#4292C6'
  119. }, {
  120. fill: '#2171B5'
  121. }, {
  122. fill: '#084594'
  123. }],
  124. markerThemes: [{
  125. fill: '#084594',
  126. type: 'circle'
  127. }, {
  128. fill: '#2171B5',
  129. type: 'cross'
  130. }, {
  131. fill: '#4292C6',
  132. type: 'plus'
  133. }]
  134. }
  135. ...or also takes just an array of colors and creates the complex object:
  136. {
  137. colors: ['#aaa', '#bcd', '#eee']
  138. }
  139. ...or takes just a base color and makes a theme from it
  140. {
  141. baseColor: '#bce'
  142. }
  143. To create a new theme you may add it to the Themes object:
  144. Ext.chart.theme.MyNewTheme = Ext.extend(Object, {
  145. constructor: function(config) {
  146. Ext.chart.theme.call(this, config, {
  147. baseColor: '#mybasecolor'
  148. });
  149. }
  150. });
  151. //Proposal:
  152. Ext.chart.theme.MyNewTheme = Ext.chart.createTheme('#basecolor');
  153. ...and then to use it provide the name of the theme (as a lower case string) in the chart config.
  154. {
  155. theme: 'mynewtheme'
  156. }
  157. */
  158. (function() {
  159. Ext.chart.theme = function(config, base) {
  160. config = config || {};
  161. var i = 0, d = +new Date(), l, colors, color,
  162. seriesThemes, markerThemes,
  163. seriesTheme, markerTheme,
  164. key, gradients = [],
  165. midColor, midL;
  166. if (config.baseColor) {
  167. midColor = Ext.draw.Color.fromString(config.baseColor);
  168. midL = midColor.getHSL()[2];
  169. if (midL &lt; 0.15) {
  170. midColor = midColor.getLighter(0.3);
  171. } else if (midL &lt; 0.3) {
  172. midColor = midColor.getLighter(0.15);
  173. } else if (midL &gt; 0.85) {
  174. midColor = midColor.getDarker(0.3);
  175. } else if (midL &gt; 0.7) {
  176. midColor = midColor.getDarker(0.15);
  177. }
  178. config.colors = [ midColor.getDarker(0.3).toString(),
  179. midColor.getDarker(0.15).toString(),
  180. midColor.toString(),
  181. midColor.getLighter(0.15).toString(),
  182. midColor.getLighter(0.3).toString()];
  183. delete config.baseColor;
  184. }
  185. if (config.colors) {
  186. colors = config.colors.slice();
  187. markerThemes = base.markerThemes;
  188. seriesThemes = base.seriesThemes;
  189. l = colors.length;
  190. base.colors = colors;
  191. for (; i &lt; l; i++) {
  192. color = colors[i];
  193. markerTheme = markerThemes[i] || {};
  194. seriesTheme = seriesThemes[i] || {};
  195. markerTheme.fill = seriesTheme.fill = markerTheme.stroke = seriesTheme.stroke = color;
  196. markerThemes[i] = markerTheme;
  197. seriesThemes[i] = seriesTheme;
  198. }
  199. base.markerThemes = markerThemes.slice(0, l);
  200. base.seriesThemes = seriesThemes.slice(0, l);
  201. //the user is configuring something in particular (either markers, series or pie slices)
  202. }
  203. for (key in base) {
  204. if (key in config) {
  205. if (Ext.isObject(config[key]) &amp;&amp; Ext.isObject(base[key])) {
  206. Ext.apply(base[key], config[key]);
  207. } else {
  208. base[key] = config[key];
  209. }
  210. }
  211. }
  212. if (config.useGradients) {
  213. colors = base.colors || (function () {
  214. var ans = [];
  215. for (i = 0, seriesThemes = base.seriesThemes, l = seriesThemes.length; i &lt; l; i++) {
  216. ans.push(seriesThemes[i].fill || seriesThemes[i].stroke);
  217. }
  218. return ans;
  219. }());
  220. for (i = 0, l = colors.length; i &lt; l; i++) {
  221. midColor = Ext.draw.Color.fromString(colors[i]);
  222. if (midColor) {
  223. color = midColor.getDarker(0.1).toString();
  224. midColor = midColor.toString();
  225. key = 'theme-' + midColor.substr(1) + '-' + color.substr(1) + '-' + d;
  226. gradients.push({
  227. id: key,
  228. angle: 45,
  229. stops: {
  230. 0: {
  231. color: midColor.toString()
  232. },
  233. 100: {
  234. color: color.toString()
  235. }
  236. }
  237. });
  238. colors[i] = 'url(#' + key + ')';
  239. }
  240. }
  241. base.gradients = gradients;
  242. base.colors = colors;
  243. }
  244. /*
  245. base.axis = Ext.apply(base.axis || {}, config.axis || {});
  246. base.axisLabel = Ext.apply(base.axisLabel || {}, config.axisLabel || {});
  247. base.axisTitle = Ext.apply(base.axisTitle || {}, config.axisTitle || {});
  248. */
  249. Ext.apply(this, base);
  250. };
  251. }());
  252. });
  253. </pre>
  254. </body>
  255. </html>