jquery.flot.stackpercent.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. (function ($) {
  2. var options = {
  3. series: {
  4. stackpercent: null
  5. } // or number/string
  6. };
  7. function init(plot) {
  8. // will be built up dynamically as a hash from x-value, or y-value if horizontal
  9. var stackBases = {};
  10. var processed = false;
  11. var stackSums = {};
  12. //set percentage for stacked chart
  13. function processRawData(plot, series, data, datapoints) {
  14. if (!processed) {
  15. processed = true;
  16. stackSums = getStackSums(plot.getData());
  17. }
  18. if (series.stackpercent == true) {
  19. var num = data.length;
  20. series.percents = [];
  21. var key_idx = 0;
  22. var value_idx = 1;
  23. if (series.bars && series.bars.horizontal && series.bars.horizontal === true) {
  24. key_idx = 1;
  25. value_idx = 0;
  26. }
  27. for (var j = 0; j < num; j++) {
  28. var sum = stackSums[data[j][key_idx] + ""];
  29. if (sum > 0) {
  30. series.percents.push(data[j][value_idx] * 100 / sum);
  31. } else {
  32. series.percents.push(0);
  33. }
  34. }
  35. }
  36. }
  37. //calculate summary
  38. function getStackSums(_data) {
  39. var data_len = _data.length;
  40. var sums = {};
  41. if (data_len > 0) {
  42. //caculate summary
  43. for (var i = 0; i < data_len; i++) {
  44. if (_data[i].stackpercent) {
  45. var key_idx = 0;
  46. var value_idx = 1;
  47. if (_data[i].bars && _data[i].bars.horizontal && _data[i].bars.horizontal === true) {
  48. key_idx = 1;
  49. value_idx = 0;
  50. }
  51. var num = _data[i].data.length;
  52. for (var j = 0; j < num; j++) {
  53. var value = 0;
  54. if (_data[i].data[j][1] != null) {
  55. value = _data[i].data[j][value_idx];
  56. }
  57. if (sums[_data[i].data[j][key_idx] + ""]) {
  58. sums[_data[i].data[j][key_idx] + ""] += value;
  59. } else {
  60. sums[_data[i].data[j][key_idx] + ""] = value;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. return sums;
  67. }
  68. function stackData(plot, s, datapoints) {
  69. if (!s.stackpercent) return;
  70. if (!processed) {
  71. stackSums = getStackSums(plot.getData());
  72. }
  73. var newPoints = [];
  74. var key_idx = 0;
  75. var value_idx = 1;
  76. if (s.bars && s.bars.horizontal && s.bars.horizontal === true) {
  77. key_idx = 1;
  78. value_idx = 0;
  79. }
  80. for (var i = 0; i < datapoints.points.length; i += 3) {
  81. // note that the values need to be turned into absolute y-values.
  82. // in other words, if you were to stack (x, y1), (x, y2), and (x, y3),
  83. // (each from different series, which is where stackBases comes in),
  84. // you'd want the new points to be (x, y1, 0), (x, y1+y2, y1), (x, y1+y2+y3, y1+y2)
  85. // generally, (x, thisValue + (base up to this point), + (base up to this point))
  86. if (!stackBases[datapoints.points[i + key_idx]]) {
  87. stackBases[datapoints.points[i + key_idx]] = 0;
  88. }
  89. newPoints[i + key_idx] = datapoints.points[i + key_idx];
  90. newPoints[i + value_idx] = datapoints.points[i + value_idx] + stackBases[datapoints.points[i + key_idx]];
  91. newPoints[i + 2] = stackBases[datapoints.points[i + key_idx]];
  92. stackBases[datapoints.points[i + key_idx]] += datapoints.points[i + value_idx];
  93. // change points to percentage values
  94. // you may need to set yaxis:{ max = 100 }
  95. if ( stackSums[newPoints[i+key_idx]+""] > 0 ){
  96. newPoints[i + value_idx] = newPoints[i + value_idx] * 100 / stackSums[newPoints[i + key_idx] + ""];
  97. newPoints[i + 2] = newPoints[i + 2] * 100 / stackSums[newPoints[i + key_idx] + ""];
  98. } else {
  99. newPoints[i + value_idx] = 0;
  100. newPoints[i + 2] = 0;
  101. }
  102. }
  103. datapoints.points = newPoints;
  104. }
  105. plot.hooks.processRawData.push(processRawData);
  106. plot.hooks.processDatapoints.push(stackData);
  107. }
  108. $.plot.plugins.push({
  109. init: init,
  110. options: options,
  111. name: 'stackpercent',
  112. version: '0.1'
  113. });
  114. })(jQuery);