tiling.squarified.base.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * DevExtreme (viz/tree_map/tiling.squarified.base.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _max = Math.max;
  11. var _round = Math.round;
  12. var tiling = require("./tiling");
  13. function compare(a, b) {
  14. return b.value - a.value
  15. }
  16. function getAspectRatio(value) {
  17. return _max(value, 1 / value)
  18. }
  19. function findAppropriateCollection(nodes, head, context) {
  20. var bestAspectRatio = 1 / 0;
  21. var nextAspectRatio;
  22. var sum = 0;
  23. var nextSum;
  24. var i;
  25. var j;
  26. var ii = nodes.length;
  27. var coeff = context.areaToValue / context.staticSide;
  28. var totalAspectRatio;
  29. for (i = head; i < ii;) {
  30. nextSum = sum + nodes[i].value;
  31. totalAspectRatio = context.staticSide / coeff / nextSum;
  32. nextAspectRatio = 0;
  33. for (j = head; j <= i; ++j) {
  34. nextAspectRatio = context.accumulate(nextAspectRatio, getAspectRatio(totalAspectRatio * nodes[j].value / nextSum), j - head + 1)
  35. }
  36. if (nextAspectRatio < bestAspectRatio) {
  37. bestAspectRatio = nextAspectRatio;
  38. sum = nextSum;
  39. ++i
  40. } else {
  41. break
  42. }
  43. }
  44. return {
  45. sum: sum,
  46. count: i - head,
  47. side: _round(coeff * sum)
  48. }
  49. }
  50. function getArea(rect) {
  51. return (rect[2] - rect[0]) * (rect[3] - rect[1])
  52. }
  53. function doStep(nodes, head, context) {
  54. var sidesData = tiling.buildSidesData(context.rect, context.directions, context.staticSideIndex);
  55. var area = getArea(context.rect);
  56. var rowData = area > 0 ? findAppropriateCollection(nodes, head, {
  57. areaToValue: area / context.sum,
  58. accumulate: context.accumulate,
  59. staticSide: sidesData.staticSide
  60. }) : {
  61. sum: 1,
  62. side: sidesData.variedSide,
  63. count: nodes.length - head
  64. };
  65. tiling.calculateRectangles(nodes, head, context.rect, sidesData, rowData);
  66. context.sum -= rowData.sum;
  67. return head + rowData.count
  68. }
  69. module.exports = function(data, accumulate, isFixedStaticSide) {
  70. var items = data.items;
  71. var ii = items.length;
  72. var i;
  73. var context = {
  74. sum: data.sum,
  75. rect: data.rect,
  76. directions: data.directions,
  77. accumulate: accumulate
  78. };
  79. if (isFixedStaticSide) {
  80. context.staticSideIndex = tiling.getStaticSideIndex(context.rect)
  81. }
  82. items.sort(compare);
  83. for (i = 0; i < ii;) {
  84. i = doStep(items, i, context)
  85. }
  86. };