jquery.blueprint.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. (function ($, window, document, undefined) {
  21. var pluginName = "jHueBlueprint",
  22. COLORS = {
  23. ORANGE: "#FB950D",
  24. GREEN: "#419E08",
  25. BLUE: "#338BB8",
  26. RED: "#CE151D",
  27. PURPLE: "#572B91",
  28. TURQUOISE: "#049D84",
  29. FALAFEL: "#774400"
  30. },
  31. TYPES = {
  32. LINECHART: "lines",
  33. BARCHART: "bars",
  34. POINTCHART: "points",
  35. PIECHART: "pie",
  36. MAP: "map"
  37. },
  38. defaults = {
  39. data: [],
  40. type: TYPES.LINECHART,
  41. fill: false,
  42. width: "100%",
  43. height: 200,
  44. borderWidth: 0,
  45. tooltips: true,
  46. enableSelection: false,
  47. isDateTime: false,
  48. timeFormat: null,
  49. isCategories: false,
  50. useCanvas: false,
  51. xAxisFormatter: null,
  52. yAxisFormatter: null,
  53. xTooltipFormatter: null,
  54. yTooltipFormatter: null,
  55. onSelect: function () {
  56. },
  57. onItemClick: function () {
  58. }
  59. };
  60. function Plugin(element, options) {
  61. this.element = element;
  62. this.options = $.extend({}, defaults, options);
  63. this._defaults = defaults;
  64. this._name = pluginName;
  65. this.init();
  66. }
  67. Plugin.prototype.init = function () {
  68. var _this = this;
  69. if (_this.options.type == TYPES.PIECHART) {
  70. flot_pie(_this);
  71. }
  72. else {
  73. flot(_this);
  74. }
  75. };
  76. Plugin.prototype.setOptions = function (options) {
  77. this.options = $.extend({}, defaults, options);
  78. };
  79. function addSerie(element, serie) {
  80. var _series = $(element).data("plotSeries");
  81. if (_series == null) {
  82. if (typeof console != "undefined") {
  83. console.warn("$(elem).jHueBlueprint('add', options) requires an existing data serie to work. Use $(elem).jHueBlueprint(options) instead.");
  84. }
  85. }
  86. else {
  87. _series.push(getSerie(serie));
  88. var _plot = $.plot(element, _series, $(element).data("plotOptions"));
  89. $(element).data("plotObj", _plot);
  90. $(element).data("plotSeries", _series);
  91. }
  92. };
  93. function showTooltip(x, y, contents) {
  94. $("<div id='jHueBlueprintTooltip'>" + contents + "</div>").css({
  95. top: y + 5,
  96. left: x + 5
  97. }).appendTo("body").fadeIn(200);
  98. }
  99. function flot(plugin) {
  100. var _this = plugin;
  101. $(_this.element).width(_this.options.width).height(_this.options.height);
  102. var _options = {
  103. grid: {
  104. borderWidth: _this.options.borderWidth,
  105. clickable: true
  106. }
  107. }
  108. if (_this.options.tooltips) {
  109. _options.grid.hoverable = true;
  110. }
  111. if (_this.options.enableSelection) {
  112. _options.selection = {
  113. mode: "x",
  114. color: COLORS.GREEN
  115. }
  116. }
  117. if (_this.options.isDateTime) {
  118. _options.xaxis = {
  119. mode: "time"
  120. }
  121. if (_this.options.timeFormat) {
  122. _options.xaxis.timeformat = _this.options.timeFormat;
  123. }
  124. }
  125. if (_this.options.isCategories) {
  126. _options.xaxis = {
  127. mode: "categories"
  128. }
  129. }
  130. if (_this.options.xAxisFormatter != null) {
  131. if (_options.xaxis == null){
  132. _options.xaxis = {
  133. tickFormatter: _this.options.yAxisFormatter
  134. }
  135. }
  136. else {
  137. _options.xaxis.tickFormatter = _this.options.xAxisFormatter;
  138. }
  139. }
  140. if (_this.options.yAxisFormatter != null) {
  141. _options.yaxis = {
  142. tickFormatter: _this.options.yAxisFormatter
  143. }
  144. }
  145. if (_this.options.useCanvas) {
  146. _options.canvas = true;
  147. }
  148. var _serie = getSerie(_this.options);
  149. var _plot = $.plot($(_this.element), [_serie], _options);
  150. $(_this.element).bind("plotselected", function (event, ranges) {
  151. _this.options.onSelect(ranges);
  152. });
  153. if (_this.options.tooltips) {
  154. var previousPoint = null;
  155. $(_this.element).bind("plothover", function (event, pos, item) {
  156. if (item) {
  157. if (previousPoint != item.dataIndex) {
  158. previousPoint = item.dataIndex;
  159. $("#jHueBlueprintTooltip").remove();
  160. var x = item.datapoint[0],
  161. y = item.datapoint[1];
  162. if (_this.options.isDateTime && typeof moment != "undefined"){
  163. x = moment(x).utc().format("YYYY-MM-DD[T]HH:mm:ss[Z]");
  164. }
  165. if (_this.options.isCategories){
  166. x = item.series.data[item.dataIndex][0];
  167. }
  168. showTooltip(item.pageX, item.pageY, "X: "+(_this.options.xTooltipFormatter?_this.options.xTooltipFormatter(x):x)+", Y: "+(_this.options.yTooltipFormatter?_this.options.yTooltipFormatter(y):y));
  169. }
  170. } else {
  171. $("#jHueBlueprintTooltip").remove();
  172. previousPoint = null;
  173. }
  174. });
  175. }
  176. $(_this.element).bind("plotclick", function (event, pos, item) {
  177. _this.options.onItemClick(pos, item);
  178. });
  179. $(_this.element).data("plotObj", _plot);
  180. $(_this.element).data("plotOptions", _options);
  181. $(_this.element).data("plotSeries", [_serie]);
  182. }
  183. function flot_pie(plugin) {
  184. var _this = plugin;
  185. $(_this.element).width(_this.options.width).height(_this.options.height);
  186. var _options = {
  187. grid: {
  188. borderWidth: _this.options.borderWidth,
  189. clickable: true
  190. },
  191. series: {
  192. pie: {
  193. show: true
  194. }
  195. }
  196. };
  197. if (_this.options.tooltips) {
  198. _options.grid.hoverable = true;
  199. }
  200. var _plot = $.plot(_this.element, _this.options.data, _options);
  201. $(_this.element).data("plotObj", _plot);
  202. }
  203. function getSerie(options) {
  204. var _chartData = options.data;
  205. if (options.isDateTime){
  206. var _newChartData = [];
  207. for (var i=0;i<_chartData.length;i++){
  208. var _tuple = _chartData[i];
  209. _tuple[0] = moment(_tuple[0]).unix()*1000;
  210. _newChartData.push(_tuple)
  211. }
  212. _chartData = _newChartData;
  213. }
  214. var _flotSerie = {
  215. data: _chartData,
  216. label: options.label
  217. }
  218. if (options.type == TYPES.LINECHART) {
  219. _flotSerie.lines = {show: true, fill: options.fill }
  220. _flotSerie.points = {show: true}
  221. }
  222. if (options.type == TYPES.BARCHART) {
  223. _flotSerie.bars = {show: true, fill: options.fill, barWidth: 0.6, align: "center" }
  224. _flotSerie.points = {show: true}
  225. }
  226. if (options.type == TYPES.POINTCHART) {
  227. _flotSerie.points = {show: true}
  228. }
  229. if (options.type == TYPES.PIECHART) {
  230. _flotSerie.pie = {show: true}
  231. }
  232. if (options.color != null) {
  233. _flotSerie.color = options.color;
  234. }
  235. return _flotSerie;
  236. }
  237. $.fn[pluginName] = function (options) {
  238. var _args = Array.prototype.slice.call(arguments);
  239. if (_args.length == 1) {
  240. if (_args[0] == "reset") { // resets the graph
  241. if (this.data("plotObj")){
  242. this.data("plotObj").setData([]);
  243. this.data("plotObj").setupGrid();
  244. this.data("plotObj").draw();
  245. this.data("plotObj", null);
  246. this.data("plotSeries", null);
  247. this.data("plotOptions", null);
  248. this.data('plugin_' + pluginName, null);
  249. }
  250. }
  251. else {
  252. return this.each(function () {
  253. if (!$.data(this, 'plugin_' + pluginName)) {
  254. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  255. }
  256. });
  257. }
  258. }
  259. else {
  260. if (_args[0] == "add") { // add a serie to the graph
  261. addSerie(this, _args[1]);
  262. }
  263. }
  264. }
  265. $[pluginName] = function (options) {
  266. if (typeof console != "undefined") {
  267. console.warn("$(elem).jHueBlueprint() is a preferred call method.");
  268. }
  269. $(options.element).jHueBlueprint(options);
  270. };
  271. $[pluginName].COLORS = COLORS;
  272. $[pluginName].TYPES = TYPES;
  273. })(jQuery, window, document);