fabochart.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. (function ($) {
  2. var point = $("<div>").addClass("fabo-point");
  3. var point_inner = $("<div>").addClass("fabo-point-inner");
  4. var value = $("<div>").addClass("fabo-value").addClass("hide-border").addClass("hide");
  5. var value_text = $("<div class='fabo-value-text'></div>");
  6. var label_text = $("<div class='fabo-value-label'></div>");
  7. $.fn.faBoChart = function (options) {
  8. // This is the easiest way to have default options.
  9. var settings = $.extend({
  10. data: {},
  11. animate: true,
  12. time: 2000,
  13. instantAnimate: true,
  14. straight: false,
  15. valueColor: "#002561",
  16. backgroundColor: "#ccc",
  17. valueTextColor: "#00C6D7",
  18. labelTextColor: "#00C6D7",
  19. gutter: "2px"
  20. }, options);
  21. point_inner.css("background-color", settings.backgroundColor);
  22. point.css("padding-left", settings.gutter);
  23. value.css({
  24. "border-right-color": settings.valueColor,
  25. "border-left-color": settings.valueColor
  26. });
  27. value_text.css("color", settings.valueTextColor);
  28. label_text.css("color", settings.labelTextColor);
  29. var straight = settings.straight;
  30. var instantAnimate = settings.instantAnimate;
  31. var data = settings.data;
  32. var count = objSize(data);
  33. var animate = settings.animate;
  34. var animation_time = settings.time;
  35. container = $(this);
  36. container.addClass("fabo-chart");
  37. container.data("straight", straight);
  38. //Set width of each point
  39. point.css("width", 100 / count + "%");
  40. var highest = 0;
  41. var keys = [];
  42. $.each(data, function (index, v) {
  43. keys.push(index);
  44. if (v > highest)
  45. highest = v;
  46. });
  47. $.each(keys, function (index, v) {
  48. var pi = point_inner.clone();
  49. var p = point.clone();
  50. var val = data[v];
  51. var next = data[keys[index + 1]];
  52. pi = $(pi).appendTo(p);
  53. p = $(p).appendTo(container);
  54. addValue(pi, val, next, v, highest, straight);
  55. });
  56. $(window).resize(function () {
  57. reloadSizes();
  58. });
  59. //If animate
  60. if (animate)
  61. animateValues(container, animation_time, instantAnimate);
  62. else
  63. $(container).find(".fabo-value").removeClass("hide");
  64. return this;
  65. };
  66. function reloadSizes() {
  67. $(".fabo-chart").each(function () {
  68. var container = $(this);
  69. var pi = $(this).find(".fabo-point-inner:first");
  70. var width = Math.ceil(pi[0].getBoundingClientRect().width);
  71. console.log(width);
  72. if (!container.data("straight")) {
  73. $(container).find(".fabo-point-inner:not(.bigger-than) .fabo-value").css("border-left-width", width + "px");
  74. $(container).find(".fabo-point-inner.bigger-than .fabo-value").css("border-right-width", width + "px");
  75. } else {
  76. $(this).find(".fabo-value").css("border-right-width", width + "px");
  77. }
  78. })
  79. }
  80. function addValue(pi, val, next, label, highest, straight) {
  81. var v = value.clone();
  82. var width = Math.ceil(pi[0].getBoundingClientRect().width);
  83. label_text.clone().text(label).appendTo(pi);
  84. value_text.clone().text(val).appendTo(pi);
  85. if (!straight) {
  86. next = (typeof next == "undefined") ? val : next;
  87. var biggerThan = (next > val);
  88. var different = 0;
  89. if (!biggerThan)
  90. different = val - next;
  91. else
  92. different = next - val;
  93. var different = (different / highest);
  94. var innerHeight = Math.ceil(pi[0].getBoundingClientRect().height);
  95. //Set height
  96. if (biggerThan) {
  97. v.css("border-right-width", width + "px");
  98. var height = val * 100 / highest;
  99. var niveau = innerHeight * different;
  100. niveau = Math.ceil(niveau);
  101. } else {
  102. v.css("border-left-width", width + "px");
  103. var height = next * 100 / highest;
  104. var niveau = innerHeight * different;
  105. niveau = Math.ceil(niveau);
  106. }
  107. pi.data("biggerThan", biggerThan);
  108. if (biggerThan)
  109. pi.addClass("bigger-than");
  110. v.css("border-top-width", niveau + "px");
  111. } else {
  112. v.css("border-right-width", width + "px");
  113. var height = val * 100 / highest;
  114. }
  115. v.css("height", height + "%");
  116. v.appendTo(pi);
  117. return pi;
  118. }
  119. function animateValues(container, time, instantAnimate) {
  120. var elm = $(container).find(".fabo-value");
  121. var timer = (time / elm.length);
  122. var origin_time = timer;
  123. if (!instantAnimate) {
  124. setTimeout(function () {
  125. elm.removeClass("hide-border");
  126. }, time + 200);
  127. } else {
  128. elm.removeClass("hide-border");
  129. }
  130. elm.each(function () {
  131. var v = $(this);
  132. setTimeout(function () {
  133. v.removeClass("hide");
  134. }, timer);
  135. timer += origin_time;
  136. });
  137. }
  138. }(jQuery));
  139. var objSize = function (obj) {
  140. var count = 0;
  141. if (typeof obj == "object") {
  142. if (Object.keys) {
  143. count = Object.keys(obj).length;
  144. } else if (window._) {
  145. count = _.keys(obj).length;
  146. } else if (window.$) {
  147. count = $.map(obj, function () {
  148. return 1;
  149. }).length;
  150. } else {
  151. for (var key in obj) if (obj.hasOwnProperty(key)) count++;
  152. }
  153. }
  154. return count;
  155. };