jquery.blueprint.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /*
  17. * jHue Blueprint plugin
  18. * it's a layer of abstraction on top of other chart/drawing/mapping plugins
  19. */
  20. jQuery.cachedScripts = {};
  21. jQuery.cacheScript = function (url, callback) {
  22. if (jQuery.cachedScripts != null && jQuery.cachedScripts[url] != null) {
  23. callback(jQuery.cachedScripts[url]);
  24. }
  25. else {
  26. var options = $.extend({}, {
  27. dataType: "script",
  28. cache: true,
  29. url: url,
  30. async: false,
  31. complete: function (script) {
  32. jQuery.cachedScripts[url] = script;
  33. callback(script);
  34. }
  35. });
  36. return jQuery.ajax(options);
  37. }
  38. };
  39. var FLOT_LOADED, FLOT_PIE_LOADED = false;
  40. (function ($, window, document, undefined) {
  41. var pluginName = "jHueBlueprint",
  42. COLORS = {
  43. ORANGE: "#FB950D",
  44. GREEN: "#419E08",
  45. BLUE: "#338BB8"
  46. },
  47. TYPES = {
  48. LINECHART: "lines",
  49. BARCHART: "bars",
  50. POINTCHART: "points",
  51. PIECHART: "pie",
  52. MAP: "map"
  53. },
  54. defaults = {
  55. data: [],
  56. type: TYPES.LINECHART,
  57. fill: false,
  58. width: "100%",
  59. height: 200
  60. };
  61. function Plugin(element, options) {
  62. this.element = element;
  63. this.options = $.extend({}, defaults, options);
  64. this._defaults = defaults;
  65. this._name = pluginName;
  66. this.init();
  67. }
  68. Plugin.prototype.init = function () {
  69. var _this = this;
  70. $.cacheScript("/static/ext/js/jquery/plugins/jquery.flot.min.js", function (script, textStatus) {
  71. FLOT_LOADED = true;
  72. if (_this.options.type == TYPES.PIECHART) {
  73. $.cacheScript("/static/ext/js/jquery/plugins/jquery.flot.pie.min.js", function (script, textStatus) {
  74. FLOT_PIE_LOADED = true;
  75. flot_pie(_this);
  76. });
  77. }
  78. else {
  79. flot(_this);
  80. }
  81. });
  82. };
  83. Plugin.prototype.setOptions = function (options) {
  84. this.options = $.extend({}, defaults, options);
  85. };
  86. function addSerie(element, serie) {
  87. if (!FLOT_LOADED) {
  88. window.setTimeout(function () {
  89. addSerie(element, serie);
  90. }, 500)
  91. }
  92. else {
  93. var _series = $(element).data("plotSeries");
  94. if (_series == null) {
  95. if (typeof console != "undefined") {
  96. console.warn("$(elem).jHueBlueprint('add', options) requires an existing data serie to work. Use $(elem).jHueBlueprint(options) instead.");
  97. }
  98. }
  99. else {
  100. _series.push(getSerie(serie));
  101. var _plot = $.plot(element, _series);
  102. $(element).data("plotObj", _plot);
  103. $(element).data("plotSeries", _series);
  104. }
  105. }
  106. };
  107. function flot(plugin) {
  108. var _this = plugin;
  109. $(_this.element).width(_this.options.width).height(_this.options.height);
  110. var _serie = getSerie(_this.options);
  111. var _plot = $.plot(_this.element, [_serie]);
  112. $(_this.element).data("plotObj", _plot);
  113. $(_this.element).data("plotSeries", [_serie]);
  114. }
  115. function flot_pie(plugin) {
  116. var _this = plugin;
  117. $(_this.element).width(_this.options.width).height(_this.options.height);
  118. var _plot = $.plot(_this.element, _this.options.data, {
  119. series: {
  120. pie: {
  121. show: true
  122. }
  123. }
  124. });
  125. $(_this.element).data("plotObj", _plot);
  126. }
  127. function getSerie(options) {
  128. var _flotSerie = {
  129. data: options.data
  130. }
  131. if (options.type == TYPES.LINECHART) {
  132. _flotSerie.lines = {show: true, fill: options.fill }
  133. }
  134. if (options.type == TYPES.BARCHART) {
  135. _flotSerie.bars = {show: true, fill: options.fill }
  136. }
  137. if (options.type == TYPES.POINTCHART) {
  138. _flotSerie.points = {show: true}
  139. }
  140. if (options.type == TYPES.PIECHART) {
  141. _flotSerie.pie = {show: true}
  142. }
  143. if (options.color != null) {
  144. _flotSerie.color = options.color;
  145. }
  146. return _flotSerie;
  147. }
  148. $.fn[pluginName] = function (options) {
  149. var _args = Array.prototype.slice.call(arguments);
  150. if (_args.length == 1) {
  151. return this.each(function () {
  152. if (!$.data(this, 'plugin_' + pluginName)) {
  153. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  154. }
  155. });
  156. }
  157. else {
  158. if (_args[0] == "add") { // add a serie to the graph
  159. addSerie(this, _args[1]);
  160. }
  161. }
  162. }
  163. $[pluginName] = function (options) {
  164. if (typeof console != "undefined") {
  165. console.warn("$(elem).jHueBlueprint() is a preferred call method.");
  166. }
  167. $(options.element).jHueBlueprint(options);
  168. };
  169. $[pluginName].COLORS = COLORS;
  170. $[pluginName].TYPES = TYPES;
  171. })(jQuery, window, document);